Skip to content

Commit

Permalink
program: improve update prelaunch oracles and add ability to delete (#…
Browse files Browse the repository at this point in the history
…956)

* account for prelaunch updating market

* add test cases

* add delete prelaunch

* CHANGELOG
  • Loading branch information
crispheaney committed Mar 13, 2024
1 parent 50a0746 commit 17dceec
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Features

- program: improve update prelaunch oracles and add ability to delete ([#956](https://github.com/drift-labs/protocol-v2/pull/956))
- program: allow user to settle realized pnl in reduce only market status ([#954](https://github.com/drift-labs/protocol-v2/pull/954))

### Fixes
Expand Down
59 changes: 57 additions & 2 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2440,12 +2440,25 @@ pub fn handle_update_prelaunch_oracle_params<'info>(
params: PrelaunchOracleParams,
) -> Result<()> {
let mut oracle = ctx.accounts.prelaunch_oracle.load_mut()?;

oracle.perp_market_index = params.perp_market_index;
let mut perp_market = ctx.accounts.perp_market.load_mut()?;
let now = Clock::get()?.unix_timestamp;

if let Some(price) = params.price {
oracle.price = price;

msg!("before mark twap ts = {:?} mark twap = {:?} mark twap 5min = {:?} bid twap = {:?} ask twap {:?}", perp_market.amm.last_mark_price_twap_ts, perp_market.amm.last_mark_price_twap, perp_market.amm.last_mark_price_twap_5min, perp_market.amm.last_bid_price_twap, perp_market.amm.last_ask_price_twap);

perp_market.amm.last_mark_price_twap_ts = now;
perp_market.amm.last_mark_price_twap = price.cast()?;
perp_market.amm.last_mark_price_twap_5min = price.cast()?;
perp_market.amm.last_bid_price_twap =
perp_market.amm.last_bid_price_twap.min(price.cast()?);
perp_market.amm.last_ask_price_twap =
perp_market.amm.last_ask_price_twap.max(price.cast()?);

msg!("after mark twap ts = {:?} mark twap = {:?} mark twap 5min = {:?} bid twap = {:?} ask twap {:?}", perp_market.amm.last_mark_price_twap_ts, perp_market.amm.last_mark_price_twap, perp_market.amm.last_mark_price_twap_5min, perp_market.amm.last_bid_price_twap, perp_market.amm.last_ask_price_twap);
}

if let Some(max_price) = params.max_price {
oracle.max_price = max_price;
}
Expand All @@ -2455,6 +2468,21 @@ pub fn handle_update_prelaunch_oracle_params<'info>(
Ok(())
}

pub fn handle_delete_prelaunch_oracle<'info>(
ctx: Context<DeletePrelaunchOracle<'info>>,
_perp_market_index: u16,
) -> Result<()> {
let perp_market = ctx.accounts.perp_market.load()?;

validate!(
perp_market.amm.oracle != ctx.accounts.prelaunch_oracle.key(),
ErrorCode::DefaultError,
"prelaunch oracle currently in use"
)?;

Ok(())
}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(mut)]
Expand Down Expand Up @@ -2919,6 +2947,33 @@ pub struct UpdatePrelaunchOracleParams<'info> {
bump,
)]
pub prelaunch_oracle: AccountLoader<'info, PrelaunchOracle>,
#[account(
mut,
constraint = perp_market.load()?.market_index == params.perp_market_index
)]
pub perp_market: AccountLoader<'info, PerpMarket>,
#[account(
has_one = admin
)]
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
)]
Expand Down
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(
ctx: Context<DeletePrelaunchOracle>,
perp_market_index: u16,
) -> Result<()> {
handle_delete_prelaunch_oracle(ctx, perp_market_index)
}
}

#[cfg(not(feature = "no-entrypoint"))]
Expand Down
23 changes: 23 additions & 0 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,29 @@ export class AdminClient extends DriftClient {
this.program.programId,
perpMarketIndex
),
perpMarket: await getPerpMarketPublicKey(
this.program.programId,
perpMarketIndex
),
},
});
}

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
),
},
});
}
Expand Down
36 changes: 36 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -4993,6 +4993,11 @@
"isMut": true,
"isSigner": false
},
{
"name": "perpMarket",
"isMut": true,
"isSigner": false
},
{
"name": "state",
"isMut": false,
Expand All @@ -5007,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
31 changes: 31 additions & 0 deletions 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 @@ -208,5 +209,35 @@ describe('prelisting', () => {
await adminDriftClient.fetchAccounts();
const price = adminDriftClient.getOracleDataForPerpMarket(0);
assert(price.price.eq(new BN(40000000)));

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 17dceec

Please sign in to comment.