mango-v4/programs/mango-v4/src/instructions/serum3_cancel_order.rs

125 lines
3.8 KiB
Rust

use anchor_lang::prelude::*;
use serum_dex::instruction::CancelOrderInstructionV2;
use crate::error::*;
use crate::state::*;
use super::Serum3Side;
use super::{OpenOrdersAmounts, OpenOrdersSlim};
use crate::logs::Serum3OpenOrdersBalanceLog;
use crate::serum3_cpi::load_open_orders_ref;
#[derive(Accounts)]
pub struct Serum3CancelOrder<'info> {
pub group: AccountLoader<'info, Group>,
#[account(
mut,
has_one = group
// owner is checked at #1
)]
pub account: AccountLoader<'info, MangoAccountFixed>,
pub owner: Signer<'info>,
#[account(mut)]
/// CHECK: Validated inline by checking against the pubkey stored in the account at #2
pub open_orders: UncheckedAccount<'info>,
#[account(
has_one = group,
has_one = serum_program,
has_one = serum_market_external,
)]
pub serum_market: AccountLoader<'info, Serum3Market>,
/// CHECK: The pubkey is checked and then it's passed to the serum cpi
pub serum_program: UncheckedAccount<'info>,
#[account(mut)]
/// CHECK: The pubkey is checked and then it's passed to the serum cpi
pub serum_market_external: UncheckedAccount<'info>,
// These accounts are forwarded directly to the serum cpi call
// and are validated there.
#[account(mut)]
/// CHECK: Validated by the serum cpi call
pub market_bids: UncheckedAccount<'info>,
#[account(mut)]
/// CHECK: Validated by the serum cpi call
pub market_asks: UncheckedAccount<'info>,
#[account(mut)]
/// CHECK: Validated by the serum cpi call
pub market_event_queue: UncheckedAccount<'info>,
}
pub fn serum3_cancel_order(
ctx: Context<Serum3CancelOrder>,
side: Serum3Side,
order_id: u128,
) -> Result<()> {
let serum_market = ctx.accounts.serum_market.load()?;
//
// Validation
//
{
let account = ctx.accounts.account.load_full()?;
// account constraint #1
require!(
account.fixed.is_owner_or_delegate(ctx.accounts.owner.key()),
MangoError::SomeError
);
// Validate open_orders #2
require!(
account
.serum3_orders(serum_market.market_index)?
.open_orders
== ctx.accounts.open_orders.key(),
MangoError::SomeError
);
}
//
// Cancel
//
let order = serum_dex::instruction::CancelOrderInstructionV2 {
side: u8::try_from(side).unwrap().try_into().unwrap(),
order_id,
};
cpi_cancel_order(ctx.accounts, order)?;
let oo_ai = &ctx.accounts.open_orders.as_ref();
let open_orders = load_open_orders_ref(oo_ai)?;
let after_oo = OpenOrdersSlim::from_oo(&open_orders);
emit!(Serum3OpenOrdersBalanceLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
base_token_index: serum_market.base_token_index,
quote_token_index: serum_market.quote_token_index,
base_total: after_oo.native_base_total(),
base_free: after_oo.native_base_free(),
quote_total: after_oo.native_quote_total(),
quote_free: after_oo.native_quote_free(),
referrer_rebates_accrued: after_oo.native_rebates(),
});
Ok(())
}
fn cpi_cancel_order(ctx: &Serum3CancelOrder, order: CancelOrderInstructionV2) -> Result<()> {
use crate::serum3_cpi;
let group = ctx.group.load()?;
serum3_cpi::CancelOrder {
program: ctx.serum_program.to_account_info(),
market: ctx.serum_market_external.to_account_info(),
bids: ctx.market_bids.to_account_info(),
asks: ctx.market_asks.to_account_info(),
event_queue: ctx.market_event_queue.to_account_info(),
open_orders: ctx.open_orders.to_account_info(),
open_orders_authority: ctx.group.to_account_info(),
}
.cancel_one(&group, order)
}