Skip to content

Commit

Permalink
add gating to individual ixs
Browse files Browse the repository at this point in the history
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
  • Loading branch information
microwavedcola1 committed Jan 24, 2023
1 parent 629eaaf commit 998d0b6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
4 changes: 4 additions & 0 deletions programs/mango-v4/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ pub enum MangoError {
HasOpenPerpTakerFills,
#[msg("deposit crosses the current group deposit limit")]
DepositLimit,
#[msg("ix is disabled")]
IxIsDisabled,
#[msg("ix disabling not allowed")]
IxDisablingNotAllowed,
}

impl MangoError {
Expand Down
3 changes: 2 additions & 1 deletion programs/mango-v4/src/instructions/account_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::state::*;
#[derive(Accounts)]
pub struct AccountClose<'info> {
#[account(
constraint = group.load()?.is_operational() @ MangoError::GroupIsHalted
constraint = group.load()?.is_operational() @ MangoError::GroupIsHalted,
constraint = group.load()?.is_ix_enabled(IxGate::AccountClose) @ MangoError::IxIsDisabled,
)]
pub group: AccountLoader<'info, Group>,

Expand Down
19 changes: 19 additions & 0 deletions programs/mango-v4/src/instructions/ix_gate_toggler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anchor_lang::prelude::*;

use crate::{error::MangoError, state::*};

#[derive(Accounts)]
pub struct IxToggler<'info> {
#[account(
mut,
has_one = admin,
)]
pub group: AccountLoader<'info, Group>,
pub admin: Signer<'info>,
}

pub fn ix_gate_toggler(ctx: Context<IxToggler>, ix_gate: u16) -> Result<()> {
let mut group = ctx.accounts.group.load_mut()?;
group.ix_gate = ix_gate;
Ok(())
}
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 @@ -13,6 +13,7 @@ pub use group_create::*;
pub use group_edit::*;
pub use group_toggle_halt::*;
pub use health_region::*;
pub use ix_gate_toggler::*;
pub use perp_cancel_all_orders::*;
pub use perp_cancel_all_orders_by_side::*;
pub use perp_cancel_order::*;
Expand Down Expand Up @@ -69,6 +70,7 @@ mod group_create;
mod group_edit;
mod group_toggle_halt;
mod health_region;
mod ix_gate_toggler;
mod perp_cancel_all_orders;
mod perp_cancel_all_orders_by_side;
mod perp_cancel_order;
Expand Down
73 changes: 71 additions & 2 deletions programs/mango-v4/src/state/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ pub struct Group {
// Set to 0 to disable, which also means by default there is no limit
pub deposit_limit_quote: u64,

pub reserved: [u8; 1880],
// Map of ixs and their state of gating
pub ix_gate: u16,

pub reserved: [u8; 1878],
}
const_assert_eq!(
size_of::<Group>(),
32 + 4 + 32 * 2 + 4 + 32 * 2 + 4 + 4 + 20 * 32 + 32 + 8 + 1880
32 + 4 + 32 * 2 + 4 + 32 * 2 + 4 + 4 + 20 * 32 + 32 + 8 + 2 + 1878
);
const_assert_eq!(size_of::<Group>(), 2736);
const_assert_eq!(size_of::<Group>() % 8, 0);
Expand All @@ -72,6 +75,72 @@ impl Group {
pub fn is_operational(&self) -> bool {
self.halted == 0
}

pub fn is_ix_enabled(&self, ix: IxGate) -> bool {
self.ix_gate & (1 << ix as u16) != 0
}
}

/// Enum for lookup into ix gate
/// ixs not included
/// - Benchmark,
/// - ComputeAccountData,
/// - IxGateToggler,
/// - PerpZeroOut,
pub enum IxGate {
AccountClose = 0,
AccountCreate,
AccountEdit,
AccountExpand,
AccountToggleFreeze,
AltExtend,
AltSet,
FlashLoan,
GroupClose,
GroupCreate,
GroupEdit,
GroupToggleHalt,
HealthRegion,
PerpCancelAllOrders,
PerpCancelAllOrdersBySide,
PerpCancelOrder,
PerpCancelOrderByClientOrderId,
PerpCloseMarket,
PerpConsumeEvents,
PerpCreateMarket,
PerpDeactivatePosition,
PerpEditMarket,
PerpLiqBasePosition,
PerpLiqForceCancelOrders,
PerpLiqQuoteAndBankruptcy,
PerpPlaceOrder,
PerpSettleFees,
PerpSettlePnl,
PerpUpdateFunding,

Serum3CancelAllOrders,
Serum3CancelOrder,
Serum3CloseOpenOrders,
Serum3CreateOpenOrders,
Serum3DeregisterMarket,
Serum3EditMarket,
Serum3LiqForceCancelOrders,
Serum3PlaceOrder,
Serum3RegisterMarket,
Serum3SettleFunds,
StubOracleClose,
StubOracleCreate,
StubOracleSet,
TokenAddBank,
TokenDeposit,
TokenDeregister,
TokenEdit,
TokenLiqBankruptcy,
TokenLiqWithToken,
TokenRegister,
TokenRegisterTrustless,
TokenUpdateIndexAndRate,
TokenWithdraw,
}

// note: using creator instead of admin, since admin can be changed
Expand Down

0 comments on commit 998d0b6

Please sign in to comment.