1/5
In this lesson, we will learn how to remove liquidity from a Uniswap V2 pool. We will utilize the `removeLiquidity()` function within the `UniswapV2Router02` contract to achieve this. We will walk through an example in which we will: * Remove liquidity from a DAI/WETH pool * Console log the output amounts of DAI and WETH we receive Let's first take a look at the `removeLiquidity()` function's inputs and outputs: ```javascript function removeLiquidity( address tokenA, address tokenB, uint liquidity, uint amountAMin, uint amountBMin, address to, uint deadline ) public virtual override ensure(deadline) returns (uint amountA, uint amountB) ``` Here are the inputs: * `tokenA` : The address of the first token in the pool. * `tokenB` : The address of the second token in the pool. * `liquidity` : The amount of liquidity pool shares to burn. * `amountAMin` : The minimum amount of `tokenA` that we would like to receive for burning our `liquidity`. * `amountBMin` : The minimum amount of `tokenB` that we would like to receive for burning our `liquidity`. * `to` : The address that we would like to receive the tokens. * `deadline` : A Unix timestamp that the transaction must be completed before. Here are the outputs: * `amountA` : The amount of `tokenA` that we received. * `amountB` : The amount of `tokenB` that we received. Now let's write some code to remove liquidity. We will first need to initialize the `UniswapV2Router02` contract. ```javascript UniswapV2Router02 private constant router = UniswapV2Router02(UNISWAP_V2_ROUTER_02); ``` The `UniswapV2Router02` contract has already been deployed to a test network in this lesson, so we are able to initialize it within our test file. To call the `removeLiquidity()` function, we will use the following code: ```javascript (uint amountA, uint amountB) = router.removeLiquidity( tokenA, tokenB, liquidity, amountAMin, amountBMin, to, deadline ); ``` We will console log the amounts of DAI and WETH that we receive from calling this function. ```javascript console2.log("DAI:", amountA); console2.log("WETH:", amountB); ``` Now we will execute the test and see the results. ```bash forge test --fork-url FORK_URL --match-path test/uniswap-v2/exercises/uniswap-v2/liquidity.test.sol --vvv ``` We have successfully removed liquidity from the DAI/WETH pool and received the expected amounts of DAI and WETH.
A practical guide to removing liquidity from Uniswap V2 pools. The lesson explains the steps required to remove liquidity from a Uniswap V2 pool by calling the removeLiquidity function within the UniswapV2Router02 contract. The code example shows how to fill in the required parameters and provides a description of each parameter.
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