Skip to content

Commit 7cc9bb0

Browse files
committed
Migrate trade_finding::Quote to alloy
1 parent 29ad31b commit 7cc9bb0

File tree

20 files changed

+237
-210
lines changed

20 files changed

+237
-210
lines changed

crates/driver/src/boundary/liquidity/balancer/v2/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use {
1717
BalancerV2WeightedPoolFactoryV3,
1818
},
1919
ethrpc::{
20-
alloy::conversions::IntoAlloy,
20+
alloy::conversions::{IntoAlloy, IntoLegacy},
2121
block_stream::{BlockRetrieving, CurrentBlockWatcher},
2222
},
2323
shared::{
@@ -68,9 +68,9 @@ fn to_interaction(
6868
let (target, value, call_data) = interaction.encode_swap();
6969

7070
eth::Interaction {
71-
target: target.into(),
72-
value: value.into(),
73-
call_data: call_data.0.into(),
71+
target: target.into_legacy().into(),
72+
value: value.into_legacy().into(),
73+
call_data: call_data.0.to_vec().into(),
7474
}
7575
}
7676

crates/driver/src/boundary/liquidity/uniswap/v2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ pub fn to_interaction(
111111
let (target, value, call_data) = interaction.encode_swap();
112112

113113
eth::Interaction {
114-
target: target.into(),
115-
value: value.into(),
116-
call_data: call_data.0.into(),
114+
target: target.into_legacy().into(),
115+
value: value.into_legacy().into(),
116+
call_data: call_data.0.to_vec().into(),
117117
}
118118
}
119119

crates/driver/src/boundary/liquidity/uniswap/v3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ pub fn to_interaction(
9494

9595
let encoded = interaction.encode();
9696
eth::Interaction {
97-
target: eth::Address(encoded.0),
98-
value: eth::Ether(encoded.1),
99-
call_data: crate::util::Bytes(encoded.2.0),
97+
target: eth::Address(encoded.0.into_legacy()),
98+
value: eth::Ether(encoded.1.into_legacy()),
99+
call_data: crate::util::Bytes(encoded.2.0.to_vec()),
100100
}
101101
}
102102

crates/e2e/tests/e2e/quote_verification.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
::alloy::primitives::address,
2+
::alloy::primitives::{Address, U256, address},
33
bigdecimal::{BigDecimal, Zero},
44
e2e::setup::{eth, *},
55
ethcontract::H160,
@@ -184,16 +184,15 @@ async fn test_bypass_verification_for_rfq_quotes(web3: Web3) {
184184
buy_token_destination: BuyTokenDestination::Erc20,
185185
},
186186
TradeKind::Legacy(LegacyTrade {
187-
out_amount: 16380122291179526144u128.into(),
187+
out_amount: U256::from(16380122291179526144u128),
188188
gas_estimate: Some(225000),
189189
interactions: vec![Interaction {
190-
target: H160::from_str("0xdef1c0ded9bec7f1a1670819833240f027b25eff")
191-
.unwrap(),
190+
target: address!("0xdef1c0ded9bec7f1a1670819833240f027b25eff"),
192191
data: const_hex::decode("aa77476c000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599000000000000000000000000000000000000000000000000e357b42c3a9d8ccf0000000000000000000000000000000000000000000000000000000004d0e79e000000000000000000000000a69babef1ca67a37ffaf7a485dfff3382056e78c0000000000000000000000009008d19f58aabd9ed0d60971565aa8510560ab41000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000066360af101ffffffffffffffffffffffffffffffffffffff0f3f47f166360a8d0000003f0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001c66b3383f287dd9c85ad90e7c5a576ea4ba1bdf5a001d794a9afa379e6b2517b47e487a1aef32e75af432cbdbd301ada42754eaeac21ec4ca744afd92732f47540000000000000000000000000000000000000000000000000000000004d0c80f").unwrap(),
193-
value: 0.into(),
192+
value: U256::ZERO,
194193
}],
195-
solver: H160::from_str("0xe3067c7c27c1038de4e8ad95a83b927d23dfbd99")
196-
.unwrap(),
194+
solver: address!("0xe3067c7c27c1038de4e8ad95a83b927d23dfbd99")
195+
,
197196
tx_origin,
198197
}),
199198
)
@@ -220,7 +219,7 @@ async fn test_bypass_verification_for_rfq_quotes(web3: Web3) {
220219
// `tx_origin: 0x0000` is currently used to bypass quote verification due to an
221220
// implementation detail of zeroex RFQ orders.
222221
// TODO: remove with #2693
223-
let verification = verify_trade(Some(H160::zero())).await;
222+
let verification = verify_trade(Some(Address::ZERO)).await;
224223
assert_eq!(&verification.unwrap(), &verified_quote);
225224

226225
// Trades using any other `tx_origin` can not bypass the verification.

crates/number/src/conversions.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ pub mod alloy {
8686
ensure!(!ratio.denom().is_zero(), "zero denominator");
8787
big_int_to_u256(&(ratio.numer() / ratio.denom()))
8888
}
89+
90+
pub fn u256_to_big_uint(input: &U256) -> BigUint {
91+
BigUint::from_bytes_be(&input.to_be_bytes::<32>())
92+
}
93+
94+
pub fn u256_to_big_int(input: &U256) -> BigInt {
95+
BigInt::from_biguint(Sign::Plus, u256_to_big_uint(input))
96+
}
97+
98+
pub fn u256_to_big_rational(input: &U256) -> BigRational {
99+
BigRational::new(u256_to_big_int(input), 1.into())
100+
}
89101
}
90102

