Skip to content

Commit dfa5e5f

Browse files
committed
Migrate TokenPair to alloy
1 parent cc417c0 commit dfa5e5f

File tree

22 files changed

+215
-166
lines changed

22 files changed

+215
-166
lines changed

crates/autopilot/src/solvable_orders.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ async fn find_unsupported_tokens(
691691
.map(|token| {
692692
let bad_token = bad_token.clone();
693693
async move {
694-
match bad_token.detect(token).await {
694+
match bad_token.detect(token.into_legacy()).await {
695695
Ok(quality) => (!quality.is_good()).then_some(token),
696696
Err(err) => {
697697
tracing::warn!(

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use {
44
infra::{self, blockchain::Ethereum},
55
},
66
anyhow::Result,
7-
ethrpc::{alloy::conversions::IntoLegacy, block_stream::CurrentBlockWatcher},
7+
ethrpc::{
8+
alloy::conversions::{IntoAlloy, IntoLegacy},
9+
block_stream::CurrentBlockWatcher,
10+
},
811
futures::future,
912
model::TokenPair,
1013
shared::{
@@ -140,7 +143,7 @@ impl Fetcher {
140143
.iter()
141144
.map(|pair| {
142145
let (a, b) = pair.get();
143-
TokenPair::new(a.into(), b.into()).expect("a != b")
146+
TokenPair::new(a.0.0.into_alloy(), b.0.0.into_alloy()).expect("a != b")
144147
})
145148
.collect();
146149

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ pub(in crate::boundary::liquidity) fn to_domain_pool(
7979
router: router(&pool),
8080
reserves: liquidity::uniswap::v2::Reserves::try_new(
8181
eth::Asset {
82-
token: pool.tokens.get().0.into(),
82+
token: pool.tokens.get().0.into_legacy().into(),
8383
amount: pool.reserves.0.into(),
8484
},
8585
eth::Asset {
86-
token: pool.tokens.get().1.into(),
86+
token: pool.tokens.get().1.into_legacy().into(),
8787
amount: pool.reserves.1.into(),
8888
},
8989
)

crates/model/src/lib.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod time;
1111
pub mod trade;
1212

1313
use {
14+
alloy::primitives::Address,
1415
const_hex::{FromHex, FromHexError},
1516
primitive_types::H160,
1617
std::{fmt, sync::LazyLock},
@@ -24,12 +25,12 @@ pub type AuctionId = i64;
2425

2526
/// Erc20 token pair specified by two contract addresses.
2627
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
27-
pub struct TokenPair(H160, H160);
28+
pub struct TokenPair(Address, Address);
2829

2930
impl TokenPair {
3031
/// Create a new token pair from two addresses.
3132
/// The addresses must not be the equal.
32-
pub fn new(token_a: H160, token_b: H160) -> Option<Self> {
33+
pub fn new(token_a: Address, token_b: Address) -> Option<Self> {
3334
match token_a.cmp(&token_b) {
3435
std::cmp::Ordering::Less => Some(Self(token_a, token_b)),
3536
std::cmp::Ordering::Equal => None,
@@ -38,13 +39,13 @@ impl TokenPair {
3839
}
3940

4041
/// Used to determine if `token` is among the pair.
41-
pub fn contains(&self, token: &H160) -> bool {
42+
pub fn contains(&self, token: &Address) -> bool {
4243
self.0 == *token || self.1 == *token
4344
}
4445

4546
/// Returns the token in the pair which is not the one passed in, or None if
4647
/// token passed in is not part of the pair
47-
pub fn other(&self, token: &H160) -> Option<H160> {
48+
pub fn other(&self, token: &Address) -> Option<Address> {
4849
if &self.0 == token {
4950
Some(self.1)
5051
} else if &self.1 == token {
@@ -56,37 +57,37 @@ impl TokenPair {
5657

5758
/// The first address is always the lower one.
5859
/// The addresses are never equal.
59-
pub fn get(&self) -> (H160, H160) {
60+
pub fn get(&self) -> (Address, Address) {
6061
(self.0, self.1)
6162
}
6263

6364
/// Lowest element according to Ord trait.
6465
pub fn first_ord() -> Self {
6566
Self(
66-
H160([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
67-
H160([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
67+
Address::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
68+
Address::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
6869
)
6970
}
7071
}
7172

7273
impl Default for TokenPair {
7374
fn default() -> Self {
74-
Self::new(H160::from_low_u64_be(0), H160::from_low_u64_be(1)).unwrap()
75+
Self::new(Address::with_last_byte(0), Address::with_last_byte(1)).unwrap()
7576
}
7677
}
7778

7879
impl IntoIterator for TokenPair {
79-
type IntoIter = std::iter::Chain<std::iter::Once<H160>, std::iter::Once<H160>>;
80-
type Item = H160;
80+
type IntoIter = std::iter::Chain<std::iter::Once<Address>, std::iter::Once<Address>>;
81+
type Item = Address;
8182

8283
fn into_iter(self) -> Self::IntoIter {
8384
std::iter::once(self.0).chain(std::iter::once(self.1))
8485
}
8586
}
8687

8788
impl<'a> IntoIterator for &'a TokenPair {
88-
type IntoIter = std::iter::Chain<std::iter::Once<&'a H160>, std::iter::Once<&'a H160>>;
89-
type Item = &'a H160;
89+
type IntoIter = std::iter::Chain<std::iter::Once<&'a Address>, std::iter::Once<&'a Address>>;
90+
type Item = &'a Address;
9091

9192
fn into_iter(self) -> Self::IntoIter {
9293
std::iter::once(&self.0).chain(std::iter::once(&self.1))
@@ -175,9 +176,9 @@ mod tests {
175176

176177
#[test]
177178
fn token_pair_contains() {
178-
let token_a = H160::from_low_u64_be(0);
179-
let token_b = H160::from_low_u64_be(1);
180-
let token_c = H160::from_low_u64_be(2);
179+
let token_a = Address::with_last_byte(0);
180+
let token_b = Address::with_last_byte(1);
181+
let token_c = Address::with_last_byte(2);
181182
let pair = TokenPair::new(token_a, token_b).unwrap();
182183

183184
assert!(pair.contains(&token_a));
@@ -187,9 +188,9 @@ mod tests {
187188

188189
#[test]
189190
fn token_pair_other() {
190-
let token_a = H160::from_low_u64_be(0);
191-
let token_b = H160::from_low_u64_be(1);
192-
let token_c = H160::from_low_u64_be(2);
191+
let token_a = Address::with_last_byte(0);
192+
let token_b = Address::with_last_byte(1);
193+
let token_c = Address::with_last_byte(2);
193194
let pair = TokenPair::new(token_a, token_b).unwrap();
194195

195196
assert_eq!(pair.other(&token_a), Some(token_b));
@@ -199,8 +200,8 @@ mod tests {
199200

200201
#[test]
201202
fn token_pair_is_sorted() {
202-
let token_a = H160::from_low_u64_be(0);
203-
let token_b = H160::from_low_u64_be(1);
203+
let token_a = Address::with_last_byte(0);
204+
let token_b = Address::with_last_byte(1);
204205
let pair_0 = TokenPair::new(token_a, token_b).unwrap();
205206
let pair_1 = TokenPair::new(token_b, token_a).unwrap();
206207
assert_eq!(pair_0, pair_1);
@@ -210,14 +211,14 @@ mod tests {
210211

211212
#[test]
212213
fn token_pair_cannot_be_equal() {
213-
let token = H160::from_low_u64_be(1);
214+
let token = Address::with_last_byte(1);
214215
assert_eq!(TokenPair::new(token, token), None);
215216
}
216217

217218
#[test]
218219
fn token_pair_iterator() {
219-
let token_a = H160::from_low_u64_be(0);
220-
let token_b = H160::from_low_u64_be(1);
220+
let token_a = Address::with_last_byte(0);
221+
let token_b = Address::with_last_byte(1);
221222
let pair = TokenPair::new(token_a, token_b).unwrap();
222223

223224
let mut iter = (&pair).into_iter();
@@ -233,9 +234,9 @@ mod tests {
233234

234235
#[test]
235236
fn token_pair_ordering() {
236-
let token_a = H160::from_low_u64_be(0);
237-
let token_b = H160::from_low_u64_be(1);
238-
let token_c = H160::from_low_u64_be(2);
237+
let token_a = Address::with_last_byte(0);
238+
let token_b = Address::with_last_byte(1);
239+
let token_c = Address::with_last_byte(2);
239240
let pair_ab = TokenPair::new(token_a, token_b).unwrap();
240241
let pair_bc = TokenPair::new(token_b, token_c).unwrap();
241242
let pair_ca = TokenPair::new(token_c, token_a).unwrap();

crates/model/src/order.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use {
99
quote::QuoteId,
1010
signature::{self, EcdsaSignature, EcdsaSigningScheme, Signature},
1111
},
12+
alloy::primitives::Address,
1213
anyhow::{Result, anyhow},
1314
app_data::{AppDataHash, hash_full_app_data},
1415
bigdecimal::BigDecimal,
@@ -257,7 +258,10 @@ impl OrderData {
257258
}
258259

259260
pub fn token_pair(&self) -> Option<TokenPair> {
260-
TokenPair::new(self.buy_token, self.sell_token)
261+
TokenPair::new(
262+
Address::from_slice(&self.buy_token.0),
263+
Address::from_slice(&self.sell_token.0),
264+
)
261265
}
262266

263267
pub fn uid(&self, domain: &DomainSeparator, owner: &H160) -> OrderUid {

crates/shared/src/bad_token/token_owner_finder/liquidity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
anyhow::Result,
88
contracts::alloy::{BalancerV2Vault, IUniswapV3Factory},
99
ethcontract::H160,
10-
ethrpc::alloy::conversions::IntoLegacy,
10+
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
1111
model::TokenPair,
1212
};
1313

@@ -22,7 +22,7 @@ impl TokenOwnerProposing for UniswapLikePairProviderFinder {
2222
Ok(self
2323
.base_tokens
2424
.iter()
25-
.filter_map(|&base_token| TokenPair::new(base_token, token))
25+
.filter_map(|base_token| TokenPair::new(base_token.into_alloy(), token.into_alloy()))
2626
.map(|pair| self.inner.pair_address(&pair))
2727
.collect())
2828
}
@@ -105,7 +105,7 @@ impl TokenOwnerProposing for UniswapV3Finder {
105105
Ok(self
106106
.base_tokens
107107
.iter()
108-
.filter_map(|base_token| TokenPair::new(*base_token, token))
108+
.filter_map(|base_token| TokenPair::new(base_token.into_alloy(), token.into_alloy()))
109109
.flat_map(|pair| self.fee_values.iter().map(move |fee| (pair, *fee)))
110110
.map(|(pair, fee)| {
111111
uniswap_v3_pair_provider::pair_address(

0 commit comments

Comments
 (0)