Balancer V2 — Contract Reference¶
Operational details for Balancer V2 weighted/stable pools: which canonical contracts the Rust engine implements (the Python companions are thin shells over it), how each contract generation is detected on-chain, and the deployment lineage used as parity ground truth.
Sources scraped for this reference (2026-09):
balancer-v2-monorepo @
f8b6f44f21afaf3c802536ed478277f945e7f256(2026-05-28, “Bump hardhat minor version (#2652)”) — also the vendored reference for the Tier-3 oracle.balancer-labs/balancer-deployments @ HEAD (
addresses/base.json,addresses/mainnet.json).
Canonical math pin (Tier-3 oracle)¶
tier3-oracle/src-balancer/BalancerSwapOracleHarness.sol compiles the
canonical library sources fetched at the pinned commit into
tier3-oracle/lib/balancer-src/:
Vendored source |
Canonical package |
Version at pin |
|---|---|---|
|
|
4.0.0 |
|
|
2.0.1 |
|
|
0.1.0 |
(vault interfaces) |
|
3.0.1 |
The harness reproduces the fee/scaling/direction sequence of the Rust engine
(degenbot-pools::simulate_balancer_weighted_swap /
simulate_balancer_stable_swap), which is the sole owner of Balancer swap
math; the Python companions are thin shells over it. Byte-exact parity is
asserted in
rust/crates/foundation/degenbot-pools/tests/tier3_balancer_swap_vs_revm.rs
(just test-tier3 balancer); artifact integrity is enforced by
tier3_harness_artifacts.rs + verify-tier3-artifacts.sh.
Contract-generation matrix¶
Contract |
Deployment lineage → factory |
Swap math |
Rust engine |
Python companion |
|---|---|---|---|---|
|
mainnet |
pow V1 (FixedPoint without the |
YES — |
shell over Rust core |
|
Base |
pow V2 (LogExpMath fast paths for |
YES — |
shell over Rust core |
|
mainnet |
|
YES — |
shell over Rust core |
|
mainnet |
|
YES — |
shell over Rust core |
|
mainnet |
|
YES — |
shell over Rust core |
Balancer V3 (the 2024-12 vault + 2026 factories) |
Base/mainnet |
hook-based, new vault |
NOT in scope (V2-vault pools only) |
— |
Shared across chains: the V2 Vault itself (0xBA12222222228d8Ba445958a75a0704d566BF2C8).
Version detection at construction¶
Weighted pow version —
detect_pow_version()(balancer/pools.py) scans the deployed bytecode for theTWOconstant (0x1bc16d674ec80000) used by they == TWOfast path, absent from 2021-era contracts →PowVersion.V1.Stable invariant version —
resolve_invariant_version()(builders/balancer_builder_base.py) maps the Vault pool specialization:General = 0→ComposableStablePool→INVARIANT_V1;MinimalSwapInfo = 1→MetaStablePool→INVARIANT_V2. Plain 2021StablePools areGeneral(3+ tokens) orTwoToken(2 tokens) with no BPT and also useINVARIANT_V2; the heuristic has no branch for them, so builds must pass theBuildPoolRequest.invariant_versionoverride (see gaps).BPT-in-balances —
detect_bpt_index()probes the token list;Nonemarks MetaStable (no BPT token).
Known deployed pools used as parity ground truth¶
Golden-oracle parity (mainnet, block 24,407,242 —
tests/golden/data/tests/balancer/):
Pool |
Notes |
|---|---|
|
2021-era weighted (pow V1) |
|
weighted pair |
|
weighted pair |
|
two-token weighted |
|
multi-token weighted (N > 2) |
|
stable (boosted/composable lineage) |
Gaps / follow-ups¶
Plain
StablePool(2021) — covered, override still required. The committed20210624-stable-poolbuild-info (solc 0.7.1) carries the deployedStableMath._calculateInvariant(amp, balances, roundUp)P_Drevision with swaps onroundUp=true— exactly the engine’sInvariantVersion.V2(calculate_invariant_deployed(..., true)). The Tier-3 companion gate exercises a 3-token plain-2021 configuration (INVARIANT_V2, no BPT) through both companion methods and both directions, and the Rust Tier-3 oracle pins that multi-token path byte-exact to the harness’s verbatim deployed invariant. The builder’sresolve_invariant_versionheuristic still maps only specializations 0 and 1 (plain 2021 pools are 0 or 2), so production builds of a plain 2021 pool must pass theBuildPoolRequest.invariant_versionoverride until the heuristic learns the no-BPT/no-rate-provider case.BalancerQueries on Base — resolved. The two addresses are per-chain deployments of the same
20220721-balancer-queriesartifact:0xE39B5e3B6D74016b2F6A9673D7d7493B6DF549d5has code on Ethereum mainnet and none on Base, while0x300Ab2038EAc391f26D9F895dc61F8F66a548833has code on Base (itsvault()is the canonical V2 Vault) and none on mainnet. The driver constant (BALANCERQUERIES_CONTRACT_ADDRESSinbalancer/deployments.py) is intentionally mainnet-scoped for the mainnet parity tests; the deployments registry’s0x300Abis the Base counterpart. No Base-scoped constant is added because no consumer queries BalancerQueries on Base.