Skip to content

Balancer Math

Balancer 2-asset weighted pools use a generalized constant-product invariant with configurable token weights:

BxwxBywy=kB_x^{w_x} \cdot B_y^{w_y} = k

where Bx`B_x` and By`B_y` are the pool’s token balances and wx`w_x`, wy`w_y` are the normalized weights (summing to 1). When wx=wy=0.5`w_x = w_y = 0.5`, this reduces to the standard Uniswap V2 constant-product invariant xy=k`x \cdot y = k`.

Spot price

P=By/wyBx/wxP = \frac{B_y / w_y}{B_x / w_x}

Impermanent loss with weights

For a price change α=Pnew/Pentry`\alpha = P_{\text{new}} / P_{\text{entry}}`, the impermanent loss for a weighted pool is:

IL(α,wx)=αwxwxα+(1wx)1IL(\alpha, w_x) = \frac{\alpha^{w_x}}{w_x \cdot \alpha + (1 - w_x)} - 1

When wx=0.5`w_x = 0.5`, this reduces to the standard 2α/(1+α)1`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 formula
  • SimulateBalancerPriceMove — project position value at a hypothetical price change

See agentic_primitives_by_category for executable examples.