Skip to content

Commit bb2bad4

Browse files
authored
Migrate price_estimation::Query to alloy (#3877)
# Description Migrate price_estimation::Query to alloy # Changes <!-- List of detailed changes (how the change is accomplished) --> - [ ] Migrate price_estimation::Query to alloy - [ ] Refactors where applicable ## How to test Tests <!-- ## Related Issues Fixes # -->
1 parent 63effd2 commit bb2bad4

File tree

12 files changed

+107
-84
lines changed

12 files changed

+107
-84
lines changed

crates/alerter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl OrderBookApi {
9292
// untouched.
9393
fn convert_eth_to_weth(token: Address) -> Address {
9494
const WETH: Address = address!("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
95-
if token == Address::from_slice(&BUY_ETH_ADDRESS.0) {
95+
if token.as_slice() == BUY_ETH_ADDRESS.as_bytes() {
9696
WETH
9797
} else {
9898
token

crates/shared/src/order_quoting.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use {
1717
chrono::{DateTime, Duration, Utc},
1818
database::quotes::{Quote as QuoteRow, QuoteKind},
1919
ethcontract::{H160, U256},
20+
ethrpc::alloy::conversions::IntoAlloy,
2021
futures::TryFutureExt,
2122
gas_estimation::GasPriceEstimating,
2223
model::{
@@ -66,8 +67,8 @@ impl QuoteParameters {
6667

6768
price_estimation::Query {
6869
verification: self.verification.clone(),
69-
sell_token: self.sell_token,
70-
buy_token: self.buy_token,
70+
sell_token: self.sell_token.into_alloy(),
71+
buy_token: self.buy_token.into_alloy(),
7172
in_amount,
7273
kind,
7374
block_dependent: true,
@@ -859,8 +860,8 @@ mod tests {
859860
from: H160([3; 20]),
860861
..Default::default()
861862
},
862-
sell_token: H160([1; 20]),
863-
buy_token: H160([2; 20]),
863+
sell_token: Address::new([1; 20]),
864+
buy_token: Address::new([2; 20]),
864865
in_amount: NonZeroU256::try_from(100).unwrap(),
865866
kind: OrderKind::Sell,
866867
block_dependent: true,
@@ -1000,8 +1001,8 @@ mod tests {
10001001
from: H160([3; 20]),
10011002
..Default::default()
10021003
},
1003-
sell_token: H160([1; 20]),
1004-
buy_token: H160([2; 20]),
1004+
sell_token: Address::new([1; 20]),
1005+
buy_token: Address::new([2; 20]),
10051006
in_amount: NonZeroU256::try_from(100).unwrap(),
10061007
kind: OrderKind::Sell,
10071008
block_dependent: true,
@@ -1136,8 +1137,8 @@ mod tests {
11361137
from: H160([3; 20]),
11371138
..Default::default()
11381139
},
1139-
sell_token: H160([1; 20]),
1140-
buy_token: H160([2; 20]),
1140+
sell_token: Address::new([1; 20]),
1141+
buy_token: Address::new([2; 20]),
11411142
in_amount: NonZeroU256::try_from(42).unwrap(),
11421143
kind: OrderKind::Buy,
11431144
block_dependent: true,

crates/shared/src/order_validation.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ impl From<CalculateQuoteError> for ValidationError {
187187
CalculateQuoteError::Price {
188188
source: PriceEstimationError::UnsupportedToken { token, reason },
189189
..
190-
} => {
191-
ValidationError::Partial(PartialValidationError::UnsupportedToken { token, reason })
192-
}
190+
} => ValidationError::Partial(PartialValidationError::UnsupportedToken {
191+
token: token.into_legacy(),
192+
reason,
193+
}),
193194
CalculateQuoteError::Price {
194195
source: PriceEstimationError::ProtocolInternal(err),
195196
..
@@ -488,7 +489,7 @@ impl OrderValidating for OrderValidator {
488489
if has_same_buy_and_sell_token(&order, self.native_token.address()) {
489490
return Err(PartialValidationError::SameBuyAndSellToken);
490491
}
491-
if order.sell_token == BUY_ETH_ADDRESS {
492+
if order.sell_token.into_alloy() == BUY_ETH_ADDRESS.into_alloy() {
492493
return Err(PartialValidationError::InvalidNativeSellToken);
493494
}
494495

@@ -857,7 +858,8 @@ pub enum OrderValidToError {
857858
/// This also checks for orders selling wrapped native token for native token.
858859
fn has_same_buy_and_sell_token(order: &PreOrderData, native_token: &Address) -> bool {
859860
order.sell_token == order.buy_token
860-
|| (order.sell_token == native_token.into_legacy() && order.buy_token == BUY_ETH_ADDRESS)
861+
|| (order.sell_token == native_token.into_legacy()
862+
&& order.buy_token.into_alloy() == BUY_ETH_ADDRESS.into_alloy())
861863
}
862864

863865
/// Retrieves the quote for an order that is being created and verify that its

crates/shared/src/price_estimation/competition/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ mod tests {
248248
PriceEstimating,
249249
Query,
250250
},
251+
alloy::primitives::Address,
251252
anyhow::anyhow,
252253
futures::channel::oneshot::channel,
253254
model::order::OrderKind,
254255
number::nonzero::U256 as NonZeroU256,
255-
primitive_types::H160,
256256
std::time::Duration,
257257
tokio::time::sleep,
258258
};
@@ -262,44 +262,44 @@ mod tests {
262262
let queries = [
263263
Arc::new(Query {
264264
verification: Default::default(),
265-
sell_token: H160::from_low_u64_le(0),
266-
buy_token: H160::from_low_u64_le(1),
265+
sell_token: Address::with_last_byte(0),
266+
buy_token: Address::with_last_byte(1),
267267
in_amount: NonZeroU256::try_from(1).unwrap(),
268268
kind: OrderKind::Buy,
269269
block_dependent: false,
270270
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
271271
}),
272272
Arc::new(Query {
273273
verification: Default::default(),
274-
sell_token: H160::from_low_u64_le(2),
275-
buy_token: H160::from_low_u64_le(3),
274+
sell_token: Address::with_last_byte(2),
275+
buy_token: Address::with_last_byte(3),
276276
in_amount: NonZeroU256::try_from(1).unwrap(),
277277
kind: OrderKind::Sell,
278278
block_dependent: false,
279279
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
280280
}),
281281
Arc::new(Query {
282282
verification: Default::default(),
283-
sell_token: H160::from_low_u64_le(2),
284-
buy_token: H160::from_low_u64_le(3),
283+
sell_token: Address::with_last_byte(2),
284+
buy_token: Address::with_last_byte(3),
285285
in_amount: NonZeroU256::try_from(1).unwrap(),
286286
kind: OrderKind::Buy,
287287
block_dependent: false,
288288
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
289289
}),
290290
Arc::new(Query {
291291
verification: Default::default(),
292-
sell_token: H160::from_low_u64_le(3),
293-
buy_token: H160::from_low_u64_le(4),
292+
sell_token: Address::with_last_byte(3),
293+
buy_token: Address::with_last_byte(4),
294294
in_amount: NonZeroU256::try_from(1).unwrap(),
295295
kind: OrderKind::Buy,
296296
block_dependent: false,
297297
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
298298
}),
299299
Arc::new(Query {
300300
verification: Default::default(),
301-
sell_token: H160::from_low_u64_le(5),
302-
buy_token: H160::from_low_u64_le(6),
301+
sell_token: Address::with_last_byte(5),
302+
buy_token: Address::with_last_byte(6),
303303
in_amount: NonZeroU256::try_from(1).unwrap(),
304304
kind: OrderKind::Buy,
305305
block_dependent: false,
@@ -344,7 +344,7 @@ mod tests {
344344
Ok(estimates[1].clone()),
345345
Err(PriceEstimationError::ProtocolInternal(anyhow!("b"))),
346346
Err(PriceEstimationError::UnsupportedToken {
347-
token: H160([0; 20]),
347+
token: Address::new([0; 20]),
348348
reason: "".to_string(),
349349
}),
350350
]);
@@ -388,8 +388,8 @@ mod tests {
388388
async fn racing_estimator_returns_early() {
389389
let query = Arc::new(Query {
390390
verification: Default::default(),
391-
sell_token: H160::from_low_u64_le(0),
392-
buy_token: H160::from_low_u64_le(1),
391+
sell_token: Address::with_last_byte(0),
392+
buy_token: Address::with_last_byte(1),
393393
in_amount: NonZeroU256::try_from(1).unwrap(),
394394
kind: OrderKind::Buy,
395395
block_dependent: false,
@@ -449,8 +449,8 @@ mod tests {
449449
async fn queries_stages_sequentially() {
450450
let query = Arc::new(Query {
451451
verification: Default::default(),
452-
sell_token: H160::from_low_u64_le(0),
453-
buy_token: H160::from_low_u64_le(1),
452+
sell_token: Address::with_last_byte(0),
453+
buy_token: Address::with_last_byte(1),
454454
in_amount: NonZeroU256::try_from(1).unwrap(),
455455
kind: OrderKind::Sell,
456456
block_dependent: false,
@@ -526,8 +526,8 @@ mod tests {
526526
async fn combines_stages_if_threshold_bigger_than_next_stage_length() {
527527
let query = Arc::new(Query {
528528
verification: Default::default(),
529-
sell_token: H160::from_low_u64_le(0),
530-
buy_token: H160::from_low_u64_le(1),
529+
sell_token: Address::with_last_byte(0),
530+
buy_token: Address::with_last_byte(1),
531531
in_amount: NonZeroU256::try_from(1).unwrap(),
532532
kind: OrderKind::Sell,
533533
block_dependent: false,

crates/shared/src/price_estimation/competition/quote.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use {
99
QuoteVerificationMode,
1010
},
1111
anyhow::Context,
12+
ethrpc::alloy::conversions::IntoLegacy,
1213
futures::future::{BoxFuture, FutureExt, TryFutureExt},
1314
model::order::OrderKind,
1415
primitive_types::{H160, U256},
@@ -26,7 +27,9 @@ impl PriceEstimating for CompetitionEstimator<Arc<dyn PriceEstimating>> {
2627
OrderKind::Buy => query.sell_token,
2728
OrderKind::Sell => query.buy_token,
2829
};
29-
let get_context = self.ranking.provide_context(out_token, query.timeout);
30+
let get_context = self
31+
.ranking
32+
.provide_context(out_token.into_legacy(), query.timeout);
3033

3134
// Filter out 0 gas cost estimate because they are obviously wrong and would
3235
// likely win the price competition which would lead to us paying huge

crates/shared/src/price_estimation/factory.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ use {
3030
anyhow::{Context as _, Result},
3131
contracts::alloy::WETH9,
3232
ethcontract::H160,
33-
ethrpc::{alloy::conversions::IntoLegacy, block_stream::CurrentBlockWatcher},
33+
ethrpc::{
34+
alloy::conversions::{IntoAlloy, IntoLegacy},
35+
block_stream::CurrentBlockWatcher,
36+
},
3437
gas_estimation::GasPriceEstimating,
3538
number::nonzero::U256 as NonZeroU256,
3639
rate_limit::RateLimiter,
@@ -299,7 +302,7 @@ impl<'a> PriceEstimatorFactory<'a> {
299302
fn sanitized(&self, estimator: Arc<dyn PriceEstimating>) -> SanitizedPriceEstimator {
300303
SanitizedPriceEstimator::new(
301304
estimator,
302-
self.network.native_token,
305+
self.network.native_token.into_alloy(),
303306
self.components.bad_token_detector.clone(),
304307
)
305308
}

crates/shared/src/price_estimation/instrumented.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ mod tests {
129129
MockPriceEstimating,
130130
PriceEstimationError,
131131
},
132+
alloy::primitives::Address,
132133
anyhow::anyhow,
133-
ethcontract::H160,
134134
model::order::OrderKind,
135135
number::nonzero::U256 as NonZeroU256,
136136
};
@@ -139,8 +139,8 @@ mod tests {
139139
async fn records_metrics_for_each_query() {
140140
let query = Arc::new(Query {
141141
verification: Default::default(),
142-
sell_token: H160([1; 20]),
143-
buy_token: H160([2; 20]),
142+
sell_token: Address::new([1; 20]),
143+
buy_token: Address::new([2; 20]),
144144
in_amount: NonZeroU256::try_from(3).unwrap(),
145145
kind: OrderKind::Sell,
146146
block_dependent: false,
@@ -152,7 +152,7 @@ mod tests {
152152
Ok(Estimate::default()),
153153
Err(PriceEstimationError::NoLiquidity),
154154
Err(PriceEstimationError::UnsupportedToken {
155-
token: H160([0; 20]),
155+
token: Address::new([0; 20]),
156156
reason: "".to_string(),
157157
}),
158158
Err(PriceEstimationError::UnsupportedOrderType("".to_string())),

crates/shared/src/price_estimation/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use {
44
arguments::{self, display_option, display_secret_option},
55
trade_finding::{Interaction, QuoteExecution},
66
},
7+
alloy::primitives::Address,
78
anyhow::{Result, ensure},
89
bigdecimal::BigDecimal,
910
ethcontract::{H160, U256},
@@ -443,7 +444,7 @@ impl Display for Arguments {
443444
#[derive(Error, Debug)]
444445
pub enum PriceEstimationError {
445446
#[error("token {token:?} is not supported: {reason:}")]
446-
UnsupportedToken { token: H160, reason: String },
447+
UnsupportedToken { token: Address, reason: String },
447448

448449
#[error("No liquidity")]
449450
NoLiquidity,
@@ -492,8 +493,8 @@ impl Clone for PriceEstimationError {
492493

493494
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, Serialize)]
494495
pub struct Query {
495-
pub sell_token: H160,
496-
pub buy_token: H160,
496+
pub sell_token: Address,
497+
pub buy_token: Address,
497498
/// For OrderKind::Sell amount is in sell_token and for OrderKind::Buy in
498499
/// buy_token.
499500
pub in_amount: NonZeroU256,

crates/shared/src/price_estimation/native/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use {
22
crate::price_estimation::{PriceEstimating, PriceEstimationError, Query},
33
bigdecimal::{BigDecimal, ToPrimitive},
4+
ethrpc::alloy::conversions::IntoAlloy,
45
futures::FutureExt,
56
model::order::OrderKind,
67
number::nonzero::U256 as NonZeroU256,
@@ -80,8 +81,8 @@ impl NativePriceEstimator {
8081
// TODO explain why we use BUY order type (shallow liquidity)
8182
fn query(&self, token: &H160, timeout: Duration) -> Query {
8283
Query {
83-
sell_token: *token,
84-
buy_token: self.native_token,
84+
sell_token: token.into_alloy(),
85+
buy_token: self.native_token.into_alloy(),
8586
in_amount: self.price_estimation_amount,
8687
kind: OrderKind::Buy,
8788
verification: Default::default(),
@@ -122,6 +123,7 @@ mod tests {
122123
use {
123124
super::*,
124125
crate::price_estimation::{Estimate, HEALTHY_PRICE_ESTIMATION_TIME, MockPriceEstimating},
126+
alloy::primitives::Address,
125127
primitive_types::H160,
126128
std::str::FromStr,
127129
};
@@ -130,8 +132,8 @@ mod tests {
130132
async fn prices_dont_get_modified() {
131133
let mut inner = MockPriceEstimating::new();
132134
inner.expect_estimate().times(1).returning(|query| {
133-
assert!(query.buy_token.to_low_u64_be() == 7);
134-
assert!(query.sell_token.to_low_u64_be() == 3);
135+
assert!(query.buy_token == Address::with_last_byte(7));
136+
assert!(query.sell_token == Address::with_last_byte(3));
135137
async {
136138
Ok(Estimate {
137139
out_amount: 123_456_789_000_000_000u128.into(),
@@ -160,8 +162,8 @@ mod tests {
160162
async fn errors_get_propagated() {
161163
let mut inner = MockPriceEstimating::new();
162164
inner.expect_estimate().times(1).returning(|query| {
163-
assert!(query.buy_token.to_low_u64_be() == 7);
164-
assert!(query.sell_token.to_low_u64_be() == 2);
165+
assert!(query.buy_token == Address::with_last_byte(7));
166+
assert!(query.sell_token == Address::with_last_byte(2));
165167
async { Err(PriceEstimationError::NoLiquidity) }.boxed()
166168
});
167169

0 commit comments

Comments
 (0)