Skip to content

Commit bc1ae68

Browse files
committed
refactor amm cache
1 parent 72a5e42 commit bc1ae68

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

programs/drift/src/instructions/lp_admin.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ids::{admin_hot_wallet, lp_pool_swap_wallet};
44
use crate::instructions::optional_accounts::{get_token_mint, load_maps, AccountMaps};
55
use crate::math::constants::{PRICE_PRECISION_U64, QUOTE_SPOT_MARKET_INDEX};
66
use crate::math::safe_math::SafeMath;
7-
use crate::state::amm_cache::{AmmCache, AMM_POSITIONS_CACHE};
7+
use crate::state::amm_cache::{AmmCache, CacheInfo, AMM_POSITIONS_CACHE};
88
use crate::state::lp_pool::{
99
AmmConstituentDatum, AmmConstituentMapping, Constituent, ConstituentCorrelations,
1010
ConstituentTargetBase, LPPool, TargetsDatum, AMM_MAP_PDA_SEED,
@@ -937,6 +937,21 @@ pub fn handle_update_initial_amm_cache_info<'c: 'info, 'info>(
937937

938938
Ok(())
939939
}
940+
941+
pub fn handle_reset_amm_cache(ctx: Context<ResetAmmCache>) -> Result<()> {
942+
let state = &ctx.accounts.state;
943+
let amm_cache = &mut ctx.accounts.amm_cache;
944+
945+
amm_cache.cache.clear();
946+
amm_cache
947+
.cache
948+
.resize_with(state.number_of_markets as usize, CacheInfo::default);
949+
amm_cache.validate(state)?;
950+
951+
msg!("AMM cache reset. markets: {}", state.number_of_markets);
952+
Ok(())
953+
}
954+
940955
#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)]
941956
pub struct OverrideAmmCacheParams {
942957
pub quote_owed_from_lp_pool: Option<i64>,
@@ -1368,3 +1383,23 @@ pub struct UpdateInitialAmmCacheInfo<'info> {
13681383
)]
13691384
pub amm_cache: Box<Account<'info, AmmCache>>,
13701385
}
1386+
1387+
#[derive(Accounts)]
1388+
pub struct ResetAmmCache<'info> {
1389+
#[account(
1390+
mut,
1391+
constraint = admin.key() == admin_hot_wallet::id() || admin.key() == state.admin
1392+
)]
1393+
pub admin: Signer<'info>,
1394+
pub state: Box<Account<'info, State>>,
1395+
#[account(
1396+
mut,
1397+
seeds = [AMM_POSITIONS_CACHE.as_ref()],
1398+
bump = amm_cache.bump,
1399+
realloc = AmmCache::space(state.number_of_markets as usize),
1400+
realloc::payer = admin,
1401+
realloc::zero = false,
1402+
)]
1403+
pub amm_cache: Box<Account<'info, AmmCache>>,
1404+
pub system_program: Program<'info, System>,
1405+
}

programs/drift/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,12 @@ pub mod drift {
20662066
handle_override_amm_cache_info(ctx, market_index, override_params)
20672067
}
20682068

2069+
pub fn reset_amm_cache<'c: 'info, 'info>(
2070+
ctx: Context<'_, '_, 'c, 'info, ResetAmmCache<'info>>,
2071+
) -> Result<()> {
2072+
handle_reset_amm_cache(ctx)
2073+
}
2074+
20692075
pub fn lp_pool_swap<'c: 'info, 'info>(
20702076
ctx: Context<'_, '_, 'c, 'info, LPPoolSwap<'info>>,
20712077
in_market_index: u16,

programs/drift/src/state/amm_cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ pub struct CacheInfo {
5757
pub oracle_validity: u8,
5858
pub lp_status_for_perp_market: u8,
5959
pub amm_position_scalar: u8,
60-
_padding: [u8; 28],
60+
_padding: [u8; 36],
6161
}
6262

6363
impl Size for CacheInfo {
64-
const SIZE: usize = 224;
64+
const SIZE: usize = 230;
6565
}
6666

6767
impl Default for CacheInfo {
@@ -86,7 +86,7 @@ impl Default for CacheInfo {
8686
quote_owed_from_lp_pool: 0i64,
8787
lp_status_for_perp_market: 0u8,
8888
amm_position_scalar: 0u8,
89-
_padding: [0u8; 28],
89+
_padding: [0u8; 36],
9090
}
9191
}
9292
}

sdk/src/idl/drift.json

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8427,6 +8427,32 @@
84278427
}
84288428
]
84298429
},
8430+
{
8431+
"name": "resetAmmCache",
8432+
"accounts": [
8433+
{
8434+
"name": "admin",
8435+
"isMut": true,
8436+
"isSigner": true
8437+
},
8438+
{
8439+
"name": "state",
8440+
"isMut": false,
8441+
"isSigner": false
8442+
},
8443+
{
8444+
"name": "ammCache",
8445+
"isMut": true,
8446+
"isSigner": false
8447+
},
8448+
{
8449+
"name": "systemProgram",
8450+
"isMut": false,
8451+
"isSigner": false
8452+
}
8453+
],
8454+
"args": []
8455+
},
84308456
{
84318457
"name": "lpPoolSwap",
84328458
"accounts": [
@@ -12193,7 +12219,7 @@
1219312219
"type": {
1219412220
"array": [
1219512221
"u8",
12196-
28
12222+
36
1219712223
]
1219812224
}
1219912225
}

0 commit comments

Comments
 (0)