1/5
## Solution: Balancer Add Liquidity Let's go over the solution for how to add liquidity to a Balancer V2 AMM. The first thing that we'll do is pull in `reth` and `weth`. We'll use an if statement to do this. ```solidity if (rethAmount > 0) { reth.transferFrom(msg.sender, address(this), rethAmount); reth.approve(address(vault), rethAmount); } ``` We will also approve the Balancer V2 contract to pull in the `reth` from this contract. The contract that we need to call is the `vault` contract. The `vault` contract in Balancer V2 is the contract that will manage all of the tokens. This is the contract where all of the tokens will be locked. So we will need to approve this contract. ```solidity reth.approve(address(vault), rethAmount); ``` Now we will do the same for `weth`. ```solidity if (wethAmount > 0) { weth.transferFrom(msg.sender, address(this), wethAmount); weth.approve(address(vault), wethAmount); } ``` That's the first step: transferring the tokens in and then approving the Balancer V2 `vault` contract to spend the tokens from this contract. The next step is to call the internal function `join`. This function will add liquidity to the Balancer V2 `vault` contract. We'll call the function `joinPool` with these parameters. The parameters that we'll need to pass are `recipient`, `assets`, and `max amounts in`. Assets will be the tokens that we will add liquidity as. The order of the token addresses is important. So we'll need to call the internal function `join`. Let's start with the recipient. The recipient will be the caller. ```solidity msg.sender ``` Next, we'll prepare the parameters `assets`, and `max amounts in`. The order that we'll need to pass in is `reth` and then `weth`. So we'll initialize an array of addresses having a length of 2. ```solidity address[] memory assets = new address[](2); ``` Then we will pass in the addresses ```solidity assets[0] = address(reth); assets[1] = address(weth); ``` Let's move on to `maxAmountsIn` ```solidity uint256[] memory maxAmountsIn= new uint256[](2); maxAmountsIn[0] = rethAmount; maxAmountsIn[1] = wethAmount; ``` Then call the function join. The final step is to refund any tokens that were not added as liquidity. When we call the function join on the Balancer V2 AMM, there is a chance that it didn't take in all of the tokens. We'll query the balance of tokens that are leftover inside this contract and then send it over to `msg.sender`. Let's start with `reth` ```solidity uint256 rethBal = reth.balanceOf(address(this)); if (rethBal > 0) { reth.transfer(msg.sender, rethBal); } ``` Then we'll do the same for `weth` ```solidity uint256 wethBal = weth.balanceOf(address(this)); if (wethBal > 0) { weth.transfer(msg.sender, wethBal); } ``` That completes the exercise to join liquidity, to add liquidity into balancer V2 AMM. Let's try executing this function against the test. ```bash forge test --fork-url $FORK_URL --match-path test/exercise-balancer.sol --match-test test_join -vvv ``` After adding liquidity, we get back approximately 2 BPT tokens.
A comprehensive guide to adding liquidity to a Balancer B2AMM. This lesson covers the code needed to transfer tokens to the Balancer B2AMM contract, approve the Balancer B2 Vault contract to spend the tokens, and call the internal join function to add liquidity to the pool. We also demonstrate a test function to verify the process.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Rocket Pool rETH staking, yield mechanisms, trading options, and contract architecture
How to distribute rewards and calculate rETH/ETH exchange rate
How to acquire and redeem rETH
Integrating rETH into DeFi protocols like AAVE V3
How to how to create leveraged ETH positions using rETH
How to provide rETH as liquidity to Balancer and Aura
Getting the USD price of rETH using Rocket Pool NAV oracle
Re-staking with EigenLayer
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on May 12, 2025
Stay in the loop with the latest Rocket Pool updates and connect with over 5,000 developers in the community.
Solidity Developer
Rocket Pool rETH IntegrationDuration: 4min
Duration: 48min
Duration: 57min
Duration: 30min
Duration: 7min
Duration: 23min
Duration: 6min
Course Overview
About the course
Rocket Pool rETH staking, yield mechanisms, trading options, and contract architecture
How to distribute rewards and calculate rETH/ETH exchange rate
How to acquire and redeem rETH
Integrating rETH into DeFi protocols like AAVE V3
How to how to create leveraged ETH positions using rETH
How to provide rETH as liquidity to Balancer and Aura
Getting the USD price of rETH using Rocket Pool NAV oracle
Re-staking with EigenLayer
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on May 12, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar