Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions crates/autopilot/src/domain/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use {
boundary::{self},
domain::{self, eth},
},
alloy::primitives::Address,
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 @@ -183,9 +184,9 @@ impl ProtocolFees {
factor,
max_volume_factor,
quote: Quote {
sell_amount: quote.sell_amount.into(),
buy_amount: quote.buy_amount.into(),
fee: quote.fee.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 @@ -208,7 +209,7 @@ 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: Address::ZERO,
});
Expand Down Expand Up @@ -340,9 +341,9 @@ pub struct Quote {
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(),
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
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
7 changes: 4 additions & 3 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,9 +84,9 @@ 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(),
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
12 changes: 6 additions & 6 deletions crates/autopilot/src/infra/persistence/dto/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ impl FeePolicy {
factor: factor.into(),
max_volume_factor: max_volume_factor.into(),
quote: Quote {
sell_amount: quote.sell_amount.into_alloy(),
buy_amount: quote.buy_amount.into_alloy(),
fee: quote.fee.into_alloy(),
sell_amount: quote.sell_amount,
buy_amount: quote.buy_amount,
fee: quote.fee,
solver: quote.solver,
},
},
Expand All @@ -319,9 +319,9 @@ impl FeePolicy {
factor: FeeFactor::try_from(factor).unwrap(),
max_volume_factor: FeeFactor::try_from(max_volume_factor).unwrap(),
quote: domain::fee::Quote {
sell_amount: quote.sell_amount.into_legacy(),
buy_amount: quote.buy_amount.into_legacy(),
fee: quote.fee.into_legacy(),
sell_amount: quote.sell_amount,
buy_amount: quote.buy_amount,
fee: quote.fee,
solver: quote.solver,
},
},
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"));
}
}
25 changes: 13 additions & 12 deletions crates/shared/src/order_quoting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,13 @@ impl OrderQuoter {
.estimate(trade_query.clone())
.map_err(|err| (EstimatorKind::Regular, err).into()),
self.native_price_estimator
.estimate_native_price(parameters.sell_token, trade_query.timeout)
.estimate_native_price(parameters.sell_token.into_alloy(), trade_query.timeout)
.map_err(|err| (EstimatorKind::NativeSell, err).into()),
// We don't care about the native price of the buy_token for the quote but we need it
// when we build the auction. To prevent creating orders which we can't settle later on
// we make the native buy_token price a requirement here as well.
self.native_price_estimator
.estimate_native_price(parameters.buy_token, trade_query.timeout)
.estimate_native_price(parameters.buy_token.into_alloy(), trade_query.timeout)
.map_err(|err| (EstimatorKind::NativeBuy, err).into()),
)?;

Expand Down Expand Up @@ -787,6 +787,7 @@ mod tests {
alloy::primitives::Address,
chrono::Utc,
ethcontract::H160,
ethrpc::alloy::conversions::IntoLegacy,
futures::FutureExt,
gas_estimation::GasPrice1559,
mockall::{Sequence, predicate::eq},
Expand Down Expand Up @@ -886,14 +887,14 @@ mod tests {
.expect_estimate_native_price()
.withf({
let sell_token = parameters.sell_token;
move |q, _| q == &sell_token
move |q, _| q.into_legacy() == sell_token
})
.returning(|_, _| async { Ok(0.2) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.withf({
let buy_token = parameters.buy_token;
move |q, _| q == &buy_token
move |q, _| q.into_legacy() == buy_token
})
.returning(|_, _| async { Ok(0.2) }.boxed());

Expand Down Expand Up @@ -1027,14 +1028,14 @@ mod tests {
.expect_estimate_native_price()
.withf({
let sell_token = parameters.sell_token;
move |q, _| q == &sell_token
move |q, _| q.into_legacy() == sell_token
})
.returning(|_, _| async { Ok(0.2) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.withf({
let buy_token = parameters.buy_token;
move |q, _| q == &buy_token
move |q, _| q.into_legacy() == buy_token
})
.returning(|_, _| async { Ok(0.2) }.boxed());

Expand Down Expand Up @@ -1163,14 +1164,14 @@ mod tests {
.expect_estimate_native_price()
.withf({
let sell_token = parameters.sell_token;
move |q, _| q == &sell_token
move |q, _| q.into_legacy() == sell_token
})
.returning(|_, _| async { Ok(0.2) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.withf({
let buy_token = parameters.buy_token;
move |q, _| q == &buy_token
move |q, _| q.into_legacy() == buy_token
})
.returning(|_, _| async { Ok(0.2) }.boxed());

Expand Down Expand Up @@ -1284,14 +1285,14 @@ mod tests {
.expect_estimate_native_price()
.withf({
let sell_token = parameters.sell_token;
move |q, _| q == &sell_token
move |q, _| q.into_legacy() == sell_token
})
.returning(|_, _| async { Ok(1.) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.withf({
let buy_token = parameters.buy_token;
move |q, _| q == &buy_token
move |q, _| q.into_legacy() == buy_token
})
.returning(|_, _| async { Ok(1.) }.boxed());

Expand Down Expand Up @@ -1358,14 +1359,14 @@ mod tests {
.expect_estimate_native_price()
.withf({
let sell_token = parameters.sell_token;
move |q, _| q == &sell_token
move |q, _| q.into_legacy() == sell_token
})
.returning(|_, _| async { Ok(1.) }.boxed());
native_price_estimator
.expect_estimate_native_price()
.withf({
let buy_token = parameters.buy_token;
move |q, _| q == &buy_token
move |q, _| q.into_legacy() == buy_token
})
.returning(|_, _| async { Err(PriceEstimationError::NoLiquidity) }.boxed());

Expand Down
Loading
Loading