Skip to content

Commit

Permalink
Fix market price check for market orders (#2315)
Browse files Browse the repository at this point in the history
# Description
#2304 fixed the checker for
limit orders. However, for market orders, which signed `sell_amount`
doesn't contain the signed `fee_amount` (so it's basically
`sell_amount_after_fee`), we need to take this fee into acount.

For limit orders nothing changes, since those have `fee_amount` = 0.

Fixes #2312
  • Loading branch information
sunce86 committed Jan 23, 2024
1 parent 83cea49 commit abd8d90
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/autopilot/src/domain/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl ProtocolFee {
if boundary::is_order_outside_market_price(
&order.data.sell_amount,
&order.data.buy_amount,
&order.data.fee_amount,
&quote.buy_amount,
&quote.sell_amount,
&quote.fee,
Expand Down
7 changes: 6 additions & 1 deletion crates/shared/src/order_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ impl OrderValidating for OrderValidator {
if is_order_outside_market_price(
&quote_parameters.sell_amount,
&quote_parameters.buy_amount,
&quote_parameters.fee_amount,
&quote.sell_amount,
&quote.buy_amount,
&quote.fee_amount,
Expand Down Expand Up @@ -980,11 +981,12 @@ async fn get_or_create_quote(
pub fn is_order_outside_market_price(
sell_amount: &U256,
buy_amount: &U256,
fee_amount: &U256,
quote_sell_amount: &U256,
quote_buy_amount: &U256,
quote_fee_amount: &U256,
) -> bool {
sell_amount.full_mul(*quote_buy_amount)
(sell_amount + fee_amount).full_mul(*quote_buy_amount)
< (quote_sell_amount + quote_fee_amount).full_mul(*buy_amount)
}

Expand Down Expand Up @@ -2427,6 +2429,7 @@ mod tests {
assert!(!is_order_outside_market_price(
&"100".into(),
&"100".into(),
&"0".into(),
&quote.sell_amount,
&quote.buy_amount,
&quote.fee_amount,
Expand All @@ -2435,6 +2438,7 @@ mod tests {
assert!(!is_order_outside_market_price(
&"100".into(),
&"90".into(),
&"0".into(),
&quote.sell_amount,
&quote.buy_amount,
&quote.fee_amount,
Expand All @@ -2443,6 +2447,7 @@ mod tests {
assert!(is_order_outside_market_price(
&"100".into(),
&"1000".into(),
&"0".into(),
&quote.sell_amount,
&quote.buy_amount,
&quote.fee_amount,
Expand Down

0 comments on commit abd8d90

Please sign in to comment.