Skip to content

Commit 2db6325

Browse files
committed
Migrate BadTokenDetecting to alloy
1 parent 7cc9bb0 commit 2db6325

File tree

12 files changed

+134
-135
lines changed

12 files changed

+134
-135
lines changed

crates/autopilot/src/arguments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ pub struct Arguments {
8181
/// bad token detector thinks they are bad. Base tokens are
8282
/// automatically allowed.
8383
#[clap(long, env, use_value_delimiter = true)]
84-
pub allowed_tokens: Vec<H160>,
84+
pub allowed_tokens: Vec<Address>,
8585

8686
/// List of token addresses to be ignored throughout service
8787
#[clap(long, env, use_value_delimiter = true)]
88-
pub unsupported_tokens: Vec<H160>,
88+
pub unsupported_tokens: Vec<Address>,
8989

9090
/// Which estimators to use to estimate token prices in terms of the chain's
9191
/// native token. Estimators with the same name need to also be specified as

crates/autopilot/src/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) {
326326
&args.shared.base_tokens,
327327
));
328328
let mut allowed_tokens = args.allowed_tokens.clone();
329-
allowed_tokens.extend(base_tokens.tokens().iter().copied());
330-
allowed_tokens.push(model::order::BUY_ETH_ADDRESS);
329+
allowed_tokens.extend(base_tokens.tokens().iter().map(|t| t.into_alloy()));
330+
allowed_tokens.push(model::order::BUY_ETH_ADDRESS.into_alloy());
331331
let unsupported_tokens = args.unsupported_tokens.clone();
332332

