Balancer Math
Balancer 2-asset weighted pools use a generalized constant-product invariant with configurable token weights:
where and are the pool’s token balances and , are the normalized weights (summing to 1). When , this reduces to the standard Uniswap V2 constant-product invariant .
Spot price
Impermanent loss with weights
For a price change , the impermanent loss for a weighted pool is:
When , this reduces to the standard 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.