From b1cff417e44a20f411bf9969e86021f1e9d35a21 Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Thu, 20 Nov 2025 17:19:17 -0500 Subject: [PATCH] program: ref-price-offset-enhance-2 --- programs/drift/src/controller/position/tests.rs | 14 +++++++------- programs/drift/src/math/amm_spread.rs | 8 +++++--- programs/drift/src/math/amm_spread/tests.rs | 6 +++--- sdk/src/math/amm.ts | 12 ++++-------- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/programs/drift/src/controller/position/tests.rs b/programs/drift/src/controller/position/tests.rs index b16f14d09..037800c66 100644 --- a/programs/drift/src/controller/position/tests.rs +++ b/programs/drift/src/controller/position/tests.rs @@ -801,7 +801,7 @@ fn amm_ref_price_offset_decay_logic() { max_ref_offset, ) .unwrap(); - assert_eq!(res, 10000); + assert_eq!(res, 576); let mut now = perp_market.amm.last_mark_price_twap_ts + 10; let mut clock_slot = perp_market.amm.last_update_slot; @@ -830,7 +830,7 @@ fn amm_ref_price_offset_decay_logic() { .unwrap(); assert_eq!(perp_market.amm.last_update_slot, clock_slot); assert_eq!(perp_market.amm.last_oracle_valid, true); - assert_eq!(perp_market.amm.reference_price_offset, 10000); + assert_eq!(perp_market.amm.reference_price_offset, 384); perp_market.amm.last_mark_price_twap_5min = (perp_market .amm @@ -978,7 +978,7 @@ fn amm_negative_ref_price_offset_decay_logic() { max_ref_offset, ) .unwrap(); - assert_eq!(res, 10000); + assert_eq!(res, 576); let mut now = perp_market.amm.last_mark_price_twap_ts + 10; let mut clock_slot = perp_market.amm.last_update_slot; @@ -1007,7 +1007,7 @@ fn amm_negative_ref_price_offset_decay_logic() { .unwrap(); assert_eq!(perp_market.amm.last_update_slot, clock_slot); assert_eq!(perp_market.amm.last_oracle_valid, true); - assert_eq!(perp_market.amm.reference_price_offset, 10000); + assert_eq!(perp_market.amm.reference_price_offset, 384); perp_market.amm.last_mark_price_twap_5min = (perp_market .amm @@ -1174,7 +1174,7 @@ fn amm_perp_ref_offset() { max_ref_offset, ) .unwrap(); - assert_eq!(res, 45000); + assert_eq!(res, 132); assert_eq!(perp_market.amm.reference_price_offset, 18000); // not updated vs market account let now = 1741207620 + 1; @@ -1207,8 +1207,8 @@ fn amm_perp_ref_offset() { let r = perp_market.amm.reserve_price().unwrap(); let (b, a) = perp_market.amm.bid_ask_price(r).unwrap(); - assert_eq!(b, 7108594); - assert_eq!(a, 7115724); + assert_eq!(b, 7098516); + assert_eq!(a, 7105646); assert_eq!( perp_market.amm.historical_oracle_data.last_oracle_price, 7101600 diff --git a/programs/drift/src/math/amm_spread.rs b/programs/drift/src/math/amm_spread.rs index 047be5463..21a7fe911 100644 --- a/programs/drift/src/math/amm_spread.rs +++ b/programs/drift/src/math/amm_spread.rs @@ -640,9 +640,11 @@ pub fn calculate_reference_price_offset( let offset_pct = if (mark_premium_avg_pct >= 0 && liquidity_fraction >= 0) || (mark_premium_avg_pct <= 0 && liquidity_fraction <= 0) { - mark_premium_avg_pct - .safe_mul(liquidity_fraction.unsigned_abs().cast::()?)? - .safe_div(2)? + mark_premium_avg_pct.safe_add( + mark_premium_avg_pct + .safe_mul(liquidity_fraction.unsigned_abs().cast::()?)? + .safe_div(PRICE_PRECISION_I64)?, + )? } else { 0 }; diff --git a/programs/drift/src/math/amm_spread/tests.rs b/programs/drift/src/math/amm_spread/tests.rs index eaf55b533..fb74a13bb 100644 --- a/programs/drift/src/math/amm_spread/tests.rs +++ b/programs/drift/src/math/amm_spread/tests.rs @@ -60,11 +60,11 @@ mod test { max_offset, ) .unwrap(); - assert_eq!(res, 455); // 237*2/3); // 1 penny divergence + assert_eq!(res, 91); // 237*2/3); // 1 penny divergence let res = calculate_reference_price_offset( rev_price, 1, - 10, + 100000, 1, 4216 * 10000, 4219 * 10000, @@ -73,7 +73,7 @@ mod test { max_offset, ) .unwrap(); - assert_eq!(res, 2035); + assert_eq!(res, 447); let res = calculate_reference_price_offset( rev_price, diff --git a/sdk/src/math/amm.ts b/sdk/src/math/amm.ts index 88a9ec9f5..0ca7ce71e 100644 --- a/sdk/src/math/amm.ts +++ b/sdk/src/math/amm.ts @@ -503,16 +503,12 @@ export function calculateReferencePriceOffset( .mul(PRICE_PRECISION) .div(reservePrice); - const inventoryPct = clampBN( - liquidityFraction.mul(new BN(maxOffsetPct)).div(PERCENTAGE_PRECISION), - new BN(maxOffsetPct).mul(new BN(-1)), - new BN(maxOffsetPct) - ); - // Only apply when inventory is consistent with recent and 24h market premium - let offsetPct = markPremiumAvgPct.add(inventoryPct); + let offsetPct = markPremiumAvgPct.add( + markPremiumAvgPct.mul(liquidityFraction).div(PERCENTAGE_PRECISION) + ); - if (!sigNum(inventoryPct).eq(sigNum(markPremiumAvgPct))) { + if (!sigNum(liquidityFraction).eq(sigNum(markPremiumAvgPct))) { offsetPct = ZERO; }