333333
let finder = token_owner_finder::init(

crates/autopilot/src/solvable_orders.rs

Lines changed: 2 additions & 2 deletions
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.into_legacy()).await {
694+
match bad_token.detect(token).await {
695695
Ok(quality) => (!quality.is_good()).then_some(token),
696696
Err(err) => {
697697
tracing::warn!(
@@ -1313,7 +1313,7 @@ mod tests {
13131313
let token0 = H160::from_low_u64_le(0);
13141314
let token1 = H160::from_low_u64_le(1);
13151315
let token2 = H160::from_low_u64_le(2);
1316-
let bad_token = Arc::new(ListBasedDetector::deny_list(vec![token0]));
1316+
let bad_token = Arc::new(ListBasedDetector::deny_list(vec![token0.into_alloy()]));
13171317
let orders = vec![
13181318
OrderBuilder::default()
13191319
.with_sell_token(token0)

crates/orderbook/src/arguments.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use {
22
alloy::primitives::Address,
3-
primitive_types::H160,
43
reqwest::Url,
54
shared::{
65
arguments::{display_option, display_secret_option},
@@ -78,7 +77,7 @@ pub struct Arguments {
7877

7978
/// List of token addresses to be ignored throughout service
8079
#[clap(long, env, use_value_delimiter = true)]
81-
pub unsupported_tokens: Vec<H160>,
80+
pub unsupported_tokens: Vec<Address>,
8281

8382
/// List of account addresses to be denied from order creation
8483
#[clap(long, env, use_value_delimiter = true)]
@@ -106,7 +105,7 @@ pub struct Arguments {
106105
/// bad token detector thinks they are bad. Base tokens are
107106
/// automatically allowed.
108107
#[clap(long, env, use_value_delimiter = true)]
109-
pub allowed_tokens: Vec<H160>,
108+
pub allowed_tokens: Vec<Address>,
110109

111110
/// Skip EIP-1271 order signature validation on creation.
112111
#[clap(long, env, action = clap::ArgAction::Set, default_value = "false")]

crates/orderbook/src/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ pub async fn run(args: Arguments) {
232232
&args.shared.base_tokens,
233233
));
234234
let mut allowed_tokens = args.allowed_tokens.clone();
235-
allowed_tokens.extend(base_tokens.tokens().iter().copied());
236-
allowed_tokens.push(BUY_ETH_ADDRESS);
235+
allowed_tokens.extend(base_tokens.tokens().iter().map(|t| t.into_alloy()));
236+
allowed_tokens.push(BUY_ETH_ADDRESS.into_alloy());
237237
let unsupported_tokens = args.unsupported_tokens.clone();
238238

239239
let uniswapv3_factory = IUniswapV3Factory::Instance::deployed(&web3.alloy)

crates/shared/src/bad_token/cache.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use {
22
super::{BadTokenDetecting, TokenQuality},
3+
alloy::primitives::Address,
34
anyhow::Result,
45
dashmap::DashMap,
56
futures::future::join_all,
6-
primitive_types::H160,
77
std::{
88
ops::Div,
99
sync::Arc,
@@ -14,15 +14,15 @@ use {
1414

1515
pub struct CachingDetector {
1616
inner: Box<dyn BadTokenDetecting>,
17-
cache: DashMap<H160, (Instant, TokenQuality)>,
17+
cache: DashMap<Address, (Instant, TokenQuality)>,
1818
cache_expiry: Duration,
1919
prefetch_time: Duration,
2020
}
2121

2222
#[async_trait::async_trait]
2323
impl BadTokenDetecting for CachingDetector {
2424
#[instrument(skip_all)]
25-
async fn detect(&self, token: H160) -> Result<TokenQuality> {
25+
async fn detect(&self, token: Address) -> Result<TokenQuality> {
2626
if let Some(quality) = self.get_from_cache(&token, Instant::now()) {
2727
return Ok(quality);
2828
}
@@ -53,13 +53,13 @@ impl CachingDetector {
5353
detector
5454
}
5555

56-
fn get_from_cache(&self, token: &H160, now: Instant) -> Option<TokenQuality> {
56+
fn get_from_cache(&self, token: &Address, now: Instant) -> Option<TokenQuality> {
5757
let (instant, quality) = self.cache.get(token)?.value().clone();
5858
let still_valid = now.saturating_duration_since(instant) < self.cache_expiry;
5959
still_valid.then_some(quality)
6060
}
6161

62-
fn insert_many_into_cache(&self, tokens: impl Iterator<Item = (H160, TokenQuality)>) {
62+
fn insert_many_into_cache(&self, tokens: impl Iterator<Item = (Address, TokenQuality)>) {
6363
let now = Instant::now();
6464
tokens.into_iter().for_each(|(token, quality)| {
6565
self.cache.insert(token, (now, quality));
@@ -134,7 +134,7 @@ mod tests {
134134

135135
for _ in 0..2 {
136136
let result = detector
137-
.detect(H160::from_low_u64_le(0))
137+
.detect(Address::with_last_byte(0))
138138
.now_or_never()
139139
.unwrap();
140140
assert!(result.unwrap().is_good());
@@ -144,7 +144,7 @@ mod tests {
144144
#[tokio::test]
145145
async fn cache_expires() {
146146
let inner = MockBadTokenDetecting::new();
147-
let token = H160::from_low_u64_le(0);
147+
let token = Address::with_last_byte(0);
148148
let detector = CachingDetector::new(
149149
Box::new(inner),
150150
Duration::from_secs(2),
@@ -193,22 +193,22 @@ mod tests {
193193
);
194194

195195
let result = detector
196-
.detect(H160::from_low_u64_le(0))
196+
.detect(Address::with_last_byte(0))
197197
.now_or_never()
198198
.unwrap();
199199
assert!(result.unwrap().is_good());
200200
// Check that the result is the same because we haven't reached the prefetch
201201
// time yet
202202
tokio::time::sleep(Duration::from_millis(100)).await;
203203
let result = detector
204-
.detect(H160::from_low_u64_le(0))
204+
.detect(Address::with_last_byte(0))
205205
.now_or_never()
206206
.unwrap();
207207
assert!(result.unwrap().is_good());
208208
// We wait so the prefetch fetches the data
209209
tokio::time::sleep(Duration::from_millis(70)).await;
210210
let result = detector
211-
.detect(H160::from_low_u64_le(0))
211+
.detect(Address::with_last_byte(0))
212212
.now_or_never()
213213
.unwrap();
214214
assert!(!result.unwrap().is_good());

crates/shared/src/bad_token/instrumented.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use {
22
super::{BadTokenDetecting, TokenQuality},
3+
alloy::primitives::Address,
34
anyhow::Result,
45
prometheus::IntCounterVec,
56
prometheus_metric_storage::MetricStorage,
@@ -33,7 +34,7 @@ pub struct InstrumentedBadTokenDetector {
3334

3435
#[async_trait::async_trait]
3536
impl BadTokenDetecting for InstrumentedBadTokenDetector {
36-
async fn detect(&self, token: ethcontract::H160) -> Result<TokenQuality> {
37+
async fn detect(&self, token: Address) -> Result<TokenQuality> {
3738
let result = self
3839
.inner
3940
.detect(token)

crates/shared/src/bad_token/list_based.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use {
22
super::{BadTokenDetecting, TokenQuality},
3+
alloy::primitives::Address,
34
anyhow::Result,
4-
primitive_types::H160,
55
std::sync::Arc,
66
tracing::instrument,
77
};
@@ -15,16 +15,16 @@ pub enum UnknownTokenStrategy {
1515

1616
/// Classify tokens with explicit allow and deny lists.
1717
pub struct ListBasedDetector {
18-
allow_list: Vec<H160>,
19-
deny_list: Vec<H160>,
18+
allow_list: Vec<Address>,
19+
deny_list: Vec<Address>,
2020
strategy: UnknownTokenStrategy,
2121
}
2222

2323
impl ListBasedDetector {
2424
/// Panics if same token is both allowed and denied.
2525
pub fn new(
26-
allow_list: Vec<H160>,
27-
deny_list: Vec<H160>,
26+
allow_list: Vec<Address>,
27+
deny_list: Vec<Address>,
2828
strategy: UnknownTokenStrategy,
2929
) -> Self {
3030
assert!(
@@ -38,7 +38,7 @@ impl ListBasedDetector {
3838
}
3939
}
4040

41-
pub fn deny_list(list: Vec<H160>) -> Self {
41+
pub fn deny_list(list: Vec<Address>) -> Self {
4242
Self {
4343
allow_list: Vec::new(),
4444
deny_list: list,
@@ -50,7 +50,7 @@ impl ListBasedDetector {
5050
#[async_trait::async_trait]
5151
impl BadTokenDetecting for ListBasedDetector {
5252
#[instrument(skip_all)]
53-
async fn detect(&self, token: ethcontract::H160) -> Result<TokenQuality> {
53+
async fn detect(&self, token: Address) -> Result<TokenQuality> {
5454
if self.allow_list.contains(&token) {
5555
return Ok(TokenQuality::Good);
5656
}
@@ -80,19 +80,19 @@ mod tests {
8080
// Would panic if used.
8181
let inner = MockBadTokenDetecting::new();
8282
let detector = ListBasedDetector {
83-
allow_list: vec![H160::from_low_u64_le(0)],
84-
deny_list: vec![H160::from_low_u64_le(1)],
83+
allow_list: vec![Address::with_last_byte(0)],
84+
deny_list: vec![Address::with_last_byte(1)],
8585
strategy: UnknownTokenStrategy::Forward(Arc::new(inner)),
8686
};
8787

8888
let result = detector
89-
.detect(H160::from_low_u64_le(0))
89+
.detect(Address::with_last_byte(0))
9090
.now_or_never()
9191
.unwrap();
9292
assert!(result.unwrap().is_good());
9393

9494
let result = detector
95-
.detect(H160::from_low_u64_le(1))
95+
.detect(Address::with_last_byte(1))
9696
.now_or_never()
9797
.unwrap();
9898
assert!(!result.unwrap().is_good());
@@ -106,7 +106,7 @@ mod tests {
106106
strategy: UnknownTokenStrategy::Allow,
107107
};
108108
let result = detector
109-
.detect(H160::from_low_u64_le(0))
109+
.detect(Address::with_last_byte(0))
110110
.now_or_never()
111111
.unwrap();
112112
assert!(result.unwrap().is_good());
@@ -117,7 +117,7 @@ mod tests {
117117
strategy: UnknownTokenStrategy::Deny,
118118
};
119119
let result = detector
120-
.detect(H160::from_low_u64_le(0))
120+
.detect(Address::with_last_byte(0))
121121
.now_or_never()
122122
.unwrap();
123123
assert!(!result.unwrap().is_good());
@@ -138,7 +138,7 @@ mod tests {
138138
};
139139

140140
let result = detector
141-
.detect(H160::from_low_u64_le(0))
141+
.detect(Address::with_last_byte(0))
142142
.now_or_never()
143143
.unwrap();
144144
assert!(result.unwrap().is_good());

crates/shared/src/bad_token/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod list_based;
44
pub mod token_owner_finder;
55
pub mod trace_call;
66

7-
use {anyhow::Result, primitive_types::H160};
7+
use {alloy::primitives::Address, anyhow::Result};
88

99
/// How well behaved a token is.
1010
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -29,5 +29,5 @@ impl TokenQuality {
2929
#[cfg_attr(any(test, feature = "test-util"), mockall::automock)]
3030
#[async_trait::async_trait]
3131
pub trait BadTokenDetecting: Send + Sync {
32-
async fn detect(&self, token: H160) -> Result<TokenQuality>;
32+
async fn detect(&self, token: Address) -> Result<TokenQuality>;
3333
}

0 commit comments

Comments
 (0)