Skip to content

Commit

Permalink
program: add ix to pause deposits/withdraws if vault invariant broken
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Dec 16, 2024
1 parent 24ed432 commit bcb2a0f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
36 changes: 35 additions & 1 deletion programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::state::oracle_map::OracleMap;
use crate::state::order_params::{
OrderParams, PlaceOrderOptions, SwiftOrderParamsMessage, SwiftServerMessage,
};
use crate::state::paused_operations::PerpOperation;
use crate::state::paused_operations::{PerpOperation, SpotOperation};
use crate::state::perp_market::{ContractType, MarketStatus, PerpMarket};
use crate::state::perp_market_map::{
get_market_set_for_spot_positions, get_market_set_for_user_positions, get_market_set_from_list,
Expand Down Expand Up @@ -2409,6 +2409,27 @@ pub fn handle_force_delete_user<'c: 'info, 'info>(
Ok(())
}

pub fn handle_pause_spot_market_deposit_withdraw(
ctx: Context<PauseSpotMarketDepositWithdraw>,
) -> Result<()> {
let spot_market = &mut load_mut!(ctx.accounts.spot_market)?;

let result =validate_spot_market_vault_amount(spot_market, ctx.accounts.spot_market_vault.amount);

validate!(
matches!(result, Err(ErrorCode::SpotMarketVaultInvariantViolated)),
ErrorCode::DefaultError,
"spot market vault amount is valid"
)?;


spot_market.paused_operations = spot_market.paused_operations | SpotOperation::Deposit as u8;
spot_market.paused_operations = spot_market.paused_operations | SpotOperation::Withdraw as u8;

Ok(())
}


#[derive(Accounts)]
pub struct FillOrder<'info> {
pub state: Box<Account<'info, State>>,
Expand Down Expand Up @@ -2905,3 +2926,16 @@ pub struct ForceDeleteUser<'info> {
/// CHECK: forced drift_signer
pub drift_signer: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct PauseSpotMarketDepositWithdraw<'info> {
pub state: Box<Account<'info, State>>,
pub keeper: Signer<'info>,
#[account(mut)]
pub spot_market: AccountLoader<'info, SpotMarket>,
#[account(
seeds = [b"spot_market_vault".as_ref(), spot_market.load()?.market_index.to_le_bytes().as_ref()],
bump,
)]
pub spot_market_vault: Box<InterfaceAccount<'info, TokenAccount>>,
}
6 changes: 6 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,12 @@ pub mod drift {
handle_post_multi_pyth_pull_oracle_updates_atomic(ctx, params)
}

pub fn pause_spot_market_deposit_withdraw(
ctx: Context<PauseSpotMarketDepositWithdraw>,
) -> Result<()> {
handle_pause_spot_market_deposit_withdraw(ctx)
}

// Admin Instructions

pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
Expand Down
32 changes: 32 additions & 0 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8936,6 +8936,38 @@ export class DriftClient {
return ix;
}

public async getPauseSpotMarketDepositWithdrawIx(
spotMarketIndex: number
): Promise<TransactionInstruction> {
const spotMarket = await this.getSpotMarketAccount(spotMarketIndex);
return this.program.instruction.pauseSpotMarketDepositWithdraw(
spotMarketIndex,
{
accounts: {
state: await this.getStatePublicKey(),
keeper: this.wallet.publicKey,
spotMarket: spotMarket.pubkey,
spotMarketVault: spotMarket.vault,
},
}
);
}

public async pauseSpotMarketDepositWithdraw(
spotMarketIndex: number,
txParams?: TxParams
): Promise<TransactionSignature> {
const { txSig } = await this.sendTransaction(
await this.buildTransaction(
await this.getPauseSpotMarketDepositWithdrawIx(spotMarketIndex),
txParams
),
[],
this.opts
);
return txSig;
}

private handleSignedTransaction(signedTxs: SignedTxData[]) {
if (this.enableMetricsEvents && this.metricsEventEmitter) {
this.metricsEventEmitter.emit('txSigned', signedTxs);
Expand Down

0 comments on commit bcb2a0f

Please sign in to comment.