1/5
## Uniswap V2: Swap Exact Tokens For Tokens In this lesson, we'll explore the `swapExactTokensForTokens` function, which allows us to swap a specific amount of tokens for another token. We'll start by simulating a transaction from a user. In our test, we'll use the `vm.prank` function to impersonate the user. ```javascript vm.prank(user); ``` The user will call the `UniswapV2Router02` contract, which contains the `swapExactTokensForTokens` function. We'll then specify the function to be called and assign the returned array of `uint` values to a variable. ```javascript uint256[] memory amounts = router.swapExactTokensForTokens( amountIn, amountOutMin, path, user, block.timestamp ); ``` These `uint` values represent the amount of each token exchanged during the swap. We can console.log these values to visualize the intermediate steps of the swap: ```javascript console.log("WETH", amounts[0]); console.log("DAI", amounts[1]); console.log("MKR", amounts[2]); ``` Lastly, we'll assert that the user's balance of MKR is greater than or equal to the minimum output amount. ```javascript assertGe(mkr.balanceOf(user), amountOutMin, "MKR balance of user"); ``` In this test, we're simulating a swap of WETH to DAI and then from DAI to MKR. This test also demonstrates how to use the `swapExactTokensForTokens` function in a practical scenario. To execute this test, we need to run `forge` with the correct fork URL and path. ```bash forge test --fork-url $FORK_URL --match-path test/uniswap-v2/exercises/UniswapV2/swap.sol -vv ``` This will run the test on a mainnet fork and provide verbose logging. This concludes our introduction to the `swapExactTokensForTokens` function. In future lessons, we'll delve deeper into the function's workings and explore additional use cases.
A comprehensive guide to understanding multi-hop swaps using Uniswap V2 Router. This lesson goes over testing an existing function called swapExactTokensForTokens, and covers how to simulate a transaction, use a forked mainnet environment, and output logs from your test.
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