Skip to content

Balancer API

Protocol-specific classes for Balancer — the weighted-pool AMM with the geometric-mean invariant. These live in balancerpy and are imported into DeFiPy’s namespace at install time.

When working through DeFiPy’s Core API, you typically don’t touch these classes directly — Join, Swap, AddLiquidity, RemoveLiquidity dispatch to them. You’ll reach for them when constructing a custom-weighted pool, querying vault state, or extending the library.

ClassModulePurpose
BalancerFactorybalancerpy.cpt.factoryDeploys weighted pools.
BalancerExchangebalancerpy.cpt.exchgThe pool — token reserves, normalized weights, swap fee, pool-share supply.
BalancerExchangeDatabalancerpy.cpt.exchgConfiguration object passed to the factory.
BalancerVaultbalancerpy.cpt.vaultVault holding pool’s underlying tokens. Exchange tokens via vault.get_token(name).
ModulePurpose
balancerpy.analytics.riskBalancerImpLoss — weighted-pool IL math, 2-asset, fee-free spot internally. Used by AnalyzeBalancerPosition and SimulateBalancerPriceMove.
  • Weights are normalized. The 50/50 ETH/DAI pool has tkn_weights = {'ETH': 0.5, 'DAI': 0.5}. Asymmetric weighting (80/20, 95/5, etc.) is supported.
  • SWAP_FEE is 0.25% by default — set on BalancerExchange at module load. Compare to V2’s 30 bps.
  • get_price bakes in the fee. pool.get_price(base, opp) applies the SWAP_FEE scale factor — correct for trade sizing, wrong for IL analysis. Compute fee-free spot manually: (b_opp / w_opp) / (b_base / w_base).
  • 2-asset only in v1. N-asset weighted pools are supported by Balancer the protocol, but DeFiPy’s IL math and analytics primitives are 2-asset for now.
  • Tokens accessed via the vault. Unlike V2/V3 where lp.factory.token_from_exchange[lp.name][lp.token0] returns the ERC20, Balancer uses lp.vault.get_token(name).
from defipy import *
eth = ERC20("ETH", "0x09")
dai = ERC20("DAI", "0x111")
# 50/50 ETH/DAI weighted pool — see tutorial for full setup.
factory = BalancerFactory("ETH-DAI factory", "0x2")
# ...exchange data construction with weights...
# lp = factory.deploy(exchg_data)

For a complete construction walkthrough see the Balancer abstract-interface tutorial.