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

Mc/ix toggler #419

Merged
merged 5 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion programs/mango-v4/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub enum MangoError {
TokenInReduceOnlyMode,
#[msg("market is in reduce only mode")]
MarketInReduceOnlyMode,
#[msg("group is halted")]
GroupIsHalted,
#[msg("the perp position has non-zero base lots")]
PerpHasBaseLots,
#[msg("there are open or unsettled serum3 orders")]
Expand All @@ -87,7 +89,7 @@ pub enum MangoError {
HasOpenPerpTakerFills,
#[msg("deposit crosses the current group deposit limit")]
DepositLimit,
#[msg("ix is disabled")]
#[msg("instruction is disabled")]
IxIsDisabled,
}

Expand Down
12 changes: 12 additions & 0 deletions programs/mango-v4/src/instructions/health_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ pub struct HealthRegionBegin<'info> {
#[account(address = tx_instructions::ID)]
pub instructions: UncheckedAccount<'info>,

#[account(
constraint = group.load()?.is_ix_enabled(IxGate::HealthRegion) @ MangoError::IxIsDisabled,
)]
pub group: AccountLoader<'info, Group>,

#[account(
mut,
has_one = group,
constraint = account.load()?.is_operational() @ MangoError::AccountIsFrozen
)]
pub account: AccountLoader<'info, MangoAccountFixed>,
Expand All @@ -30,8 +36,14 @@ pub struct HealthRegionBegin<'info> {
/// remaining_accounts: health accounts for account
#[derive(Accounts)]
pub struct HealthRegionEnd<'info> {
#[account(
constraint = group.load()?.is_ix_enabled(IxGate::HealthRegion) @ MangoError::IxIsDisabled,
)]
pub group: AccountLoader<'info, Group>,
microwavedcola1 marked this conversation as resolved.
Show resolved Hide resolved

#[account(
mut,
has_one = group,
constraint = account.load()?.is_operational() @ MangoError::AccountIsFrozen
)]
pub account: AccountLoader<'info, MangoAccountFixed>,
Expand Down
90 changes: 88 additions & 2 deletions programs/mango-v4/src/instructions/ix_gate_set.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,106 @@
use anchor_lang::prelude::*;

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

#[derive(Accounts)]
pub struct IxGateSet<'info> {
#[account(
mut,
has_one = admin,
// group <-> admin relation is checked at #1
)]
pub group: AccountLoader<'info, Group>,
pub admin: Signer<'info>,
}

