0/5
In this lesson, we'll explore the code for `swapExactTokensForTokens` from Uniswap V2. This function allows us to swap tokens for other tokens. For instance, if we want to swap 1,000 DAI for the maximum possible amount of WETH, we'd use `swapExactTokensForTokens` for this purpose. Let's review how this function works. The function uses a Uniswap V2 library function called `getAmountOut` to calculate an array of `uint` values. This array, called `amounts`, stores the amount of tokens exchanged during the swap process. The first element in the `amounts` array represents the input token amount. The last element holds the output token amount, while any intermediate elements correspond to amounts exchanged during multi-hop swaps. The `swapExactTokensForTokens` function first transfers the input tokens to the pair contract. This pair contract is calculated using a function called `create2` based on the input and output token addresses provided in the `path` array. The `path` array contains the addresses of tokens involved in the swap. For instance, if we're swapping DAI to WETH and then to MKR, the `path` array would look like this: ```javascript [DAI, WETH, MKR] ``` Inside the `swap` function, the function loops through the `path` array, iterating from index zero to `path.length - 1`. Each iteration calculates the pair contract address, and then executes the `swap` function on the pair contract. We'll delve into the details of `getAmountOut` in a later lesson.
A detailed look at the code for UniSwap V2 Router swap functions - This video lesson takes a detailed look at the code for the UniSwap V2 Router swap functions. We will cover two functions in this lesson: swapExactTokensForTokens and swapTokensForExactTokens. We will also discuss the internal function called swap, and explain how each function calculates the amount of tokens to transfer for a given 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