> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eco.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Balancer V2 (balancer-v2)

> Generalized AMM with weighted pools, single Vault architecture, and flash loans.

> **In a Sauce program**, swap with `@sauce/router`'s function for the venue (`uniswapV2`, `uniswapV3`, `uniswapV4`, `curve`, `balancerV2`, …; Slipstream, PancakeSwap V3 and Algebra pools through `uniswapV3`), with a minimum output and, where the pool takes one, a price limit ([the `@sauce/router` reference](https://docs.eco.com/sauce-libraries/router); several pools under one limit, [the distributed swap](https://docs.eco.com/quickstarts/sauce/distributed-swap.md)).

Generalized AMM with weighted pools, single Vault architecture, and flash loans. Supports custom pool types (weighted, stable, linear, composable stable) and multi-token pools with up to 8 tokens per pool.

| Fact | Value |
| - | - |
| Category | DEX |
| Family | `Balancer`: `balancer-v2` |
| Namespace | `BalancerV2` on ethereum, optimism, bsc, gnosis, polygon, base, arbitrum, avalanche |
| Chains | ethereum, optimism, bsc, gnosis, polygon, base, arbitrum, avalanche |

## What it offers

* **swap**: Execute a single swap through the Vault specifying pool ID and assets
* **addLiquidity**: Join a pool by depositing tokens via the Vault
* **removeLiquidity**: Exit a pool by withdrawing tokens via the Vault

## Notes

* Single Vault contract holds ALL pool liquidity (same address `0xBA12...` on every chain)
* Pools are identified by `bytes32 poolId`, NOT by contract address
* The Vault model means tokens only need to be approved to ONE contract for all Balancer pools
* Weighted pools: custom token weights (e.g., 80/20 ETH/USDC); Stable pools: pegged assets; Linear pools: wrapping yield-bearing tokens
* `batchSwap` is gas-efficient for multi-hop trades since tokens only move between Vault internal accounts
* Flash loans from Balancer Vault are zero-fee when the swap route stays within Balancer pools
* Internal balances: users can keep tokens in the Vault to save gas on repeated trades
* `userData` encoding varies by pool type - check Balancer docs for the specific pool's join/exit types
* Use Balancer for multi-token pools (3+ tokens), weighted index exposure, or flash loans
* From a Pot program, `@sauce/router` swaps through the Vault directly: `balancerV2(vault, tokenIn, tokenOut, amountIn, poolId, minOut, recipient)` approves the Vault for exactly `amountIn` and calls `swap` (GIVEN\_IN, internal balances off) with `minOut` as its limit, returning the amount out; the allowance ends at zero. `tryBalancerV2` catches a failed swap, resets the approval and returns `0`. The `balancerV2Swap` action is the same call

## Its names in a program

On Ethereum, `BalancerV2` is a namespace: its contracts need no import.

```ts theme={null}
function main() {
  // Ethereum's vault, `0xBA12222222228d8Ba445958a75a0704d566BF2C8`.
  require(BalancerV2.Vault != 0);
}
```

## Deployments

| Chain | Contract | Address | In a program |
| - | - | - | - |
| [Ethereum](https://docs.eco.com/sauce-sdk/reference/chains/ethereum.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |
| [Optimism](https://docs.eco.com/sauce-sdk/reference/chains/optimism.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |
| [BNB Chain](https://docs.eco.com/sauce-sdk/reference/chains/bsc.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |
| [Gnosis](https://docs.eco.com/sauce-sdk/reference/chains/gnosis.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |
| [Polygon](https://docs.eco.com/sauce-sdk/reference/chains/polygon.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |
| [Base](https://docs.eco.com/sauce-sdk/reference/chains/base.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |
| [Arbitrum One](https://docs.eco.com/sauce-sdk/reference/chains/arbitrum.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |
| [Avalanche C-Chain](https://docs.eco.com/sauce-sdk/reference/chains/avalanche.md) | vault | `0xBA12222222228d8Ba445958a75a0704d566BF2C8` | `BalancerV2.Vault` |

## ABI

### `BalancerV2VaultABI`

`import BalancerV2VaultABI from "./protocols/balancer-v2/BalancerV2VaultABI.json";` Speaks for: vault.

* `swap((bytes32 poolId, uint8 kind, address assetIn, address assetOut, uint256 amount, bytes userData) singleSwap, (address sender, bool fromInternalBalance, address recipient, bool toInternalBalance) funds, uint256 limit, uint256 deadline) payable returns (uint256 amountCalculated)`
* `batchSwap(uint8 kind, (bytes32 poolId, uint256 assetInIndex, uint256 assetOutIndex, uint256 amount, bytes userData)[] swaps, address[] assets, (address sender, bool fromInternalBalance, address recipient, bool toInternalBalance) funds, int256[] limits, uint256 deadline) payable returns (int256[] assetDeltas)`
* `joinPool(bytes32 poolId, address sender, address recipient, (address[] assets, uint256[] maxAmountsIn, bytes userData, bool fromInternalBalance) request) payable`
* `exitPool(bytes32 poolId, address sender, address recipient, (address[] assets, uint256[] minAmountsOut, bytes userData, bool toInternalBalance) request)`
* `flashLoan(address recipient, address[] tokens, uint256[] amounts, bytes userData)`
* `getPoolTokens(bytes32 poolId) view returns (address[] tokens, uint256[] balances, uint256 lastChangeBlock)`

## Next steps

* [Protocols](https://docs.eco.com/sauce-sdk/reference/protocols.md): the other protocols, by what they do.
* [Protocols and tokens](https://docs.eco.com/sauce/names.md): calling a contract by its namespace, or through its ABI.
* [Glossary](https://docs.eco.com/sauce/glossary.md): the words these pages use, in plain terms.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.