1+ use core:: mem:: take;
2+
3+ use generic_array_struct:: generic_array_struct;
14use inf1_core:: instructions:: swap:: IxAccs ;
2- use inf1_test_utils:: { fill_mock_prog_accs, AccountMap } ;
5+ use inf1_ctl_jiminy:: {
6+ instructions:: swap:: v2:: {
7+ exact_in:: NewSwapExactInV2IxPreAccsBuilder , IxPreKeysOwned , NewSwapEntryAccsBuilder ,
8+ SwapEntryAccs ,
9+ } ,
10+ keys:: { LST_STATE_LIST_ID , POOL_STATE_ID } ,
11+ } ;
12+ use inf1_pp_core:: pair:: Pair ;
13+ use inf1_test_utils:: {
14+ fill_mock_prog_accs, lst_state_list_account, mock_mint, mock_sys_acc, mock_token_acc, raw_mint,
15+ raw_token_acc, AccountMap , LstStateListData , VerPoolState ,
16+ } ;
17+ use solana_account:: Account ;
18+ use solana_pubkey:: Pubkey ;
319
420pub fn fill_swap_prog_accs < I , C , D , P > (
521 am : & mut AccountMap ,
@@ -12,3 +28,122 @@ pub fn fill_swap_prog_accs<I, C, D, P>(
1228) {
1329 fill_mock_prog_accs ( am, [ * inp_calc_prog, * out_calc_prog, * pricing_prog] ) ;
1430}
31+
32+ #[ generic_array_struct( builder pub ) ]
33+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
34+ pub struct SwapTokenU64s < T > {
35+ pub reserves_bal : T ,
36+ pub acc_bal : T ,
37+ pub mint_supply : T ,
38+ }
39+
40+ #[ generic_array_struct( builder pub ) ]
41+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
42+ pub struct SwapTokenAddrs < T > {
43+ pub mint : T ,
44+ pub acc : T ,
45+ }
46+
47+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
48+ pub struct SwapTokenArg < U , A > {
49+ pub u64s : SwapTokenU64s < U > ,
50+ pub addrs : SwapTokenAddrs < A > ,
51+ }
52+
53+ type SwapTokenArgVals = SwapTokenArg < u64 , [ u8 ; 32 ] > ;
54+
55+ /// Assumes
56+ /// - both mints are tokenkeg mints
57+ /// - both tokens are 9 decimals
58+ pub fn swap_pre_accs (
59+ signer : & [ u8 ; 32 ] ,
60+ ps : & VerPoolState ,
61+ lsl : & LstStateListData ,
62+ args : & Pair < SwapTokenArgVals > ,
63+ ) -> ( IxPreKeysOwned , AccountMap ) {
64+ let mut pair = args. map ( |args| {
65+ if args. addrs . mint ( ) == ps. lp_token_mint ( ) {
66+ lp_accs ( ps, signer, & args)
67+ } else {
68+ lst_accs ( lsl, signer, & args)
69+ }
70+ } ) ;
71+ let accounts = NewSwapExactInV2IxPreAccsBuilder :: start ( )
72+ . with_signer ( ( ( * signer) . into ( ) , mock_sys_acc ( 1_000_000_000 ) ) )
73+ . with_pool_state ( ( POOL_STATE_ID . into ( ) , ps. into_account ( ) ) )
74+ . with_lst_state_list ( (
75+ LST_STATE_LIST_ID . into ( ) ,
76+ lst_state_list_account ( lsl. lst_state_list . clone ( ) ) ,
77+ ) )
78+ // move instead of clone
79+ . with_inp_mint ( take ( pair. inp . mint_mut ( ) ) )
80+ . with_inp_acc ( take ( pair. inp . acc_mut ( ) ) )
81+ . with_inp_token_program ( take ( pair. inp . token_program_mut ( ) ) )
82+ . with_inp_pool_reserves ( take ( pair. inp . pool_reserves_mut ( ) ) )
83+ . with_out_mint ( take ( pair. out . mint_mut ( ) ) )
84+ . with_out_acc ( take ( pair. out . acc_mut ( ) ) )
85+ . with_out_token_program ( take ( pair. out . token_program_mut ( ) ) )
86+ . with_out_pool_reserves ( take ( pair. out . pool_reserves_mut ( ) ) )
87+ . build ( ) ;
88+ let ix_prefix = IxPreKeysOwned :: new ( accounts. 0 . each_ref ( ) . map ( |( pk, _) | pk. to_bytes ( ) ) ) ;
89+
90+ ( ix_prefix, accounts. 0 . into_iter ( ) . collect ( ) )
91+ }
92+
93+ fn lp_accs (
94+ ps : & VerPoolState ,
95+ signer : & [ u8 ; 32 ] ,
96+ SwapTokenArg { u64s, addrs } : & SwapTokenArgVals ,
97+ ) -> SwapEntryAccs < ( Pubkey , Account ) > {
98+ if u64s. reserves_bal ( ) != u64s. mint_supply ( ) {
99+ panic ! (
100+ "reserves_bal {} != mint_supply {}. Set both to eq." ,
101+ u64s. reserves_bal( ) ,
102+ u64s. mint_supply( )
103+ ) ;
104+ }
105+ let lp_mint = (
106+ Pubkey :: new_from_array ( * ps. lp_token_mint ( ) ) ,
107+ mock_mint ( raw_mint ( Some ( POOL_STATE_ID ) , None , * u64s. mint_supply ( ) , 9 ) ) ,
108+ ) ;
109+ let acc = (
110+ Pubkey :: new_from_array ( * addrs. acc ( ) ) ,
111+ mock_token_acc ( raw_token_acc ( * ps. lp_token_mint ( ) , * signer, * u64s. acc_bal ( ) ) ) ,
112+ ) ;
113+ NewSwapEntryAccsBuilder :: start ( )
114+ . with_mint ( lp_mint. clone ( ) )
115+ . with_acc ( acc)
116+ . with_pool_reserves ( lp_mint)
117+ . with_token_program ( mollusk_svm_programs_token:: token:: keyed_account ( ) )
118+ . build ( )
119+ }
120+
121+ fn lst_accs (
122+ lsl : & LstStateListData ,
123+ signer : & [ u8 ; 32 ] ,
124+ SwapTokenArg { u64s, addrs } : & SwapTokenArgVals ,
125+ ) -> SwapEntryAccs < ( Pubkey , Account ) > {
126+ let mint = (
127+ Pubkey :: new_from_array ( * addrs. mint ( ) ) ,
128+ // dont-care abt mint and freeze auths
129+ mock_mint ( raw_mint ( None , None , * u64s. mint_supply ( ) , 9 ) ) ,
130+ ) ;
131+ let acc = (
132+ Pubkey :: new_from_array ( * addrs. acc ( ) ) ,
133+ mock_token_acc ( raw_token_acc ( * addrs. mint ( ) , * signer, * u64s. acc_bal ( ) ) ) ,
134+ ) ;
135+ let reserves = (
136+ lsl. all_pool_reserves [ addrs. mint ( ) ] . into ( ) ,
137+ mock_token_acc ( raw_token_acc (
138+ * addrs. mint ( ) ,
139+ POOL_STATE_ID ,
140+ * u64s. reserves_bal ( ) ,
141+ ) ) ,
142+ ) ;
143+ NewSwapEntryAccsBuilder :: start ( )
144+ . with_mint ( mint)
145+ . with_acc ( acc)
146+ . with_pool_reserves ( reserves)
147+ . with_token_program ( mollusk_svm_programs_token:: token:: keyed_account ( ) )
148+ . build ( )
149+ }
0 commit comments