Skip to content

Commit a105ca2

Browse files
committed
amm available for low risk orders
1 parent 151cd42 commit a105ca2

File tree

14 files changed

+171
-29
lines changed

14 files changed

+171
-29
lines changed

programs/drift/src/controller/orders.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,10 +1104,27 @@ pub fn fill_perp_order(
11041104
&market.amm.oracle_source,
11051105
oracle::LogMode::SafeMMOracle,
11061106
market.amm.oracle_slot_delay_override,
1107+
market.amm.slots_before_stale_for_amm_low_risk,
11071108
)?;
11081109

1109-
oracle_valid_for_amm_fill =
1110-
is_oracle_valid_for_action(safe_oracle_validity, Some(DriftAction::FillOrderAmm))?;
1110+
let is_order_low_risk_for_amm = user.orders[order_index].is_low_risk_for_amm(
1111+
safe_oracle_price_data.delay,
1112+
state.min_perp_auction_duration,
1113+
slot,
1114+
)?;
1115+
1116+
let oracle_valid_for_amm_fill_immediate = is_oracle_valid_for_action(
1117+
safe_oracle_validity,
1118+
Some(DriftAction::FillOrderAmmImmediate),
1119+
)?;
1120+
1121+
let oracle_valid_for_amm_fill_low_risk = is_oracle_valid_for_action(
1122+
safe_oracle_validity,
1123+
Some(DriftAction::FillOrderAmmLowRisk),
1124+
)?;
1125+
1126+
oracle_valid_for_amm_fill = oracle_valid_for_amm_fill_immediate
1127+
|| (is_order_low_risk_for_amm && oracle_valid_for_amm_fill_low_risk);
11111128

