11use anchor_lang:: { prelude:: * , Accounts , Key , Result } ;
2- use anchor_spl:: token_interface:: { TokenAccount , TokenInterface , Mint } ;
2+ use anchor_spl:: token_interface:: { Mint , TokenAccount , TokenInterface } ;
33
44use crate :: error:: ErrorCode ;
5- use crate :: math:: { oracle:: { is_oracle_valid_for_action, DriftAction } , safe_math:: SafeMath } ;
5+ use crate :: math:: {
6+ oracle:: { is_oracle_valid_for_action, DriftAction } ,
7+ safe_math:: SafeMath ,
8+ } ;
69use crate :: msg;
710use crate :: state:: spot_market:: SpotBalanceType ;
8- use crate :: state:: {
9- lp_pool:: { AmmConstituentDatum , AmmConstituentMappingFixed , LPPool , WeightValidationFlags , Constituent } ,
11+ use crate :: state:: {
12+ lp_pool:: {
13+ AmmConstituentDatum , AmmConstituentMappingFixed , Constituent , LPPool , WeightValidationFlags ,
14+ } ,
1015 perp_market_map:: MarketSet ,
1116 state:: State ,
1217 user:: MarketType ,
@@ -114,7 +119,6 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
114119 in_amount : u64 ,
115120 min_out_amount : u64 ,
116121) -> Result < ( ) > {
117-
118122 validate ! (
119123 in_market_index != out_market_index,
120124 ErrorCode :: InvalidSpotMarketAccount ,
@@ -151,37 +155,47 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
151155
152156 let in_oracle_id = in_spot_market. oracle_id ( ) ;
153157 let out_oracle_id = out_spot_market. oracle_id ( ) ;
154-
158+
155159 let in_oracle = * oracle_map. get_price_data ( & in_oracle_id) ?;
156160 let out_oracle = * oracle_map. get_price_data ( & out_oracle_id) ?;
157161
158- update_spot_market_cumulative_interest (
159- & mut in_spot_market,
160- Some ( & in_oracle) ,
161- now,
162- ) ?;
163-
164- update_spot_market_cumulative_interest (
165- & mut out_spot_market,
166- Some ( & out_oracle) ,
167- now,
168- ) ?;
169-
170- let in_constituent_balance = in_constituent. get_full_balance ( in_constituent_token_account. amount ) ?;
171- msg ! ( "in_constituent: {}, in_constituent_balance: {}" , in_constituent. constituent_index, in_constituent_balance) ;
172- let out_constituent_balance = out_constituent. get_full_balance ( out_constituent_token_account. amount ) ?;
173- msg ! ( "out_constituent: {}, out_constituent_balance: {}" , out_constituent. constituent_index, out_constituent_balance) ;
174-
175- let in_target_weight = constituent_target_weights. get_target_weight ( in_constituent. constituent_index ) ?;
176- let out_target_weight = constituent_target_weights. get_target_weight ( out_constituent. constituent_index ) ?;
162+ update_spot_market_cumulative_interest ( & mut in_spot_market, Some ( & in_oracle) , now) ?;
163+
164+ update_spot_market_cumulative_interest ( & mut out_spot_market, Some ( & out_oracle) , now) ?;
165+
166+ let in_constituent_balance =
167+ in_constituent. get_full_balance ( in_constituent_token_account. amount ) ?;
168+ msg ! (
169+ "in_constituent: {}, in_constituent_balance: {}" ,
170+ in_constituent. constituent_index,
171+ in_constituent_balance
172+ ) ;
173+ let out_constituent_balance =
174+ out_constituent. get_full_balance ( out_constituent_token_account. amount ) ?;
175+ msg ! (
176+ "out_constituent: {}, out_constituent_balance: {}" ,
177+ out_constituent. constituent_index,
178+ out_constituent_balance
179+ ) ;
180+
181+ let in_target_weight =
182+ constituent_target_weights. get_target_weight ( in_constituent. constituent_index ) ?;
183+ let out_target_weight =
184+ constituent_target_weights. get_target_weight ( out_constituent. constituent_index ) ?;
177185
178186 let ( in_amount, out_amount, in_fee, out_fee) = lp_pool. get_swap_amount (
179- & in_oracle, & out_oracle,
180- & in_constituent, & out_constituent,
181- & in_spot_market, & out_spot_market,
182- in_constituent_token_account. amount , out_constituent_token_account. amount ,
183- in_target_weight, out_target_weight,
184- in_amount) ?;
187+ & in_oracle,
188+ & out_oracle,
189+ & in_constituent,
190+ & out_constituent,
191+ & in_spot_market,
192+ & out_spot_market,
193+ in_constituent_token_account. amount ,
194+ out_constituent_token_account. amount ,
195+ in_target_weight,
196+ out_target_weight,
197+ in_amount,
198+ ) ?;
185199 let out_amount_net_fees = if out_fee > 0 {
186200 out_amount. safe_sub ( out_fee. unsigned_abs ( ) as u64 ) ?
187201 } else {
@@ -191,16 +205,23 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
191205 validate ! (
192206 out_amount_net_fees >= min_out_amount,
193207 ErrorCode :: SlippageOutsideLimit ,
194- format!( "Slippage outside limit: out_amount_net_fees({}) < min_out_amount({})" , out_amount_net_fees, min_out_amount) . as_str( )
208+ format!(
209+ "Slippage outside limit: out_amount_net_fees({}) < min_out_amount({})" ,
210+ out_amount_net_fees, min_out_amount
211+ )
212+ . as_str( )
195213 ) ?;
196214
197215 in_constituent. record_swap_fees ( in_fee) ?;
198216 out_constituent. record_swap_fees ( out_fee) ?;
199217
200-
201218 // interactions: CPIs
202-
203- let ( transfer_from_vault, transfer_from_constituent) = out_constituent. get_amount_from_vaults_to_withdraw ( out_constituent_token_account. amount , out_amount_net_fees) ?;
219+
220+ let ( transfer_from_vault, transfer_from_constituent) = out_constituent
221+ . get_amount_from_vaults_to_withdraw (
222+ out_constituent_token_account. amount ,
223+ out_amount_net_fees,
224+ ) ?;
204225
205226 // transfer in from user token account to token vault
206227 receive (
@@ -312,4 +333,4 @@ pub struct LPSwap<'info> {
312333 pub authority : Signer < ' info > ,
313334
314335 pub token_program : Interface < ' info , TokenInterface > ,
315- }
336+ }
0 commit comments