From f48ccded7c5bd3371a384ab9138334191699aa52 Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Tue, 8 Jul 2025 09:39:06 -0400 Subject: [PATCH 1/4] program: disable-hlm-on-custom-max-leverage-check --- programs/drift/src/instructions/keeper.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/programs/drift/src/instructions/keeper.rs b/programs/drift/src/instructions/keeper.rs index 3c2f7852ad..c5a68811f6 100644 --- a/programs/drift/src/instructions/keeper.rs +++ b/programs/drift/src/instructions/keeper.rs @@ -26,6 +26,7 @@ use crate::instructions::constraints::*; use crate::instructions::optional_accounts::{load_maps, AccountMaps}; use crate::math::casting::Cast; use crate::math::constants::QUOTE_SPOT_MARKET_INDEX; +use crate::math::margin; use crate::math::margin::{calculate_user_equity, meets_settle_pnl_maintenance_margin_requirement}; use crate::math::orders::{estimate_price_from_side, find_bids_and_asks_from_users}; use crate::math::position::calculate_base_asset_value_and_pnl_with_oracle_price; @@ -2664,7 +2665,7 @@ pub fn handle_disable_user_high_leverage_mode<'c: 'info, 'info>( for position in user.perp_positions.iter().filter(|p| !p.is_available()) { let perp_market = perp_market_map.get_ref(&position.market_index)?; - if perp_market.is_high_leverage_mode_enabled() { + if perp_market.is_high_leverage_mode_enabled() && user.max_margin_ratio < perp_market.margin_ratio_initial { requires_invariant_check = true; break; // Exit early if invariant check is required } From 872fe867b0d98f147c65515e264e4c0d06037b49 Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Tue, 8 Jul 2025 09:48:05 -0400 Subject: [PATCH 2/4] cleanup --- programs/drift/src/instructions/keeper.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/programs/drift/src/instructions/keeper.rs b/programs/drift/src/instructions/keeper.rs index c5a68811f6..8014eb62b5 100644 --- a/programs/drift/src/instructions/keeper.rs +++ b/programs/drift/src/instructions/keeper.rs @@ -26,7 +26,6 @@ use crate::instructions::constraints::*; use crate::instructions::optional_accounts::{load_maps, AccountMaps}; use crate::math::casting::Cast; use crate::math::constants::QUOTE_SPOT_MARKET_INDEX; -use crate::math::margin; use crate::math::margin::{calculate_user_equity, meets_settle_pnl_maintenance_margin_requirement}; use crate::math::orders::{estimate_price_from_side, find_bids_and_asks_from_users}; use crate::math::position::calculate_base_asset_value_and_pnl_with_oracle_price; From fbc93750f58a46c6bdcd8b9f3b3b354dda4e36e8 Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Fri, 11 Jul 2025 11:36:57 -0400 Subject: [PATCH 3/4] program: add amm-decay-for-reference-price-offset --- programs/drift/src/controller/amm.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/programs/drift/src/controller/amm.rs b/programs/drift/src/controller/amm.rs index 9aa33e087e..180426d68b 100644 --- a/programs/drift/src/controller/amm.rs +++ b/programs/drift/src/controller/amm.rs @@ -257,7 +257,13 @@ pub fn update_spreads(market: &mut PerpMarket, reserve_price: u64) -> DriftResul market.amm.long_spread = long_spread; market.amm.short_spread = short_spread; - market.amm.reference_price_offset = reference_price_offset; + + if reference_price_offset == 0 && market.amm.reference_price_offset != 0 { + // smooth decay toward 0 + market.amm.reference_price_offset = market.amm.reference_price_offset.safe_sub(market.amm.reference_price_offset.safe_div(10)?)?; + } else { + market.amm.reference_price_offset = reference_price_offset; + } update_spread_reserves(market)?; From 1f053b02dc73f292664d44361331eea5b41d34b0 Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Fri, 11 Jul 2025 15:28:25 -0400 Subject: [PATCH 4/4] correct max offset, remove err lines --- programs/drift/src/instructions/keeper.rs | 2 +- programs/drift/src/state/perp_market.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/drift/src/instructions/keeper.rs b/programs/drift/src/instructions/keeper.rs index 8014eb62b5..3c2f7852ad 100644 --- a/programs/drift/src/instructions/keeper.rs +++ b/programs/drift/src/instructions/keeper.rs @@ -2664,7 +2664,7 @@ pub fn handle_disable_user_high_leverage_mode<'c: 'info, 'info>( for position in user.perp_positions.iter().filter(|p| !p.is_available()) { let perp_market = perp_market_map.get_ref(&position.market_index)?; - if perp_market.is_high_leverage_mode_enabled() && user.max_margin_ratio < perp_market.margin_ratio_initial { + if perp_market.is_high_leverage_mode_enabled() { requires_invariant_check = true; break; // Exit early if invariant check is required } diff --git a/programs/drift/src/state/perp_market.rs b/programs/drift/src/state/perp_market.rs index 8983162b7b..7b2748ec26 100644 --- a/programs/drift/src/state/perp_market.rs +++ b/programs/drift/src/state/perp_market.rs @@ -1233,7 +1233,7 @@ impl AMM { // always allow 1-100 bps of price offset, up to half of the market's max_spread let lb_bps = (PERCENTAGE_PRECISION.cast::()? / 10000).safe_mul(lower_bound_multiplier)?; - let max_offset = (self.max_spread.cast::()? / 2).max(lb_bps); + let max_offset = (self.max_spread.cast::()? / 2).min(lb_bps); Ok(max_offset) }