@@ -1135,15 +1135,37 @@ pub fn handle_initialize_perp_market(
11351135
11361136pub fn handle_initialize_amm_cache ( ctx : Context < InitializeAmmCache > ) -> Result < ( ) > {
11371137 let amm_cache = & mut ctx. accounts . amm_cache ;
1138- let state = & ctx. accounts . state ;
1139- amm_cache
1140- . cache
1141- . resize_with ( state. number_of_markets as usize , CacheInfo :: default) ;
11421138 amm_cache. bump = ctx. bumps . amm_cache ;
11431139
11441140 Ok ( ( ) )
11451141}
11461142
1143+ pub fn handle_resize_amm_cache ( ctx : Context < ResizeAmmCache > ) -> Result < ( ) > {
1144+ let amm_cache = & mut ctx. accounts . amm_cache ;
1145+ let state = & ctx. accounts . state ;
1146+ let current_size = amm_cache. cache . len ( ) ;
1147+ let new_size = ( state. number_of_markets as usize ) . min ( current_size + 20_usize ) ;
1148+
1149+ msg ! (
1150+ "resizing amm cache from {} entries to {}" ,
1151+ current_size,
1152+ new_size
1153+ ) ;
1154+
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) ?;
1165+
1166+ Ok ( ( ) )
1167+ }
1168+
11471169#[ access_control(
11481170 perp_market_valid( & ctx. accounts. perp_market)
11491171) ]
@@ -5511,7 +5533,7 @@ pub struct InitializeAmmCache<'info> {
55115533 #[ account(
55125534 init,
55135535 seeds = [ AMM_POSITIONS_CACHE . as_ref( ) ] ,
5514- space = AmmCache :: space ( state . number_of_markets as usize ) ,
5536+ space = AmmCache :: init_space ( ) ,
55155537 bump,
55165538 payer = admin
55175539 ) ]
@@ -5520,6 +5542,27 @@ pub struct InitializeAmmCache<'info> {
55205542 pub system_program : Program < ' info , System > ,
55215543}
55225544
5545+ #[ derive( Accounts ) ]
5546+ pub struct ResizeAmmCache < ' info > {
5547+ #[ account(
5548+ mut ,
5549+ constraint = admin. key( ) == admin_hot_wallet:: id( ) || admin. key( ) == state. admin
5550+ ) ]
5551+ pub admin : Signer < ' info > ,
5552+ pub state : Box < Account < ' info , State > > ,
5553+ #[ account(
5554+ mut ,
5555+ seeds = [ AMM_POSITIONS_CACHE . as_ref( ) ] ,
5556+ bump,
5557+ realloc = AmmCache :: space( amm_cache. cache. len( ) + ( state. number_of_markets as usize - amm_cache. cache. len( ) ) . min( 20_usize ) ) ,
5558+ realloc:: payer = admin,
5559+ realloc:: zero = false ,
5560+ ) ]
5561+ pub amm_cache : Box < Account < ' info , AmmCache > > ,
5562+ pub rent : Sysvar < ' info , Rent > ,
5563+ pub system_program : Program < ' info , System > ,
5564+ }
5565+
55235566#[ derive( Accounts ) ]
55245567pub struct DeleteInitializedPerpMarket < ' info > {
55255568 #[ account( mut ) ]
0 commit comments