1/5
We looked at the function `swapExactTokensForTokens` on the Uniswap V2 contract. This function will try to swap all of your token in, and then give as much as possible of token out. There's another function in the Uniswap V2 contract called `swapTokensForExactTokens`. This function will try to spend the minimum amount of token in to give the user some amount that the user specified. So if you look at the inputs: `amountOut` will be the amount of token that the user wants. `amountInMax` will be the maximum amount of token in that the user is willing to spend. `path` will be the tokens. If we're only dealing with one pair contract, this path will contain the addresses of the two tokens. If the swap involves two pair contracts, then this path would contain three token addresses: two will be the receiver of the token out, and that last one will be the last timestamp that this swap is valid. And this function will return `amounts`. The first element of this array will contain the amount of token that went in. And the last element will contain the amount of token that was sent over to the two addresses. As an exercise, we want you to write some code that will call `swapTokensForExactTokens`. So here I have the exercise setup. ```javascript function testSwapTokensForExactTokens() public { address[] memory path = new address[](3); path[0] = WETH; path[1] = DAI; path[2] = MKR; uint amountOut = 0.1 * 1e18; uint amountInMax = 1e18; assertEq(mkr.balanceOf(user), amountOut, "MKR balance of user"); } ``` Again for this exercise we will swap from WETH to DAI, and then from DAI to MKR. And we'll say that the amount of MKR that we want is 0.1 * 1e18. MKR has 18 decimals. The amount of WETH that we're willing to spend is one WETH. So here I want you to write a code that will simulate the user calling `swapTokensForExactTokens`.
A comprehensive overview of a Uniswap V2 Router smart contract - This lesson covers two functions included in the Uniswap V2 Router contract. The first function, "swapExactTokensForTokens", allows the user to exchange any number of tokens in for as many tokens out as possible. The second function, "swapTokensForExactTokens" allows the user to specify an exact amount of tokens out in exchange for a minimum amount of tokens in.
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