From 765e0027342acfa282a64e7f7da1aeb3c71dd854 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Wed, 24 Jan 2024 11:07:37 +0100 Subject: [PATCH 1/3] Fix bug for fee market price check --- crates/autopilot/src/domain/fee/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/autopilot/src/domain/fee/mod.rs b/crates/autopilot/src/domain/fee/mod.rs index 84b5766aca..7247e79bce 100644 --- a/crates/autopilot/src/domain/fee/mod.rs +++ b/crates/autopilot/src/domain/fee/mod.rs @@ -51,8 +51,8 @@ impl ProtocolFee { &order.data.sell_amount, &order.data.buy_amount, &order.data.fee_amount, - "e.buy_amount, "e.sell_amount, + "e.buy_amount, "e.fee, ) { vec![self.policy] From a0e14b267e9754806788598197588c697cfcaea5 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Wed, 24 Jan 2024 11:42:27 +0100 Subject: [PATCH 2/3] Named structs --- crates/autopilot/src/boundary/mod.rs | 2 +- crates/autopilot/src/domain/fee/mod.rs | 16 +++-- crates/shared/src/order_validation.rs | 83 +++++++++++++++----------- 3 files changed, 60 insertions(+), 41 deletions(-) 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 7247e79bce..ec38c7f974 100644 --- a/crates/autopilot/src/domain/fee/mod.rs +++ b/crates/autopilot/src/domain/fee/mod.rs @@ -48,12 +48,16 @@ impl ProtocolFee { 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.sell_amount, - "e.buy_amount, - "e.fee, + &boundary::Amounts { + sell: order.data.sell_amount, + buy: order.data.buy_amount, + fee: order.data.fee_amount, + }, + &boundary::Amounts { + sell: quote.sell_amount, + buy: quote.buy_amount, + fee: quote.fee, + }, ) { vec![self.policy] } else { diff --git a/crates/shared/src/order_validation.rs b/crates/shared/src/order_validation.rs index 926be71565..6d82e84c90 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,20 @@ async fn get_or_create_quote( Ok(quote) } +/// Amounts used for market price checker. +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 +2240,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, + }, )); } } From 28fe3d4865942cb914df2f497607bddba848ecff Mon Sep 17 00:00:00 2001 From: sunce86 Date: Wed, 24 Jan 2024 11:47:25 +0100 Subject: [PATCH 3/3] fix trace log --- crates/autopilot/src/domain/fee/mod.rs | 25 ++++++++++++------------- crates/shared/src/order_validation.rs | 1 + 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/autopilot/src/domain/fee/mod.rs b/crates/autopilot/src/domain/fee/mod.rs index ec38c7f974..e62d7dd680 100644 --- a/crates/autopilot/src/domain/fee/mod.rs +++ b/crates/autopilot/src/domain/fee/mod.rs @@ -46,19 +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( - &boundary::Amounts { - sell: order.data.sell_amount, - buy: order.data.buy_amount, - fee: order.data.fee_amount, - }, - &boundary::Amounts { - sell: quote.sell_amount, - buy: quote.buy_amount, - fee: 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_, "e) { vec![self.policy] } else { vec![] diff --git a/crates/shared/src/order_validation.rs b/crates/shared/src/order_validation.rs index 6d82e84c90..7d0ccfc23b 100644 --- a/crates/shared/src/order_validation.rs +++ b/crates/shared/src/order_validation.rs @@ -870,6 +870,7 @@ async fn get_or_create_quote( } /// Amounts used for market price checker. +#[derive(Debug)] pub struct Amounts { pub sell: U256, pub buy: U256,