11use {
2- crate :: {
3- instructions,
4- utils:: { self , fixtures} ,
5- } ,
6- bonfida_test_utils:: ProgramTestExt ,
2+ crate :: { instructions, utils} ,
3+ maplit:: hashmap,
74 perpetuals:: {
85 instructions:: {
96 ClosePositionParams , OpenPositionParams , RemoveLiquidityParams , SwapParams ,
107 } ,
118 state:: position:: Side ,
129 } ,
13- solana_program_test:: ProgramTest ,
1410 solana_sdk:: signer:: Signer ,
1511} ;
1612
17- const ROOT_AUTHORITY : usize = 0 ;
18- const PERPETUALS_UPGRADE_AUTHORITY : usize = 1 ;
19- const MULTISIG_MEMBER_A : usize = 2 ;
20- const MULTISIG_MEMBER_B : usize = 3 ;
21- const MULTISIG_MEMBER_C : usize = 4 ;
22- const PAYER : usize = 5 ;
23- const USER_ALICE : usize = 6 ;
24- const USER_MARTIN : usize = 7 ;
25- const USER_PAUL : usize = 8 ;
26-
27- const KEYPAIRS_COUNT : usize = 9 ;
28-
2913const USDC_DECIMALS : u8 = 6 ;
3014const ETH_DECIMALS : u8 = 9 ;
3115
3216pub async fn basic_interactions ( ) {
33- let mut program_test = ProgramTest :: default ( ) ;
34-
35- // Initialize the accounts that will be used during the test suite
36- let keypairs =
37- utils:: create_and_fund_multiple_accounts ( & mut program_test, KEYPAIRS_COUNT ) . await ;
38-
39- // Initialize mints
40- let usdc_mint = program_test
41- . add_mint ( None , USDC_DECIMALS , & keypairs[ ROOT_AUTHORITY ] . pubkey ( ) )
42- . 0 ;
43- let eth_mint = program_test
44- . add_mint ( None , ETH_DECIMALS , & keypairs[ ROOT_AUTHORITY ] . pubkey ( ) )
45- . 0 ;
46-
47- // Deploy the perpetuals program onchain as upgradeable program
48- utils:: add_perpetuals_program ( & mut program_test, & keypairs[ PERPETUALS_UPGRADE_AUTHORITY ] ) . await ;
49-
50- // Start the client and connect to localnet validator
51- let mut program_test_ctx = program_test. start_with_context ( ) . await ;
52-
53- let upgrade_authority = & keypairs[ PERPETUALS_UPGRADE_AUTHORITY ] ;
54-
55- let multisig_signers = & [
56- & keypairs[ MULTISIG_MEMBER_A ] ,
57- & keypairs[ MULTISIG_MEMBER_B ] ,
58- & keypairs[ MULTISIG_MEMBER_C ] ,
59- ] ;
60-
61- instructions:: test_init (
62- & mut program_test_ctx,
63- upgrade_authority,
64- fixtures:: init_params_permissions_full ( 1 ) ,
65- multisig_signers,
66- )
67- . await
68- . unwrap ( ) ;
69-
70- // Initialize and fund associated token accounts
71- {
72- // Alice: mint 1k USDC
73- {
74- utils:: initialize_and_fund_token_account (
75- & mut program_test_ctx,
76- & usdc_mint,
77- & keypairs[ USER_ALICE ] . pubkey ( ) ,
78- & keypairs[ ROOT_AUTHORITY ] ,
79- utils:: scale ( 1_000 , USDC_DECIMALS ) ,
80- )
81- . await ;
82- }
83-
84- // Martin: mint 100 USDC and 2 ETH
85- {
86- utils:: initialize_and_fund_token_account (
87- & mut program_test_ctx,
88- & usdc_mint,
89- & keypairs[ USER_MARTIN ] . pubkey ( ) ,
90- & keypairs[ ROOT_AUTHORITY ] ,
91- utils:: scale ( 100 , USDC_DECIMALS ) ,
92- )
93- . await ;
94-
95- utils:: initialize_and_fund_token_account (
96- & mut program_test_ctx,
97- & eth_mint,
98- & keypairs[ USER_MARTIN ] . pubkey ( ) ,
99- & keypairs[ ROOT_AUTHORITY ] ,
100- utils:: scale ( 2 , ETH_DECIMALS ) ,
101- )
102- . await ;
103- }
104-
105- // Paul: mint 150 USDC
106- {
107- utils:: initialize_and_fund_token_account (
108- & mut program_test_ctx,
109- & usdc_mint,
110- & keypairs[ USER_PAUL ] . pubkey ( ) ,
111- & keypairs[ ROOT_AUTHORITY ] ,
112- utils:: scale ( 150 , USDC_DECIMALS ) ,
113- )
114- . await ;
115-
116- utils:: initialize_token_account (
117- & mut program_test_ctx,
118- & eth_mint,
119- & keypairs[ USER_PAUL ] . pubkey ( ) ,
120- )
121- . await ;
122- }
123- }
124-
125- let ( pool_pda, _, lp_token_mint_pda, _, _) = utils:: setup_pool_with_custodies_and_liquidity (
126- & mut program_test_ctx,
127- & keypairs[ MULTISIG_MEMBER_A ] ,
128- "FOO" ,
129- & keypairs[ PAYER ] ,
130- multisig_signers,
17+ let test_setup = utils:: TestSetup :: new (
18+ vec ! [
19+ utils:: UserParam {
20+ name: "alice" ,
21+ token_balances: hashmap! {
22+ "usdc" => utils:: scale( 1_000 , USDC_DECIMALS ) ,
23+ } ,
24+ } ,
25+ utils:: UserParam {
26+ name: "martin" ,
27+ token_balances: hashmap! {
28+ "usdc" => utils:: scale( 100 , USDC_DECIMALS ) ,
29+ "eth" => utils:: scale( 2 , ETH_DECIMALS ) ,
30+ } ,
31+ } ,
32+ utils:: UserParam {
33+ name: "paul" ,
34+ token_balances: hashmap! {
35+ "usdc" => utils:: scale( 150 , USDC_DECIMALS ) ,
36+ } ,
37+ } ,
38+ ] ,
13139 vec ! [
132- utils:: SetupCustodyWithLiquidityParams {
133- setup_custody_params: utils:: SetupCustodyParams {
134- mint: usdc_mint,
135- decimals: USDC_DECIMALS ,
40+ utils:: MintParam {
41+ name: "usdc" ,
42+ decimals: USDC_DECIMALS ,
43+ } ,
44+ utils:: MintParam {
45+ name: "eth" ,
46+ decimals: ETH_DECIMALS ,
47+ } ,
48+ ] ,
49+ vec ! [ "admin_a" , "admin_b" , "admin_c" ] ,
50+ "main_pool" ,
51+ vec ! [
52+ utils:: NamedSetupCustodyWithLiquidityParams {
53+ setup_custody_params: utils:: NamedSetupCustodyParams {
54+ mint_name: "usdc" ,
13655 is_stable: true ,
13756 is_virtual: false ,
13857 target_ratio: utils:: ratio_from_percentage( 50.0 ) ,
@@ -145,14 +64,12 @@ pub async fn basic_interactions() {
14564 fees: None ,
14665 borrow_rate: None ,
14766 } ,
148- // Alice: add 1k USDC liquidity
14967 liquidity_amount: utils:: scale( 1_000 , USDC_DECIMALS ) ,
150- payer : utils :: copy_keypair ( & keypairs [ USER_ALICE ] ) ,
68+ payer_user_name : "alice" ,
15169 } ,
152- utils:: SetupCustodyWithLiquidityParams {
153- setup_custody_params: utils:: SetupCustodyParams {
154- mint: eth_mint,
155- decimals: ETH_DECIMALS ,
70+ utils:: NamedSetupCustodyWithLiquidityParams {
71+ setup_custody_params: utils:: NamedSetupCustodyParams {
72+ mint_name: "eth" ,
15673 is_stable: false ,
15774 is_virtual: false ,
15875 target_ratio: utils:: ratio_from_percentage( 50.0 ) ,
@@ -165,26 +82,32 @@ pub async fn basic_interactions() {
16582 fees: None ,
16683 borrow_rate: None ,
16784 } ,
168- // Martin: add 1 ETH liquidity
16985 liquidity_amount: utils:: scale( 1 , ETH_DECIMALS ) ,
170- payer : utils :: copy_keypair ( & keypairs [ USER_MARTIN ] ) ,
86+ payer_user_name : "martin" ,
17187 } ,
17288 ] ,
17389 )
17490 . await ;
17591
92+ let alice = test_setup. get_user_keypair_by_name ( "alice" ) ;
93+ let martin = test_setup. get_user_keypair_by_name ( "martin" ) ;
94+ let paul = test_setup. get_user_keypair_by_name ( "paul" ) ;
95+
96+ let usdc_mint = & test_setup. get_mint_by_name ( "usdc" ) ;
97+ let eth_mint = & test_setup. get_mint_by_name ( "eth" ) ;
98+
17699 // Simple open/close position
177100 {
178101 // Martin: Open 0.1 ETH position
179102 let position_pda = instructions:: test_open_position (
180- & mut program_test_ctx,
181- & keypairs [ USER_MARTIN ] ,
182- & keypairs [ PAYER ] ,
183- & pool_pda,
184- & eth_mint,
103+ & mut test_setup . program_test_ctx . borrow_mut ( ) ,
104+ martin ,
105+ & test_setup . payer_keypair ,
106+ & test_setup . pool_pda ,
107+ eth_mint,
185108 OpenPositionParams {
186109 // max price paid (slippage implied)
187- price : utils:: scale ( 1_550 , ETH_DECIMALS ) ,
110+ price : utils:: scale ( 1_550 , USDC_DECIMALS ) ,
188111 collateral : utils:: scale_f64 ( 0.1 , ETH_DECIMALS ) ,
189112 size : utils:: scale_f64 ( 0.1 , ETH_DECIMALS ) ,
190113 side : Side :: Long ,
@@ -196,10 +119,10 @@ pub async fn basic_interactions() {
196119
197120 // Martin: Close the ETH position
198121 instructions:: test_close_position (
199- & mut program_test_ctx,
200- & keypairs [ USER_MARTIN ] ,
201- & keypairs [ PAYER ] ,
202- & pool_pda,
122+ & mut test_setup . program_test_ctx . borrow_mut ( ) ,
123+ martin ,
124+ & test_setup . payer_keypair ,
125+ & test_setup . pool_pda ,
203126 & eth_mint,
204127 & position_pda,
205128 ClosePositionParams {
@@ -215,10 +138,10 @@ pub async fn basic_interactions() {
215138 {
216139 // Paul: Swap 150 USDC for ETH
217140 instructions:: test_swap (
218- & mut program_test_ctx,
219- & keypairs [ USER_PAUL ] ,
220- & keypairs [ PAYER ] ,
221- & pool_pda,
141+ & mut test_setup . program_test_ctx . borrow_mut ( ) ,
142+ paul ,
143+ & test_setup . payer_keypair ,
144+ & test_setup . pool_pda ,
222145 & eth_mint,
223146 // The program receives USDC
224147 & usdc_mint,
@@ -238,21 +161,21 @@ pub async fn basic_interactions() {
238161
239162 // Remove liquidity
240163 {
241- let alice_lp_token = utils:: find_associated_token_account (
242- & keypairs[ USER_ALICE ] . pubkey ( ) ,
243- & lp_token_mint_pda,
244- )
245- . 0 ;
164+ let alice_lp_token =
165+ utils:: find_associated_token_account ( & alice. pubkey ( ) , & test_setup. lp_token_mint_pda ) . 0 ;
246166
247- let alice_lp_token_balance =
248- utils:: get_token_account_balance ( & mut program_test_ctx, alice_lp_token) . await ;
167+ let alice_lp_token_balance = utils:: get_token_account_balance (
168+ & mut test_setup. program_test_ctx . borrow_mut ( ) ,
169+ alice_lp_token,
170+ )
171+ . await ;
249172
250173 // Alice: Remove 100% of provided liquidity (1k USDC less fees)
251174 instructions:: test_remove_liquidity (
252- & mut program_test_ctx,
253- & keypairs [ USER_ALICE ] ,
254- & keypairs [ PAYER ] ,
255- & pool_pda,
175+ & mut test_setup . program_test_ctx . borrow_mut ( ) ,
176+ alice ,
177+ & test_setup . payer_keypair ,
178+ & test_setup . pool_pda ,
256179 & usdc_mint,
257180 RemoveLiquidityParams {
258181 lp_amount_in : alice_lp_token_balance,
0 commit comments