Skip to content
26 changes: 14 additions & 12 deletions crates/autopilot/src/domain/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use {
boundary::{self},
domain::{self, eth},
},
alloy::primitives::{Address, U256},
app_data::Validator,
derive_more::Into,
primitive_types::{H160, U256},
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
primitive_types::H160,
rust_decimal::Decimal,
std::{collections::HashSet, str::FromStr},
};
Expand Down Expand Up @@ -182,10 +184,10 @@ impl ProtocolFees {
factor,
max_volume_factor,
quote: Quote {
sell_amount: quote.sell_amount.into(),
buy_amount: quote.buy_amount.into(),
fee: quote.fee.into(),
solver: quote.solver.into(),
sell_amount: quote.sell_amount.0.into_alloy(),
buy_amount: quote.buy_amount.0.into_alloy(),
fee: quote.fee.0.into_alloy(),
solver: quote.solver,
},
}
}
Expand All @@ -207,9 +209,9 @@ impl ProtocolFees {
let reference_quote = quote.clone().unwrap_or(domain::Quote {
order_uid: order.metadata.uid.into(),
sell_amount: order.data.sell_amount.into(),
buy_amount: U256::zero().into(),
buy_amount: U256::ZERO.into_legacy().into(),
fee: order.data.fee_amount.into(),
solver: H160::zero().into(),
solver: Address::ZERO,
});

let partner_fee =
Expand Down Expand Up @@ -333,16 +335,16 @@ pub struct Quote {
pub buy_amount: U256,
/// The amount that needs to be paid, denominated in the sell token.
pub fee: U256,
pub solver: H160,
pub solver: Address,
}

impl Quote {
fn from_domain(value: &domain::Quote) -> Self {
Self {
sell_amount: value.sell_amount.into(),
buy_amount: value.buy_amount.into(),
fee: value.fee.into(),
solver: value.solver.into(),
sell_amount: value.sell_amount.0.into_alloy(),
buy_amount: value.buy_amount.0.into_alloy(),
fee: value.fee.0.into_alloy(),
solver: value.solver,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/autopilot/src/domain/quote/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
super::OrderUid,
crate::{boundary::Amounts, domain::eth},
alloy::primitives::Address,
};

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -9,7 +10,7 @@ pub struct Quote {
pub sell_amount: eth::SellTokenAmount,
pub buy_amount: eth::TokenAmount,
pub fee: eth::SellTokenAmount,
pub solver: eth::Address,
pub solver: Address,
}

impl From<&Quote> for Amounts {
Expand Down
7 changes: 4 additions & 3 deletions crates/autopilot/src/domain/settlement/trade/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use {
util::conv::U256Ext,
},
error::Math,
ethrpc::alloy::conversions::IntoLegacy,
num::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub},
std::collections::HashMap,
};
Expand Down Expand Up @@ -383,9 +384,9 @@ impl Trade {
side: self.side,
},
Quote {
sell: quote.sell_amount.into(),
buy: quote.buy_amount.into(),
fee: quote.fee.into(),
sell: quote.sell_amount.into_legacy().into(),
buy: quote.buy_amount.into_legacy().into(),
fee: quote.fee.into_legacy().into(),
},
)?;
self.surplus_over(&self.prices.custom, quote)
Expand Down
9 changes: 5 additions & 4 deletions crates/autopilot/src/infra/persistence/dto/fee_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
crate::{boundary, domain},
anyhow::Context,
database::fee_policies::{FeePolicy, FeePolicyKind},
ethrpc::alloy::conversions::IntoAlloy,
};

