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 20, 2024
1 parent 45985dc commit d9e0556
Show file tree
Hide file tree
Showing 25 changed files with 336 additions and 178 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions crates/driver/src/boundary/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use {
},
async_trait::async_trait,
ethcontract::{transaction::TransactionBuilder, transport::DynTransport},
number::U256,
primitive_types::U256 as RawU256,
solver::{
settlement_access_list::AccessListEstimating,
settlement_submission::{
Expand All @@ -30,7 +32,7 @@ pub use {gas_estimation::GasPriceEstimating, solver::settlement_submission::Glob
#[derive(Debug, Clone)]
pub struct Config {
pub additional_tip_percentage: f64,
pub gas_price_cap: eth::U256,
pub gas_price_cap: U256,
pub target_confirm_time: std::time::Duration,
pub max_confirm_time: std::time::Duration,
pub retry_interval: std::time::Duration,
Expand All @@ -51,7 +53,7 @@ pub enum Kind {
/// The MEVBlocker private mempool.
MEVBlocker {
url: reqwest::Url,
max_additional_tip: eth::U256,
max_additional_tip: RawU256,
use_soft_cancellations: bool,
},
}
Expand Down Expand Up @@ -148,7 +150,7 @@ impl Mempool {
.transaction_count(solver.address().into(), None)
.await
.map_err(anyhow::Error::from)?;
let max_fee_per_gas = eth::U256::from(settlement.gas.price.max()).to_f64_lossy();
let max_fee_per_gas = RawU256::from(settlement.gas.price.max()).to_f64_lossy();
let gas_price_estimator = SubmitterGasPriceEstimator {
inner: self.gas_price_estimator.as_ref(),
max_fee_per_gas: max_fee_per_gas.min(self.config.gas_price_cap.to_f64_lossy()),
Expand Down
11 changes: 9 additions & 2 deletions crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
},
futures::future::{join_all, BoxFuture, FutureExt, Shared},
itertools::Itertools,
number::U256,
std::{
collections::{HashMap, HashSet},
sync::{Arc, Mutex},
Expand Down Expand Up @@ -410,8 +411,14 @@ impl From<Price> for eth::U256 {
}
}

impl From<eth::U256> for Price {
fn from(value: eth::U256) -> Self {
impl From<Price> for U256 {
fn from(value: Price) -> Self {
value.0.into()
}
}

impl From<U256> for Price {
fn from(value: U256) -> Self {
Self(value.into())
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/driver/src/domain/competition/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
},
bigdecimal::Zero,
num::CheckedDiv,
number::U256,
};
pub use {fees::FeePolicy, signature::Signature};

Expand Down Expand Up @@ -56,6 +57,12 @@ impl From<eth::U256> for SellAmount {
}
}

impl From<U256> for SellAmount {
fn from(value: U256) -> Self {
Self(value.into())
}
}

impl From<eth::TokenAmount> for SellAmount {
fn from(value: eth::TokenAmount) -> Self {
Self(value.into())
Expand All @@ -68,6 +75,12 @@ impl From<SellAmount> for eth::U256 {
}
}

impl From<SellAmount> for U256 {
fn from(sell_amount: SellAmount) -> Self {
sell_amount.0.into()
}
}

/// An amount denominated in the sell token for [`Side::Sell`] [`Order`]s, or in
/// the buy token for [`Side::Buy`] [`Order`]s.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -79,6 +92,12 @@ impl From<eth::U256> for TargetAmount {
}
}

impl From<U256> for TargetAmount {
fn from(value: U256) -> Self {
Self(value.into())
}
}

impl From<TargetAmount> for eth::U256 {
fn from(value: TargetAmount) -> Self {
value.0
Expand Down
9 changes: 9 additions & 0 deletions crates/driver/src/domain/competition/score.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::{boundary, domain::eth},
number::U256,
std::cmp::Ordering,
};

Expand All @@ -22,6 +23,14 @@ impl TryFrom<eth::U256> for Score {
}
}

impl TryFrom<U256> for Score {
type Error = Error;

fn try_from(value: U256) -> Result<Self, Self::Error> {
eth::U256::from(value).try_into()
}
}

/// Represents the observed quality of a solution. It's defined as surplus +
/// fees.
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
Expand Down
37 changes: 37 additions & 0 deletions crates/driver/src/domain/eth/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
super::{Ether, U256},
bigdecimal::Zero,
num::zero,
number::U256 as NumberU256,
std::{ops, ops::Add},
};

Expand All @@ -18,6 +19,12 @@ impl From<U256> for Gas {
}
}

impl From<NumberU256> for Gas {
fn from(value: NumberU256) -> Self {
Self(value.into())
}
}

impl From<u64> for Gas {
fn from(value: u64) -> Self {
Self(value.into())
Expand All @@ -30,6 +37,12 @@ impl From<Gas> for U256 {
}
}

impl From<Gas> for NumberU256 {
fn from(value: Gas) -> Self {
value.0.into()
}
}

impl Add for Gas {
type Output = Self;

Expand Down Expand Up @@ -147,6 +160,12 @@ impl From<U256> for FeePerGas {
}
}

impl From<NumberU256> for FeePerGas {
fn from(value: NumberU256) -> Self {
Self(value.into())
}
}

impl ops::Add<FeePerGas> for FeePerGas {
type Output = FeePerGas;

Expand All @@ -161,6 +180,12 @@ impl From<FeePerGas> for U256 {
}
}

impl From<FeePerGas> for NumberU256 {
fn from(value: FeePerGas) -> Self {
value.0.into()
}
}

impl ops::Mul<FeePerGas> for Gas {
type Output = Ether;

Expand All @@ -181,12 +206,24 @@ impl From<U256> for EffectiveGasPrice {
}
}

impl From<NumberU256> for EffectiveGasPrice {
fn from(value: NumberU256) -> Self {
Self(value.into())
}
}

impl From<EffectiveGasPrice> for U256 {
fn from(value: EffectiveGasPrice) -> Self {
value.0.into()
}
}

impl From<EffectiveGasPrice> for NumberU256 {
fn from(value: EffectiveGasPrice) -> Self {
value.0.into()
}
}

impl Add for EffectiveGasPrice {
type Output = Self;

Expand Down
25 changes: 25 additions & 0 deletions crates/driver/src/domain/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod allowance;
mod eip712;
mod gas;

use number::U256 as NumberU256;
pub use {
allowance::Allowance,
eip712::{DomainFields, DomainSeparator},
Expand Down Expand Up @@ -201,12 +202,24 @@ impl From<U256> for TokenAmount {
}
}

impl From<NumberU256> for TokenAmount {
fn from(value: NumberU256) -> Self {
Self(value.into())
}
}

impl From<TokenAmount> for U256 {
fn from(value: TokenAmount) -> Self {
value.0
}
}

impl From<TokenAmount> for NumberU256 {
fn from(value: TokenAmount) -> Self {
value.0.into()
}
}

impl From<u128> for TokenAmount {
fn from(value: u128) -> Self {
Self(value.into())
Expand Down Expand Up @@ -271,6 +284,12 @@ impl From<U256> for Ether {
}
}

impl From<NumberU256> for Ether {
fn from(value: NumberU256) -> Self {
Self(value.into())
}
}

impl From<Ether> for num::BigInt {
fn from(value: Ether) -> Self {
let mut bytes = [0; 32];
Expand All @@ -285,6 +304,12 @@ impl From<Ether> for U256 {
}
}

impl From<Ether> for NumberU256 {
fn from(value: Ether) -> Self {
value.0.into()
}
}

impl From<i32> for Ether {
fn from(value: i32) -> Self {
Self(value.into())
Expand Down
7 changes: 2 additions & 5 deletions crates/driver/src/infra/api/routes/quote/dto/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use {
crate::{
domain::{competition, eth, quote, time},
infra::solver::Timeouts,
util::serialize,
},
number::U256,
serde::Deserialize,
serde_with::serde_as,
};

impl Order {
Expand All @@ -23,14 +22,12 @@ impl Order {
}
}

#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Order {
sell_token: eth::H160,
buy_token: eth::H160,
#[serde_as(as = "serialize::U256")]
amount: eth::U256,
amount: U256,
kind: Kind,
deadline: chrono::DateTime<chrono::Utc>,
}
Expand Down
10 changes: 4 additions & 6 deletions crates/driver/src/infra/api/routes/quote/dto/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use {
domain::{eth, quote},
util::serialize,
},
number::U256,
serde::Serialize,
serde_with::serde_as,
};

impl Quote {
pub fn new(quote: &quote::Quote) -> Self {
Self {
amount: quote.amount,
amount: quote.amount.into(),
interactions: quote
.interactions
.iter()
Expand All @@ -25,12 +26,10 @@ impl Quote {
}
}

#[serde_as]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Quote {
#[serde_as(as = "serialize::U256")]
amount: eth::U256,
amount: U256,
interactions: Vec<Interaction>,
solver: eth::H160,
}
Expand All @@ -40,8 +39,7 @@ pub struct Quote {
#[serde(rename_all = "camelCase")]
struct Interaction {
target: eth::H160,
#[serde_as(as = "serialize::U256")]
value: eth::U256,
value: U256,
#[serde_as(as = "serialize::Hex")]
call_data: Vec<u8>,
}
Loading

0 comments on commit d9e0556

Please sign in to comment.