Skip to main content

Balancer V2

Balancer V2 is a flexible automated market maker (AMM) protocol that allows for customizable liquidity pools with multiple tokens and weights. It introduces significant improvements over V1, including protocol fees, asset managers, and weighted math.

Overview

Balancer V2 introduced several key innovations:

  • Weighted Pools: Support for up to 8 tokens with customizable weights
  • Stable Pools: Optimized for trading between assets that should have the same value (like stablecoins)
  • Boosted Pools: Enhanced yield through integration with lending protocols
  • Protocol Fees: Configurable fees that can be directed to the Balancer DAO
  • Asset Managers: External contracts that can manage idle assets in pools
  • Gas Optimization: Significant gas savings compared to V1

Integration with Bloctopus

When using Bloctopus with a forked Ethereum network, you can interact with Balancer V2 contracts that are already deployed on mainnet. This allows you to test your DeFi applications against real Balancer V2 liquidity pools.

info

This sample guide is for forked Ethereum networks but you can adapt the respective contract addresses when using other forked networks (e.g. Optimism, Arbitrum)

Key Contracts

The main Balancer V2 contracts you'll interact with:

  • Vault: 0xBA12222222228d8Ba445958a75a0704d566BF2C8
  • BalancerHelpers: 0x5aDDCCa35b7A0D07C74063c48700C8590E87864E
  • WeightedPoolFactory: 0x8E9aa87E45e92bad84D5F8DD1bff34Fb92637dE9
  • StablePoolFactory: 0xc66Ba2B6595D3613CCab350C886aCE23866EDe24

Sample Implementation

The DeFi Samples repository contains ready-to-use scripts for interacting with Balancer V2:

  • Query: query.js - Get information about Balancer V2 pools, tokens, weights, and fees
  • Quote: quote.js - Get price quotes for token swaps using BalancerHelpers
  • Swap: swap.js - Execute token swaps through the Balancer Vault

Running the Scripts

To run these scripts with Bloctopus:

  1. Clone the DeFi Samples repository:

    git clone https://github.com/LZeroAnalytics/defi-samples.git
    cd defi-samples
  2. Install dependencies:

    npm install
  3. Create a .env file with your Bloctopus RPC endpoint and private key:

    RPC_URL=https://your-bloctopus-rpc-endpoint
    PRIVATE_KEY=your-private-key
  4. Run a script:

    npx hardhat run scripts/protocols/balancer-v2/query.js
    npx hardhat run scripts/protocols/balancer-v2/quote.js
    npx hardhat run scripts/protocols/balancer-v2/swap.js

Testing with Bloctopus

To test Balancer V2 interactions using Bloctopus:

  1. Create a forked Ethereum network in Bloctopus
  2. Configure your Hardhat project to use the Bloctopus RPC endpoint
  3. Run your scripts or tests against the forked network

Resources