Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Nov 29, 2024
1 parent 68ce1fd commit 34ca52b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
13 changes: 12 additions & 1 deletion programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ fn get_maker_orders_info(
}

if maker_order.has_oracle_price_offset() && is_protected_maker {
if !oracle_valid_for_amm_fill || (oracle_delay > 0 && !user_can_skip_duration && taker_order_age < protected_maker_min_age) {
if !protected_maker_oracle_limit_can_fill(oracle_valid_for_amm_fill, oracle_delay, user_can_skip_duration, taker_order_age, protected_maker_min_age) {
continue;
}
}
Expand All @@ -1727,6 +1727,17 @@ fn get_maker_orders_info(
Ok(maker_orders_info)
}

#[inline(always)]
fn protected_maker_oracle_limit_can_fill(
oracle_valid_for_amm_fill: bool,
oracle_delay: i64,
user_can_skip_duration: bool,
taker_order_age: u64,
protected_maker_min_age: u64,
) -> bool {
oracle_valid_for_amm_fill && (oracle_delay == 0 || user_can_skip_duration || taker_order_age > protected_maker_min_age)
}

#[inline(always)]
fn insert_maker_order_info(
maker_orders_info: &mut Vec<(Pubkey, usize, u64)>,
Expand Down
14 changes: 14 additions & 0 deletions programs/drift/src/controller/orders/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12226,3 +12226,17 @@ mod update_maker_fills_map {
assert_eq!(*map.get(&maker_key).unwrap(), -2 * fill as i64);
}
}

pub mod protected_maker_oracle_limit_can_fill {
use crate::controller::orders::protected_maker_oracle_limit_can_fill;

#[test]
fn test() {
assert!(protected_maker_oracle_limit_can_fill(true, 0, true, 10, 10)); // all cases
assert!(protected_maker_oracle_limit_can_fill(true, 0, false, 9, 10)); // oracle delay is 0
assert!(protected_maker_oracle_limit_can_fill(true, 1, false, 10, 9)); // min age passed
assert!(protected_maker_oracle_limit_can_fill(true, 1, true, 9, 10)); // user exempt
assert!(!protected_maker_oracle_limit_can_fill(true, 1, false, 10, 11)); // no condition met
assert!(!protected_maker_oracle_limit_can_fill(false, 0, true, 10, 10)); // oracle valid for amm fill is false
}
}
2 changes: 1 addition & 1 deletion programs/drift/src/math/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub fn can_fill_with_amm(
slot: u64,
fill_mode: FillMode,
) -> DriftResult<bool> {
Ok(!(amm_availability == AMMAvailability::Unavailable)
Ok(amm_availability != AMMAvailability::Unavailable
&& valid_oracle_price.is_some()
&& (amm_availability == AMMAvailability::Immediate
|| is_amm_available_liquidity_source(order, min_auction_duration, slot, fill_mode)?))
Expand Down

0 comments on commit 34ca52b

Please sign in to comment.