Skip to content

Commit 5b23692

Browse files
committed
added stablesurge pools
1 parent 65747a8 commit 5b23692

File tree

33 files changed

+1858
-3
lines changed

33 files changed

+1858
-3
lines changed

crates/contracts/artifacts/BalancerV3StableSurgeHook.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

crates/contracts/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,7 @@ fn main() {
16441644
generate_contract("BalancerV3WeightedPool");
16451645
generate_contract("BalancerV3StablePool");
16461646
generate_contract("BalancerV3StableSurgePool");
1647+
generate_contract("BalancerV3StableSurgeHook");
16471648
generate_contract("BalancerV3GyroECLPPool");
16481649
generate_contract("BalancerV3Gyro2CLPPool");
16491650
generate_contract("BalancerV3ReClammPool");

crates/contracts/src/bin/vendor.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ fn run() -> Result<()> {
188188
"BalancerV3StableSurgePoolFactoryV2",
189189
"balancer/balancer-deployments/6c09a56033f2072cfac8823105df24e08dac812c/v3/tasks/20250404-v3-stable-surge-pool-factory-v2/artifact/StableSurgePoolFactory.json",
190190
)?
191+
.github(
192+
"BalancerV3StableSurgeHook",
193+
"balancer/balancer-deployments/6c09a56033f2072cfac8823105df24e08dac812c/v3/tasks/20250403-v3-stable-surge-hook-v2/artifact/StableSurgeHook.json",
194+
)?
191195
.manual(
192196
"BalancerV3QuantAMMWeightedPoolFactory",
193197
"Manually vendored ABI for Quant AMM Weighted Pool Factory contract",

crates/contracts/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ include_contracts! {
8282
BalancerV3StablePool;
8383
BalancerV3StablePoolFactory;
8484
BalancerV3StablePoolFactoryV2;
85+
BalancerV3StableSurgeHook;
8586
BalancerV3StableSurgePool;
8687
BalancerV3StableSurgePoolFactory;
8788
BalancerV3StableSurgePoolFactoryV2;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use {
1616
BalancerV3QuantAMMWeightedPoolFactory,
1717
BalancerV3StablePoolFactory,
1818
BalancerV3StablePoolFactoryV2,
19+
BalancerV3StableSurgePoolFactory,
20+
BalancerV3StableSurgePoolFactoryV2,
1921
BalancerV3Vault,
2022
BalancerV3WeightedPoolFactory,
2123
GPv2Settlement,
@@ -44,6 +46,7 @@ pub mod gyro_e;
4446
pub mod quantamm;
4547
pub mod reclamm;
4648
pub mod stable;
49+
pub mod stable_surge;
4750
pub mod weighted;
4851

4952
/// Maps a Chain to the corresponding GqlChain for Balancer V3 API.
@@ -174,6 +177,30 @@ async fn init_liquidity(
174177
)
175178
})
176179
.collect::<Vec<_>>(),
180+
config
181+
.stable_surge
182+
.iter()
183+
.map(|&factory| {
184+
(
185+
BalancerFactoryKind::StableSurge,
186+
BalancerV3StableSurgePoolFactory::at(&web3, factory.into())
187+
.raw_instance()
188+
.clone(),
189+
)
190+
})
191+
.collect::<Vec<_>>(),
192+
config
193+
.stable_surge_v2
194+
.iter()
195+
.map(|&factory| {
196+
(
197+
BalancerFactoryKind::StableSurgeV2,
198+
BalancerV3StableSurgePoolFactoryV2::at(&web3, factory.into())
199+
.raw_instance()
200+
.clone(),
201+
)
202+
})
203+
.collect::<Vec<_>>(),
177204
config
178205
.gyro_e
179206
.iter()
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use {
2+
crate::{
3+
boundary::Result,
4+
domain::{
5+
eth,
6+
liquidity::{self, balancer},
7+
},
8+
},
9+
shared::sources::balancer_v3::pool_fetching::StablePoolVersion,
10+
solver::liquidity::{BalancerV3StableSurgePoolOrder, balancer_v3},
11+
};
12+
13+
/// Median gas used per BalancerV3StableSurgeSwapGivenOutInteraction.
14+
/// StableSurge pools have slightly higher gas cost due to dynamic fee
15+
/// calculation in the hook, but the base swap is still the same as stable
16+
/// pools.
17+
// TODO: Estimate actual gas usage for Balancer V3 StableSurge pools
18+
// Using a slightly higher estimate than stable pools for the hook overhead
19+
const GAS_PER_SWAP: u64 = 93_000;
20+
21+
pub fn to_domain(
22+
id: liquidity::Id,
23+
pool: BalancerV3StableSurgePoolOrder,
24+
) -> Result<liquidity::Liquidity> {
25+
Ok(liquidity::Liquidity {
26+
id,
27+
gas: GAS_PER_SWAP.into(),
28+
kind: liquidity::Kind::BalancerV3StableSurge(balancer::v3::stable_surge::Pool {
29+
batch_router: batch_router(&pool),
30+
id: pool_id(&pool),
31+
reserves: balancer::v3::stable_surge::Reserves::try_new(
32+
pool.reserves
33+
.into_iter()
34+
.map(|(token, reserve)| {
35+
Ok(balancer::v3::stable_surge::Reserve {
36+
asset: eth::Asset {
37+
token: token.into(),
38+
amount: reserve.balance.into(),
39+
},
40+
scale: balancer::v3::ScalingFactor::from_raw(
41+
reserve.scaling_factor.as_uint256(),
42+
)?,
43+
})
44+
})
45+
.collect::<Result<_>>()?,
46+
)?,
47+
amplification_parameter: balancer::v3::stable_surge::AmplificationParameter::new(
48+
pool.amplification_parameter.factor(),
49+
pool.amplification_parameter.precision(),
50+
)?,
51+
fee: balancer::v3::Fee::from_raw(pool.fee.as_uint256()),
52+
version: match pool.version {
53+
StablePoolVersion::V1 => balancer::v3::stable_surge::Version::V1,
54+
StablePoolVersion::V2 => balancer::v3::stable_surge::Version::V2,
55+
},
56+
surge_threshold_percentage: balancer::v3::stable_surge::SurgeThresholdPercentage::new(
57+
pool.surge_threshold_percentage.as_uint256(),
58+
)?,
59+
max_surge_fee_percentage: balancer::v3::stable_surge::MaxSurgeFeePercentage::new(
60+
pool.max_surge_fee_percentage.as_uint256(),
61+
)?,
62+
}),
63+
})
64+
}
65+
66+
fn batch_router(pool: &BalancerV3StableSurgePoolOrder) -> eth::ContractAddress {
67+
pool.settlement_handling
68+
.as_any()
69+
.downcast_ref::<balancer_v3::SettlementHandler>()
70+
.expect("downcast balancer v3 settlement handler")
71+
.batch_router()
72+
.address()
73+
.into()
74+
}
75+
76+
fn pool_id(pool: &BalancerV3StableSurgePoolOrder) -> balancer::v3::Id {
77+
pool.settlement_handling
78+
.as_any()
79+
.downcast_ref::<balancer_v3::SettlementHandler>()
80+
.expect("downcast balancer v3 settlement handler")
81+
.pool_id()
82+
.into()
83+
}
84+
85+
pub fn to_interaction(
86+
pool: &liquidity::balancer::v3::stable_surge::Pool,
87+
input: &liquidity::MaxInput,
88+
output: &liquidity::ExactOutput,
89+
receiver: &eth::Address,
90+
) -> eth::Interaction {
91+
super::to_interaction(
92+
&super::Pool {
93+
batch_router: pool.batch_router,
94+
id: pool.id,
95+
},
96+
input,
97+
output,
98+
receiver,
99+
)
100+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ impl Fetcher {
199199
Liquidity::BalancerV3Gyro2CLP(pool) => balancer::v3::gyro_2clp::to_domain(id, pool),
200200
Liquidity::BalancerV3ReClamm(pool) => balancer::v3::reclamm::to_domain(id, pool),
201201
Liquidity::BalancerV3QuantAmm(pool) => balancer::v3::quantamm::to_domain(id, pool),
202+
Liquidity::BalancerV3StableSurge(pool) => balancer::v3::stable_surge::to_domain(id, pool),
202203
Liquidity::LimitOrder(pool) => zeroex::to_domain(id, pool),
203204
Liquidity::Concentrated(pool) => uniswap::v3::to_domain(id, pool),
204205
Liquidity::Erc4626(order) => erc4626::to_domain(id, order),

crates/driver/src/domain/competition/solution/encoding.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ pub fn liquidity_interaction(
431431
liquidity::Kind::BalancerV3QuantAmm(pool) => pool
432432
.swap(&input, &output, &settlement.address().into())
433433
.ok(),
434+
liquidity::Kind::BalancerV3StableSurge(pool) => pool
435+
.swap(&input, &output, &settlement.address().into())
436+
.ok(),
434437
liquidity::Kind::Swapr(pool) => pool
435438
.swap(&input, &output, &settlement.address().into())
436439
.ok(),

crates/driver/src/domain/competition/solution/interaction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl Interaction {
4141
liquidity::Kind::UniswapV3(pool) => pool.router.into(),
4242
liquidity::Kind::BalancerV2Stable(pool) => pool.vault.into(),
4343
liquidity::Kind::BalancerV3Stable(pool) => pool.batch_router.into(),
44+
liquidity::Kind::BalancerV3StableSurge(pool) => pool.batch_router.into(),
4445
liquidity::Kind::BalancerV2Weighted(pool) => pool.vault.into(),
4546
liquidity::Kind::BalancerV3Weighted(pool) => pool.batch_router.into(),
4647
liquidity::Kind::BalancerV2GyroE(pool) => pool.vault.into(),
@@ -69,6 +70,7 @@ impl Interaction {
6970
| liquidity::Kind::BalancerV3Gyro2CLP(_)
7071
| liquidity::Kind::BalancerV3ReClamm(_)
7172
| liquidity::Kind::BalancerV3QuantAmm(_)
73+
| liquidity::Kind::BalancerV3StableSurge(_)
7274
| liquidity::Kind::Swapr(_)
7375
| liquidity::Kind::ZeroEx(_) => vec![
7476
eth::Allowance {

crates/driver/src/domain/liquidity/balancer/v3/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod gyro_e;
88
pub mod quantamm;
99
pub mod reclamm;
1010
pub mod stable;
11+
pub mod stable_surge;
1112
pub mod weighted;
1213

1314
/// A Balancer V3 pool ID.

0 commit comments

Comments
 (0)