Skip to content

Commit 722f553

Browse files
authored
add delete amm cache ix (#1999)
1 parent f297f60 commit 722f553

File tree

5 files changed

+136
-14
lines changed

5 files changed

+136
-14
lines changed

programs/drift/src/instructions/admin.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,11 @@ pub fn handle_resize_amm_cache(ctx: Context<ResizeAmmCache>) -> Result<()> {
11661166
Ok(())
11671167
}
11681168

1169+
pub fn handle_delete_amm_cache(_ctx: Context<DeleteAmmCache>) -> Result<()> {
1170+
msg!("deleted amm cache");
1171+
Ok(())
1172+
}
1173+
11691174
#[access_control(
11701175
perp_market_valid(&ctx.accounts.perp_market)
11711176
)]
@@ -5562,6 +5567,23 @@ pub struct ResizeAmmCache<'info> {
55625567
pub system_program: Program<'info, System>,
55635568
}
55645569

5570+
#[derive(Accounts)]
5571+
pub struct DeleteAmmCache<'info> {
5572+
#[account(mut)]
5573+
pub admin: Signer<'info>,
5574+
#[account(
5575+
has_one = admin
5576+
)]
5577+
pub state: Box<Account<'info, State>>,
5578+
#[account(
5579+
mut,
5580+
seeds = [AMM_POSITIONS_CACHE.as_ref()],
5581+
bump,
5582+
close = admin,
5583+
)]
5584+
pub amm_cache: Box<Account<'info, AmmCache>>,
5585+
}
5586+
55655587
#[derive(Accounts)]
55665588
pub struct DeleteInitializedPerpMarket<'info> {
55675589
#[account(mut)]

programs/drift/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,18 @@ pub mod drift {
10201020
handle_initialize_amm_cache(ctx)
10211021
}
10221022

