Balancer Math
Balancer 2-asset weighted pools use a generalized constant-product invariant with configurable token weights:
where \(B_x\) and \(B_y\) are the pool’s token balances and \(w_x\), \(w_y\) are the normalized weights (summing to 1). When \(w_x = w_y = 0.5\), this reduces to the standard Uniswap V2 constant-product invariant \(x \cdot y = k\).
Spot price
Impermanent loss with weights
For a price change \(\alpha = P_{\text{new}} / P_{\text{entry}}\), the impermanent loss for a weighted pool is:
When \(w_x = 0.5\), this reduces to the standard \(2\sqrt{\alpha}/(1+\alpha) - 1\) formula used in Uniswap V2. Asymmetric weights compress or amplify IL exposure depending on the direction of price movement.
DeFiPy implementation
DeFiPy’s Balancer math lives in BalancerPy. The weighted-pool invariant, spot-price calculation, and swap math are implemented in balancerpy.cpt.exchg.BalancerExchange. The impermanent loss formula is implemented in balancerpy.analytics.risk.BalancerImpLoss.
The agentic primitives that consume Balancer math:
AnalyzeBalancerPosition— full position decomposition using the weighted IL formulaSimulateBalancerPriceMove— project position value at a hypothetical price change
See agentic_primitives_by_category for executable examples.
Note
Full derivation notebook (matching the depth of the Uniswap V2 and V3 math pages) is planned for v2.1.