Skip to content

Commit 5e4e61d

Browse files
committed
add additional tests and address comments
1 parent 7749204 commit 5e4e61d

File tree

5 files changed

+70
-28
lines changed

5 files changed

+70
-28
lines changed

programs/drift/src/controller/amm.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,25 @@ pub fn update_spreads(
193193
market.amm.max_base_asset_reserve,
194194
)?;
195195

196-
let mut signed_liquidity_ratio =
196+
let signed_liquidity_ratio =
197197
liquidity_ratio.safe_mul(market.amm.get_protocol_owned_position()?.signum().cast()?)?;
198198

199199
let deadband_pct = market.amm.get_reference_price_offset_deadband_pct()?;
200-
if signed_liquidity_ratio.unsigned_abs() <= deadband_pct {
201-
signed_liquidity_ratio = 0;
202-
} else {
203-
signed_liquidity_ratio = signed_liquidity_ratio.safe_sub(
204-
deadband_pct
205-
.cast::<i128>()?
206-
.safe_mul(signed_liquidity_ratio.signum())?,
207-
)?;
208-
}
200+
let liquidity_fraction_after_deadband =
201+
if signed_liquidity_ratio.unsigned_abs() <= deadband_pct {
202+
0
203+
} else {
204+
signed_liquidity_ratio.safe_sub(
205+
deadband_pct
206+
.cast::<i128>()?
207+
.safe_mul(signed_liquidity_ratio.signum())?,
208+
)?
209+
};
209210

210211
amm_spread::calculate_reference_price_offset(
211212
reserve_price,
212213
market.amm.last_24h_avg_funding_rate,
213-
signed_liquidity_ratio,
214+
liquidity_fraction_after_deadband,
214215
market.amm.min_order_size,
215216
market
216217
.amm

programs/drift/src/instructions/admin.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,24 @@ pub fn handle_update_perp_market_reference_price_offset_deadband_pct(
34613461
reference_price_offset_deadband_pct
34623462
);
34633463

3464+
let liquidity_ratio =
3465+
crate::math::amm_spread::calculate_inventory_liquidity_ratio_for_reference_price_offset(
3466+
perp_market.amm.base_asset_amount_with_amm,
3467+
perp_market.amm.base_asset_reserve,
3468+
perp_market.amm.min_base_asset_reserve,
3469+
perp_market.amm.max_base_asset_reserve,
3470+
)?;
3471+
3472+
let signed_liquidity_ratio = liquidity_ratio.safe_mul(
3473+
perp_market
3474+
.amm
3475+
.get_protocol_owned_position()?
3476+
.signum()
3477+
.cast()?,
3478+
)?;
3479+
3480+
msg!("current signed liquidity ratio: {}", signed_liquidity_ratio);
3481+
34643482
perp_market.amm.reference_price_offset_deadband_pct = reference_price_offset_deadband_pct;
34653483
Ok(())
34663484
}

programs/drift/src/math/amm_spread.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ pub fn calculate_inventory_liquidity_ratio_for_reference_price_offset(
199199
min_base_asset_reserve: u128,
200200
max_base_asset_reserve: u128,
201201
) -> DriftResult<i128> {
202-
// computes min(1, x/(1-x)) for 0 < x < 1
203-
204202
// inventory scale
205203
let (max_bids, max_asks) = _calculate_market_open_bids_asks(
206204
base_asset_reserve,

programs/drift/src/math/amm_spread/tests.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,40 @@ mod test {
219219

220220
let reserve_price = 4216 * 10000;
221221

222-
let max_ref_offset = market.amm.get_max_reference_price_offset().unwrap();
223-
assert_eq!(max_ref_offset, 10000);
224-
market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 3 / 20) as i128;
222+
market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 7 / 20) as i128;
223+
let inventory_ratio = calculate_inventory_liquidity_ratio_for_reference_price_offset(
224+
market.amm.base_asset_amount_with_amm,
225+
market.amm.base_asset_reserve,
226+
market.amm.min_base_asset_reserve,
227+
market.amm.max_base_asset_reserve,
228+
)
229+
.unwrap();
230+
assert_eq!(inventory_ratio, 100000); // 10%
231+
232+
market.amm.reference_price_offset_deadband_pct = 10; // 10%
233+
234+
// If inventory exceeds threshold positive ref price offset
235+
market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 8 / 20) as i128;
236+
let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap();
237+
assert!(market.amm.reference_price_offset > 0);
238+
239+
// If inventory is small, goes to 0
240+
market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 6 / 20) as i128;
241+
let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap();
242+
assert_eq!(market.amm.reference_price_offset, 0);
243+
244+
// Same for short pos
245+
// Make sure that the premium is also short
246+
market.amm.last_24h_avg_funding_rate = -1;
247+
market.amm.last_mark_price_twap_5min = 4216 * 10000 - 2 * 10000;
248+
market.amm.last_mark_price_twap = 4216 * 10000 - 2 * 10000;
249+
market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 8 / 20) as i128 * -1;
225250
let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap();
226-
assert_eq!(market.amm.reference_price_offset, 10);
251+
println!("ref offset: {}", market.amm.reference_price_offset);
252+
assert!(market.amm.reference_price_offset < 0);
227253

