Skip to content

Commit 052daca

Browse files
committed
all builds
1 parent efb38fd commit 052daca

File tree

10 files changed

+620
-2878
lines changed

10 files changed

+620
-2878
lines changed

programs/drift/src/controller/orders.rs

Lines changed: 15 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::state::order_params::{
6262
ModifyOrderParams, OrderParams, PlaceOrderOptions, PostOnlyParam,
6363
};
6464
use crate::state::paused_operations::{PerpOperation, SpotOperation};
65-
use crate::state::perp_market::{AMMAvailability, MarketStatus, PerpMarket};
65+
use crate::state::perp_market::{MarketStatus, PerpMarket};
6666
use crate::state::perp_market_map::PerpMarketMap;
6767
use crate::state::protected_maker_mode_config::ProtectedMakerParams;
6868
use crate::state::spot_fulfillment_params::{ExternalSpotFill, SpotFulfillmentParams};
@@ -88,10 +88,6 @@ mod tests;
8888

8989
#[cfg(test)]
9090
mod amm_jit_tests;
91-
92-
#[cfg(test)]
93-
mod amm_lp_jit_tests;
94-
9591
#[cfg(test)]
9692
mod fuel_tests;
9793

@@ -998,13 +994,8 @@ pub fn fill_perp_order(
998994
.position(|order| order.order_id == order_id && order.status == OrderStatus::Open)
999995
.ok_or_else(print_error!(ErrorCode::OrderDoesNotExist))?;
1000996

1001-
let (order_status, market_index, order_market_type, order_direction) = get_struct_values!(
1002-
user.orders[order_index],
1003-
status,
1004-
market_index,
1005-
market_type,
1006-
direction
1007-
);
997+
let (order_status, market_index, order_market_type) =
998+
get_struct_values!(user.orders[order_index], status, market_index, market_type);
1008999

10091000
validate!(
10101001
order_market_type == MarketType::Perp,
@@ -1070,14 +1061,9 @@ pub fn fill_perp_order(
10701061
let safe_oracle_validity: OracleValidity;
10711062
let oracle_price: i64;
10721063
let oracle_twap_5min: i64;
1073-
let perp_market_index: u16;
10741064
let user_can_skip_duration: bool;
1075-
let amm_can_skip_duration: bool;
1076-
let amm_has_low_enough_inventory: bool;
1077-
let oracle_valid_for_amm_fill: bool;
10781065
let oracle_stale_for_margin: bool;
1079-
let min_auction_duration: u8;
1080-
let mut amm_is_available = !state.amm_paused()?;
1066+
let mut amm_is_available: bool = !state.amm_paused()?;
10811067
{
10821068
let market = &mut perp_market_map.get_ref_mut(&market_index)?;
10831069
validation::perp_market::validate_perp_market(market)?;
@@ -1107,76 +1093,37 @@ pub fn fill_perp_order(
11071093
market.amm.slots_before_stale_for_amm_low_risk,
11081094
)?;
11091095

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,
1096+
user_can_skip_duration = user.can_skip_auction_duration(user_stats)?;
1097+
amm_is_available &= market.amm_can_fill_order(
1098+
&user.orders[order_index],
11131099
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(
1100+
fill_mode,
1101+
state,
11221102
safe_oracle_validity,
1123-
Some(DriftAction::FillOrderAmmLowRisk),
1103+
user_can_skip_duration,
1104+
&mm_oracle_price_data,
11241105
)?;
11251106

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);
1128-
11291107
oracle_stale_for_margin = mm_oracle_price_data.get_delay()
11301108
> state
11311109
.oracle_guard_rails
11321110
.validity
11331111
.slots_before_stale_for_margin;
11341112

1135-
amm_is_available &= oracle_valid_for_amm_fill;
1136-
amm_is_available &= !market.is_operation_paused(PerpOperation::AmmFill);
1137-
amm_is_available &= !market.has_too_much_drawdown()?;
1138-
1139-
// We are already using safe oracle data from MM oracle.
1140-
// But AMM isnt available if we could have used MM oracle but fell back due to price diff
1141-
let amm_available_mm_oracle_recent_but_volatile =
1142-
if mm_oracle_price_data.is_enabled() && mm_oracle_price_data.is_mm_oracle_as_recent() {
1143-
let amm_available = !mm_oracle_price_data.is_mm_exchange_diff_bps_high();
1144-
amm_available
1145-
} else {
1146-
true
1147-
};
1148-
amm_is_available &= amm_available_mm_oracle_recent_but_volatile;
1149-
1150-
let amm_wants_to_jit_make = market.amm.amm_wants_to_jit_make(order_direction)?;
1151-
amm_has_low_enough_inventory = market
1152-
.amm
1153-
.amm_has_low_enough_inventory(amm_wants_to_jit_make)?;
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-
)?;
1159-
1160-
user_can_skip_duration = user.can_skip_auction_duration(user_stats)?;
1161-
11621113
reserve_price_before = market.amm.reserve_price()?;
11631114
oracle_price = mm_oracle_price_data.get_price();
11641115
oracle_twap_5min = market
11651116
.amm
11661117
.historical_oracle_data
11671118
.last_oracle_price_twap_5min;
1168-
perp_market_index = market.market_index;
1169-
1170-
min_auction_duration =
1171-
market.get_min_perp_auction_duration(state.min_perp_auction_duration);
11721119
}
11731120

11741121
// allow oracle price to be used to calculate limit price if it's valid or stale for amm
11751122
let valid_oracle_price =
11761123
if is_oracle_valid_for_action(safe_oracle_validity, Some(DriftAction::OracleOrderPrice))? {
11771124
Some(oracle_price)
11781125
} else {
1179-
msg!("Perp market = {} oracle deemed invalid", perp_market_index);
1126+
msg!("Perp market = {} oracle deemed invalid", market_index);
11801127
None
11811128
};
11821129

@@ -1305,16 +1252,6 @@ pub fn fill_perp_order(
13051252
return Ok((0, 0));
13061253
}
13071254

1308-
let amm_availability = if amm_is_available {
1309-
if amm_can_skip_duration && user_can_skip_duration {
1310-
AMMAvailability::Immediate
1311-
} else {
1312-
AMMAvailability::AfterMinDuration
1313-
}
1314-
} else {
1315-
AMMAvailability::Unavailable
1316-
};
1317-
13181255
let (base_asset_amount, quote_asset_amount) = fulfill_perp_order(
13191256
user,
13201257
order_index,
@@ -1335,8 +1272,7 @@ pub fn fill_perp_order(
13351272
valid_oracle_price,
13361273
now,
13371274
slot,
1338-
min_auction_duration,
1339-
amm_availability,
1275+
amm_is_available,
13401276
fill_mode,
13411277
oracle_stale_for_margin,
13421278
rev_share_escrow,
@@ -1802,8 +1738,7 @@ fn fulfill_perp_order(
18021738
valid_oracle_price: Option<i64>,
18031739
now: i64,
18041740
slot: u64,
1805-
min_auction_duration: u8,
1806-
amm_availability: AMMAvailability,
1741+
amm_is_available: bool,
18071742
fill_mode: FillMode,
18081743
oracle_stale_for_margin: bool,
18091744
rev_share_escrow: &mut Option<&mut RevenueShareEscrowZeroCopyMut>,
@@ -1827,19 +1762,13 @@ fn fulfill_perp_order(
18271762

18281763
let fulfillment_methods = {
18291764
let market = perp_market_map.get_ref(&market_index)?;
1830-
let oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;
1831-
18321765
determine_perp_fulfillment_methods(
18331766
&user.orders[user_order_index],
18341767
maker_orders_info,
18351768
&market.amm,
18361769
reserve_price_before,
1837-
Some(oracle_price),
18381770
limit_price,
1839-
amm_availability,
1840-
slot,
1841-
min_auction_duration,
1842-
fill_mode,
1771+
amm_is_available,
18431772
)?
18441773
};
18451774

0 commit comments

Comments
 (0)