1/5
Let's walk through the code for the function, `swapTokensForExactTokens`. We are going to swap the minimum input for some specified output. For example, let's say we're willing to spend a maximum of 3,000 DAI and we want to get out exactly 1 WETH, then we would call this function. If getting 1 WETH would result in spending more than 3,000 DAI, this function will revert. Let's take a look at the code. First, we call a function called `getAmountsIn`. The last element in this `amounts` array will contain the amount of token that came out. This will be the `amountOut` specified by the user. And the first element will contain the amount of token that is needed to get the `amountOut`. We can see this check over here, `amounts[0]` must be less than or equal to `amountInMax`. The rest of the elements in this array will contain the amounts of tokens for the intermediate trades. Next, we transfer the input token, `path[0]` to the first pair contract, which is calculated by calling the function `pairFor` and passing `path[0]` and `path[1]`. The amount to transfer is stored in `amounts[0]`. Again, it might be surprising that we directly transfer the tokens before calling the `swap` function on the Uniswap V2 pair contract. Next, we call the internal function `swap`. What this function will do is for each pair contract, it will call the function `swap`. Inside the `for` loop, the last swap will send the output token to this `to` address. All other intermediate swaps will send the output token over to the next pair contract to call the function `swap`. So, this is the function for `swapTokensForExactTokens`. ```javascript function swapTokensForExactTokens( uint amountOutMin, address[] calldata path, address to, uint deadline ) external virtual override ensure(deadline) payable { // calculates amountIn based on the desired amountOut uint amountIn = UniswapV2Library.getAmountsIn(amountOut, path)[0]; // checks if the amountIn is less than or equal to the user's max input require(amountIn <= amountInMax, 'UniswapV2Router: EXCESSIVE_INPUT_AMOUNT'); // transfers the user's input token to the first pair contract for trading TransferHelper.safeTransferFrom( msg.sender, UniswapV2Library.pairForFactory(factory, path[0], path[1]), amountIn ); // performs the swap in the loop, traversing through all pairs in the path swap(amounts, path, to); } ```
A comprehensive guide to the UniSwap V2 Router's swapTokensForExactTokens function. The lesson covers the function's purpose, how it works with other functions, and its application in swapping tokens for exact amounts.
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