Skip to content

Commit 160ea49

Browse files
authored
program: update-amm-spread-and-availability-constraints (#1663)
* program: update-amm-spread-and-availability-constraints * fix cargo tests
1 parent 3fa3ef0 commit 160ea49

File tree

5 files changed

+36
-33
lines changed

5 files changed

+36
-33
lines changed

programs/drift/src/controller/position/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,8 +2338,8 @@ fn recenter_amm_1() {
23382338
)
23392339
.unwrap();
23402340

2341-
assert_eq!(r1_orig, 334837204625);
2342-
assert_eq!(r2_orig, 703359043);
2341+
assert_eq!(r1_orig, 334835721519);
2342+
assert_eq!(r2_orig, 704842149);
23432343

23442344
let current_k = perp_market.amm.sqrt_k;
23452345
let _current_peg = perp_market.amm.peg_multiplier;

programs/drift/src/controller/repeg/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ pub fn update_amm_test() {
159159
244822 // (market.margin_ratio_initial * 100) as u32
160160
);
161161

162-
assert_eq!(bid, 9781849613);
162+
assert_eq!(bid, 9757967541);
163163
assert!(bid < (oracle_price_data.price as u64));
164164
assert_eq!(reserve_price, 12743902015);
165-
assert_eq!(ask, 12901837192);
165+
assert_eq!(ask, 12877955120);
166166
assert!(ask >= (oracle_price_data.price as u64));
167167
assert_eq!(
168168
(ask - bid) * 1000000 / reserve_price,

programs/drift/src/math/amm_spread.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ pub fn calculate_long_short_vol_spread(
121121

122122
let vol_spread: u128 = last_oracle_conf_pct
123123
.cast::<u128>()?
124-
.max(market_avg_std_pct.safe_div(2)?);
124+
.max(market_avg_std_pct.safe_div(4)?);
125125

126126
let factor_clamp_min: u128 = PERCENTAGE_PRECISION / 100; // .01
127-
let factor_clamp_max: u128 = 16 * PERCENTAGE_PRECISION / 10; // 1.6
127+
let factor_clamp_max: u128 = PERCENTAGE_PRECISION; // 1
128128

129129
let long_vol_spread_factor: u128 = long_intensity_volume
130130
.cast::<u128>()?
@@ -141,7 +141,7 @@ pub fn calculate_long_short_vol_spread(
141141
let conf_component = if last_oracle_conf_pct > PERCENTAGE_PRECISION_U64 / 400 {
142142
last_oracle_conf_pct
143143
} else {
144-
last_oracle_conf_pct.safe_div(10)?
144+
last_oracle_conf_pct.safe_div(20)?
145145
};
146146

147147
Ok((

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ mod test {
490490
.unwrap();
491491

492492
assert_eq!(long_spread_btc, 250);
493-
assert_eq!(short_spread_btc, 74142);
493+
assert_eq!(short_spread_btc, 74117);
494494

495495
let (long_spread_btc1, short_spread_btc1) = calculate_spread(
496496
500,
@@ -1064,8 +1064,8 @@ mod test {
10641064
volume_24h,
10651065
)
10661066
.unwrap();
1067-
assert_eq!(long_vspread, 1639);
1068-
assert_eq!(short_vspread, 4918);
1067+
assert_eq!(long_vspread, 819);
1068+
assert_eq!(short_vspread, 2459);
10691069

10701070
// since short volume ~= 3 * long volume intensity, expect short spread to be larger by this factor
10711071
assert_eq!(short_vspread >= long_vspread * 3, true);
@@ -1125,15 +1125,15 @@ mod test {
11251125
.unwrap();
11261126

11271127
// since total_fee_minus_distributions <=0, 10 * vol spread
1128-
assert_eq!(long_spread, 16390); // vs 2500
1128+
assert_eq!(long_spread, 8190); // vs 2500
11291129
assert_eq!(
11301130
long_spread
11311131
> (base_spread
11321132
* ((DEFAULT_LARGE_BID_ASK_FACTOR / BID_ASK_SPREAD_PRECISION) as u32)),
11331133
true
11341134
);
11351135

1136-
assert_eq!(short_spread, 49180);
1136+
assert_eq!(short_spread, 24590);
11371137
assert_eq!(
11381138
short_spread
11391139
> (base_spread
@@ -1164,8 +1164,8 @@ mod test {
11641164
)
11651165
.unwrap();
11661166

1167-
assert_eq!(long_spread, 1639);
1168-
assert_eq!(short_spread, 4918);
1167+
assert_eq!(long_spread, 819);
1168+
assert_eq!(short_spread, 2459);
11691169

11701170
let (long_spread, short_spread) = calculate_spread(
11711171
base_spread,
@@ -1189,8 +1189,8 @@ mod test {
11891189
volume_24h,
11901190
)
11911191
.unwrap();
1192-
assert_eq!(long_spread, 195556);
1193-
assert_eq!(short_spread, 4444);
1192+
assert_eq!(long_spread, 197666);
1193+
assert_eq!(short_spread, 2334);
11941194

11951195
let (long_spread, short_spread) = calculate_spread(
11961196
base_spread,
@@ -1214,8 +1214,8 @@ mod test {
12141214
volume_24h,
12151215
)
12161216
.unwrap();
1217-
assert_eq!(long_spread, 1639);
1218-
assert_eq!(short_spread, 24917);
1217+
assert_eq!(long_spread, 819);
1218+
assert_eq!(short_spread, 22458);
12191219
}
12201220

12211221
#[test]
@@ -1254,8 +1254,8 @@ mod test {
12541254
volume_24h,
12551255
)
12561256
.unwrap();
1257-
assert_eq!(long_vspread, 1639);
1258-
assert_eq!(short_vspread, 4918);
1257+
assert_eq!(long_vspread, 819);
1258+
assert_eq!(short_vspread, 2459);
12591259

12601260
// since short volume ~= 3 * long volume intensity, expect short spread to be larger by this factor
12611261
assert_eq!(short_vspread >= long_vspread * 3, true);
@@ -1315,15 +1315,15 @@ mod test {
13151315
.unwrap();
13161316

13171317
// since total_fee_minus_distributions <=0, 10 * vol spread
1318-
assert_eq!(long_spread, 16390); // vs 2500
1318+
assert_eq!(long_spread, 8190); // vs 2500
13191319
assert_eq!(
13201320
long_spread
13211321
> (base_spread
13221322
* ((DEFAULT_LARGE_BID_ASK_FACTOR / BID_ASK_SPREAD_PRECISION) as u32)),
13231323
true
13241324
);
13251325

1326-
assert_eq!(short_spread, 99180);
1326+
assert_eq!(short_spread, 74590);
13271327
assert_eq!(
13281328
short_spread
13291329
> (base_spread
@@ -1354,8 +1354,8 @@ mod test {
13541354
)
13551355
.unwrap();
13561356

1357-
assert_eq!(long_spread, 1639);
1358-
assert_eq!(short_spread, 9918);
1357+
assert_eq!(long_spread, 819);
1358+
assert_eq!(short_spread, 7459);
13591359

13601360
let (long_spread, short_spread) = calculate_spread(
13611361
base_spread,
@@ -1379,8 +1379,8 @@ mod test {
13791379
volume_24h,
13801380
)
13811381
.unwrap();
1382-
assert_eq!(long_spread, 197138); // big cause of oracel pct
1383-
assert_eq!(short_spread, 2862);
1382+
assert_eq!(long_spread, 197814); // big cause of oracel pct
1383+
assert_eq!(short_spread, 2186);
13841384

13851385
let (long_spread, short_spread) = calculate_spread(
13861386
base_spread,
@@ -1404,8 +1404,8 @@ mod test {
14041404
volume_24h,
14051405
)
14061406
.unwrap();
1407-
assert_eq!(long_spread, 1639);
1408-
assert_eq!(short_spread, 76584); // big
1407+
assert_eq!(long_spread, 819);
1408+
assert_eq!(short_spread, 74125); // big
14091409
}
14101410

14111411
#[test]
@@ -1513,8 +1513,8 @@ mod test {
15131513
427588331503,
15141514
)
15151515
.unwrap();
1516-
assert_eq!(long_spread, 22137);
1517-
assert_eq!(short_spread, 217356);
1516+
assert_eq!(long_spread, 11068);
1517+
assert_eq!(short_spread, 135916);
15181518
}
15191519

15201520
#[test]

programs/drift/src/state/perp_market.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,12 @@ impl PerpMarket {
336336
return Ok(false);
337337
}
338338

339-
let amm_low_inventory_and_profitable =
340-
self.amm.net_revenue_since_last_funding > 0 && amm_lp_allowed_to_jit_make;
341-
let amm_oracle_no_latency = self.amm.oracle_source == OracleSource::Prelaunch;
339+
let amm_low_inventory_and_profitable = self.amm.net_revenue_since_last_funding
340+
>= DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT
341+
&& amm_lp_allowed_to_jit_make;
342+
let amm_oracle_no_latency = self.amm.oracle_source == OracleSource::Prelaunch
343+
|| (self.amm.historical_oracle_data.last_oracle_delay == 0
344+
&& self.amm.oracle_source == OracleSource::PythLazer);
342345
let can_skip = amm_low_inventory_and_profitable || amm_oracle_no_latency;
343346

344347
if can_skip {

0 commit comments

Comments
 (0)