1/5
We're going to cover how swaps work in Uniswap V2, specifically the contracts and functions involved. We'll look at both single-hop and multi-hop swaps. A single-hop swap uses just one pair contract to swap between two tokens. A multi-hop swap requires two or more pair contracts, in a series, to swap two tokens. Let's start with a single-hop swap. In this example, we'll swap WETH for DAI. The user first calls the `swapExactTokensForTokens` function on the Router contract. This function takes the amount of WETH the user wants to swap and the address of the DAI token. The Router then transfers the WETH to the DAI/WETH pair contract. Once the WETH is transferred to the pair contract, the Router calls the `swap` function on the pair contract. This function swaps the WETH for DAI, and the Router then transfers the DAI back to the user. We can use the Router to swap other tokens, too. For instance, if the user wanted to swap ETH for DAI, they would call the `swapExactETHForTokens` function instead. Next, let's look at a multi-hop swap. We'll swap WETH for MKR. Here, we see that there is no direct WETH/MKR pair contract. Instead, we have a DAI/WETH pair and a DAI/MKR pair. In this case, the user calls the `swapExactTokensForTokens` function on the Router to swap WETH for MKR. The Router transfers the WETH to the DAI/WETH pair and calls the `swap` function. The WETH is swapped for DAI. The Router then transfers the DAI to the DAI/MKR pair contract and calls the `swap` function. The DAI is swapped for MKR. Finally, the Router transfers the MKR back to the user. This example is of a multi-hop swap: the Router swaps WETH to DAI, and then DAI to MKR.
A comprehensive guide to Uniswap V2 Swaps - The lesson covers the different functions the router contract uses to swap between ERC20 tokens and native ETH, as well as how multiple contracts can be called to exchange between tokens not directly supported by a single pool.
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