1/5
## Adding Liquidity to a Uniswap v2 Pair We'll now add liquidity to the DAI/WETH Uniswap v2 pair contract. We've already given our user some DAI and WETH to work with. Let's add some liquidity to this pair. To add liquidity, we need to call the `addLiquidity` function on the `UniswapV2Router02` contract. The router contract will handle the transfer of DAI and WETH from the user to the pair contract. We've already initialized the router contract in our code: ```javascript UniswapV2Router02 private constant router = UniswapV2Router02(UNISWAP_V2_ROUTER_02); ``` The code for adding liquidity to the DAI/WETH pair contract is: ```javascript (uint amountA, uint amountB, uint liquidity) = router.addLiquidity( tokenA: DAI, tokenB: WETH, amountADesired: 1e6 * 1e18, amountBDesired: 100 * 1e18, amountAMin: 1, amountBMin: 1, to: user, deadline: block.timestamp ); ``` The parameters for the `addLiquidity` function are: - `tokenA`: This is the address of DAI. - `tokenB`: This is the address of WETH. - `amountADesired`: We'll send 1 million DAI. In the setup function, we've given the user 1 million DAI. - `amountBDesired`: We'll send 100 WETH. In the setup function, we've given the user 100 WETH. - `amountAMin`: This is the minimum amount of DAI that the pair contract must receive; otherwise, the function call will revert. We'll set this to 1. - `amountBMin`: This is the minimum amount of WETH that the pair contract must receive; otherwise, the function call will revert. We'll set this to 1. - `to`: This is the address that will receive the pool shares. We'll set this to `user`. - `deadline`: This is the latest timestamp that the function call is valid. We'll set this to `block.timestamp`. We'll also console.log the outputs, which are `amountA`, `amountB`, and `liquidity`. ```javascript console.log("DAI", amountA); console.log("WETH", amountB); console.log("LP", liquidity); ``` We'll run the test inside our terminal. First, we'll set an environment variable called `FORK_URL`. We'll use this URL to execute our test against the main network. ```bash FORK_URL=https://eth-mainnet.alchemy.com/v2/Kx7ZpZEh8qkls4Jn_OSqM4AjS5DK4 ``` Next, we'll execute the test by typing: ```bash forge test --fork-url $FORK_URL --match-path test/uniswap-v2/exercises/uniswapv2/liquidity.test.sol ``` We've now successfully executed the test. It looks like we added 100 WETH to the DAI/WETH Uniswap v2 pair, and when we add 100 WETH, we also added this amount of DAI. We can see the amount of DAI that was sent to the pair contract, which is roughly 2,928,818. This is because 1 WETH is currently worth about 2,928 DAI.
A comprehensive guide to adding liquidity to a Uniswap V2 pool - The lesson covers using a test to ensure you have properly added liquidity to a Uniswap V2 pool. It demonstrates how to send in tokens and set minimum amounts that the pair must receive. The code also prints out various parameters for reference.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on October 9, 2024
Solidity Developer
Uniswap V2Duration: 14min
Duration: 1h 20min
Duration: 10min
Duration: 54min
Duration: 25min
Duration: 26min
Duration: 1h 03min
Duration: 59min
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on October 9, 2024
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