pub fn from_domain(
Expand Down Expand Up @@ -83,10 +84,10 @@ pub fn try_into_domain(
quote: {
let quote = quote.ok_or(Error::MissingQuote)?;
domain::fee::Quote {
sell_amount: quote.sell_amount.into(),
buy_amount: quote.buy_amount.into(),
fee: quote.fee.into(),
solver: quote.solver.into(),
sell_amount: quote.sell_amount.0.into_alloy(),
buy_amount: quote.buy_amount.0.into_alloy(),
fee: quote.fee.0.into_alloy(),
solver: quote.solver,
}
},
},
Expand Down
25 changes: 13 additions & 12 deletions crates/autopilot/src/infra/persistence/dto/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
boundary::{self},
domain::{self, OrderUid, eth, fee::FeeFactor},
},
alloy::primitives::Address,
app_data::AppDataHash,
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
number::serialization::HexOrDecimalU256,
Expand Down Expand Up @@ -336,31 +337,31 @@ impl FeePolicy {
#[serde(rename_all = "camelCase")]
pub struct Quote {
#[serde_as(as = "HexOrDecimalU256")]
pub sell_amount: U256,
pub sell_amount: alloy::primitives::U256,
#[serde_as(as = "HexOrDecimalU256")]
pub buy_amount: U256,
pub buy_amount: alloy::primitives::U256,
#[serde_as(as = "HexOrDecimalU256")]
pub fee: U256,
pub solver: H160,
pub fee: alloy::primitives::U256,
pub solver: Address,
}

impl Quote {
fn from_domain(quote: domain::Quote) -> Self {
Quote {
sell_amount: quote.sell_amount.0,
buy_amount: quote.buy_amount.0,
fee: quote.fee.0,
solver: quote.solver.0,
sell_amount: quote.sell_amount.0.into_alloy(),
buy_amount: quote.buy_amount.0.into_alloy(),
fee: quote.fee.0.into_alloy(),
solver: quote.solver,
}
}

pub fn to_domain(&self, order_uid: OrderUid) -> domain::Quote {
domain::Quote {
order_uid,
sell_amount: self.sell_amount.into(),
buy_amount: self.buy_amount.into(),
fee: self.fee.into(),
solver: self.solver.into(),
sell_amount: self.sell_amount.into_legacy().into(),
buy_amount: self.buy_amount.into_legacy().into(),
fee: self.fee.into_legacy().into(),
solver: self.solver,
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/autopilot/src/infra/persistence/dto/quote.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use {
crate::{
boundary,
domain::{self, eth},
domain::{self},
},
alloy::primitives::Address,
bigdecimal::{
FromPrimitive,
num_traits::{CheckedDiv, CheckedMul},
Expand Down Expand Up @@ -33,7 +34,7 @@ pub fn into_domain(quote: boundary::database::orders::Quote) -> Result<domain::Q
.ok_or(QuoteError::U256Overflow)?
.into(),
fee: fee.into(),
solver: eth::H160::from(quote.solver.0).into(),
solver: Address::new(quote.solver.0),
})
}

Expand Down
24 changes: 12 additions & 12 deletions crates/autopilot/src/solvable_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl SolvableOrdersCache {
.timed_future(
"weth_price_fetch",
self.native_price_estimator
.estimate_native_price(self.weth, Default::default()),
.estimate_native_price(self.weth.into_alloy(), Default::default()),
)
.await
.expect("weth price fetching can never fail");
Expand Down Expand Up @@ -924,17 +924,17 @@ mod tests {
let mut native_price_estimator = MockNativePriceEstimating::new();
native_price_estimator
.expect_estimate_native_price()
.withf(move |token, _| *token == token1)
.withf(move |token, _| *token == token1.into_alloy())
.returning(|_, _| async { Ok(2.) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token2)
.withf(move |token, _| *token == token2.into_alloy())
.returning(|_, _| async { Err(PriceEstimationError::NoLiquidity) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token3)
.withf(move |token, _| *token == token3.into_alloy())
.returning(|_, _| async { Ok(0.25) }.boxed());

let native_price_estimator = CachingNativePriceEstimator::new(
Expand Down Expand Up @@ -1005,27 +1005,27 @@ mod tests {
let mut native_price_estimator = MockNativePriceEstimating::new();
native_price_estimator
.expect_estimate_native_price()
.withf(move |token, _| *token == token1)
.withf(move |token, _| *token == token1.into_alloy())
.returning(|_, _| async { Ok(2.) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token2)
.withf(move |token, _| *token == token2.into_alloy())
.returning(|_, _| async { Err(PriceEstimationError::NoLiquidity) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token3)
.withf(move |token, _| *token == token3.into_alloy())
.returning(|_, _| async { Ok(0.25) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token4)
.withf(move |token, _| *token == token4.into_alloy())
.returning(|_, _| async { Ok(0.) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token5)
.withf(move |token, _| *token == token5.into_alloy())
.returning(|_, _| async { Ok(5.) }.boxed());

let native_price_estimator = CachingNativePriceEstimator::new(
Expand Down Expand Up @@ -1112,17 +1112,17 @@ mod tests {
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token3)
.withf(move |token, _| *token == token3.into_alloy())
.returning(|_, _| async { Ok(3.) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token_approx1)
.withf(move |token, _| *token == token_approx1.into_alloy())
.returning(|_, _| async { Ok(40.) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.times(1)
.withf(move |token, _| *token == token_approx2)
.withf(move |token, _| *token == token_approx2.into_alloy())
.returning(|_, _| async { Ok(50.) }.boxed());

let native_price_estimator = CachingNativePriceEstimator::new(
Expand Down
15 changes: 6 additions & 9 deletions crates/orderbook/src/api/get_native_price.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use {
crate::api::{ApiReply, IntoWarpReply},
alloy::primitives::Address,
anyhow::Result,
ethcontract::H160,
model::quote::NativeTokenPrice,
shared::price_estimation::native::NativePriceEstimating,
std::{convert::Infallible, sync::Arc, time::Duration},
warp::{Filter, Rejection, hyper::StatusCode, reply::with_status},
};

fn get_native_prices_request() -> impl Filter<Extract = (H160,), Error = Rejection> + Clone {
warp::path!("v1" / "token" / H160 / "native_price").and(warp::get())
fn get_native_prices_request() -> impl Filter<Extract = (Address,), Error = Rejection> + Clone {
warp::path!("v1" / "token" / Address / "native_price").and(warp::get())
}

pub fn get_native_price(
estimator: Arc<dyn NativePriceEstimating>,
quote_timeout: Duration,
) -> impl Filter<Extract = (ApiReply,), Error = Rejection> + Clone {
get_native_prices_request().and_then(move |token: H160| {
get_native_prices_request().and_then(move |token: Address| {
let estimator = estimator.clone();
async move {
let result = estimator.estimate_native_price(token, quote_timeout).await;
Expand All @@ -34,7 +34,7 @@ pub fn get_native_price(

#[cfg(test)]
mod tests {
use {super::*, futures::FutureExt, hex_literal::hex, warp::test::request};
use {super::*, alloy::primitives::address, futures::FutureExt, warp::test::request};

#[test]
fn native_prices_query() {
Expand All @@ -45,9 +45,6 @@ mod tests {
.now_or_never()
.unwrap()
.unwrap();
assert_eq!(
result,
H160(hex!("dac17f958d2ee523a2206206994597c13d831ec7"))
);
assert_eq!(result, address!("dac17f958d2ee523a2206206994597c13d831ec7"));
}
}
10 changes: 5 additions & 5 deletions crates/orderbook/src/api/get_orders_by_tx.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use {
crate::{api::ApiReply, orderbook::Orderbook},
alloy::primitives::B256,
anyhow::Result,
ethcontract::H256,
reqwest::StatusCode,
std::{convert::Infallible, sync::Arc},
warp::{Filter, Rejection, reply::with_status},
};

pub fn get_orders_by_tx_request() -> impl Filter<Extract = (H256,), Error = Rejection> + Clone {
warp::path!("v1" / "transactions" / H256 / "orders").and(warp::get())
pub fn get_orders_by_tx_request() -> impl Filter<Extract = (B256,), Error = Rejection> + Clone {
warp::path!("v1" / "transactions" / B256 / "orders").and(warp::get())
}

pub fn get_orders_by_tx(
orderbook: Arc<Orderbook>,
) -> impl Filter<Extract = (ApiReply,), Error = Rejection> + Clone {
get_orders_by_tx_request().and_then(move |hash: H256| {
get_orders_by_tx_request().and_then(move |hash: B256| {
let orderbook = orderbook.clone();
async move {
let result = orderbook.get_orders_for_tx(&hash).await;
Expand Down Expand Up @@ -42,6 +42,6 @@ mod tests {
.filter(&get_orders_by_tx_request())
.await
.unwrap();
assert_eq!(result.0, H256::from_str(hash_str).unwrap().0);
assert_eq!(result.0, B256::from_str(hash_str).unwrap().0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alloy's from_str calls from_hex

Making use of hex::decode_to_array

Creates an instance of type Self from the given hex string, or fails with a custom error type.

Both, upper and lower case characters are valid and can even be mixed (e.g. f9b4ca, F9B4CA and f9B4Ca are all valid strings).

H256 from_str:

/// Creates a hash type instance from the given string.
///
/// # Note
///
/// The given input string is interpreted in big endian.
///
/// # Errors
///
/// - When encountering invalid non hex-digits
/// - Upon empty string input or invalid input length in general
fn from_str(input: &str) -> $crate::core_::result::Result<$name, $crate::rustc_hex::FromHexError> {
	let input = input.strip_prefix("0x").unwrap_or(input);
	let mut iter = $crate::rustc_hex::FromHexIter::new(input);
	let mut result = Self::zero();
	for byte in result.as_mut() {
		*byte = iter.next().ok_or(Self::Err::InvalidHexLength)??;
	}
	if iter.next().is_some() {
		return Err(Self::Err::InvalidHexLength)
	}
	Ok(result)
}

Both methods strip the prefix and seem to work the same.

}
}
Loading
Loading