11121129
oracle_stale_for_margin = mm_oracle_price_data.get_delay()
11131130
> state
@@ -1134,8 +1151,11 @@ pub fn fill_perp_order(
11341151
amm_has_low_enough_inventory = market
11351152
.amm
11361153
.amm_has_low_enough_inventory(amm_wants_to_jit_make)?;
1137-
amm_can_skip_duration =
1138-
market.can_skip_auction_duration(&state, amm_has_low_enough_inventory)?;
1154+
amm_can_skip_duration = market.can_skip_auction_duration(
1155+
&state,
1156+
amm_has_low_enough_inventory,
1157+
is_order_low_risk_for_amm,
1158+
)?;
11391159

11401160
user_can_skip_duration = user.can_skip_auction_duration(user_stats)?;
11411161

@@ -3138,6 +3158,7 @@ pub fn trigger_order(
31383158
.last_oracle_price_twap,
31393159
perp_market.get_max_confidence_interval_multiplier()?,
31403160
0,
3161+
0,
31413162
)?;
31423163

31433164
let is_oracle_valid =
@@ -5435,6 +5456,7 @@ pub fn trigger_spot_order(
54355456
spot_market.historical_oracle_data.last_oracle_price_twap,
54365457
spot_market.get_max_confidence_interval_multiplier()?,
54375458
0,
5459+
0,
54385460
)?;
54395461
let strict_oracle_price = StrictOraclePrice {
54405462
current: oracle_price_data.price,

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,7 @@ pub mod fulfill_order_with_maker_order {
21792179
market.amm.historical_oracle_data.last_oracle_price_twap,
21802180
market.get_max_confidence_interval_multiplier().unwrap(),
21812181
0,
2182+
0,
21822183
)
21832184
.unwrap();
21842185

@@ -3107,6 +3108,32 @@ pub mod fulfill_order {
31073108
assert!(validate_market_within_price_band(&market, &state, oracle_price).is_err());
31083109
}
31093110

3111+
#[test]
3112+
fn test_order_is_low_risk_for_amm() {
3113+
let mut order = Order {
3114+
market_index: 0,
3115+
order_type: OrderType::Market,
3116+
direction: PositionDirection::Long,
3117+
base_asset_amount: 1000,
3118+
..Order::default()
3119+
};
3120+
3121+
// True if no oracle delay and no auction duration
3122+
assert!(order.is_low_risk_for_amm(0, 0, 500).unwrap());
3123+
3124+
// True if oracle delay but order older than auction duration
3125+
order.slot = 95;
3126+
assert!(order.is_low_risk_for_amm(20, 3, 100).unwrap());
3127+
3128+
// True if order is new but the order slot is older than the oracle delay
3129+
order.slot = 97;
3130+
assert!(order.is_low_risk_for_amm(2, 3, 100).unwrap());
3131+
3132+
// False if order is newer than auction duration and oracle delay
3133+
order.slot = 98;
3134+
assert!(!order.is_low_risk_for_amm(4, 3, 100).unwrap());
3135+
}
3136+
31103137
#[test]
31113138
fn fulfill_with_amm_skip_auction_duration() {
31123139
let mut oracle_price = get_pyth_price(100, 6);
@@ -3159,17 +3186,25 @@ pub mod fulfill_order {
31593186
..State::default()
31603187
};
31613188

3162-
assert!(!market.can_skip_auction_duration(&state, false).unwrap());
3189+
assert!(!market
3190+
.can_skip_auction_duration(&state, false, false)
3191+
.unwrap());
31633192

31643193
market.amm.net_revenue_since_last_funding = 1;
3165-
assert!(!market.can_skip_auction_duration(&state, false).unwrap());
3166-
assert!(market.can_skip_auction_duration(&state, true).unwrap());
3194+
assert!(!market
3195+
.can_skip_auction_duration(&state, false, false)
3196+
.unwrap());
3197+
assert!(market
3198+
.can_skip_auction_duration(&state, true, false)
3199+
.unwrap());
31673200

31683201
assert!(!state.amm_immediate_fill_paused().unwrap());
31693202
state.exchange_status = 0b10000000;
31703203
assert!(state.amm_immediate_fill_paused().unwrap());
31713204

3172-
assert!(!market.can_skip_auction_duration(&state, true).unwrap());
3205+
assert!(!market
3206+
.can_skip_auction_duration(&state, true, false)
3207+
.unwrap());
31733208
}
31743209

31753210
#[test]

programs/drift/src/controller/pnl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub fn settle_pnl(
135135
.last_oracle_price_twap,
136136
perp_market.get_max_confidence_interval_multiplier()?,
137137
0,
138+
0,
138139
)?;
139140

140141
if !is_oracle_valid_for_action(oracle_validity, Some(DriftAction::SettlePnl))?

programs/drift/src/controller/repeg.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ pub fn _update_amm(
176176
&market.amm.oracle_source,
177177
oracle::LogMode::SafeMMOracle,
178178
0,
179+
0,
179180
)?;
180181

181182
let mut amm_update_cost = 0;
@@ -230,7 +231,7 @@ pub fn _update_amm(
230231

231232
update_spreads(market, reserve_price_after, Some(clock_slot))?;
232233

233-
if is_oracle_valid_for_action(oracle_validity, Some(DriftAction::FillOrderAmm))? {
234+
if is_oracle_valid_for_action(oracle_validity, Some(DriftAction::FillOrderAmmImmediate))? {
234235
if !amm_not_successfully_updated {
235236
market.amm.last_update_slot = clock_slot;
236237
}
@@ -265,6 +266,7 @@ pub fn update_amm_and_check_validity(
265266
&market.amm.oracle_source,
266267
LogMode::SafeMMOracle,
267268
0,
269+
0,
268270
)?;
269271

270272
validate!(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub fn update_amm_test() {
115115
&market.amm.oracle_source,
116116
LogMode::ExchangeOracle,
117117
0,
118+
0,
118119
)
119120
.unwrap()
120121
== OracleValidity::Valid;
@@ -249,6 +250,7 @@ pub fn update_amm_test_bad_oracle() {
249250
&market.amm.oracle_source,
250251
LogMode::None,
251252
0,
253+
0,
252254
)
253255
.unwrap()
254256
== OracleValidity::Valid;

programs/drift/src/controller/spot_balance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ pub fn update_spot_market_and_check_validity(
412412
&spot_market.oracle_source,
413413
LogMode::ExchangeOracle,
414414
0,
415+
0,
415416
)?;
416417

417418
validate!(

programs/drift/src/instructions/admin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,8 @@ pub fn handle_initialize_perp_market(
10801080
reference_price_offset: 0,
10811081
amm_inventory_spread_adjustment: 0,
10821082
reference_price_offset_deadband_pct: 0,
1083-
padding: [0; 2],
1083+
slots_before_stale_for_amm_low_risk: 0,
1084+
padding: [0; 1],
10841085
last_funding_oracle_twap: 0,
10851086
},
10861087
};

programs/drift/src/instructions/user.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,7 @@ pub fn handle_transfer_perp_position<'c: 'info, 'info>(
17891789
.last_oracle_price_twap,
17901790
perp_market.get_max_confidence_interval_multiplier()?,
17911791
perp_market.amm.oracle_slot_delay_override,
1792+
perp_market.amm.slots_before_stale_for_amm_low_risk,
17921793
)?;
17931794
step_size = perp_market.amm.order_step_size;
17941795
tick_size = perp_market.amm.order_tick_size;

programs/drift/src/math/margin.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info(
265265
spot_market.historical_oracle_data.last_oracle_price_twap,
266266
spot_market.get_max_confidence_interval_multiplier()?,
267267
0,
268+
0,
268269
)?;
269270

270271
let mut skip_token_value = false;
@@ -517,6 +518,7 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info(
517518
.last_oracle_price_twap,
518519
quote_spot_market.get_max_confidence_interval_multiplier()?,
519520
0,
521+
0,
520522
)?;
521523

522524
let strict_quote_price = StrictOraclePrice::new(
@@ -535,6 +537,7 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info(
535537
market.amm.historical_oracle_data.last_oracle_price_twap,
536538
market.get_max_confidence_interval_multiplier()?,
537539
0,
540+
0,
538541
)?;
539542

540543
let perp_position_custom_margin_ratio =
@@ -950,6 +953,7 @@ pub fn calculate_user_equity(
950953
spot_market.historical_oracle_data.last_oracle_price_twap,
951954
spot_market.get_max_confidence_interval_multiplier()?,
952955
0,
956+
0,
953957
)?;
954958
all_oracles_valid &=
955959
is_oracle_valid_for_action(oracle_validity, Some(DriftAction::MarginCalc))?;
@@ -980,6 +984,7 @@ pub fn calculate_user_equity(
980984
.last_oracle_price_twap,
981985
quote_spot_market.get_max_confidence_interval_multiplier()?,
982986
0,
987+
0,
983988
)?;
984989

985990
all_oracles_valid &=
@@ -995,6 +1000,7 @@ pub fn calculate_user_equity(
9951000
market.amm.historical_oracle_data.last_oracle_price_twap,
9961001
market.get_max_confidence_interval_multiplier()?,
9971002
0,
1003+
0,
9981004
)?;
9991005

10001006
all_oracles_valid &=

0 commit comments

Comments
 (0)