pub fn ix_gate_set(ctx: Context<IxGateSet>, ix_gate: u128) -> Result<()> {
let mut group = ctx.accounts.group.load_mut()?;

msg!("old {:?}, new {:?}", group.ix_gate, ix_gate);

let mut require_group_admin = false;
for i in 0..=47 {
microwavedcola1 marked this conversation as resolved.
Show resolved Hide resolved
// only admin can re-enable
if group.ix_gate & (1 << i) == 1 && ix_gate & (1 << i) == 0 {
require_group_admin = true;
}
}

log_if_changed(&group, ix_gate, IxGate::AccountClose);
log_if_changed(&group, ix_gate, IxGate::AccountCreate);
log_if_changed(&group, ix_gate, IxGate::AccountEdit);
log_if_changed(&group, ix_gate, IxGate::AccountExpand);
log_if_changed(&group, ix_gate, IxGate::AccountToggleFreeze);
log_if_changed(&group, ix_gate, IxGate::AltExtend);
log_if_changed(&group, ix_gate, IxGate::AltSet);
log_if_changed(&group, ix_gate, IxGate::FlashLoan);
log_if_changed(&group, ix_gate, IxGate::GroupClose);
log_if_changed(&group, ix_gate, IxGate::GroupCreate);
log_if_changed(&group, ix_gate, IxGate::HealthRegion);
log_if_changed(&group, ix_gate, IxGate::PerpCancelAllOrders);
log_if_changed(&group, ix_gate, IxGate::PerpCancelAllOrdersBySide);
log_if_changed(&group, ix_gate, IxGate::PerpCancelOrder);
log_if_changed(&group, ix_gate, IxGate::PerpCancelOrderByClientOrderId);
log_if_changed(&group, ix_gate, IxGate::PerpCloseMarket);
log_if_changed(&group, ix_gate, IxGate::PerpConsumeEvents);
log_if_changed(&group, ix_gate, IxGate::PerpCreateMarket);
log_if_changed(&group, ix_gate, IxGate::PerpDeactivatePosition);
log_if_changed(&group, ix_gate, IxGate::PerpLiqBasePosition);
log_if_changed(&group, ix_gate, IxGate::PerpLiqForceCancelOrders);
log_if_changed(&group, ix_gate, IxGate::PerpLiqQuoteAndBankruptcy);
log_if_changed(&group, ix_gate, IxGate::PerpPlaceOrder);
log_if_changed(&group, ix_gate, IxGate::PerpSettleFees);
log_if_changed(&group, ix_gate, IxGate::PerpSettlePnl);
log_if_changed(&group, ix_gate, IxGate::PerpUpdateFunding);
log_if_changed(&group, ix_gate, IxGate::Serum3CancelAllOrders);
log_if_changed(&group, ix_gate, IxGate::Serum3CancelOrder);
log_if_changed(&group, ix_gate, IxGate::Serum3CloseOpenOrders);
log_if_changed(&group, ix_gate, IxGate::Serum3CreateOpenOrders);
log_if_changed(&group, ix_gate, IxGate::Serum3DeregisterMarket);
log_if_changed(&group, ix_gate, IxGate::Serum3EditMarket);
log_if_changed(&group, ix_gate, IxGate::Serum3LiqForceCancelOrders);
log_if_changed(&group, ix_gate, IxGate::Serum3PlaceOrder);
log_if_changed(&group, ix_gate, IxGate::Serum3RegisterMarket);
log_if_changed(&group, ix_gate, IxGate::Serum3SettleFunds);
log_if_changed(&group, ix_gate, IxGate::StubOracleClose);
log_if_changed(&group, ix_gate, IxGate::StubOracleCreate);
log_if_changed(&group, ix_gate, IxGate::StubOracleSet);
log_if_changed(&group, ix_gate, IxGate::TokenAddBank);
log_if_changed(&group, ix_gate, IxGate::TokenDeposit);
log_if_changed(&group, ix_gate, IxGate::TokenDeregister);
log_if_changed(&group, ix_gate, IxGate::TokenLiqBankruptcy);
log_if_changed(&group, ix_gate, IxGate::TokenLiqWithToken);
log_if_changed(&group, ix_gate, IxGate::TokenRegister);
log_if_changed(&group, ix_gate, IxGate::TokenRegisterTrustless);
log_if_changed(&group, ix_gate, IxGate::TokenUpdateIndexAndRate);
log_if_changed(&group, ix_gate, IxGate::TokenWithdraw);

group.ix_gate = ix_gate;

// account constraint #1
if require_group_admin {
require!(
group.admin == ctx.accounts.admin.key(),
MangoError::SomeError
);
} else {
require!(
group.admin == ctx.accounts.admin.key()
|| group.security_admin == ctx.accounts.admin.key(),
MangoError::SomeError
);
}
Ok(())
}

