Skip to content

Commit

Permalink
Create a wrap for eth::U256 in order to ensure correct serde
Browse files Browse the repository at this point in the history
  • Loading branch information
m-lord-renkse committed Feb 21, 2024
1 parent a0e3ec7 commit cd66af2
Show file tree
Hide file tree
Showing 97 changed files with 1,147 additions and 1,009 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 5 additions & 13 deletions crates/alerter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@ use {
anyhow::{Context, Result},
clap::Parser,
model::order::{OrderClass, OrderKind, OrderStatus, OrderUid, BUY_ETH_ADDRESS},
number::serialization::HexOrDecimalU256,
primitive_types::{H160, U256},
primitive_types::H160,
prometheus::IntGauge,
reqwest::Client,
serde_with::serde_as,
std::{
collections::HashMap,
time::{Duration, Instant},
},
url::Url,
};

#[serde_as]
#[derive(Debug, serde::Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Order {
kind: OrderKind,
buy_token: H160,
#[serde_as(as = "HexOrDecimalU256")]
buy_amount: U256,
buy_amount: number::U256,
sell_token: H160,
#[serde_as(as = "HexOrDecimalU256")]
sell_amount: U256,
sell_amount: number::U256,
uid: OrderUid,
partially_fillable: bool,
#[serde(flatten)]
Expand Down Expand Up @@ -130,14 +125,11 @@ impl ZeroExApi {
.append_pair("buyToken", &format!("{buy_token:#x}"))
.append_pair(amount_name, &amount.to_string());

#[serde_as]
#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
#[serde_as(as = "HexOrDecimalU256")]
pub sell_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub buy_amount: U256,
pub sell_amount: number::U256,
pub buy_amount: number::U256,
}