228-
// If base asset amount with amm is small, reference price offset is 0
229-
market.amm.reference_price_offset_deadband_pct = 10;
230-
market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 3 / 20) as i128;
254+
// Same for short pos
255+
market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 6 / 20) as i128 * -1;
231256
let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap();
232257
assert_eq!(market.amm.reference_price_offset, 0);
233258
}

sdk/src/math/amm.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,12 @@ export function calculateInventoryLiquidityRatioForReferencePriceOffset(
402402
maxBaseAssetReserve
403403
);
404404

405-
const minSideLiquidity = openBids.abs().add(openAsks.abs()).div(TWO);
405+
const avgSideLiquidity = openBids.abs().add(openAsks.abs()).div(TWO);
406406

407407
const inventoryScaleBN = BN.min(
408408
baseAssetAmountWithAmm
409409
.mul(PERCENTAGE_PRECISION)
410-
.div(BN.max(minSideLiquidity, ONE))
410+
.div(BN.max(avgSideLiquidity, ONE))
411411
.abs(),
412412
PERCENTAGE_PRECISION
413413
);
@@ -683,13 +683,13 @@ export function calculateSpreadBN(
683683
shortSpread = Math.max(
684684
shortSpread,
685685
lastOracleReservePriceSpreadPct.abs().toNumber() +
686-
shortVolSpread.toNumber()
686+
shortVolSpread.toNumber()
687687
);
688688
} else if (lastOracleReservePriceSpreadPct.lt(ZERO)) {
689689
longSpread = Math.max(
690690
longSpread,
691691
lastOracleReservePriceSpreadPct.abs().toNumber() +
692-
longVolSpread.toNumber()
692+
longVolSpread.toNumber()
693693
);
694694
}
695695
spreadTerms.longSpreadwPS = longSpread;
@@ -777,7 +777,7 @@ export function calculateSpreadBN(
777777
maxRetreat,
778778
Math.floor(
779779
(baseSpread * netRevenueSinceLastFunding.abs().toNumber()) /
780-
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT.abs().toNumber()
780+
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT.abs().toNumber()
781781
)
782782
);
783783
}
@@ -1035,7 +1035,7 @@ export function calculateSpreadReserves(
10351035
maxOffset = Math.max(
10361036
amm.maxSpread / 2,
10371037
(PERCENTAGE_PRECISION.toNumber() / 10000) *
1038-
(amm.curveUpdateIntensity - 100)
1038+
(amm.curveUpdateIntensity - 100)
10391039
);
10401040

10411041
const liquidityFraction =
@@ -1052,8 +1052,8 @@ export function calculateSpreadReserves(
10521052
let liquidityFractionAfterDeadband = liquidityFractionSigned;
10531053
const deadbandPct = amm.referencePriceOffsetDeadbandPct
10541054
? PERCENTAGE_PRECISION.mul(
1055-
new BN(amm.referencePriceOffsetDeadbandPct as number)
1056-
).divn(100)
1055+
new BN(amm.referencePriceOffsetDeadbandPct as number)
1056+
).divn(100)
10571057
: ZERO;
10581058
if (!liquidityFractionAfterDeadband.eq(ZERO) && deadbandPct.gt(ZERO)) {
10591059
const abs = liquidityFractionAfterDeadband.abs();

0 commit comments

Comments
 (0)