Skip to content
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
14 changes: 7 additions & 7 deletions programs/drift/src/controller/position/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions programs/drift/src/math/amm_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>()?)?
.safe_div(2)?
mark_premium_avg_pct.safe_add(
mark_premium_avg_pct
.safe_mul(liquidity_fraction.unsigned_abs().cast::<i64>()?)?
.safe_div(PRICE_PRECISION_I64)?,
)?
} else {
0
};
Expand Down
6 changes: 3 additions & 3 deletions programs/drift/src/math/amm_spread/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 4 additions & 8 deletions sdk/src/math/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading