0/5
## Uniswap V2 Remove Liquidity We're going to take a look at the `removeLiquidity` function, which allows users to withdraw their liquidity from a Uniswap V2 pair contract. This function is part of the UniswapV2Router02 contract, which is located within the V2-periphery repository. The `removeLiquidity` function accepts six inputs: - `tokenA`: The address of the first token in the pair contract. - `tokenB`: The address of the second token in the pair contract. - `liquidity`: The amount of liquidity shares to burn. - `amountAMin`: The minimum amount of tokenA the user expects to receive. - `amountBMin`: The minimum amount of tokenB the user expects to receive. - `to`: The address where the tokens will be sent. The function returns two outputs: - `amountA`: The actual amount of tokenA received. - `amountB`: The actual amount of tokenB received. The `removeLiquidity` function first computes the address of the pair contract using the `pairFor` function from the UniswapV2Library contract. ```javascript pair = UniswapV2Library.pairFor(factory, tokenA, tokenB); ``` The code then checks if the pair contract exists by calling the `transferFrom` function on the pair contract. ```javascript UniswapV2Pair(pair).transferFrom(msg.sender, pair, liquidity); ``` If the pair contract does not exist, this line of code will fail. Next, the function transfers the liquidity shares from the user to the pair contract. ```javascript UniswapV2Pair(pair).transferFrom(msg.sender, pair, liquidity); ``` The function then calls the `burn` function on the pair contract to burn the liquidity shares. ```javascript UniswapV2Pair(pair).burn(to); ``` The `burn` function returns the actual amounts of tokenA and tokenB received, which are stored in the `amount0` and `amount1` variables. The code then sorts the tokens to determine which one is `amount0` and which one is `amount1`. ```javascript (amount0, amount1) = UniswapV2Library.sortTokens(tokenA, tokenB); ``` Finally, the code checks that the actual amounts received are greater than or equal to the minimum amounts specified by the user. ```javascript require(amount0 >= amountAMin, 'UniswapV2Router: INSUFFICIENT_A_AMOUNT'); require(amount1 >= amountBMin, 'UniswapV2Router: INSUFFICIENT_B_AMOUNT'); ``` This ensures that the user receives at least the minimum amount of each token that they expected. The `removeLiquidity` function is an important part of the Uniswap V2 protocol, as it allows users to withdraw their liquidity from a pair contract and receive their underlying tokens.
A detailed walkthrough of Uniswap's removeLiquidity function. This lesson shows the parameters and outputs for the function, and provides a breakdown of what happens behind the scenes.
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