@@ -1107,17 +1107,6 @@ pub fn handle_initialize_perp_market(
11071107
11081108 safe_increment ! ( state. number_of_markets, 1 ) ;
11091109
1110- let amm_cache = & mut ctx. accounts . amm_cache ;
1111- let current_len = amm_cache. cache . len ( ) ;
1112- amm_cache
1113- . cache
1114- . resize_with ( current_len + 1 , CacheInfo :: default) ;
1115- let current_market_info = amm_cache. cache . get_mut ( current_len) . unwrap ( ) ;
1116- current_market_info. slot = clock_slot;
1117- current_market_info. oracle = perp_market. amm . oracle ;
1118- current_market_info. oracle_source = u8:: from ( perp_market. amm . oracle_source ) ;
1119- amm_cache. validate ( state) ?;
1120-
11211110 controller:: amm:: update_concentration_coef ( perp_market, concentration_coef_scale) ?;
11221111 crate :: dlog!( oracle_price) ;
11231112
@@ -1140,28 +1129,32 @@ pub fn handle_initialize_amm_cache(ctx: Context<InitializeAmmCache>) -> Result<(
11401129 Ok ( ( ) )
11411130}
11421131
1143- pub fn handle_resize_amm_cache ( ctx : Context < ResizeAmmCache > ) -> Result < ( ) > {
1132+ pub fn handle_add_market_to_amm_cache ( ctx : Context < AddMarketToAmmCache > ) -> Result < ( ) > {
11441133 let amm_cache = & mut ctx. accounts . amm_cache ;
1145- let state = & ctx. accounts . state ;
1134+ let perp_market = ctx. accounts . perp_market . load ( ) ?;
1135+
1136+ for cache_info in amm_cache. cache . iter ( ) {
1137+ validate ! (
1138+ cache_info. market_index != perp_market. market_index,
1139+ ErrorCode :: DefaultError ,
1140+ "Market index {} already in amm cache" ,
1141+ perp_market. market_index
1142+ ) ?;
1143+ }
1144+
11461145 let current_size = amm_cache. cache . len ( ) ;
1147- let new_size = ( state . number_of_markets as usize ) . min ( current_size + 20_usize ) ;
1146+ let new_size = current_size . saturating_add ( 1 ) ;
11481147
11491148 msg ! (
11501149 "resizing amm cache from {} entries to {}" ,
11511150 current_size,
11521151 new_size
11531152 ) ;
11541153
1155- let growth = new_size. saturating_sub ( current_size) ;
1156- validate ! (
1157- growth <= 20 ,
1158- ErrorCode :: DefaultError ,
1159- "cannot grow amm_cache by more than 20 entries in a single resize (requested +{})" ,
1160- growth
1161- ) ?;
1162-
1163- amm_cache. cache . resize_with ( new_size, CacheInfo :: default) ;
1164- amm_cache. validate ( state) ?;
1154+ amm_cache. cache . resize_with ( new_size, || CacheInfo {
1155+ market_index : perp_market. market_index ,
1156+ ..CacheInfo :: default ( )
1157+ } ) ;
11651158
11661159 Ok ( ( ) )
11671160}
@@ -3364,7 +3357,8 @@ pub fn handle_update_perp_market_paused_operations(
33643357
33653358 if * ctx. accounts . admin . key != ctx. accounts . state . admin {
33663359 validate ! (
3367- paused_operations == PerpOperation :: UpdateFunding as u8 ,
3360+ paused_operations == PerpOperation :: UpdateFunding as u8
3361+ || paused_operations == PerpOperation :: SettleRevPool as u8 ,
33683362 ErrorCode :: DefaultError ,
33693363 "signer must be admin" ,
33703364 ) ?;
@@ -3586,20 +3580,6 @@ pub fn handle_update_perp_market_reference_price_offset_deadband_pct(
35863580 Ok ( ( ) )
35873581}
35883582
3589- pub fn handle_update_lp_cooldown_time (
3590- ctx : Context < AdminUpdateState > ,
3591- lp_cooldown_time : u64 ,
3592- ) -> Result < ( ) > {
3593- msg ! (
3594- "lp_cooldown_time: {} -> {}" ,
3595- ctx. accounts. state. lp_cooldown_time,
3596- lp_cooldown_time
3597- ) ;
3598-
3599- ctx. accounts . state . lp_cooldown_time = lp_cooldown_time;
3600- Ok ( ( ) )
3601- }
3602-
36033583pub fn handle_update_perp_fee_structure (
36043584 ctx : Context < AdminUpdateState > ,
36053585 fee_structure : FeeStructure ,
@@ -3819,7 +3799,14 @@ pub fn handle_update_perp_market_oracle(
38193799 perp_market. amm . oracle = oracle;
38203800 perp_market. amm . oracle_source = oracle_source;
38213801
3822- amm_cache. update_perp_market_fields ( perp_market) ?;
3802+ if amm_cache
3803+ . cache
3804+ . iter ( )
3805+ . find ( |cache_info| cache_info. market_index == perp_market. market_index )
3806+ . is_some ( )
3807+ {
3808+ amm_cache. update_perp_market_fields ( perp_market) ?;
3809+ }
38233810
38243811 Ok ( ( ) )
38253812}
@@ -5511,15 +5498,6 @@ pub struct InitializePerpMarket<'info> {
55115498 payer = admin
55125499 ) ]
55135500 pub perp_market : AccountLoader < ' info , PerpMarket > ,
5514- #[ account(
5515- mut ,
5516- seeds = [ AMM_POSITIONS_CACHE . as_ref( ) ] ,
5517- bump = amm_cache. bump,
5518- realloc = AmmCache :: space( amm_cache. cache. len( ) + 1_usize ) ,
5519- realloc:: payer = admin,
5520- realloc:: zero = false ,
5521- ) ]
5522- pub amm_cache : Box < Account < ' info , AmmCache > > ,
55235501 /// CHECK: checked in `initialize_perp_market`
55245502 pub oracle : AccountInfo < ' info > ,
55255503 pub rent : Sysvar < ' info , Rent > ,
@@ -5547,7 +5525,7 @@ pub struct InitializeAmmCache<'info> {
55475525}
55485526
55495527#[ derive( Accounts ) ]
5550- pub struct ResizeAmmCache < ' info > {
5528+ pub struct AddMarketToAmmCache < ' info > {
55515529 #[ account(
55525530 mut ,
55535531 constraint = admin. key( ) == admin_hot_wallet:: id( ) || admin. key( ) == state. admin
@@ -5558,22 +5536,23 @@ pub struct ResizeAmmCache<'info> {
55585536 mut ,
55595537 seeds = [ AMM_POSITIONS_CACHE . as_ref( ) ] ,
55605538 bump,
5561- realloc = AmmCache :: space( amm_cache. cache. len( ) + ( state . number_of_markets as usize - amm_cache . cache . len ( ) ) . min ( 20_usize ) ) ,
5539+ realloc = AmmCache :: space( amm_cache. cache. len( ) + 1 ) ,
55625540 realloc:: payer = admin,
55635541 realloc:: zero = false ,
55645542 ) ]
55655543 pub amm_cache : Box < Account < ' info , AmmCache > > ,
5544+ pub perp_market : AccountLoader < ' info , PerpMarket > ,
55665545 pub rent : Sysvar < ' info , Rent > ,
55675546 pub system_program : Program < ' info , System > ,
55685547}
55695548
55705549#[ derive( Accounts ) ]
55715550pub struct DeleteAmmCache < ' info > {
5572- #[ account( mut ) ]
5573- pub admin : Signer < ' info > ,
55745551 #[ account(
5575- has_one = admin
5552+ mut ,
5553+ constraint = admin. key( ) == admin_hot_wallet:: id( ) || admin. key( ) == state. admin
55765554 ) ]
5555+ pub admin : Signer < ' info > ,
55775556 pub state : Box < Account < ' info , State > > ,
55785557 #[ account(
55795558 mut ,
0 commit comments