diff --git a/crates/autopilot/src/boundary/mod.rs b/crates/autopilot/src/boundary/mod.rs index 386369cc22..add6f1822f 100644 --- a/crates/autopilot/src/boundary/mod.rs +++ b/crates/autopilot/src/boundary/mod.rs @@ -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}; diff --git a/crates/autopilot/src/domain/fee/mod.rs b/crates/autopilot/src/domain/fee/mod.rs index 84b5766aca..e62d7dd680 100644 --- a/crates/autopilot/src/domain/fee/mod.rs +++ b/crates/autopilot/src/domain/fee/mod.rs @@ -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, - "e.buy_amount, - "e.sell_amount, - "e.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_, "e) { vec![self.policy] } else { vec![] diff --git a/crates/shared/src/order_validation.rs b/crates/shared/src/order_validation.rs index 926be71565..7d0ccfc23b 100644 --- a/crates/shared/src/order_validation.rs +++ b/crates/shared/src/order_validation.rs @@ -671,12 +671,16 @@ impl OrderValidating for OrderValidator { let class = match (class, "e) { (OrderClass::Market, Some(quote)) if is_order_outside_market_price( - "e_parameters.sell_amount, - "e_parameters.buy_amount, - "e_parameters.fee_amount, - "e.sell_amount, - "e.buy_amount, - "e.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"); @@ -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( @@ -2237,30 +2241,42 @@ mod tests { // at market price assert!(!is_order_outside_market_price( - &"100".into(), - &"100".into(), - &"0".into(), - "e.sell_amount, - "e.buy_amount, - "e.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(), - "e.sell_amount, - "e.buy_amount, - "e.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(), - "e.sell_amount, - "e.buy_amount, - "e.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, + }, )); } }