5/5
## How to Simulate a Uniswap V2 Swap In this lesson, we will learn how to simulate a swap on Uniswap V2. To do this, we will use Foundry. ### Prerequisites * You should have a FORK_URL set up for your environment. You can get this from a service like Alchemy. * You should have the UniswapV2 contracts deployed in your environment. ### Simulating a Swap We will be testing the `swapTokensForExactTokens` function. ```solidity // Receive an exact amount of output tokens for as few input tokens // as possible function test_swapTokensForExactTokens() public { address[] memory path = new address[](3); path[0] = WETH; path[1] = DAI; path[2] = MKR; uint256 amountOut = 0.1 * 1e18; // 1e17 uint256 amountInMax = 1e18; vm.prank(user); // Input token amount and all subsequent output token amounts uint256[] memory amounts = router.swapTokensForExactTokens({ amountOut: amountOut, amountInMax: amountInMax, path: path, to: user, deadline: block.timestamp }); console2.log("WETH: %18e", amounts[0]); console2.log("DAI: %18e", amounts[1]); console2.log("MKR: %18e", amounts[2]); assertEq(mkr.balanceOf(user), amountOut, "MKR balance of user"); } ``` ### Running the Test We'll run the test with the following command: ```bash forge test --fork-url $FORK_URL --mp test/uniswap-v2/exercises/UniswapV2Swap.test.sol --mt test_swapTokensForExactTokens -vvv ``` ### The Test Results The test results will include logs. Inside the logs you will see that we had 1 WETH initially, but our test used a smaller amount of WETH. We also confirmed that we received 0.1 MKR.
A comprehensive guide to testing a Uniswap V2 smart contract. This lesson covers how to set up a unit test to run against your contract using the Foundry testing framework. It demonstrates how to set up a unit test, include input parameters, and perform assertions to ensure the function returns the expected result.
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