5/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: amountIn, amountOutMin: amountOutMin, path: path, to: user, deadline: 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 console2.log("WETH: %18e", amounts[0]); console2.log("DAI: %18e", amounts[1]); console2.log("MKR: %18e", 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 --mp test/uniswap-v2/exercises/UniswapV2Swap.test.sol -vvv ``` 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.
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.
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.
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:
Lastly, we'll assert that the user's balance of MKR is greater than or equal to the minimum output amount.
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.
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 June 6, 2025
Duration: 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 June 6, 2025