Skip to content

program: add high leverage reduce only #1756

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub fn liquidate_perp(
let margin_ratio = perp_market_map.get_ref(&market_index)?.get_margin_ratio(
user_base_asset_amount.cast()?,
MarginRequirementType::Maintenance,
user.is_high_leverage_mode(),
user.is_high_leverage_mode(false),
)?;

let margin_ratio_with_buffer = margin_ratio.safe_add(liquidation_margin_buffer_ratio)?;
Expand All @@ -351,7 +351,7 @@ pub fn liquidate_perp(
.price;

let liquidator_fee = get_liquidation_fee(
market.get_base_liquidator_fee(user.is_high_leverage_mode()),
market.get_base_liquidator_fee(user.is_high_leverage_mode(false)),
market.get_max_liquidation_fee()?,
user.last_active_slot,
slot,
Expand Down Expand Up @@ -964,7 +964,7 @@ pub fn liquidate_perp_with_fill(
let margin_ratio = perp_market_map.get_ref(&market_index)?.get_margin_ratio(
user_base_asset_amount.cast()?,
MarginRequirementType::Maintenance,
user.is_high_leverage_mode(),
user.is_high_leverage_mode(false),
)?;

let margin_ratio_with_buffer = margin_ratio.safe_add(liquidation_margin_buffer_ratio)?;
Expand Down
10 changes: 5 additions & 5 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn place_perp_order(
if let Some(config) = high_leverage_mode_config {
let mut config = load_mut!(config)?;
if !config.is_full() || params.is_max_leverage_order() {
config.update_user(user)?;
config.enable_high_leverage(user)?;
} else {
msg!("high leverage mode config is full");
}
Expand Down Expand Up @@ -2123,7 +2123,7 @@ pub fn fulfill_perp_order_with_amm(
user_stats,
fee_structure,
&MarketType::Perp,
user.is_high_leverage_mode(),
user.is_high_leverage_mode(false),
)?;
let (base_asset_amount, limit_price) = calculate_base_asset_amount_for_amm_to_fulfill(
&user.orders[order_index],
Expand Down Expand Up @@ -2232,7 +2232,7 @@ pub fn fulfill_perp_order_with_amm(
quote_asset_amount_surplus,
order_post_only,
market.fee_adjustment,
user.is_high_leverage_mode(),
user.is_high_leverage_mode(false),
)?;

let user_position_delta =
Expand Down Expand Up @@ -2723,7 +2723,7 @@ pub fn fulfill_perp_order_with_match(
referrer_stats,
&MarketType::Perp,
market.fee_adjustment,
taker.is_high_leverage_mode(),
taker.is_high_leverage_mode(false),
)?;

// Increment the markets house's total fee variables
Expand Down Expand Up @@ -3366,7 +3366,7 @@ pub fn burn_user_lp_shares_for_risk_reduction(
quote_oracle_price,
margin_calc.margin_shortage()?,
user_custom_margin_ratio,
user.is_high_leverage_mode(),
user.is_high_leverage_mode(false),
)?;

let (position_delta, pnl) = burn_lp_shares(
Expand Down
8 changes: 7 additions & 1 deletion programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2734,7 +2734,13 @@ pub fn handle_disable_user_high_leverage_mode<'c: 'info, 'info>(
"user must be in high leverage mode"
)?;

user.margin_mode = MarginMode::Default;
let new_margin_mode = if user.margin_mode == MarginMode::HighLeverageReduceOnly {
MarginMode::Default
} else {
MarginMode::HighLeverage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean HighLeverageReduceOnly ?

};

user.margin_mode = new_margin_mode;

let custom_margin_ratio_before = user.max_margin_ratio;
user.max_margin_ratio = 0;
Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3427,7 +3427,7 @@ pub fn handle_enable_user_high_leverage_mode<'c: 'info, 'info>(

let mut config = load_mut!(ctx.accounts.high_leverage_mode_config)?;

config.update_user(&mut user)?;
config.enable_high_leverage(&mut user)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/math/margin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info(
}

let user_pool_id = user.pool_id;
let user_high_leverage_mode = user.is_high_leverage_mode();
let user_high_leverage_mode = user.is_high_leverage_mode(context.margin_type == MarginRequirementType::Maintenance);

for spot_position in user.spot_positions.iter() {
validation::position::validate_spot_position(spot_position)?;
Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/math/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ pub fn calculate_max_perp_order_size(
)?;

let user_custom_margin_ratio = user.max_margin_ratio;
let user_high_leverage_mode = user.is_high_leverage_mode();
let user_high_leverage_mode = user.is_high_leverage_mode(false);

let free_collateral_before = total_collateral.safe_sub(margin_requirement.cast()?)?;

Expand Down
10 changes: 8 additions & 2 deletions programs/drift/src/state/high_leverage_mode_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub struct HighLeverageModeConfig {
pub max_users: u32,
pub current_users: u32,
pub reduce_only: u8,
pub padding: [u8; 31],
pub padding1: [u8; 3],
pub reduce_only_users: u32,
pub padding2: [u8; 24],
}

// implement SIZE const for ProtocolIfSharesTransferConfig
Expand Down Expand Up @@ -44,11 +46,15 @@ impl HighLeverageModeConfig {
(self.current_users >= self.max_users) || self.is_reduce_only()
}

pub fn update_user(&mut self, user: &mut User) -> DriftResult {
pub fn enable_high_leverage(&mut self, user: &mut User) -> DriftResult {
if user.margin_mode == MarginMode::HighLeverage {
return Ok(());
}

if user.margin_mode == MarginMode::HighLeverageReduceOnly {
self.reduce_only_users = self.reduce_only_users.saturating_sub(1);
}

user.margin_mode = MarginMode::HighLeverage;

validate!(
Expand Down
5 changes: 3 additions & 2 deletions programs/drift/src/state/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ impl User {
false
}

pub fn is_high_leverage_mode(&self) -> bool {
self.margin_mode == MarginMode::HighLeverage
pub fn is_high_leverage_mode(&self, include_reduce_only: bool) -> bool {
self.margin_mode == MarginMode::HighLeverage || (include_reduce_only && self.margin_mode == MarginMode::HighLeverageReduceOnly)
}

pub fn get_fuel_bonus_numerator(&self, now: i64) -> DriftResult<i64> {
Expand Down Expand Up @@ -2103,6 +2103,7 @@ pub enum MarginMode {
#[default]
Default,
HighLeverage,
HighLeverageReduceOnly,
}

#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, PartialEq, Debug, Eq)]
Expand Down
Loading