forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lending: Add test to generate and optionally dump genesis accounts (s…
…olana-labs#967) * Automate primordial accounts * Add genesis account dumping as test * Add test-dump-genesis-accounts feature flag to create state * Run cargo fmt * Delete gitignore * Add test-bpf feature to genesis test Co-authored-by: Justin Starry <justin@solana.com>
- Loading branch information
Showing
16 changed files
with
514 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
# fixtures | ||
|
||
### SOL / USDC Serum Dex Accounts | ||
### SOL / USDC / SRM Dex Accounts | ||
|
||
``` | ||
$ solana config set --url https://api.mainnet-beta.solana.com | ||
$ solana account 7xMDbYTCqQEcK2aM9LbetGtNFJpzKdfXzLL5juaLh4GJ --output-file sol_usdc_dex_market.bin | ||
$ solana account 4VndUfHkmh6RWTQbXSVjY3wbSfqGjoPbuPHMoatV272H --output-file sol_usdc_dex_market_bids.bin | ||
$ solana account 6LTxKpMyGnbHM5rRx7f3eZHF9q3gnUBV5ucXF9LvrB3M --output-file sol_usdc_dex_market_asks.bin | ||
$ solana account CDdR97S8y96v3To93aKvi3nCnjUrbuVSuumw8FLvbVeg --output-file srm_usdc_dex_market.bin | ||
$ solana account DkxpXtF1EyjHomQcEhH54498gdhUN3t1sZCjReFNYZZn --output-file srm_usdc_dex_market_bids.bin | ||
$ solana account DRqgRZqfdD6PLHKSU7ydyVXWMUpvkqhzLZ1JSKn1iB1K --output-file srm_usdc_dex_market_asks.bin | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[237,94,30,208,149,9,211,113,89,130,186,215,210,240,152,201,225,9,164,38,6,66,245,251,44,121,38,3,210,200,217,24,114,133,232,227,86,67,182,15,253,36,214,87,201,19,105,189,111,157,211,250,12,167,115,73,3,116,254,73,245,75,104,105] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[58,208,121,211,147,191,252,194,27,112,242,181,234,8,178,221,146,159,34,49,159,191,43,185,225,149,79,50,132,251,191,160,190,232,3,127,76,66,126,248,122,226,243,163,63,22,80,127,18,193,145,126,162,197,113,216,136,135,137,237,47,207,231,220] |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[54,145,24,15,126,194,110,22,253,254,251,8,199,129,146,68,215,154,174,126,138,116,37,4,117,86,129,129,89,175,244,126,148,200,124,73,201,204,74,241,165,229,204,237,90,194,169,78,162,215,149,186,235,67,211,112,2,102,233,153,22,216,200,163] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
#![cfg(feature = "test-bpf")] | ||
|
||
mod helpers; | ||
|
||
use helpers::genesis::GenesisAccounts; | ||
use helpers::*; | ||
use solana_sdk::signature::Keypair; | ||
use spl_token_lending::{instruction::BorrowAmountType, state::INITIAL_COLLATERAL_RATE}; | ||
|
||
#[tokio::test] | ||
async fn test_success() { | ||
let (mut test, lending) = setup_test(); | ||
|
||
let LendingTest { | ||
sol_usdc_dex_market, | ||
srm_usdc_dex_market, | ||
usdc_mint, | ||
srm_mint, | ||
} = lending; | ||
|
||
// Initialize Lending Market | ||
let lending_market = add_lending_market(&mut test, usdc_mint.pubkey); | ||
|
||
const LAMPORTS_TO_SOL: u64 = 1_000_000_000; | ||
const FRACTIONAL_TO_USDC: u64 = 1_000_000; | ||
const FRACTIONAL_TO_SRM: u64 = 1_000_000; | ||
|
||
// Market and collateral are setup to fill two orders in the dex market at an average | ||
// price of 2210.5 | ||
const fn lamports_to_usdc_fractional(lamports: u64) -> u64 { | ||
lamports / LAMPORTS_TO_SOL * (2210 + 2211) / 2 * FRACTIONAL_TO_USDC / 1000 | ||
}; | ||
|
||
const USER_SOL_DEPOSIT_LAMPORTS: u64 = 10_000 * LAMPORTS_TO_SOL; | ||
const USER_SOL_COLLATERAL_LAMPORTS: u64 = 8_500 * LAMPORTS_TO_SOL; | ||
const INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS: u64 = 32_500 * LAMPORTS_TO_SOL; | ||
const TOTAL_SOL: u64 = USER_SOL_DEPOSIT_LAMPORTS + INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS; | ||
const INITIAL_USDC_RESERVE_SUPPLY_FRACTIONAL: u64 = lamports_to_usdc_fractional(TOTAL_SOL); | ||
const INITIAL_SRM_RESERVE_SUPPLY_FRACTIONAL: u64 = 20_000 * FRACTIONAL_TO_SRM; | ||
|
||
let user_accounts_owner = Keypair::new(); | ||
|
||
let usdc_reserve = add_reserve( | ||
&mut test, | ||
&user_accounts_owner, | ||
&lending_market, | ||
AddReserveArgs { | ||
name: "usdc".to_owned(), | ||
dex_market_pubkey: None, | ||
liquidity_amount: INITIAL_USDC_RESERVE_SUPPLY_FRACTIONAL, | ||
liquidity_mint_decimals: usdc_mint.decimals, | ||
liquidity_mint_pubkey: usdc_mint.pubkey, | ||
user_liquidity_amount: USER_SOL_DEPOSIT_LAMPORTS, | ||
config: TEST_RESERVE_CONFIG, | ||
..AddReserveArgs::default() | ||
}, | ||
); | ||
|
||
let sol_reserve = add_reserve( | ||
&mut test, | ||
&user_accounts_owner, | ||
&lending_market, | ||
AddReserveArgs { | ||
name: "sol".to_owned(), | ||
dex_market_pubkey: Some(sol_usdc_dex_market.pubkey), | ||
liquidity_amount: INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS, | ||
liquidity_mint_pubkey: spl_token::native_mint::id(), | ||
liquidity_mint_decimals: 9, | ||
user_liquidity_amount: USER_SOL_DEPOSIT_LAMPORTS, | ||
config: TEST_RESERVE_CONFIG, | ||
..AddReserveArgs::default() | ||
}, | ||
); | ||
|
||
let srm_reserve = add_reserve( | ||
&mut test, | ||
&user_accounts_owner, | ||
&lending_market, | ||
AddReserveArgs { | ||
name: "srm".to_owned(), | ||
dex_market_pubkey: Some(srm_usdc_dex_market.pubkey), | ||
liquidity_amount: INITIAL_SRM_RESERVE_SUPPLY_FRACTIONAL, | ||
liquidity_mint_decimals: srm_mint.decimals, | ||
liquidity_mint_pubkey: srm_mint.pubkey, | ||
user_liquidity_amount: USER_SOL_DEPOSIT_LAMPORTS, | ||
config: TEST_RESERVE_CONFIG, | ||
..AddReserveArgs::default() | ||
}, | ||
); | ||
|
||
let (mut banks_client, payer, _recent_blockhash) = test.start().await; | ||
|
||
// Verify lending market | ||
let lending_market_info = lending_market.get_state(&mut banks_client).await; | ||
assert_eq!(lending_market_info.is_initialized, true); | ||
assert_eq!(lending_market_info.quote_token_mint, usdc_mint.pubkey); | ||
|
||
// Verify reserves | ||
usdc_reserve.validate_state(&mut banks_client).await; | ||
sol_reserve.validate_state(&mut banks_client).await; | ||
srm_reserve.validate_state(&mut banks_client).await; | ||
|
||
let usdc_liquidity_supply = | ||
get_token_balance(&mut banks_client, usdc_reserve.liquidity_supply).await; | ||
assert_eq!( | ||
usdc_liquidity_supply, | ||
INITIAL_USDC_RESERVE_SUPPLY_FRACTIONAL | ||
); | ||
let user_usdc_collateral_balance = | ||
get_token_balance(&mut banks_client, usdc_reserve.user_collateral_account).await; | ||
assert_eq!( | ||
user_usdc_collateral_balance, | ||
INITIAL_COLLATERAL_RATE * INITIAL_USDC_RESERVE_SUPPLY_FRACTIONAL | ||
); | ||
|
||
let sol_liquidity_supply = | ||
get_token_balance(&mut banks_client, sol_reserve.liquidity_supply).await; | ||
assert_eq!(sol_liquidity_supply, INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS); | ||
let user_sol_balance = | ||
get_token_balance(&mut banks_client, sol_reserve.user_liquidity_account).await; | ||
assert_eq!(user_sol_balance, USER_SOL_DEPOSIT_LAMPORTS); | ||
let user_sol_collateral_balance = | ||
get_token_balance(&mut banks_client, sol_reserve.user_collateral_account).await; | ||
assert_eq!( | ||
user_sol_collateral_balance, | ||
INITIAL_COLLATERAL_RATE * INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS | ||
); | ||
|
||
// Deposit SOL | ||
lending_market | ||
.deposit( | ||
&mut banks_client, | ||
&user_accounts_owner, | ||
&payer, | ||
&sol_reserve, | ||
USER_SOL_DEPOSIT_LAMPORTS, | ||
) | ||
.await; | ||
|
||
// Verify deposit | ||
let sol_liquidity_supply = | ||
get_token_balance(&mut banks_client, sol_reserve.liquidity_supply).await; | ||
assert_eq!( | ||
sol_liquidity_supply, | ||
INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS + USER_SOL_DEPOSIT_LAMPORTS | ||
); | ||
let user_sol_balance = | ||
get_token_balance(&mut banks_client, sol_reserve.user_liquidity_account).await; | ||
assert_eq!(user_sol_balance, 0); | ||
let user_sol_collateral_balance = | ||
get_token_balance(&mut banks_client, sol_reserve.user_collateral_account).await; | ||
assert_eq!( | ||
user_sol_collateral_balance, | ||
INITIAL_COLLATERAL_RATE * TOTAL_SOL | ||
); | ||
|
||
// Borrow USDC with SOL collateral | ||
let obligation = lending_market | ||
.borrow( | ||
&mut banks_client, | ||
&payer, | ||
BorrowArgs { | ||
deposit_reserve: &sol_reserve, | ||
borrow_reserve: &usdc_reserve, | ||
dex_market: &sol_usdc_dex_market, | ||
borrow_amount_type: BorrowAmountType::CollateralDepositAmount, | ||
amount: INITIAL_COLLATERAL_RATE * USER_SOL_COLLATERAL_LAMPORTS, | ||
user_accounts_owner: &user_accounts_owner, | ||
obligation: None, | ||
}, | ||
) | ||
.await; | ||
|
||
// Borrow more USDC using existing obligation account | ||
lending_market | ||
.borrow( | ||
&mut banks_client, | ||
&payer, | ||
BorrowArgs { | ||
deposit_reserve: &sol_reserve, | ||
borrow_reserve: &usdc_reserve, | ||
dex_market: &sol_usdc_dex_market, | ||
borrow_amount_type: BorrowAmountType::CollateralDepositAmount, | ||
amount: lamports_to_usdc_fractional( | ||
usdc_reserve.config.loan_to_value_ratio as u64 * USER_SOL_COLLATERAL_LAMPORTS | ||
/ 100, | ||
), | ||
user_accounts_owner: &user_accounts_owner, | ||
obligation: Some(obligation), | ||
}, | ||
) | ||
.await; | ||
|
||
// Deposit USDC | ||
lending_market | ||
.deposit( | ||
&mut banks_client, | ||
&user_accounts_owner, | ||
&payer, | ||
&usdc_reserve, | ||
2 * INITIAL_COLLATERAL_RATE | ||
* lamports_to_usdc_fractional( | ||
usdc_reserve.config.loan_to_value_ratio as u64 * USER_SOL_COLLATERAL_LAMPORTS | ||
/ 100, | ||
), | ||
) | ||
.await; | ||
|
||
// Borrow SOL with USDC collateral | ||
lending_market | ||
.borrow( | ||
&mut banks_client, | ||
&payer, | ||
BorrowArgs { | ||
deposit_reserve: &usdc_reserve, | ||
borrow_reserve: &sol_reserve, | ||
dex_market: &sol_usdc_dex_market, | ||
borrow_amount_type: BorrowAmountType::CollateralDepositAmount, | ||
amount: INITIAL_COLLATERAL_RATE | ||
* lamports_to_usdc_fractional( | ||
usdc_reserve.config.loan_to_value_ratio as u64 | ||
* USER_SOL_COLLATERAL_LAMPORTS | ||
/ 100, | ||
), | ||
user_accounts_owner: &user_accounts_owner, | ||
obligation: None, | ||
}, | ||
) | ||
.await; | ||
|
||
// Borrow SRM with USDC collateral | ||
lending_market | ||
.borrow( | ||
&mut banks_client, | ||
&payer, | ||
BorrowArgs { | ||
deposit_reserve: &usdc_reserve, | ||
borrow_reserve: &srm_reserve, | ||
dex_market: &srm_usdc_dex_market, | ||
borrow_amount_type: BorrowAmountType::CollateralDepositAmount, | ||
amount: INITIAL_COLLATERAL_RATE | ||
* lamports_to_usdc_fractional( | ||
usdc_reserve.config.loan_to_value_ratio as u64 | ||
* USER_SOL_COLLATERAL_LAMPORTS | ||
/ 100, | ||
), | ||
user_accounts_owner: &user_accounts_owner, | ||
obligation: None, | ||
}, | ||
) | ||
.await; | ||
|
||
let mut genesis_accounts = GenesisAccounts::default(); | ||
lending_market | ||
.add_to_genesis(&mut banks_client, &mut genesis_accounts) | ||
.await; | ||
sol_reserve | ||
.add_to_genesis(&mut banks_client, &mut genesis_accounts) | ||
.await; | ||
srm_reserve | ||
.add_to_genesis(&mut banks_client, &mut genesis_accounts) | ||
.await; | ||
usdc_reserve | ||
.add_to_genesis(&mut banks_client, &mut genesis_accounts) | ||
.await; | ||
sol_usdc_dex_market | ||
.add_to_genesis(&mut banks_client, &mut genesis_accounts) | ||
.await; | ||
srm_usdc_dex_market | ||
.add_to_genesis(&mut banks_client, &mut genesis_accounts) | ||
.await; | ||
|
||
// Only dump the accounts if the feature is specified | ||
#[cfg(feature = "test-dump-genesis-accounts")] | ||
genesis_accounts.write_yaml(); | ||
} |
Oops, something went wrong.