@@ -32,8 +32,7 @@ use crate::{
3232 update_constituent_target_base_for_derivatives, AmmConstituentDatum ,
3333 AmmConstituentMappingFixed , Constituent , ConstituentCorrelationsFixed ,
3434 ConstituentTargetBaseFixed , LPPool , TargetsDatum , LP_POOL_SWAP_AUM_UPDATE_DELAY ,
35- MAX_AMM_CACHE_ORACLE_STALENESS_FOR_TARGET_CALC ,
36- MAX_AMM_CACHE_STALENESS_FOR_TARGET_CALC ,
35+ MAX_ORACLE_STALENESS_FOR_TARGET_CALC , MAX_STALENESS_FOR_TARGET_CALC ,
3736 } ,
3837 oracle_map:: OracleMap ,
3938 perp_market_map:: MarketSet ,
@@ -57,7 +56,7 @@ use crate::controller::spot_balance::update_spot_market_cumulative_interest;
5756use crate :: controller:: token:: { receive, send_from_program_vault_with_signature_seeds} ;
5857use crate :: instructions:: constraints:: * ;
5958use crate :: state:: lp_pool:: {
60- AmmInventoryAndPrices , ConstituentIndexAndDecimalAndPrice , CONSTITUENT_PDA_SEED ,
59+ AmmInventoryAndPricesAndSlots , ConstituentIndexAndDecimalAndPrice , CONSTITUENT_PDA_SEED ,
6160 LP_POOL_TOKEN_VAULT_PDA_SEED ,
6261} ;
6362
@@ -100,37 +99,14 @@ pub fn handle_update_constituent_target_base<'c: 'info, 'info>(
10099 let constituent_map =
101100 ConstituentMap :: load ( & ConstituentSet :: new ( ) , & lp_pool_key, remaining_accounts) ?;
102101
103- let mut amm_inventories: Vec < AmmInventoryAndPrices > =
102+ let mut amm_inventories: Vec < AmmInventoryAndPricesAndSlots > =
104103 Vec :: with_capacity ( amm_cache. len ( ) as usize ) ;
105- for ( idx , cache_info) in amm_cache. iter ( ) . enumerate ( ) {
104+ for ( _ , cache_info) in amm_cache. iter ( ) . enumerate ( ) {
106105 if cache_info. lp_status_for_perp_market == 0 {
107106 continue ;
108107 }
109- if !is_oracle_valid_for_action (
110- OracleValidity :: try_from ( cache_info. oracle_validity ) ?,
111- Some ( DriftAction :: UpdateLpConstituentTargetBase ) ,
112- ) ? {
113- msg ! (
114- "Oracle data for perp market {} is invalid. Skipping update" ,
115- idx,
116- ) ;
117- continue ;
118- }
119-
120- if slot. safe_sub ( cache_info. slot ) ? > MAX_AMM_CACHE_STALENESS_FOR_TARGET_CALC {
121- msg ! ( "Amm cache for perp market {}. Skipping update" , idx) ;
122- continue ;
123- }
124-
125- if slot. safe_sub ( cache_info. oracle_slot ) ? > MAX_AMM_CACHE_ORACLE_STALENESS_FOR_TARGET_CALC {
126- msg ! (
127- "Amm cache oracle for perp market {} is stale. Skipping update" ,
128- idx
129- ) ;
130- continue ;
131- }
132108
133- amm_inventories. push ( AmmInventoryAndPrices {
109+ amm_inventories. push ( AmmInventoryAndPricesAndSlots {
134110 inventory : {
135111 let scaled_position = cache_info
136112 . position
@@ -143,6 +119,8 @@ pub fn handle_update_constituent_target_base<'c: 'info, 'info>(
143119 )
144120 } ,
145121 price : cache_info. oracle_price ,
122+ last_oracle_slot : cache_info. oracle_slot ,
123+ last_position_slot : cache_info. slot ,
146124 } ) ;
147125 }
148126 msg ! ( "amm inventories: {:?}" , amm_inventories) ;
@@ -400,8 +378,18 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
400378 out_oracle. price ,
401379 lp_pool. last_aum ,
402380 ) ?;
381+ let in_target_datum = constituent_target_base. get ( in_constituent. constituent_index as u32 ) ;
382+ let in_target_position_slot_delay = slot. saturating_sub ( in_target_datum. last_position_slot ) ;
383+ let in_target_oracle_slot_delay = slot. saturating_sub ( in_target_datum. last_oracle_slot ) ;
384+ let out_target_datum = constituent_target_base. get ( out_constituent. constituent_index as u32 ) ;
385+ let out_target_position_slot_delay = slot. saturating_sub ( out_target_datum. last_position_slot ) ;
386+ let out_target_oracle_slot_delay = slot. saturating_sub ( out_target_datum. last_oracle_slot ) ;
403387
404388 let ( in_amount, out_amount, in_fee, out_fee) = lp_pool. get_swap_amount (
389+ in_target_position_slot_delay,
390+ out_target_position_slot_delay,
391+ in_target_oracle_slot_delay,
392+ out_target_oracle_slot_delay,
405393 & in_oracle,
406394 & out_oracle,
407395 & in_constituent,
@@ -541,6 +529,9 @@ pub fn handle_view_lp_pool_swap_fees<'c: 'info, 'info>(
541529 let constituent_correlations: AccountZeroCopy < ' _ , i64 , ConstituentCorrelationsFixed > =
542530 ctx. accounts . constituent_correlations . load_zc ( ) ?;
543531
532+ let constituent_target_base: AccountZeroCopy < ' _ , TargetsDatum , ConstituentTargetBaseFixed > =
533+ ctx. accounts . constituent_target_base . load_zc ( ) ?;
534+
544535 let AccountMaps {
545536 perp_market_map : _,
546537 spot_market_map,
@@ -580,7 +571,18 @@ pub fn handle_view_lp_pool_swap_fees<'c: 'info, 'info>(
580571 0 ,
581572 ) ?;
582573
574+ let in_target_datum = constituent_target_base. get ( in_constituent. constituent_index as u32 ) ;
575+ let in_target_position_slot_delay = slot. saturating_sub ( in_target_datum. last_position_slot ) ;
576+ let in_target_oracle_slot_delay = slot. saturating_sub ( in_target_datum. last_oracle_slot ) ;
577+ let out_target_datum = constituent_target_base. get ( out_constituent. constituent_index as u32 ) ;
578+ let out_target_position_slot_delay = slot. saturating_sub ( out_target_datum. last_position_slot ) ;
579+ let out_target_oracle_slot_delay = slot. saturating_sub ( out_target_datum. last_oracle_slot ) ;
580+
583581 let ( in_amount, out_amount, in_fee, out_fee) = lp_pool. get_swap_amount (
582+ in_target_position_slot_delay,
583+ out_target_position_slot_delay,
584+ in_target_oracle_slot_delay,
585+ out_target_oracle_slot_delay,
584586 & in_oracle,
585587 & out_oracle,
586588 & in_constituent,
@@ -722,8 +724,14 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>(
722724
723725 let dlp_total_supply = ctx. accounts . lp_mint . supply ;
724726
727+ let in_target_datum = constituent_target_base. get ( in_constituent. constituent_index as u32 ) ;
728+ let in_target_position_slot_delay = slot. saturating_sub ( in_target_datum. last_position_slot ) ;
729+ let in_target_oracle_slot_delay = slot. saturating_sub ( in_target_datum. last_oracle_slot ) ;
730+
725731 let ( lp_amount, in_amount, lp_fee_amount, in_fee_amount) = lp_pool
726732 . get_add_liquidity_mint_amount (
733+ in_target_position_slot_delay,
734+ in_target_oracle_slot_delay,
727735 & in_spot_market,
728736 & in_constituent,
729737 in_amount,
@@ -930,8 +938,14 @@ pub fn handle_view_lp_pool_add_liquidity_fees<'c: 'info, 'info>(
930938
931939 let dlp_total_supply = ctx. accounts . lp_mint . supply ;
932940
941+ let in_target_datum = constituent_target_base. get ( in_constituent. constituent_index as u32 ) ;
942+ let in_target_position_slot_delay = slot. saturating_sub ( in_target_datum. last_position_slot ) ;
943+ let in_target_oracle_slot_delay = slot. saturating_sub ( in_target_datum. last_oracle_slot ) ;
944+
933945 let ( lp_amount, in_amount, lp_fee_amount, in_fee_amount) = lp_pool
934946 . get_add_liquidity_mint_amount (
947+ in_target_position_slot_delay,
948+ in_target_oracle_slot_delay,
935949 & in_spot_market,
936950 & in_constituent,
937951 in_amount,
@@ -1069,8 +1083,14 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>(
10691083
10701084 let dlp_total_supply = ctx. accounts . lp_mint . supply ;
10711085
1086+ let out_target_datum = constituent_target_base. get ( out_constituent. constituent_index as u32 ) ;
1087+ let out_target_position_slot_delay = slot. saturating_sub ( out_target_datum. last_position_slot ) ;
1088+ let out_target_oracle_slot_delay = slot. saturating_sub ( out_target_datum. last_oracle_slot ) ;
1089+
10721090 let ( lp_burn_amount, out_amount, lp_fee_amount, out_fee_amount) = lp_pool
10731091 . get_remove_liquidity_amount (
1092+ out_target_position_slot_delay,
1093+ out_target_oracle_slot_delay,
10741094 & out_spot_market,
10751095 & out_constituent,
10761096 lp_to_burn,
@@ -1315,8 +1335,14 @@ pub fn handle_view_lp_pool_remove_liquidity_fees<'c: 'info, 'info>(
13151335
13161336 let dlp_total_supply = ctx. accounts . lp_mint . supply ;
13171337
1338+ let out_target_datum = constituent_target_base. get ( out_constituent. constituent_index as u32 ) ;
1339+ let out_target_position_slot_delay = slot. saturating_sub ( out_target_datum. last_position_slot ) ;
1340+ let out_target_oracle_slot_delay = slot. saturating_sub ( out_target_datum. last_oracle_slot ) ;
1341+
13181342 let ( lp_burn_amount, out_amount, lp_fee_amount, out_fee_amount) = lp_pool
13191343 . get_remove_liquidity_amount (
1344+ out_target_position_slot_delay,
1345+ out_target_oracle_slot_delay,
13201346 & out_spot_market,
13211347 & out_constituent,
13221348 lp_to_burn,
0 commit comments