Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow purging of anyone's orders and positions in force-closed markets #1009

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions programs/mango-v4/src/accounts_ix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub use perp_liq_base_or_positive_pnl::*;
pub use perp_liq_force_cancel_orders::*;
pub use perp_liq_negative_pnl_or_bankruptcy::*;
pub use perp_place_order::*;
pub use perp_prune_orders::*;
pub use perp_settle_fees::*;
pub use perp_settle_pnl::*;
pub use perp_update_funding::*;
Expand Down Expand Up @@ -125,6 +126,7 @@ mod perp_liq_base_or_positive_pnl;
mod perp_liq_force_cancel_orders;
mod perp_liq_negative_pnl_or_bankruptcy;
mod perp_place_order;
mod perp_prune_orders;
mod perp_settle_fees;
mod perp_settle_pnl;
mod perp_update_funding;
Expand Down
27 changes: 27 additions & 0 deletions programs/mango-v4/src/accounts_ix/perp_prune_orders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::state::{BookSide, Group, PerpMarket};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct PerpPruneOrders<'info> {
#[account()]
pub group: AccountLoader<'info, Group>,

#[account(
mut,
has_one = group,
// owner is not checked on purpose
)]
pub account: AccountLoader<'info, MangoAccountFixed>,

#[account(
mut,
has_one = group,
has_one = bids,
has_one = asks,
)]
pub perp_market: AccountLoader<'info, PerpMarket>,
#[account(mut)]
pub bids: AccountLoader<'info, BookSide>,
#[account(mut)]
pub asks: AccountLoader<'info, BookSide>,
}
2 changes: 2 additions & 0 deletions programs/mango-v4/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use perp_liq_base_or_positive_pnl::*;
pub use perp_liq_force_cancel_orders::*;
pub use perp_liq_negative_pnl_or_bankruptcy::*;
pub use perp_place_order::*;
pub use perp_prune_orders::*;
pub use perp_settle_fees::*;
pub use perp_settle_pnl::*;
pub use perp_update_funding::*;
Expand Down Expand Up @@ -106,6 +107,7 @@ mod perp_liq_base_or_positive_pnl;
mod perp_liq_force_cancel_orders;
mod perp_liq_negative_pnl_or_bankruptcy;
mod perp_place_order;
mod perp_prune_orders;
mod perp_settle_fees;
mod perp_settle_pnl;
mod perp_update_funding;
Expand Down
25 changes: 25 additions & 0 deletions programs/mango-v4/src/instructions/perp_prune_orders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anchor_lang::prelude::*;

use crate::accounts_ix::*;
use crate::error::MangoError;
use crate::state::*;

pub fn perp_prune_orders(ctx: Context<PerpPruneOrders>, limit: u8) -> Result<()> {
let mut perp_market: PerpMarket = ctx.accounts.perp_market.load_mut()?;
// only allow pruning orders when market is in force-close
require!(perp_market.is_force_close(), MangoError::SomeError);

let mut account = ctx.accounts.account.load_full_mut()?;
let mut book = Orderbook {
bids: ctx.accounts.bids.load_mut()?,
asks: ctx.accounts.asks.load_mut()?,
};

book.cancel_all_orders(
account,
&ctx.accounts.account.key(),
&mut perp_market,
limit,
None,
)
}
6 changes: 6 additions & 0 deletions programs/mango-v4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,12 @@ pub mod mango_v4 {
Ok(())
}

pub fn perp_prune_orders(ctx: Context<PerpPruneOrders>, limit: u8) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::perp_prune_orders(ctx, limit)?;
Ok(())
}

pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::perp_consume_events(ctx, limit)?;
Expand Down
Loading