2022-03-19 04:11:56 -07:00
|
|
|
use anchor_lang::prelude::*;
|
2022-06-21 04:02:30 -07:00
|
|
|
|
2022-04-01 23:59:07 -07:00
|
|
|
use serum_dex::instruction::CancelOrderInstructionV2;
|
2022-03-19 04:11:56 -07:00
|
|
|
|
|
|
|
use crate::error::*;
|
2022-06-21 02:45:38 -07:00
|
|
|
use crate::serum3_cpi::load_open_orders_ref;
|
2022-03-19 04:11:56 -07:00
|
|
|
use crate::state::*;
|
|
|
|
|
2022-06-21 02:45:38 -07:00
|
|
|
use super::OpenOrdersSlim;
|
2022-04-01 23:59:07 -07:00
|
|
|
use super::Serum3Side;
|
2022-06-21 02:45:38 -07:00
|
|
|
use checked_math as cm;
|
2022-03-19 04:11:56 -07:00
|
|
|
|
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct Serum3CancelOrder<'info> {
|
|
|
|
pub group: AccountLoader<'info, Group>,
|
|
|
|
|
2022-07-25 07:07:53 -07:00
|
|
|
#[account(mut, has_one = group)]
|
|
|
|
pub account: AccountLoaderDynamic<'info, MangoAccount>,
|
2022-03-19 04:11:56 -07:00
|
|
|
pub owner: Signer<'info>,
|
|
|
|
|
|
|
|
#[account(mut)]
|
2022-06-22 03:45:42 -07:00
|
|
|
/// CHECK: Validated inline by checking against the pubkey stored in the account
|
2022-03-19 04:11:56 -07:00
|
|
|
pub open_orders: UncheckedAccount<'info>,
|
|
|
|
|
|
|
|
#[account(
|
|
|
|
has_one = group,
|
|
|
|
has_one = serum_program,
|
|
|
|
has_one = serum_market_external,
|
|
|
|
)]
|
|
|
|
pub serum_market: AccountLoader<'info, Serum3Market>,
|
2022-06-22 03:45:42 -07:00
|
|
|
/// CHECK: The pubkey is checked and then it's passed to the serum cpi
|
2022-03-19 04:11:56 -07:00
|
|
|
pub serum_program: UncheckedAccount<'info>,
|
|
|
|
#[account(mut)]
|
2022-06-22 03:45:42 -07:00
|
|
|
/// CHECK: The pubkey is checked and then it's passed to the serum cpi
|
2022-03-19 04:11:56 -07:00
|
|
|
pub serum_market_external: UncheckedAccount<'info>,
|
|
|
|
|
|
|
|
// These accounts are forwarded directly to the serum cpi call
|
|
|
|
// and are validated there.
|
2022-06-27 02:38:55 -07:00
|
|
|
#[account(mut)]
|
2022-06-22 03:45:42 -07:00
|
|
|
/// CHECK: Validated by the serum cpi call
|
2022-03-19 04:11:56 -07:00
|
|
|
pub market_bids: UncheckedAccount<'info>,
|
|
|
|
#[account(mut)]
|
2022-06-22 03:45:42 -07:00
|
|
|
/// CHECK: Validated by the serum cpi call
|
2022-03-19 04:11:56 -07:00
|
|
|
pub market_asks: UncheckedAccount<'info>,
|
|
|
|
#[account(mut)]
|
2022-06-22 03:45:42 -07:00
|
|
|
/// CHECK: Validated by the serum cpi call
|
2022-03-19 04:11:56 -07:00
|
|
|
pub market_event_queue: UncheckedAccount<'info>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serum3_cancel_order(
|
|
|
|
ctx: Context<Serum3CancelOrder>,
|
2022-04-01 23:59:07 -07:00
|
|
|
side: Serum3Side,
|
|
|
|
order_id: u128,
|
2022-03-19 04:11:56 -07:00
|
|
|
) -> Result<()> {
|
2022-06-21 02:45:38 -07:00
|
|
|
let serum_market = ctx.accounts.serum_market.load()?;
|
|
|
|
|
2022-03-19 04:11:56 -07:00
|
|
|
//
|
|
|
|
// Validation
|
|
|
|
//
|
|
|
|
{
|
|
|
|
let account = ctx.accounts.account.load()?;
|
2022-07-25 07:07:53 -07:00
|
|
|
require!(
|
|
|
|
account.fixed.is_owner_or_delegate(ctx.accounts.owner.key()),
|
|
|
|
MangoError::SomeError
|
|
|
|
);
|
|
|
|
|
|
|
|
require!(!account.fixed.is_bankrupt(), MangoError::IsBankrupt);
|
2022-03-30 01:00:52 -07:00
|
|
|
|
2022-03-19 04:11:56 -07:00
|
|
|
// Validate open_orders
|
|
|
|
require!(
|
|
|
|
account
|
2022-07-25 07:07:53 -07:00
|
|
|
.serum3_find(serum_market.market_index)
|
2022-03-19 04:53:24 -07:00
|
|
|
.ok_or_else(|| error!(MangoError::SomeError))?
|
2022-03-19 04:11:56 -07:00
|
|
|
.open_orders
|
|
|
|
== ctx.accounts.open_orders.key(),
|
|
|
|
MangoError::SomeError
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2022-03-21 04:55:05 -07:00
|
|
|
// Cancel
|
2022-03-19 04:11:56 -07:00
|
|
|
//
|
2022-06-21 02:45:38 -07:00
|
|
|
let before_oo = {
|
|
|
|
let open_orders = load_open_orders_ref(ctx.accounts.open_orders.as_ref())?;
|
2022-06-21 04:02:30 -07:00
|
|
|
OpenOrdersSlim::from_oo(&open_orders)
|
2022-06-21 02:45:38 -07:00
|
|
|
};
|
2022-04-01 23:59:07 -07:00
|
|
|
let order = serum_dex::instruction::CancelOrderInstructionV2 {
|
2022-04-02 00:52:40 -07:00
|
|
|
side: u8::try_from(side).unwrap().try_into().unwrap(),
|
2022-04-01 23:59:07 -07:00
|
|
|
order_id,
|
|
|
|
};
|
2022-03-23 01:33:51 -07:00
|
|
|
cpi_cancel_order(ctx.accounts, order)?;
|
2022-03-19 04:11:56 -07:00
|
|
|
|
2022-06-21 02:45:38 -07:00
|
|
|
{
|
|
|
|
let open_orders = load_open_orders_ref(ctx.accounts.open_orders.as_ref())?;
|
2022-06-21 04:02:30 -07:00
|
|
|
let after_oo = OpenOrdersSlim::from_oo(&open_orders);
|
2022-06-21 02:45:38 -07:00
|
|
|
let mut account = ctx.accounts.account.load_mut()?;
|
|
|
|
decrease_maybe_loan(
|
|
|
|
serum_market.market_index,
|
2022-07-25 07:07:53 -07:00
|
|
|
&mut account.borrow_mut(),
|
2022-06-21 02:45:38 -07:00
|
|
|
&before_oo,
|
|
|
|
&after_oo,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-19 04:11:56 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-21 02:45:38 -07:00
|
|
|
// if free has increased, the free increase is reduction in reserved, reduce this from
|
|
|
|
// the cached
|
|
|
|
pub fn decrease_maybe_loan(
|
|
|
|
market_index: Serum3MarketIndex,
|
2022-07-25 07:07:53 -07:00
|
|
|
account: &mut MangoAccountRefMut,
|
2022-06-21 02:45:38 -07:00
|
|
|
before_oo: &OpenOrdersSlim,
|
|
|
|
after_oo: &OpenOrdersSlim,
|
|
|
|
) {
|
2022-07-25 07:07:53 -07:00
|
|
|
let serum3_account = account.serum3_find_mut(market_index).unwrap();
|
2022-06-21 02:45:38 -07:00
|
|
|
|
|
|
|
if after_oo.native_coin_free > before_oo.native_coin_free {
|
|
|
|
let native_coin_free_increase = after_oo.native_coin_free - before_oo.native_coin_free;
|
|
|
|
serum3_account.previous_native_coin_reserved =
|
|
|
|
cm!(serum3_account.previous_native_coin_reserved - native_coin_free_increase);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pc
|
|
|
|
if after_oo.native_pc_free > before_oo.native_pc_free {
|
|
|
|
let free_pc_increase = after_oo.native_pc_free - before_oo.native_pc_free;
|
|
|
|
serum3_account.previous_native_pc_reserved =
|
|
|
|
cm!(serum3_account.previous_native_pc_reserved - free_pc_increase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 23:59:07 -07:00
|
|
|
fn cpi_cancel_order(ctx: &Serum3CancelOrder, order: CancelOrderInstructionV2) -> Result<()> {
|
2022-03-19 04:11:56 -07:00
|
|
|
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(),
|
|
|
|
}
|
2022-04-01 23:59:07 -07:00
|
|
|
.cancel_one(&group, order)
|
2022-03-19 04:11:56 -07:00
|
|
|
}
|