Skip to content

Commit ef94b8d

Browse files
authored
Migrate Balancer V2 from ethcontract to alloy primitives (#4002)
# Description Migrate the balancer V2 # Changes - [x] Replace `ethcontract::primitives` with `alloy::primitives` across crates: - driver/src/boundary/liquidity/balancerv2 - shares/sources/balancerv2 - solvers/src/liquidity/balancerv2 - solvers/src/boundary/liquidity - [x] Remove the now redundant intoalloy and intolegacy adapters. ## How to test 1. `cargo check -p e2e --tests` 2. Run e2e tests --------- Signed-off-by: Aryan Godara <[email protected]>
1 parent c7fd798 commit ef94b8d

File tree

18 files changed

+355
-327
lines changed

18 files changed

+355
-327
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use {
66
liquidity::{self, balancer},
77
},
88
},
9-
ethrpc::alloy::conversions::IntoAlloy,
109
solver::liquidity::{StablePoolOrder, balancer_v2},
1110
};
1211

@@ -28,20 +27,20 @@ pub fn to_domain(id: liquidity::Id, pool: StablePoolOrder) -> Result<liquidity::
2827
Ok(balancer::v2::stable::Reserve {
2928
asset: eth::Asset {
3029
token: token.into(),
31-
amount: reserve.balance.into_alloy().into(),
30+
amount: reserve.balance.into(),
3231
},
3332
scale: balancer::v2::ScalingFactor::from_raw(
34-
reserve.scaling_factor.as_uint256().into_alloy(),
33+
reserve.scaling_factor.as_uint256(),
3534
)?,
3635
})
3736
})
3837
.collect::<Result<_>>()?,
3938
)?,
4039
amplification_parameter: balancer::v2::stable::AmplificationParameter::new(
41-
pool.amplification_parameter.factor().into_alloy(),
42-
pool.amplification_parameter.precision().into_alloy(),
40+
pool.amplification_parameter.factor(),
41+
pool.amplification_parameter.precision(),
4342
)?,
44-
fee: balancer::v2::Fee::from_raw(pool.fee.as_uint256().into_alloy()),
43+
fee: balancer::v2::Fee::from_raw(pool.fee.as_uint256()),
4544
}),
4645
})
4746
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use {
66
liquidity::{self, balancer},
77
},
88
},
9-
ethrpc::alloy::conversions::IntoAlloy,
109
shared::sources::balancer_v2::pool_fetching::WeightedPoolVersion,
1110
solver::liquidity::{WeightedProductOrder, balancer_v2},
1211
};
@@ -29,19 +28,19 @@ pub fn to_domain(id: liquidity::Id, pool: WeightedProductOrder) -> Result<liquid
2928
Ok(balancer::v2::weighted::Reserve {
3029
asset: eth::Asset {
3130
token: token.into(),
32-
amount: reserve.common.balance.into_alloy().into(),
31+
amount: reserve.common.balance.into(),
3332
},
3433
weight: balancer::v2::weighted::Weight::from_raw(
35-
reserve.weight.as_uint256().into_alloy(),
34+
reserve.weight.as_uint256(),
3635
),
3736
scale: balancer::v2::ScalingFactor::from_raw(
38-
reserve.common.scaling_factor.as_uint256().into_alloy(),
37+
reserve.common.scaling_factor.as_uint256(),
3938
)?,
4039
})
4140
})
4241
.collect::<Result<_>>()?,
4342
)?,
44-
fee: balancer::v2::Fee::from_raw(pool.fee.as_uint256().into_alloy()),
43+
fee: balancer::v2::Fee::from_raw(pool.fee.as_uint256()),
4544
version: match pool.version {
4645
WeightedPoolVersion::V0 => balancer::v2::weighted::Version::V0,
4746
WeightedPoolVersion::V3Plus => balancer::v2::weighted::Version::V3Plus,

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ mod tests {
245245
use {
246246
super::*,
247247
crate::sources::balancer_v2::swap::fixed_point::Bfp,
248+
alloy::primitives::U256,
248249
ethcontract::H256,
249250
maplit::hashmap,
250251
};
@@ -343,12 +344,16 @@ mod tests {
343344
Token {
344345
address: Address::repeat_byte(0x33),
345346
decimals: 3,
346-
weight: Some(Bfp::from_wei(500_000_000_000_000_000u128.into())),
347+
weight: Some(Bfp::from_wei(U256::from(
348+
500_000_000_000_000_000_u128
349+
))),
347350
},
348351
Token {
349352
address: Address::repeat_byte(0x44),
350353
decimals: 4,
351-
weight: Some(Bfp::from_wei(500_000_000_000_000_000u128.into())),
354+
weight: Some(Bfp::from_wei(U256::from(
355+
500_000_000_000_000_000_u128
356+
))),
352357
},
353358
],
354359
},
@@ -381,12 +386,16 @@ mod tests {
381386
Token {
382387
address: Address::repeat_byte(0x33),
383388
decimals: 3,
384-
weight: Some(Bfp::from_wei(500_000_000_000_000_000u128.into())),
389+
weight: Some(Bfp::from_wei(U256::from(
390+
500_000_000_000_000_000_u128
391+
))),
385392
},
386393
Token {
387394
address: Address::repeat_byte(0x44),
388395
decimals: 4,
389-
weight: Some(Bfp::from_wei(500_000_000_000_000_000u128.into())),
396+
weight: Some(Bfp::from_wei(U256::from(
397+
500_000_000_000_000_000_u128
398+
))),
390399
},
391400
],
392401
},

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ mod tests {
233233
pools::{MockFactoryIndexing, common::MockPoolInfoFetching, weighted},
234234
swap::fixed_point::Bfp,
235235
},
236+
alloy::primitives::U256,
236237
maplit::{hashmap, hashset},
237238
mockall::predicate::eq,
238239
};
@@ -256,7 +257,9 @@ mod tests {
256257
let tokens: Vec<Address> = (start..=end + 1)
257258
.map(|i| Address::with_last_byte(i as u8))
258259
.collect();
259-
let weights: Vec<Bfp> = (start..=end + 1).map(|i| Bfp::from_wei(i.into())).collect();
260+
let weights: Vec<Bfp> = (start..=end + 1)
261+
.map(|i| Bfp::from_wei(U256::from(i)))
262+
.collect();
260263
let creation_events: Vec<(PoolCreated, u64)> = (start..=end)
261264
.map(|i| {
262265
(
@@ -284,8 +287,8 @@ mod tests {
284287
block_created: 0,
285288
},
286289
weights: vec![
287-
Bfp::from_wei(500_000_000_000_000_000u128.into()),
288-
Bfp::from_wei(500_000_000_000_000_000u128.into()),
290+
Bfp::from_wei(U256::from(500_000_000_000_000_000_u128)),
291+
Bfp::from_wei(U256::from(500_000_000_000_000_000_u128)),
289292
],
290293
},
291294
weighted::PoolInfo {
@@ -301,9 +304,9 @@ mod tests {
301304
block_created: 0,
302305
},
303306
weights: vec![
304-
Bfp::from_wei(500_000_000_000_000_000u128.into()),
305-
Bfp::from_wei(250_000_000_000_000_000u128.into()),
306-
Bfp::from_wei(250_000_000_000_000_000u128.into()),
307+
Bfp::from_wei(U256::from(500_000_000_000_000_000_u128)),
308+
Bfp::from_wei(U256::from(250_000_000_000_000_000_u128)),
309+
Bfp::from_wei(U256::from(250_000_000_000_000_000_u128)),
307310
],
308311
},
309312
weighted::PoolInfo {
@@ -315,8 +318,8 @@ mod tests {
315318
block_created: 0,
316319
},
317320
weights: vec![
318-
Bfp::from_wei(500_000_000_000_000_000u128.into()),
319-
Bfp::from_wei(500_000_000_000_000_000u128.into()),
321+
Bfp::from_wei(U256::from(500_000_000_000_000_000_u128)),
322+
Bfp::from_wei(U256::from(500_000_000_000_000_000_u128)),
320323
],
321324
},
322325
],
@@ -444,7 +447,7 @@ mod tests {
444447
scaling_factors: vec![Bfp::exp10(0)],
445448
block_created: 3,
446449
},
447-
weights: vec![Bfp::from_wei(1337.into())],
450+
weights: vec![Bfp::from_wei(U256::from(1337u64))],
448451
};
449452
let new_creation = PoolCreated {
450453
pool: new_pool.common.address,

crates/shared/src/sources/balancer_v2/pools/common.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use {
99
},
1010
token_info::TokenInfoFetching,
1111
},
12-
alloy::primitives::Address,
12+
alloy::primitives::{Address, U256},
1313
anyhow::{Context, Result, anyhow, ensure},
1414
contracts::alloy::{BalancerV2BasePool, BalancerV2Vault},
15-
ethcontract::{BlockId, H256, U256},
15+
ethcontract::{BlockId, H256},
1616
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
1717
futures::{FutureExt as _, future::BoxFuture},
1818
std::{collections::BTreeMap, future::Future, sync::Arc},
@@ -89,7 +89,7 @@ impl<Factory> PoolInfoFetcher<Factory> {
8989
) -> Result<PoolInfo> {
9090
let pool = self.base_pool_at(pool_address);
9191

92-
let pool_id = pool.getPoolId().call().await?.into_legacy();
92+
let pool_id = pool.getPoolId().call().await?;
9393
let tokens = self
9494
.vault
9595
.getPoolTokens(pool_id.0.into())
@@ -99,7 +99,7 @@ impl<Factory> PoolInfoFetcher<Factory> {
9999
let scaling_factors = self.scaling_factors(&tokens).await?;
100100

101101
Ok(PoolInfo {
102-
id: pool_id,
102+
id: pool_id.into_legacy(),
103103
address: pool_address,
104104
tokens,
105105
scaling_factors,
@@ -150,7 +150,7 @@ impl<Factory> PoolInfoFetcher<Factory> {
150150
async move {
151151
let (paused, swap_fee, pool_tokens) =
152152
futures::try_join!(fetch_paused, fetch_swap_fee, pool_tokens)?;
153-
let swap_fee = Bfp::from_wei(swap_fee.into_legacy());
153+
let swap_fee = Bfp::from_wei(swap_fee);
154154

155155
let balances = pool_tokens.balances;
156156
let tokens = pool_tokens.tokens.into_iter().collect::<Vec<_>>();
@@ -160,7 +160,7 @@ impl<Factory> PoolInfoFetcher<Factory> {
160160
(
161161
address,
162162
TokenState {
163-
balance: balance.into_legacy(),
163+
balance,
164164
scaling_factor,
165165
},
166166
)
@@ -371,7 +371,6 @@ mod tests {
371371
},
372372
anyhow::bail,
373373
contracts::alloy::BalancerV2Vault,
374-
ethcontract::U256,
375374
maplit::{btreemap, hashmap},
376375
mockall::predicate,
377376
std::future,
@@ -401,7 +400,7 @@ mod tests {
401400
&BalancerV2Vault::BalancerV2Vault::getPoolTokensReturn {
402401
tokens: tokens.to_vec(),
403402
balances: vec![],
404-
lastChangeBlock: U256::zero().into_alloy(),
403+
lastChangeBlock: U256::ZERO,
405404
},
406405
);
407406
asserter.push_success(&get_pool_tokens_response);
@@ -463,26 +462,23 @@ mod tests {
463462
BalancerV2BasePool::BalancerV2BasePool::getPausedStateCall::abi_encode_returns(
464463
&BalancerV2BasePool::BalancerV2BasePool::getPausedStateReturn {
465464
paused: false,
466-
pauseWindowEndTime: U256::zero().into_alloy(),
467-
bufferPeriodEndTime: U256::zero().into_alloy(),
465+
pauseWindowEndTime: U256::ZERO,
466+
bufferPeriodEndTime: U256::ZERO,
468467
},
469468
);
470469
asserter.push_success(&get_paused_state_response);
471470
let get_swap_fee_percentage_response =
472471
BalancerV2BasePool::BalancerV2BasePool::getSwapFeePercentageCall::abi_encode_returns(
473-
&bfp!("0.003").as_uint256().into_alloy(),
472+
&bfp!("0.003").as_uint256(),
474473
);
475474
asserter.push_success(&get_swap_fee_percentage_response);
476475

477476
let get_pool_tokens_response =
478477
BalancerV2Vault::BalancerV2Vault::getPoolTokensCall::abi_encode_returns(
479478
&BalancerV2Vault::BalancerV2Vault::getPoolTokensReturn {
480479
tokens: tokens.to_vec(),
481-
balances: balances
482-
.iter()
483-
.map(|b| b.as_uint256().into_alloy())
484-
.collect(),
485-
lastChangeBlock: U256::zero().into_alloy(),
480+
balances: balances.iter().map(|b| b.as_uint256()).collect(),
481+
lastChangeBlock: U256::ZERO,
486482
},
487483
);
488484
asserter.push_success(&get_pool_tokens_response);
@@ -554,24 +550,24 @@ mod tests {
554550
BalancerV2BasePool::BalancerV2BasePool::getPausedStateCall::abi_encode_returns(
555551
&BalancerV2BasePool::BalancerV2BasePool::getPausedStateReturn {
556552
paused: false,
557-
pauseWindowEndTime: U256::zero().into_alloy(),
558-
bufferPeriodEndTime: U256::zero().into_alloy(),
553+
pauseWindowEndTime: U256::ZERO,
554+
bufferPeriodEndTime: U256::ZERO,
559555
},
560556
);
561557
asserter.push_success(&get_paused_state_response);
562558

563559
let get_swap_fee_percentage_response =
564560
BalancerV2BasePool::BalancerV2BasePool::getSwapFeePercentageCall::abi_encode_returns(
565-
&U256::zero().into_alloy(),
561+
&U256::ZERO,
566562
);
567563
asserter.push_success(&get_swap_fee_percentage_response);
568564

569565
let get_pool_tokens_response =
570566
BalancerV2Vault::BalancerV2Vault::getPoolTokensCall::abi_encode_returns(
571567
&BalancerV2Vault::BalancerV2Vault::getPoolTokensReturn {
572568
tokens: vec![Address::repeat_byte(1), Address::repeat_byte(4)],
573-
balances: vec![U256::zero().into_alloy(), U256::zero().into_alloy()],
574-
lastChangeBlock: U256::zero().into_alloy(),
569+
balances: vec![U256::ZERO, U256::ZERO],
570+
lastChangeBlock: U256::ZERO,
575571
},
576572
);
577573
asserter.push_success(&get_pool_tokens_response);
@@ -619,14 +615,14 @@ mod tests {
619615
BalancerV2BasePool::BalancerV2BasePool::getPausedStateCall::abi_encode_returns(
620616
&BalancerV2BasePool::BalancerV2BasePool::getPausedStateReturn {
621617
paused: false,
622-
pauseWindowEndTime: U256::zero().into_alloy(),
623-
bufferPeriodEndTime: U256::zero().into_alloy(),
618+
pauseWindowEndTime: U256::ZERO,
619+
bufferPeriodEndTime: U256::ZERO,
624620
},
625621
);
626622
asserter.push_success(&get_paused_state_response);
627623
let get_swap_fee_percentage_response =
628624
BalancerV2BasePool::BalancerV2BasePool::getSwapFeePercentageCall::abi_encode_returns(
629-
&swap_fee.as_uint256().into_alloy(),
625+
&swap_fee.as_uint256(),
630626
);
631627
asserter.push_success(&get_swap_fee_percentage_response);
632628

@@ -679,9 +675,9 @@ mod tests {
679675
balances: pool_state
680676
.tokens
681677
.values()
682-
.map(|token| token.common.balance.into_alloy())
678+
.map(|token| token.common.balance)
683679
.collect(),
684-
lastChangeBlock: U256::zero().into_alloy(),
680+
lastChangeBlock: U256::ZERO,
685681
},
686682
);
687683
asserter.push_success(&get_pool_tokens_response);
@@ -734,15 +730,15 @@ mod tests {
734730
BalancerV2BasePool::BalancerV2BasePool::getPausedStateCall::abi_encode_returns(
735731
&BalancerV2BasePool::BalancerV2BasePool::getPausedStateReturn {
736732
paused: true,
737-
pauseWindowEndTime: U256::zero().into_alloy(),
738-
bufferPeriodEndTime: U256::zero().into_alloy(),
733+
pauseWindowEndTime: U256::ZERO,
734+
bufferPeriodEndTime: U256::ZERO,
739735
},
740736
);
741737
asserter.push_success(&get_paused_state_response);
742738

743739
let get_swap_fee_percentage_response =
744740
BalancerV2BasePool::BalancerV2BasePool::getSwapFeePercentageCall::abi_encode_returns(
745-
&U256::zero().into_alloy(),
741+
&U256::ZERO,
746742
);
747743
asserter.push_success(&get_swap_fee_percentage_response);
748744

@@ -751,7 +747,7 @@ mod tests {
751747
&BalancerV2Vault::BalancerV2Vault::getPoolTokensReturn {
752748
tokens: vec![],
753749
balances: vec![],
754-
lastChangeBlock: U256::zero().into_alloy(),
750+
lastChangeBlock: U256::ZERO,
755751
},
756752
);
757753
asserter.push_success(&get_pool_tokens_response);
@@ -813,15 +809,15 @@ mod tests {
813809
BalancerV2BasePool::BalancerV2BasePool::getPausedStateCall::abi_encode_returns(
814810
&BalancerV2BasePool::BalancerV2BasePool::getPausedStateReturn {
815811
paused: false,
816-
pauseWindowEndTime: U256::zero().into_alloy(),
817-
bufferPeriodEndTime: U256::zero().into_alloy(),
812+
pauseWindowEndTime: U256::ZERO,
813+
bufferPeriodEndTime: U256::ZERO,
818814
},
819815
);
820816
asserter.push_success(&get_paused_state_response);
821817

822818
let get_swap_fee_percentage_response =
823819
BalancerV2BasePool::BalancerV2BasePool::getSwapFeePercentageCall::abi_encode_returns(
824-
&U256::zero().into_alloy(),
820+
&U256::ZERO,
825821
);
826822
asserter.push_success(&get_swap_fee_percentage_response);
827823

@@ -830,7 +826,7 @@ mod tests {
830826
&BalancerV2Vault::BalancerV2Vault::getPoolTokensReturn {
831827
tokens: vec![],
832828
balances: vec![],
833-
lastChangeBlock: U256::zero().into_alloy(),
829+
lastChangeBlock: U256::ZERO,
834830
},
835831
);
836832
asserter.push_success(&get_pool_tokens_response);

0 commit comments

Comments
 (0)