91103
pub fn rational_to_big_decimal<T>(value: &Ratio<T>) -> BigDecimal

crates/shared/src/conversions.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,23 @@ impl U256Ext for U256 {
5555
.expect("ceiling division arithmetic error")
5656
}
5757
}
58+
59+
impl U256Ext for alloy::primitives::U256 {
60+
fn to_big_int(&self) -> BigInt {
61+
number::conversions::alloy::u256_to_big_int(self)
62+
}
63+
64+
fn to_big_rational(&self) -> BigRational {
65+
number::conversions::alloy::u256_to_big_rational(self)
66+
}
67+
68+
fn checked_ceil_div(&self, other: &Self) -> Option<Self> {
69+
self.checked_add(other.checked_sub(alloy::primitives::U256::ONE)?)?
70+
.checked_div(*other)
71+
}
72+
73+
fn ceil_div(&self, other: &Self) -> Self {
74+
self.checked_ceil_div(other)
75+
.expect("ceiling division arithmetic error")
76+
}
77+
}

crates/shared/src/interaction.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use {
2-
ethcontract::Bytes,
3-
ethrpc::alloy::conversions::IntoLegacy,
2+
alloy::primitives::{Address, Bytes, U256},
43
model::interaction::InteractionData,
5-
primitive_types::{H160, U256},
64
};
75

86
pub trait Interaction: std::fmt::Debug + Send + Sync {
@@ -20,9 +18,9 @@ impl Interaction for Box<dyn Interaction> {
2018
}
2119

2220
pub type EncodedInteraction = (
23-
H160, // target
24-
U256, // value
25-
Bytes<Vec<u8>>, // callData
21+
Address, // target
22+
U256, // value
23+
Bytes, // callData
2624
);
2725

2826
impl Interaction for EncodedInteraction {
@@ -34,9 +32,9 @@ impl Interaction for EncodedInteraction {
3432
impl Interaction for InteractionData {
3533
fn encode(&self) -> EncodedInteraction {
3634
(
37-
self.target.into_legacy(),
38-
self.value.into_legacy(),
39-
Bytes(self.call_data.clone()),
35+
self.target,
36+
self.value,
37+
Bytes::copy_from_slice(self.call_data.as_slice()),
4038
)
4139
}
4240
}

crates/shared/src/order_quoting.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use {
1717
chrono::{DateTime, Duration, Utc},
1818
database::quotes::{Quote as QuoteRow, QuoteKind},
1919
ethcontract::{H160, U256},
20-
ethrpc::alloy::conversions::IntoAlloy,
2120
futures::TryFutureExt,
2221
gas_estimation::GasPriceEstimating,
2322
model::{
@@ -568,8 +567,8 @@ impl OrderQuoter {
568567
.pre_interactions
569568
.iter()
570569
.map(|i| InteractionData {
571-
target: i.target.into_alloy(),
572-
value: i.value.into_alloy(),
570+
target: i.target,
571+
value: i.value,
573572
call_data: i.data.clone(),
574573
})
575574
.collect(),

crates/shared/src/price_estimation/trade_finder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use {
1313
},
1414
crate::trade_finding::{TradeError, TradeFinding},
1515
anyhow::{Result, anyhow},
16+
ethrpc::alloy::conversions::IntoLegacy,
1617
futures::future::FutureExt,
1718
rate_limit::RateLimiter,
1819
std::sync::Arc,
@@ -85,9 +86,9 @@ impl Inner {
8586

8687
let quote = self.finder.get_quote(&query).await?;
8788
Ok(Estimate {
88-
out_amount: quote.out_amount,
89+
out_amount: quote.out_amount.into_legacy(),
8990
gas: quote.gas_estimate,
90-
solver: quote.solver,
91+
solver: quote.solver.into_legacy(),
9192
verified: false,
9293
execution: quote.execution,
9394
})

0 commit comments

Comments
 (0)