Skip to content

Commit

Permalink
Fix bug for fee market price check (#2324)
Browse files Browse the repository at this point in the history
# Description
Quote sell amount and quote buy amount were substituted.

🤷
  • Loading branch information
sunce86 authored Jan 24, 2024
1 parent 09c681f commit afcc32f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
2 changes: 1 addition & 1 deletion crates/autopilot/src/boundary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use {
signature::{EcdsaSignature, Signature},
DomainSeparator,
},
shared::order_validation::is_order_outside_market_price,
shared::order_validation::{is_order_outside_market_price, Amounts},
};
use {ethrpc::Web3, url::Url};

Expand Down
21 changes: 12 additions & 9 deletions crates/autopilot/src/domain/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ impl ProtocolFee {
return vec![];
};

tracing::debug!(?order.metadata.uid, ?self.policy, ?order.data.sell_amount, ?order.data.buy_amount, ?quote, "checking if order is outside market price");
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,
) {
let order_ = boundary::Amounts {
sell: order.data.sell_amount,
buy: order.data.buy_amount,
fee: order.data.fee_amount,
};
let quote = boundary::Amounts {
sell: quote.sell_amount,
buy: quote.buy_amount,
fee: quote.fee,
};
tracing::debug!(?order.metadata.uid, ?self.policy, ?order_, ?quote, "checking if order is outside market price");
if boundary::is_order_outside_market_price(&order_, &quote) {
vec![self.policy]
} else {
vec![]
Expand Down
84 changes: 50 additions & 34 deletions crates/shared/src/order_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,16 @@ impl OrderValidating for OrderValidator {
let class = match (class, &quote) {
(OrderClass::Market, Some(quote))
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,
&Amounts {
sell: data.sell_amount,
buy: data.buy_amount,
fee: data.fee_amount,
},
&Amounts {
sell: quote.sell_amount,
buy: quote.buy_amount,
fee: quote.fee_amount,
},
) =>
{
tracing::debug!(%uid, ?owner, ?class, "order being flagged as outside market price");
Expand Down Expand Up @@ -865,21 +869,21 @@ async fn get_or_create_quote(
Ok(quote)
}

/// Amounts used for market price checker.
#[derive(Debug)]
pub struct Amounts {
pub sell: U256,
pub buy: U256,
pub fee: U256,
}

/// Checks whether or not an order's limit price is outside the market price
/// specified by the quote.
///
/// Note that this check only looks at the order's limit price and the market
/// price and is independent of amounts or trade direction.
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 + fee_amount).full_mul(*quote_buy_amount)
< (quote_sell_amount + quote_fee_amount).full_mul(*buy_amount)
pub fn is_order_outside_market_price(order: &Amounts, quote: &Amounts) -> bool {
(order.sell + order.fee).full_mul(quote.buy) < (quote.sell + quote.fee).full_mul(order.buy)
}

pub fn convert_signing_scheme_into_quote_signing_scheme(
Expand Down Expand Up @@ -2237,30 +2241,42 @@ mod tests {

// at market price
assert!(!is_order_outside_market_price(
&"100".into(),
&"100".into(),
&"0".into(),
&quote.sell_amount,
&quote.buy_amount,
&quote.fee_amount,
&Amounts {
sell: 100.into(),
buy: 100.into(),
fee: 0.into(),
},
&Amounts {
sell: quote.sell_amount,
buy: quote.buy_amount,
fee: quote.fee_amount,
},
));
// willing to buy less than market price
assert!(!is_order_outside_market_price(
&"100".into(),
&"90".into(),
&"0".into(),
&quote.sell_amount,
&quote.buy_amount,
&quote.fee_amount,
&Amounts {
sell: 100.into(),
buy: 90.into(),
fee: 0.into(),
},
&Amounts {
sell: quote.sell_amount,
buy: quote.buy_amount,
fee: quote.fee_amount,
},
));
// wanting to buy more than market price
assert!(is_order_outside_market_price(
&"100".into(),
&"1000".into(),
&"0".into(),
&quote.sell_amount,
&quote.buy_amount,
&quote.fee_amount,
&Amounts {
sell: 100.into(),
buy: 1000.into(),
fee: 0.into(),
},
&Amounts {
sell: quote.sell_amount,
buy: quote.buy_amount,
fee: quote.fee_amount,
},
));
}
}

0 comments on commit afcc32f

Please sign in to comment.