zero out perp position and orders for a broken market (#413)
* zero out perp position and orders for a broken market Signed-off-by: microwavedcola1 <microwavedcola@gmail.com> * fix Signed-off-by: microwavedcola1 <microwavedcola@gmail.com> * client code Signed-off-by: microwavedcola1 <microwavedcola@gmail.com> * fixes from review Signed-off-by: microwavedcola1 <microwavedcola@gmail.com> Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
c30fa1dbb3
commit
f0c797a2e4
|
@ -29,6 +29,7 @@ pub use perp_place_order::*;
|
||||||
pub use perp_settle_fees::*;
|
pub use perp_settle_fees::*;
|
||||||
pub use perp_settle_pnl::*;
|
pub use perp_settle_pnl::*;
|
||||||
pub use perp_update_funding::*;
|
pub use perp_update_funding::*;
|
||||||
|
pub use perp_zero_out::*;
|
||||||
pub use serum3_cancel_all_orders::*;
|
pub use serum3_cancel_all_orders::*;
|
||||||
pub use serum3_cancel_order::*;
|
pub use serum3_cancel_order::*;
|
||||||
pub use serum3_close_open_orders::*;
|
pub use serum3_close_open_orders::*;
|
||||||
|
@ -84,6 +85,7 @@ mod perp_place_order;
|
||||||
mod perp_settle_fees;
|
mod perp_settle_fees;
|
||||||
mod perp_settle_pnl;
|
mod perp_settle_pnl;
|
||||||
mod perp_update_funding;
|
mod perp_update_funding;
|
||||||
|
mod perp_zero_out;
|
||||||
mod serum3_cancel_all_orders;
|
mod serum3_cancel_all_orders;
|
||||||
mod serum3_cancel_order;
|
mod serum3_cancel_order;
|
||||||
mod serum3_close_open_orders;
|
mod serum3_close_open_orders;
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
use anchor_lang::prelude::*;
|
||||||
|
|
||||||
|
use crate::error::*;
|
||||||
|
use crate::state::*;
|
||||||
|
|
||||||
|
#[derive(Accounts)]
|
||||||
|
pub struct PerpZeroOutForMarket<'info> {
|
||||||
|
#[account(
|
||||||
|
has_one = admin,
|
||||||
|
constraint = group.load()?.is_operational() @ MangoError::GroupIsHalted,
|
||||||
|
constraint = group.load()?.is_testing()
|
||||||
|
)]
|
||||||
|
pub group: AccountLoader<'info, Group>,
|
||||||
|
|
||||||
|
#[account(
|
||||||
|
mut,
|
||||||
|
has_one = group,
|
||||||
|
constraint = account.load()?.is_operational() @ MangoError::AccountIsFrozen
|
||||||
|
)]
|
||||||
|
pub account: AccountLoader<'info, MangoAccountFixed>,
|
||||||
|
|
||||||
|
#[account(
|
||||||
|
has_one = group,
|
||||||
|
constraint = perp_market.load()?.perp_market_index == 1
|
||||||
|
)]
|
||||||
|
pub perp_market: AccountLoader<'info, PerpMarket>,
|
||||||
|
|
||||||
|
pub admin: Signer<'info>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn perp_zero_out_for_market(ctx: Context<PerpZeroOutForMarket>) -> Result<()> {
|
||||||
|
let mut account = ctx.accounts.account.load_full_mut()?;
|
||||||
|
|
||||||
|
let perp_market = ctx.accounts.perp_market.load()?;
|
||||||
|
|
||||||
|
let perp_position = account.perp_position_mut(perp_market.perp_market_index)?;
|
||||||
|
*perp_position = PerpPosition::default();
|
||||||
|
|
||||||
|
for i in 0..account.header.perp_oo_count() {
|
||||||
|
let oo = account.perp_order_mut_by_raw_index(i);
|
||||||
|
if !oo.is_active_for_market(perp_market.perp_market_index) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
*oo = PerpOpenOrder::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -557,6 +557,10 @@ pub mod mango_v4 {
|
||||||
instructions::perp_deactivate_position(ctx)
|
instructions::perp_deactivate_position(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn perp_zero_out_for_market(ctx: Context<PerpZeroOutForMarket>) -> Result<()> {
|
||||||
|
instructions::perp_zero_out_for_market(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn perp_place_order(
|
pub fn perp_place_order(
|
||||||
ctx: Context<PerpPlaceOrder>,
|
ctx: Context<PerpPlaceOrder>,
|
||||||
|
|
|
@ -1900,6 +1900,23 @@ export class MangoClient {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async perpZeroOutForMarket(
|
||||||
|
group: Group,
|
||||||
|
mangoAccount: MangoAccount,
|
||||||
|
perpMarketIndex: PerpMarketIndex,
|
||||||
|
): Promise<TransactionSignature> {
|
||||||
|
const perpMarket = group.getPerpMarketByMarketIndex(perpMarketIndex);
|
||||||
|
return await this.program.methods
|
||||||
|
.perpZeroOutForMarket()
|
||||||
|
.accounts({
|
||||||
|
group: group.publicKey,
|
||||||
|
account: mangoAccount.publicKey,
|
||||||
|
perpMarket: perpMarket.publicKey,
|
||||||
|
admin: group.admin,
|
||||||
|
})
|
||||||
|
.rpc();
|
||||||
|
}
|
||||||
|
|
||||||
public async perpPlaceOrder(
|
public async perpPlaceOrder(
|
||||||
group: Group,
|
group: Group,
|
||||||
mangoAccount: MangoAccount,
|
mangoAccount: MangoAccount,
|
||||||
|
|
|
@ -2858,6 +2858,32 @@ export type MangoV4 = {
|
||||||
],
|
],
|
||||||
"args": []
|
"args": []
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "perpZeroOutForMarket",
|
||||||
|
"accounts": [
|
||||||
|
{
|
||||||
|
"name": "group",
|
||||||
|
"isMut": false,
|
||||||
|
"isSigner": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "account",
|
||||||
|
"isMut": true,
|
||||||
|
"isSigner": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "perpMarket",
|
||||||
|
"isMut": false,
|
||||||
|
"isSigner": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "admin",
|
||||||
|
"isMut": false,
|
||||||
|
"isSigner": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"args": []
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "perpPlaceOrder",
|
"name": "perpPlaceOrder",
|
||||||
"accounts": [
|
"accounts": [
|
||||||
|
@ -10748,6 +10774,32 @@ export const IDL: MangoV4 = {
|
||||||
],
|
],
|
||||||
"args": []
|
"args": []
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "perpZeroOutForMarket",
|
||||||
|
"accounts": [
|
||||||
|
{
|
||||||
|
"name": "group",
|
||||||
|
"isMut": false,
|
||||||
|
"isSigner": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "account",
|
||||||
|
"isMut": true,
|
||||||
|
"isSigner": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "perpMarket",
|
||||||
|
"isMut": false,
|
||||||
|
"isSigner": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "admin",
|
||||||
|
"isMut": false,
|
||||||
|
"isSigner": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"args": []
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "perpPlaceOrder",
|
"name": "perpPlaceOrder",
|
||||||
"accounts": [
|
"accounts": [
|
||||||
|
|
Loading…
Reference in New Issue