Skip to content

Commit

Permalink
add delete prelaunch
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Mar 13, 2024
1 parent 182a31a commit 749b632
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
37 changes: 37 additions & 0 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,21 @@ pub fn handle_update_prelaunch_oracle_params<'info>(
Ok(())
}

pub fn handle_delete_prelaunch_oracle<'info>(

Check warning on line 2471 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2471

Added line #L2471 was not covered by tests
ctx: Context<DeletePrelaunchOracle<'info>>,
_perp_market_index: u16,
) -> Result<()> {
let perp_market = ctx.accounts.perp_market.load()?;

Check warning on line 2475 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2475

Added line #L2475 was not covered by tests

validate!(
perp_market.amm.oracle != ctx.accounts.prelaunch_oracle.key(),
ErrorCode::DefaultError,

Check warning on line 2479 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2477-L2479

Added lines #L2477 - L2479 were not covered by tests
"prelaunch oracle currently in use"
)?;

Ok(())

Check warning on line 2483 in programs/drift/src/instructions/admin.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/instructions/admin.rs#L2483

Added line #L2483 was not covered by tests
}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(mut)]
Expand Down Expand Up @@ -2942,3 +2957,25 @@ pub struct UpdatePrelaunchOracleParams<'info> {
)]
pub state: Box<Account<'info, State>>,
}

#[derive(Accounts)]
#[instruction(perp_market_index: u16,)]
pub struct DeletePrelaunchOracle<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [b"prelaunch_oracle".as_ref(), perp_market_index.to_le_bytes().as_ref()],
bump,
close = admin
)]
pub prelaunch_oracle: AccountLoader<'info, PrelaunchOracle>,
#[account(
constraint = perp_market.load()?.market_index == perp_market_index
)]
pub perp_market: AccountLoader<'info, PerpMarket>,
#[account(
has_one = admin
)]
pub state: Box<Account<'info, State>>,
}
7 changes: 7 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,13 @@ pub mod drift {
) -> Result<()> {
handle_update_prelaunch_oracle_params(ctx, params)
}

pub fn delete_prelaunch_oracle(

Check warning on line 1175 in programs/drift/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/lib.rs#L1175

Added line #L1175 was not covered by tests
ctx: Context<DeletePrelaunchOracle>,
perp_market_index: u16,
) -> Result<()> {
handle_delete_prelaunch_oracle(ctx, perp_market_index)

Check warning on line 1179 in programs/drift/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/lib.rs#L1179

Added line #L1179 was not covered by tests
}
}

#[cfg(not(feature = "no-entrypoint"))]
Expand Down
19 changes: 19 additions & 0 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1871,4 +1871,23 @@ export class AdminClient extends DriftClient {
},
});
}

public async deletePrelaunchOracle(
perpMarketIndex: number
): Promise<TransactionSignature> {
return await this.program.rpc.deletePrelaunchOracle(perpMarketIndex, {
accounts: {
admin: this.wallet.publicKey,
state: await this.getStatePublicKey(),
prelaunchOracle: await getPrelaunchOraclePublicKey(
this.program.programId,
perpMarketIndex
),
perpMarket: await getPerpMarketPublicKey(
this.program.programId,
perpMarketIndex
),
},
});
}
}
31 changes: 31 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -5012,6 +5012,37 @@
}
}
]
},
{
"name": "deletePrelaunchOracle",
"accounts": [
{
"name": "admin",
"isMut": true,
"isSigner": true
},
{
"name": "prelaunchOracle",
"isMut": true,
"isSigner": false
},
{
"name": "perpMarket",
"isMut": false,
"isSigner": false
},
{
"name": "state",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "perpMarketIndex",
"type": "u16"
}
]
}
],
"accounts": [
Expand Down
30 changes: 29 additions & 1 deletion tests/prelisting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {

import {
initializeQuoteSpotMarket,
mockOracle,
mockUSDCMint,
mockUserUSDCAccount,
} from './testHelpers';
Expand Down Expand Up @@ -209,7 +210,34 @@ describe('prelisting', () => {
const price = adminDriftClient.getOracleDataForPerpMarket(0);
assert(price.price.eq(new BN(40000000)));

const markTwap = adminDriftClient.getPerpMarketAccount(0).amm.lastMarkPriceTwap;
const markTwap =
adminDriftClient.getPerpMarketAccount(0).amm.lastMarkPriceTwap;
assert(markTwap.eq(new BN(40000000)));
});

it('delete', async () => {
try {
await adminDriftClient.deletePrelaunchOracle(0);
assert(false);
} catch (e) {
console.log('Delete successfully failed');
}

const oldOracleKey = adminDriftClient.getPerpMarketAccount(0).amm.oracle;

const newOracle = await mockOracle(40);
await adminDriftClient.updatePerpMarketOracle(
0,
newOracle,
OracleSource.PYTH
);

await adminDriftClient.deletePrelaunchOracle(0);

const result = await connection.getAccountInfoAndContext(
oldOracleKey,
'processed'
);
assert(result.value === null);
});
});

0 comments on commit 749b632

Please sign in to comment.