fn log_if_changed(group: &Group, ix_gate: u128, ix: IxGate) {
let old = group.is_ix_enabled(ix);
let new = ix_gate & (1 << ix as u128) == 0;
if old == new {
microwavedcola1 marked this conversation as resolved.
Show resolved Hide resolved
msg!(
"{:?} ix old {:?}, new {:?}",
microwavedcola1 marked this conversation as resolved.
Show resolved Hide resolved
ix,
if old { "enabled" } else { "disabled" },
if new { "enabled" } else { "disabled" }
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

use crate::error::MangoError;
use crate::state::{
BookSide, Group, MangoAccountFixed, MangoAccountLoader, Orderbook, PerpMarket, Side, IxGate,
BookSide, Group, IxGate, MangoAccountFixed, MangoAccountLoader, Orderbook, PerpMarket, Side,
};

#[derive(Accounts)]
Expand Down
4 changes: 3 additions & 1 deletion programs/mango-v4/src/instructions/perp_cancel_order.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anchor_lang::prelude::*;

use crate::error::*;
use crate::state::{BookSide, Group, MangoAccountFixed, MangoAccountLoader, Orderbook, PerpMarket, IxGate};
use crate::state::{
BookSide, Group, IxGate, MangoAccountFixed, MangoAccountLoader, Orderbook, PerpMarket,
};

#[derive(Accounts)]
pub struct PerpCancelOrder<'info> {
Expand Down
2 changes: 1 addition & 1 deletion programs/mango-v4/src/instructions/perp_consume_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;
use bytemuck::cast_ref;

use crate::error::MangoError;
use crate::state::{EventQueue, MangoAccountFixed, MangoAccountLoader, IxGate};
use crate::state::{EventQueue, IxGate, MangoAccountFixed, MangoAccountLoader};
use crate::state::{EventType, FillEvent, Group, OutEvent, PerpMarket};

use crate::logs::{emit_perp_balances, FillLog};
Expand Down
2 changes: 1 addition & 1 deletion programs/mango-v4/src/instructions/perp_update_funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

use crate::accounts_zerocopy::*;
use crate::error::MangoError;
use crate::state::{BookSide, Group, Orderbook, PerpMarket, IxGate};
use crate::state::{BookSide, Group, IxGate, Orderbook, PerpMarket};

#[derive(Accounts)]
pub struct PerpUpdateFunding<'info> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

use crate::error::MangoError;
use crate::logs::{UpdateIndexLog, UpdateRateLog};
use crate::state::{HOUR, IxGate};
use crate::state::{IxGate, HOUR};
use crate::{
accounts_zerocopy::{AccountInfoRef, LoadMutZeroCopyRef, LoadZeroCopyRef},
state::{Bank, Group, MintInfo},
Expand Down
99 changes: 50 additions & 49 deletions programs/mango-v4/src/state/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,65 +79,66 @@ impl Group {
/// Enum for lookup into ix gate
/// note:
/// total ix files 56,
/// ix files included 47,
/// ix files not included 9,
/// ix files included 48,
/// ix files not included 8,
/// - Benchmark,
/// - ComputeAccountData,
/// - GroupCreate
/// - GroupEdit
/// - HealthRegion
/// - IxGateSet,
/// - PerpZeroOut,
/// - PerpEditMarket,
/// - TokenEdit,
#[derive(Copy, Clone, Debug)]
pub enum IxGate {
microwavedcola1 marked this conversation as resolved.
Show resolved Hide resolved
AccountClose = 0,
AccountCreate,
AccountEdit,
AccountExpand,
AccountToggleFreeze,
AltExtend,
AltSet,
FlashLoan,
GroupClose,
GroupCreate,
PerpCancelAllOrders = 10,
PerpCancelAllOrdersBySide,
PerpCancelOrder,
PerpCancelOrderByClientOrderId,
PerpCloseMarket,
PerpConsumeEvents,
PerpCreateMarket,
PerpDeactivatePosition,
PerpLiqBasePosition,
PerpLiqForceCancelOrders,
PerpLiqQuoteAndBankruptcy = 20,
PerpPlaceOrder,
PerpSettleFees,
PerpSettlePnl,
PerpUpdateFunding,
Serum3CancelAllOrders,
Serum3CancelOrder,
Serum3CloseOpenOrders,
Serum3CreateOpenOrders,
Serum3DeregisterMarket,
Serum3EditMarket = 30,
Serum3LiqForceCancelOrders,
Serum3PlaceOrder,
Serum3RegisterMarket,
Serum3SettleFunds,
StubOracleClose,
StubOracleCreate,
StubOracleSet,
TokenAddBank,
TokenDeposit,
TokenDeregister = 40,
TokenLiqBankruptcy,
TokenLiqWithToken,
TokenRegister,
TokenRegisterTrustless,
TokenUpdateIndexAndRate,
TokenWithdraw,
AccountCreate = 1,
AccountEdit = 2,
AccountExpand = 3,
AccountToggleFreeze = 4,
AltExtend = 5,
AltSet = 6,
FlashLoan = 7,
GroupClose = 8,
GroupCreate = 9,
HealthRegion = 10,
PerpCancelAllOrders = 11,
PerpCancelAllOrdersBySide = 12,
PerpCancelOrder = 13,
PerpCancelOrderByClientOrderId = 14,
PerpCloseMarket = 15,
PerpConsumeEvents = 16,
PerpCreateMarket = 17,
PerpDeactivatePosition = 18,
PerpLiqBasePosition = 19,
PerpLiqForceCancelOrders = 20,
PerpLiqQuoteAndBankruptcy = 21,
PerpPlaceOrder = 22,
PerpSettleFees = 23,
PerpSettlePnl = 24,
PerpUpdateFunding = 25,
Serum3CancelAllOrders = 26,
Serum3CancelOrder = 27,
Serum3CloseOpenOrders = 28,
Serum3CreateOpenOrders = 29,
Serum3DeregisterMarket = 30,
Serum3EditMarket = 31,
Serum3LiqForceCancelOrders = 32,
Serum3PlaceOrder = 33,
Serum3RegisterMarket = 34,
Serum3SettleFunds = 35,
StubOracleClose = 36,
StubOracleCreate = 37,
StubOracleSet = 38,
TokenAddBank = 39,
TokenDeposit = 40,
TokenDeregister = 41,
TokenLiqBankruptcy = 42,
TokenLiqWithToken = 43,
TokenRegister = 44,
TokenRegisterTrustless = 45,
TokenUpdateIndexAndRate = 46,
TokenWithdraw = 47,
}

// note: using creator instead of admin, since admin can be changed
Expand Down
5 changes: 4 additions & 1 deletion programs/mango-v4/tests/program_test/mango_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use mango_v4::instructions::{
InterestRateParams, Serum3OrderType, Serum3SelfTradeBehavior, Serum3Side,
};
use mango_v4::state::{MangoAccount, MangoAccountValue};
use solana_program::example_mocks::solana_sdk::signature::{Keypair, Signer};
use solana_program::instruction::Instruction;
use solana_program_test::BanksClientError;
use solana_sdk::instruction;
Expand Down Expand Up @@ -3629,6 +3628,7 @@ impl ClientInstruction for ComputeAccountDataInstruction {
}

pub struct HealthRegionBeginInstruction {
pub group: Pubkey,
pub account: Pubkey,
microwavedcola1 marked this conversation as resolved.
Show resolved Hide resolved
}
#[async_trait::async_trait(?Send)]
Expand Down Expand Up @@ -3657,6 +3657,7 @@ impl ClientInstruction for HealthRegionBeginInstruction {
.await;

let accounts = Self::Accounts {
group: self.group,
instructions: solana_program::sysvar::instructions::id(),
account: self.account,
};
Expand All @@ -3673,6 +3674,7 @@ impl ClientInstruction for HealthRegionBeginInstruction {
}

pub struct HealthRegionEndInstruction {
pub group: Pubkey,
pub account: Pubkey,
pub affected_bank: Option<Pubkey>,
}
Expand Down Expand Up @@ -3702,6 +3704,7 @@ impl ClientInstruction for HealthRegionEndInstruction {
.await;

let accounts = Self::Accounts {
group: self.group,
account: self.account,
};

Expand Down
Loading