let response: Response = self
Expand Down
6 changes: 3 additions & 3 deletions crates/autopilot/src/boundary/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub fn to_domain(
uid: order.metadata.uid.into(),
sell_token: order.data.sell_token,
buy_token: order.data.buy_token,
sell_amount: order.data.sell_amount,
buy_amount: order.data.buy_amount,
user_fee: order.data.fee_amount,
sell_amount: *order.data.sell_amount,
buy_amount: *order.data.buy_amount,
user_fee: *order.data.fee_amount,
protocol_fees,
valid_to: order.data.valid_to,
kind: order.data.kind.into(),
Expand Down
63 changes: 34 additions & 29 deletions crates/autopilot/src/database/onchain_order_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,9 @@ async fn get_quote(
let parameters = QuoteSearchParameters {
sell_token: H160::from(order_data.sell_token.0),
buy_token: H160::from(order_data.buy_token.0),
sell_amount: order_data.sell_amount,
buy_amount: order_data.buy_amount,
fee_amount: order_data.fee_amount,
sell_amount: *order_data.sell_amount,
buy_amount: *order_data.buy_amount,
fee_amount: *order_data.fee_amount,
kind: order_data.kind,
signing_scheme: quote_signing_scheme,
additional_gas: 0,
Expand All @@ -569,9 +569,14 @@ async fn get_quote(
true => None,
false => Some(order_data.fee_amount),
};
get_quote_and_check_fee(quoter, &parameters.clone(), Some(*quote_id), fee_amount)
.await
.map_err(onchain_order_placement_error_from)
get_quote_and_check_fee(
quoter,
&parameters.clone(),
Some(*quote_id),
fee_amount.map(Into::into),
)
.await
.map_err(onchain_order_placement_error_from)
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -658,11 +663,11 @@ fn extract_order_data_from_onchain_order_placement_event(
sell_token: order_placement.order.0,
buy_token: order_placement.order.1,
receiver,
sell_amount: order_placement.order.3,
buy_amount: order_placement.order.4,
sell_amount: order_placement.order.3.into(),
buy_amount: order_placement.order.4.into(),
valid_to: order_placement.order.5,
app_data: AppDataHash(order_placement.order.6 .0),
fee_amount: order_placement.order.7,
fee_amount: order_placement.order.7.into(),
kind: OrderKind::from_contract_bytes(order_placement.order.8 .0)?,
partially_fillable: order_placement.order.9,
sell_token_balance: SellTokenSource::from_contract_bytes(order_placement.order.10 .0)?,
Expand Down Expand Up @@ -767,11 +772,11 @@ mod test {
sell_token,
buy_token,
receiver: Some(receiver),
sell_amount,
buy_amount,
sell_amount: sell_amount.into(),
buy_amount: buy_amount.into(),
valid_to,
app_data: AppDataHash(app_data.0),
fee_amount,
fee_amount: fee_amount.into(),
kind: OrderKind::Sell,
partially_fillable: order_placement.order.9,
sell_token_balance: SellTokenSource::Erc20,
Expand Down Expand Up @@ -813,11 +818,11 @@ mod test {
sell_token,
buy_token,
receiver: None,
sell_amount,
buy_amount,
sell_amount: sell_amount.into(),
buy_amount: buy_amount.into(),
valid_to,
app_data: AppDataHash(app_data.0),
fee_amount,
fee_amount: fee_amount.into(),
kind: OrderKind::Sell,
partially_fillable: order_placement.order.9,
sell_token_balance: SellTokenSource::Erc20,
Expand All @@ -844,11 +849,11 @@ mod test {
sell_token,
buy_token,
receiver: Some(receiver),
sell_amount,
buy_amount,
sell_amount: sell_amount.into(),
buy_amount: buy_amount.into(),
valid_to,
app_data: AppDataHash(app_data.0),
fee_amount,
fee_amount: fee_amount.into(),
kind: OrderKind::Sell,
partially_fillable: false,
sell_token_balance: SellTokenSource::Erc20,
Expand Down Expand Up @@ -893,11 +898,11 @@ mod test {
sell_token,
buy_token,
receiver: Some(receiver),
sell_amount,
buy_amount,
sell_amount: sell_amount.into(),
buy_amount: buy_amount.into(),
valid_to,
app_data: AppDataHash(app_data.0),
fee_amount,
fee_amount: fee_amount.into(),
kind: OrderKind::Sell,
partially_fillable: order_placement.order.9,
sell_token_balance: SellTokenSource::Erc20,
Expand Down Expand Up @@ -952,11 +957,11 @@ mod test {
sell_token,
buy_token,
receiver: Some(receiver),
sell_amount,
buy_amount,
sell_amount: sell_amount.into(),
buy_amount: buy_amount.into(),
valid_to,
app_data: AppDataHash(app_data.0),
fee_amount,
fee_amount: fee_amount.into(),
kind: OrderKind::Sell,
partially_fillable: false,
sell_token_balance: SellTokenSource::Erc20,
Expand Down Expand Up @@ -1004,8 +1009,8 @@ mod test {
sell_token,
buy_token,
receiver: Some(receiver),
sell_amount,
buy_amount,
sell_amount: sell_amount.into(),
buy_amount: buy_amount.into(),
valid_to,
app_data: AppDataHash(app_data.0),
fee_amount: 0.into(),
Expand Down Expand Up @@ -1252,11 +1257,11 @@ mod test {
sell_token,
buy_token,
receiver: Some(receiver),
sell_amount,
buy_amount,
sell_amount: sell_amount.into(),
buy_amount: buy_amount.into(),
valid_to,
app_data: AppDataHash(app_data.0),
fee_amount,
fee_amount: fee_amount.into(),
kind: OrderKind::Sell,
partially_fillable: order_placement.order.9,
sell_token_balance: SellTokenSource::Erc20,
Expand Down
6 changes: 3 additions & 3 deletions crates/autopilot/src/decoded_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ impl DecodedTrade {
let order = OrderData {
sell_token: tokens[self.sell_token_index.as_u64() as usize],
buy_token: tokens[self.buy_token_index.as_u64() as usize],
sell_amount: self.sell_amount,
buy_amount: self.buy_amount,
sell_amount: self.sell_amount.into(),
buy_amount: self.buy_amount.into(),
valid_to: self.valid_to,
app_data: AppDataHash(self.app_data.0),
fee_amount: self.fee_amount,
fee_amount: self.fee_amount.into(),
kind: self.flags.order_kind(),
partially_fillable: self.flags.partially_fillable(),
receiver: Some(self.receiver),
Expand Down
6 changes: 3 additions & 3 deletions crates/autopilot/src/domain/fee/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl Surplus {
Some(policy)
} else {
let order_ = boundary::Amounts {
sell: order.data.sell_amount,
buy: order.data.buy_amount,
fee: order.data.fee_amount,
sell: *order.data.sell_amount,
buy: *order.data.buy_amount,
fee: *order.data.fee_amount,
};
let quote_ = boundary::Amounts {
sell: quote.sell_amount,
Expand Down
Loading

0 comments on commit cd66af2

Please sign in to comment.