Skip to content

Commit b9ff845

Browse files
jmg-duartem-sz
andauthored
Migrate balancer v2 sources to alloy (#4023)
# Description Migrates Balancer V2 sources module from `ethcontract` to `alloy` primitives, continuing the project-wide modernization effort. This simplifies the codebase and improves type safety across pool fetching, pool implementations, and swap math calculations. # Changes - [x] Replace `ethcontract` contract bindings with `alloy` types in pool fetching - [x] Add alloy block support to `recent_block_cache.rs` - [x] Migrate all pool types (Weighted, Stable, Composable Stable, LBP) to alloy - [x] Convert swap math modules to use alloy primitives - [x] Update pool storage, registry, and boundary liquidity handlers - [x] Remove ethcontract-specific workarounds from `sources/mod.rs` ## How to test Existing tests --------- Co-authored-by: Marcin Szymczak <[email protected]>
1 parent bae34b6 commit b9ff845

File tree

28 files changed

+201
-403
lines changed

28 files changed

+201
-403
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ use {
1616
BalancerV2WeightedPoolFactory,
1717
BalancerV2WeightedPoolFactoryV3,
1818
},
19-
ethrpc::{
20-
alloy::conversions::IntoLegacy,
21-
block_stream::{BlockRetrieving, CurrentBlockWatcher},
22-
},
19+
ethrpc::block_stream::{BlockRetrieving, CurrentBlockWatcher},
2320
shared::{
2421
http_solver::model::TokenAmount,
2522
sources::balancer_v2::{
@@ -181,12 +178,7 @@ async fn init_liquidity(
181178
boundary::liquidity::http_client(),
182179
web3.clone(),
183180
&contracts,
184-
config
185-
.pool_deny_list
186-
.iter()
187-
.copied()
188-
.map(IntoLegacy::into_legacy)
189-
.collect(),
181+
config.pool_deny_list.to_vec(),
190182
)
191183
.await
192184
.context("failed to create balancer pool fetcher")?,

crates/shared/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
99
doctest = false
1010

1111
[dependencies]
12-
alloy = { workspace = true, features = ["sol-types", "signer-local"] }
12+
alloy = { workspace = true, features = ["sol-types", "signer-local", "rand"] }
1313
anyhow = { workspace = true }
1414
app-data = { workspace = true }
1515
bytes-hex = { workspace = true }

crates/shared/src/arguments.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
use {
55
crate::{
66
gas_price_estimation::GasEstimatorType,
7-
sources::{
8-
BaselineSource,
9-
balancer_v2::BalancerFactoryKind,
10-
uniswap_v2::UniV2BaselineSourceParameters,
11-
},
7+
sources::{BaselineSource, uniswap_v2::UniV2BaselineSourceParameters},
128
tenderly_api,
139
},
1410
alloy::primitives::Address,
@@ -218,12 +214,6 @@ pub struct Arguments {
218214
#[clap(long, env, action = clap::ArgAction::Set, default_value = "false")]
219215
pub use_internal_buffers: bool,
220216

221-
/// The Balancer V2 factories to consider for indexing liquidity. Allows
222-
/// specific pool kinds to be disabled via configuration. Will use all
223-
/// supported Balancer V2 factory kinds if not specified.
224-
#[clap(long, env, value_enum, ignore_case = true, use_value_delimiter = true)]
225-
pub balancer_factories: Option<Vec<BalancerFactoryKind>>,
226-
227217
/// Value of the authorization header for the solver competition post api.
228218
#[clap(long, env)]
229219
pub solver_competition_auth: Option<String>,
@@ -396,7 +386,6 @@ impl Display for Arguments {
396386
pool_cache_maximum_retries,
397387
pool_cache_delay_between_retries,
398388
use_internal_buffers,
399-
balancer_factories,
400389
solver_competition_auth,
401390
network_block_interval,
402391
settlement_contract_address,
@@ -439,7 +428,6 @@ impl Display for Arguments {
439428
"pool_cache_delay_between_retries: {pool_cache_delay_between_retries:?}"
440429
)?;
441430
writeln!(f, "use_internal_buffers: {use_internal_buffers}")?;
442-
writeln!(f, "balancer_factories: {balancer_factories:?}")?;
443431
display_secret_option(
444432
f,
445433
"solver_competition_auth",

crates/shared/src/recent_block_cache.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
2727
use {
2828
crate::request_sharing::BoxRequestSharing,
29+
alloy::eips::BlockId,
2930
anyhow::{Context, Result},
3031
cached::{Cached, SizedCache},
3132
ethcontract::BlockNumber,
@@ -84,6 +85,16 @@ impl From<Block> for BlockNumber {
8485
}
8586
}
8687

88+
impl From<Block> for BlockId {
89+
fn from(value: Block) -> Self {
90+
match value {
91+
Block::Recent => BlockId::latest(),
92+
Block::Number(n) => BlockId::number(n),
93+
Block::Finalized => BlockId::finalized(),
94+
}
95+
}
96+
}
97+
8798
/// Recent block cache for arbitrary key-value pairs.
8899
///
89100
/// Caches on-chain data for a specific number of blocks and automatically

crates/shared/src/sources/balancer_v2/graph_api.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
use {
1212
super::swap::fixed_point::Bfp,
1313
crate::{event_handling::MAX_REORG_BLOCK_COUNT, subgraph::SubgraphClient},
14-
alloy::primitives::Address,
14+
alloy::primitives::{Address, B256},
1515
anyhow::Result,
16-
ethcontract::H256,
1716
reqwest::{Client, Url},
1817
serde::Deserialize,
1918
serde_json::json,
@@ -50,7 +49,7 @@ impl BalancerSubgraphClient {
5049
let block_number = self.get_safe_block().await?;
5150

5251
let mut pools = Vec::new();
53-
let mut last_id = H256::default();
52+
let mut last_id = B256::default();
5453

5554
// We do paging by last ID instead of using `skip`. This is the
5655
// suggested approach to paging best performance:
@@ -148,7 +147,7 @@ impl RegisteredPools {
148147
#[serde(rename_all = "camelCase")]
149148
pub struct PoolData {
150149
pub pool_type: PoolType,
151-
pub id: H256,
150+
pub id: B256,
152151
pub address: Address,
153152
pub factory: Address,
154153
pub swap_enabled: bool,
@@ -246,7 +245,6 @@ mod tests {
246245
super::*,
247246
crate::sources::balancer_v2::swap::fixed_point::Bfp,
248247
alloy::primitives::U256,
249-
ethcontract::H256,
250248
maplit::hashmap,
251249
};
252250

@@ -336,7 +334,7 @@ mod tests {
336334
pools: vec![
337335
PoolData {
338336
pool_type: PoolType::Weighted,
339-
id: H256([0x11; 32]),
337+
id: B256::repeat_byte(0x11),
340338
address: Address::repeat_byte(0x22),
341339
factory: Address::repeat_byte(0x55),
342340
swap_enabled: true,
@@ -359,7 +357,7 @@ mod tests {
359357
},
360358
PoolData {
361359
pool_type: PoolType::Stable,
362-
id: H256([0x11; 32]),
360+
id: B256::repeat_byte(0x11),
363361
address: Address::repeat_byte(0x22),
364362
factory: Address::repeat_byte(0x55),
365363
swap_enabled: true,
@@ -378,7 +376,7 @@ mod tests {
378376
},
379377
PoolData {
380378
pool_type: PoolType::LiquidityBootstrapping,
381-
id: H256([0x11; 32]),
379+
id: B256::repeat_byte(0x11),
382380
address: Address::repeat_byte(0x22),
383381
factory: Address::repeat_byte(0x55),
384382
swap_enabled: true,
@@ -401,7 +399,7 @@ mod tests {
401399
},
402400
PoolData {
403401
pool_type: PoolType::ComposableStable,
404-
id: H256([0x11; 32]),
402+
id: B256::repeat_byte(0x11),
405403
address: Address::repeat_byte(0x22),
406404
factory: Address::repeat_byte(0x55),
407405
swap_enabled: true,
@@ -447,7 +445,7 @@ mod tests {
447445
#[test]
448446
fn groups_pools_by_factory() {
449447
let pool = |factory: Address, id: u8| PoolData {
450-
id: H256([id; 32]),
448+
id: B256::repeat_byte(id),
451449
factory,
452450
pool_type: PoolType::Weighted,
453451
address: Default::default(),

crates/shared/src/sources/balancer_v2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ pub mod pools;
4444
pub mod swap;
4545

4646
pub use self::{
47-
pool_fetching::{BalancerFactoryKind, BalancerPoolFetcher, BalancerPoolFetching},
47+
pool_fetching::{BalancerPoolFetcher, BalancerPoolFetching},
4848
pools::{Pool, PoolKind},
4949
};

crates/shared/src/sources/balancer_v2/pool_fetching/aggregate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use {
55
super::internal::InternalPoolFetching,
66
crate::{recent_block_cache::Block, sources::balancer_v2::pools::Pool},
7+
alloy::primitives::B256,
78
anyhow::Result,
8-
ethcontract::H256,
99
futures::future,
1010
model::TokenPair,
1111
std::collections::HashSet,
@@ -25,7 +25,7 @@ impl Aggregate {
2525

2626
#[async_trait::async_trait]
2727
impl InternalPoolFetching for Aggregate {
28-
async fn pool_ids_for_token_pairs(&self, token_pairs: HashSet<TokenPair>) -> HashSet<H256> {
28+
async fn pool_ids_for_token_pairs(&self, token_pairs: HashSet<TokenPair>) -> HashSet<B256> {
2929
future::join_all(
3030
self.fetchers
3131
.iter()
@@ -37,7 +37,7 @@ impl InternalPoolFetching for Aggregate {
3737
.collect()
3838
}
3939

40-
async fn pools_by_id(&self, pool_ids: HashSet<H256>, block: Block) -> Result<Vec<Pool>> {
40+
async fn pools_by_id(&self, pool_ids: HashSet<B256>, block: Block) -> Result<Vec<Pool>> {
4141
Ok(future::try_join_all(
4242
self.fetchers
4343
.iter()

crates/shared/src/sources/balancer_v2/pool_fetching/cache.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use {
99
recent_block_cache::{Block, CacheConfig, CacheFetching, CacheKey, RecentBlockCache},
1010
sources::balancer_v2::pools::Pool,
1111
},
12+
alloy::primitives::B256,
1213
anyhow::Result,
13-
ethcontract::H256,
1414
ethrpc::block_stream::CurrentBlockWatcher,
1515
std::{collections::HashSet, sync::Arc},
1616
};
1717

1818
/// Internal type alias used for inner recent block cache.
19-
type PoolCache<Inner> = RecentBlockCache<H256, Pool, CacheFetcher<Inner>>;
19+
type PoolCache<Inner> = RecentBlockCache<B256, Pool, CacheFetcher<Inner>>;
2020

2121
/// A cached pool fetcher that wraps an inner `InternalPoolFetching`
2222
/// implementation.
@@ -52,18 +52,18 @@ where
5252
async fn pool_ids_for_token_pairs(
5353
&self,
5454
token_pairs: HashSet<model::TokenPair>,
55-
) -> HashSet<H256> {
55+
) -> HashSet<B256> {
5656
self.inner.pool_ids_for_token_pairs(token_pairs).await
5757
}
5858

59-
async fn pools_by_id(&self, pool_ids: HashSet<H256>, block: Block) -> Result<Vec<Pool>> {
59+
async fn pools_by_id(&self, pool_ids: HashSet<B256>, block: Block) -> Result<Vec<Pool>> {
6060
self.cache.fetch(pool_ids, block).await
6161
}
6262
}
6363

64-
impl CacheKey<Pool> for H256 {
64+
impl CacheKey<Pool> for B256 {
6565
fn first_ord() -> Self {
66-
H256::zero()
66+
B256::ZERO
6767
}
6868

6969
fn for_value(pool: &Pool) -> Self {
@@ -79,11 +79,11 @@ impl CacheKey<Pool> for H256 {
7979
struct CacheFetcher<Inner>(Arc<Inner>);
8080

8181
#[async_trait::async_trait]
82-
impl<Inner> CacheFetching<H256, Pool> for CacheFetcher<Inner>
82+
impl<Inner> CacheFetching<B256, Pool> for CacheFetcher<Inner>
8383
where
8484
Inner: InternalPoolFetching,
8585
{
86-
async fn fetch_values(&self, pool_ids: HashSet<H256>, at_block: Block) -> Result<Vec<Pool>> {
86+
async fn fetch_values(&self, pool_ids: HashSet<B256>, at_block: Block) -> Result<Vec<Pool>> {
8787
self.0.pools_by_id(pool_ids, at_block).await
8888
}
8989
}

crates/shared/src/sources/balancer_v2/pool_fetching/internal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
44
use {
55
crate::{recent_block_cache::Block, sources::balancer_v2::pools::Pool},
6+
alloy::primitives::B256,
67
anyhow::Result,
7-
ethcontract::H256,
88
model::TokenPair,
99
std::collections::HashSet,
1010
};
@@ -16,8 +16,8 @@ use {
1616
#[async_trait::async_trait]
1717
pub trait InternalPoolFetching: Send + Sync + 'static {
1818
/// Retrives all pool IDs that trade the specified pairs.
19-
async fn pool_ids_for_token_pairs(&self, token_pairs: HashSet<TokenPair>) -> HashSet<H256>;
19+
async fn pool_ids_for_token_pairs(&self, token_pairs: HashSet<TokenPair>) -> HashSet<B256>;
2020

2121
/// Fetches current pool states for the specified IDs and block.
22-
async fn pools_by_id(&self, pool_ids: HashSet<H256>, block: Block) -> Result<Vec<Pool>>;
22+
async fn pools_by_id(&self, pool_ids: HashSet<B256>, block: Block) -> Result<Vec<Pool>>;
2323
}

0 commit comments

Comments
 (0)