Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 28b3a0a

Browse files
author
Orex
committed
Implement test_setup
1 parent b9a6699 commit 28b3a0a

File tree

16 files changed

+1144
-1149
lines changed

16 files changed

+1144
-1149
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

programs/perpetuals/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ solana-program-test = "1.14.18"
4242
solana-sdk = "1.14.18"
4343
tokio = { version = "1.0.0", features = ["macros"]}
4444
bonfida-test-utils = "0.2.1"
45-
bincode = "1.3.3"
45+
bincode = "1.3.3"
46+
maplit = "1.0.2"

programs/perpetuals/tests/native/instructions/test_add_pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub async fn test_add_pool(
8989

9090
assert_eq!(*perpetuals_account.pools.last().unwrap(), pool_pda);
9191
assert_eq!(
92-
perpetuals_account.inception_time,
92+
utils::get_current_unix_timestamp(program_test_ctx).await,
9393
pool_account.inception_time
9494
);
9595

programs/perpetuals/tests/native/instructions/test_open_position.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use {
44
bonfida_test_utils::ProgramTestContextExt,
55
perpetuals::{
66
instructions::OpenPositionParams,
7-
state::{custody::Custody, perpetuals::Perpetuals, position::Position},
7+
state::{custody::Custody, position::Position},
88
},
99
solana_program_test::{BanksClientError, ProgramTestContext},
1010
solana_sdk::signer::{keypair::Keypair, Signer},
@@ -89,15 +89,13 @@ pub async fn test_open_position(
8989
// Check the position
9090
{
9191
let position_account = utils::get_account::<Position>(program_test_ctx, position_pda).await;
92-
let perpetuals_account =
93-
utils::get_account::<Perpetuals>(program_test_ctx, perpetuals_pda).await;
9492

9593
assert_eq!(position_account.owner, owner.pubkey());
9694
assert_eq!(position_account.pool, *pool_pda);
9795
assert_eq!(position_account.custody, custody_pda);
9896
assert_eq!(
9997
position_account.open_time,
100-
perpetuals_account.inception_time
98+
utils::get_current_unix_timestamp(program_test_ctx).await
10199
);
102100
assert_eq!(position_account.update_time, 0);
103101
assert_eq!(position_account.side, params.side);

programs/perpetuals/tests/native/tests_suite/basic_interactions.rs

Lines changed: 76 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,57 @@
11
use {
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-
2913
const USDC_DECIMALS: u8 = 6;
3014
const ETH_DECIMALS: u8 = 9;
3115

3216
pub 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

Comments
 (0)