1/5
The `burn` function inside the UniswapV2Pair contract allows us to withdraw liquidity from a pair. When the `burn` function is called, it will send tokens to the address specified in the `to` input. The actual amount of each token that was withdrawn from the pool will be returned in the `amount0` and `amount1` outputs. We'll walk through the `burn` function line by line. The function first gets the balance of token0 and token1: ```javascript address token0 = IERC20(token).balance(address(this)); ``` ```javascript address token1 = IERC20(token).balance(address(this)); ``` Next, it calls an internal function `mintFee`: ```javascript bool feeOn = mintFee(reserve0, reserve1); ``` We've covered `mintFee` in a previous lesson. The `burn` function calculates the amount of token0 and token1 to send to the `to` address using this equation: ```javascript amount0 = liquidity * mul(balance0 / totalSupply, reserve0); ``` ```javascript amount1 = liquidity * mul(balance1 / totalSupply, reserve1); ``` This formula calculates the proportional share of the total reserves for each token based on the liquidity being withdrawn. Next, the `burn` function checks that the amount of each token to send is greater than zero: ```javascript require(amount0 > 0 && amount1 > 0, 'UniswapV2: INSUFFICIENT_LIQUIDITY_BURNED'); ``` After that, the function burns the liquidity shares that we've withdrawn from the pool. ```javascript burn(address(this), liquidity); ``` Finally, the `burn` function updates the balance of each token in the pool to reflect the liquidity withdrawal. ```javascript balance0 = IERC20(token0).balanceOf(address(this)); ``` ```javascript balance1 = IERC20(token1).balanceOf(address(this)); ``` This ensures that the balances are up-to-date. The last step is to update the internal state of the pool: ```javascript update(balance0, balance1, reserve0, reserve1); ``` This ensures that the `reserve0` and `reserve1` values are updated to reflect the changes in the pool's balances.
A detailed explanation of the `burn` function in Uniswap V2 pair contract - The lesson covers the different steps involved in the execution of the `burn` function, including the calculations to determine the amount of tokens to be burned and transferred. It also explains why the amount of tokens to burn is implied by the pool shares provided as input.
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