@@ -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}
@@ -3819,7 +3812,14 @@ pub fn handle_update_perp_market_oracle(
38193812 perp_market. amm . oracle = oracle;
38203813 perp_market. amm . oracle_source = oracle_source;
38213814
3822- amm_cache. update_perp_market_fields ( perp_market) ?;
3815+ if amm_cache
3816+ . cache
3817+ . iter ( )
3818+ . find ( |cache_info| cache_info. market_index == perp_market. market_index )
3819+ . is_some ( )
3820+ {
3821+ amm_cache. update_perp_market_fields ( perp_market) ?;
3822+ }
38233823
38243824 Ok ( ( ) )
38253825}
@@ -5511,15 +5511,6 @@ pub struct InitializePerpMarket<'info> {
55115511 payer = admin
55125512 ) ]
55135513 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 > > ,
55235514 /// CHECK: checked in `initialize_perp_market`
55245515 pub oracle : AccountInfo < ' info > ,
55255516 pub rent : Sysvar < ' info , Rent > ,
@@ -5547,7 +5538,7 @@ pub struct InitializeAmmCache<'info> {
55475538}
55485539
55495540#[ derive( Accounts ) ]
5550- pub struct ResizeAmmCache < ' info > {
5541+ pub struct AddMarketToAmmCache < ' info > {
55515542 #[ account(
55525543 mut ,
55535544 constraint = admin. key( ) == admin_hot_wallet:: id( ) || admin. key( ) == state. admin
@@ -5558,22 +5549,23 @@ pub struct ResizeAmmCache<'info> {
55585549 mut ,
55595550 seeds = [ AMM_POSITIONS_CACHE . as_ref( ) ] ,
55605551 bump,
5561- realloc = AmmCache :: space( amm_cache. cache. len( ) + ( state . number_of_markets as usize - amm_cache . cache . len ( ) ) . min ( 20_usize ) ) ,
5552+ realloc = AmmCache :: space( amm_cache. cache. len( ) + 1 ) ,
55625553 realloc:: payer = admin,
55635554 realloc:: zero = false ,
55645555 ) ]
55655556 pub amm_cache : Box < Account < ' info , AmmCache > > ,
5557+ pub perp_market : AccountLoader < ' info , PerpMarket > ,
55665558 pub rent : Sysvar < ' info , Rent > ,
55675559 pub system_program : Program < ' info , System > ,
55685560}
55695561
55705562#[ derive( Accounts ) ]
55715563pub struct DeleteAmmCache < ' info > {
5572- #[ account( mut ) ]
5573- pub admin : Signer < ' info > ,
55745564 #[ account(
5575- has_one = admin
5565+ mut ,
5566+ constraint = admin. key( ) == admin_hot_wallet:: id( ) || admin. key( ) == state. admin
55765567 ) ]
5568+ pub admin : Signer < ' info > ,
55775569 pub state : Box < Account < ' info , State > > ,
55785570 #[ account(
55795571 mut ,
0 commit comments