1/5
We're going to learn about how Uniswap V2 Router handles adding liquidity. The `addLiquidity` function takes the address of two ERC20 contracts, the amount of each token desired, and the minimum amount of each token that must be added. It also takes a deadline parameter which represents the latest timestamp at which the function can be executed. If the deadline has passed, the function call will fail. The `addLiquidity` function has two modes of operation: * `addLiquidity`, which is called for all ERC20 pairs * `addLiquidityETH`, which is called for pairs where one of the ERC20 tokens is ETH We'll focus on `addLiquidityETH`. ## How `addLiquidityETH` works When you call `addLiquidityETH`, the function first checks if the pair contract has already been deployed. If not, the function deploys the contract. Next, the function calculates the optimal amounts of each token to be added to the liquidity pool. This is done based on the current price of the pair and the amount of each token the user has provided. Finally, the function transfers the tokens to the pair contract, and calls the `mint` function to mint liquidity tokens. Here are the steps the `addLiquidityETH` function takes: 1. **Check if the pair contract exists.** The function first calls the `getPair` function on the Uniswap factory contract. If the function returns address zero, then the pair contract hasn't been deployed yet. ```javascript if (UniswapV2Factory(factory).getPair(tokenA, tokenB) == address(0)) { UniswapV2Factory(factory).createPair(tokenA, tokenB); } ``` 2. **Calculate the optimal amounts.** The function calls the `quote` function in the Uniswap V2 library to calculate the optimal amount of token B to be added for the amount of token A the user has provided. ```javascript uint amountBOptimal = UniswapV2Library.quote(amountADesired, reserveA, reserveB); ``` 3. **Check the minimum amounts.** The function then checks that the optimal amounts are greater than or equal to the minimum amounts specified by the user. ```javascript require(amountBOptimal >= amountBMin, 'UniswapV2Router: INSUFFICIENT_B_AMOUNT'); ``` 4. **Transfer the tokens.** The function then transfers the calculated amounts of token A and token B to the pair contract. ```javascript TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA); TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB); ``` 5. **Mint liquidity tokens.** Finally, the function calls the `mint` function on the pair contract. ```javascript liquidity = IUniswapV2Pair(pair).mint(to); ``` ## `quote` function The `quote` function in the Uniswap V2 library is used to calculate the optimal amount of one token that can be swapped for a given amount of another token. It uses the following formula: ```javascript amountB = (amountA * reserveB) / reserveA ``` This formula is based on the constant product formula of Uniswap V2, which states that the product of the reserves of two tokens in a liquidity pool must always remain constant. The `quote` function is used in the `addLiquidityETH` function to determine the optimal amount of WETH to be added for a given amount of the other token. ## Summary The `addLiquidityETH` function is a key part of the Uniswap V2 Router contract that allows users to add liquidity to a liquidity pool. It's a complex function with several steps, but ultimately, it ensures that the user's tokens are used to create a new liquidity position in a way that is fair and balanced.
A detailed look at the Uniswap V2 Router code - This lesson covers how Uniswap V2 Router handles adding liquidity to a B2 pair, including how it calculates the amount of each token needed based on the current reserves and how it utilizes the Uniswap V2 Library to perform the quote.
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