1023+
pub fn resize_amm_cache<'c: 'info, 'info>(
1024+
ctx: Context<'_, '_, 'c, 'info, ResizeAmmCache<'info>>,
1025+
) -> Result<()> {
1026+
handle_resize_amm_cache(ctx)
1027+
}
1028+
1029+
pub fn delete_amm_cache<'c: 'info, 'info>(
1030+
ctx: Context<'_, '_, 'c, 'info, DeleteAmmCache<'info>>,
1031+
) -> Result<()> {
1032+
handle_delete_amm_cache(ctx)
1033+
}
1034+
10231035
pub fn update_initial_amm_cache_info<'c: 'info, 'info>(
10241036
ctx: Context<'_, '_, 'c, 'info, UpdateInitialAmmCacheInfo<'info>>,
10251037
) -> Result<()> {

sdk/src/adminClient.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ export class AdminClient extends DriftClient {
665665
public async resizeAmmCache(
666666
txParams?: TxParams
667667
): Promise<TransactionSignature> {
668-
const initializeAmmCacheIx = await this.getInitializeAmmCacheIx();
668+
const initializeAmmCacheIx = await this.getResizeAmmCacheIx();
669669

670670
const tx = await this.buildTransaction(initializeAmmCacheIx, txParams);
671671

@@ -688,6 +688,28 @@ export class AdminClient extends DriftClient {
688688
});
689689
}
690690

691+
public async deleteAmmCache(
692+
txParams?: TxParams
693+
): Promise<TransactionSignature> {
694+
const deleteAmmCacheIx = await this.getDeleteAmmCacheIx();
695+
696+
const tx = await this.buildTransaction(deleteAmmCacheIx, txParams);
697+
698+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
699+
700+
return txSig;
701+
}
702+
703+
public async getDeleteAmmCacheIx(): Promise<TransactionInstruction> {
704+
return await this.program.instruction.deleteAmmCache({
705+
accounts: {
706+
state: await this.getStatePublicKey(),
707+
admin: this.getStateAccount().admin,
708+
ammCache: getAmmCachePublicKey(this.program.programId),
709+
},
710+
});
711+
}
712+
691713
public async updateInitialAmmCacheInfo(
692714
perpMarketIndexes: number[],
693715
txParams?: TxParams

sdk/src/idl/drift.json

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,6 +4453,58 @@
44534453
],
44544454
"args": []
44554455
},
4456+
{
4457+
"name": "resizeAmmCache",
4458+
"accounts": [
4459+
{
4460+
"name": "admin",
4461+
"isMut": true,
4462+
"isSigner": true
4463+
},
4464+
{
4465+
"name": "state",
4466+
"isMut": false,
4467+
"isSigner": false
4468+
},
4469+
{
4470+
"name": "ammCache",
4471+
"isMut": true,
4472+
"isSigner": false
4473+
},
4474+
{
4475+
"name": "rent",
4476+
"isMut": false,
4477+
"isSigner": false
4478+
},
4479+
{
4480+
"name": "systemProgram",
4481+
"isMut": false,
4482+
"isSigner": false
4483+
}
4484+
],
4485+
"args": []
4486+
},
4487+
{
4488+
"name": "deleteAmmCache",
4489+
"accounts": [
4490+
{
4491+
"name": "admin",
4492+
"isMut": true,
4493+
"isSigner": true
4494+
},
4495+
{
4496+
"name": "state",
4497+
"isMut": false,
4498+
"isSigner": false
4499+
},
4500+
{
4501+
"name": "ammCache",
4502+
"isMut": true,
4503+
"isSigner": false
4504+
}
4505+
],
4506+
"args": []
4507+
},
44564508
{
44574509
"name": "updateInitialAmmCacheInfo",
44584510
"accounts": [
@@ -13058,12 +13110,6 @@
1305813110
"type": {
1305913111
"option": "u16"
1306013112
}
13061-
},
13062-
{
13063-
"name": "isolatedPositionDeposit",
13064-
"type": {
13065-
"option": "u64"
13066-
}
1306713113
}
1306813114
]
1306913115
}
@@ -13129,12 +13175,6 @@
1312913175
"type": {
1313013176
"option": "u16"
1313113177
}
13132-
},
13133-
{
13134-
"name": "isolatedPositionDeposit",
13135-
"type": {
13136-
"option": "u64"
13137-
}
1313813178
}
1313913179
]
1314013180
}
@@ -15118,6 +15158,9 @@
1511815158
},
1511915159
{
1512015160
"name": "SafeMMOracle"
15161+
},
15162+
{
15163+
"name": "Margin"
1512115164
}
1512215165
]
1512315166
}
@@ -19745,4 +19788,4 @@
1974519788
"msg": "Invalid Lp Pool Id for Operation"
1974619789
}
1974719790
]
19748-
}
19791+
}

tests/lpPool.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,4 +1733,27 @@ describe('LP Pool', () => {
17331733
assert(e.message.includes('0x18'));
17341734
}
17351735
});
1736+
1737+
it('can delete amm cache and then init and realloc and update', async () => {
1738+
const ammCacheKey = getAmmCachePublicKey(program.programId);
1739+
const ammCacheBefore = (await adminClient.program.account.ammCache.fetch(
1740+
ammCacheKey
1741+
)) as AmmCache;
1742+
1743+
await adminClient.deleteAmmCache();
1744+
await adminClient.initializeAmmCache();
1745+
await adminClient.resizeAmmCache();
1746+
await adminClient.updateInitialAmmCacheInfo([0, 1, 2]);
1747+
await adminClient.updateAmmCache([0, 1, 2]);
1748+
1749+
const ammCacheAfter = (await adminClient.program.account.ammCache.fetch(
1750+
ammCacheKey
1751+
)) as AmmCache;
1752+
1753+
for (let i = 0; i < ammCacheBefore.cache.length; i++) {
1754+
assert(
1755+
ammCacheBefore.cache[i].position.eq(ammCacheAfter.cache[i].position)
1756+
);
1757+
}
1758+
});
17361759
});

0 commit comments

Comments
 (0)