Skip to content

Commit

Permalink
add protected maker mode config
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Nov 29, 2024
1 parent 946e304 commit 9d371e1
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
2 changes: 2 additions & 0 deletions programs/drift/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ pub enum ErrorCode {
InvalidSwiftOrderId,
#[msg("Invalid pool id")]
InvalidPoolId,
#[msg("Invalid Protected Maker Mode Config")]
InvalidProtectedMakerModeConfig,
}

#[macro_export]
Expand Down
63 changes: 63 additions & 0 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use crate::state::perp_market::{
ContractTier, ContractType, InsuranceClaim, MarketStatus, PerpMarket, PoolBalance, AMM,
};
use crate::state::perp_market_map::get_writable_perp_market_set;
use crate::state::protected_maker_mode_config::ProtectedMakerModeConfig;
use crate::state::spot_market::{
AssetTier, InsuranceFund, SpotBalanceType, SpotFulfillmentConfigStatus, SpotMarket,
};
Expand Down Expand Up @@ -4198,6 +4199,32 @@ pub fn handle_update_high_leverage_mode_config(
Ok(())
}

pub fn handle_initialize_protected_maker_mode_config(
ctx: Context<InitializeProtectedMakerModeConfig>,
max_users: u32,
) -> Result<()> {
let mut config = ctx.accounts.protected_maker_mode_config.load_init()?;

config.max_users = max_users;

Ok(())
}

pub fn handle_update_protected_maker_mode_config(
ctx: Context<UpdateProtectedMakerModeConfig>,
max_users: u32,
reduce_only: bool,
) -> Result<()> {
let mut config = load_mut!(ctx.accounts.protected_maker_mode_config)?;

config.max_users = max_users;
config.reduce_only = reduce_only as u8;

config.validate()?;

Ok(())
}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(mut)]
Expand Down Expand Up @@ -4848,3 +4875,39 @@ pub struct UpdateHighLeverageModeConfig<'info> {
)]
pub state: Box<Account<'info, State>>,
}

#[derive(Accounts)]
pub struct InitializeProtectedMakerModeConfig<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
init,
seeds = [b"protected_maker_mode_config".as_ref()],
space = ProtectedMakerModeConfig::SIZE,
bump,
payer = admin
)]
pub protected_maker_mode_config: AccountLoader<'info, ProtectedMakerModeConfig>,
#[account(
has_one = admin
)]
pub state: Box<Account<'info, State>>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct UpdateProtectedMakerModeConfig<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [b"protected_maker_mode_config".as_ref()],
bump,
)]
pub protected_maker_mode_config: AccountLoader<'info, ProtectedMakerModeConfig>,
#[account(
has_one = admin
)]
pub state: Box<Account<'info, State>>,
}
15 changes: 15 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,21 @@ pub mod drift {
) -> Result<()> {
handle_update_high_leverage_mode_config(ctx, max_users, reduce_only)
}

pub fn initialize_protected_maker_mode_config(
ctx: Context<InitializeProtectedMakerModeConfig>,
max_users: u32,
) -> Result<()> {
handle_initialize_protected_maker_mode_config(ctx, max_users)
}

pub fn update_protected_maker_mode_config(
ctx: Context<UpdateProtectedMakerModeConfig>,
max_users: u32,
reduce_only: bool,
) -> Result<()> {
handle_update_protected_maker_mode_config(ctx, max_users, reduce_only)
}
}

#[cfg(not(feature = "no-entrypoint"))]
Expand Down
3 changes: 2 additions & 1 deletion programs/drift/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod order_params;
pub mod paused_operations;
pub mod perp_market;
pub mod perp_market_map;
pub mod protected_maker_mode_config;
pub mod rfq_user;
pub mod settle_pnl_mode;
pub mod spot_fulfillment_params;
Expand All @@ -22,4 +23,4 @@ pub mod state;
pub mod swift_user;
pub mod traits;
pub mod user;
pub mod user_map;
pub mod user_map;
37 changes: 37 additions & 0 deletions programs/drift/src/state/protected_maker_mode_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::error::DriftResult;
use crate::error::ErrorCode;
use crate::state::traits::Size;
use crate::validate;
use anchor_lang::prelude::*;

#[account(zero_copy(unsafe))]
#[derive(Default, Eq, PartialEq, Debug)]
#[repr(C)]
pub struct ProtectedMakerModeConfig {
pub max_users: u32,
pub current_users: u32,
pub reduce_only: u8,
pub padding: [u8; 31],
}

impl Size for ProtectedMakerModeConfig {
const SIZE: usize = 48;
}

impl ProtectedMakerModeConfig {
pub fn validate(&self) -> DriftResult {
validate!(
self.current_users <= self.max_users,
ErrorCode::InvalidProtectedMakerModeConfig,
"current users ({}) > max users ({})",
self.current_users,
self.max_users
)?;

Ok(())
}

pub fn is_reduce_only(&self) -> bool {
self.reduce_only > 0
}
}

0 comments on commit 9d371e1

Please sign in to comment.