0/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. ```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; vm.prank(user); uint256[] memory amounts = router.swapTokensForExactTokens( amountOut, amountInMax, path, user, block.timestamp ); console.log("WETH", amounts[0]); console.log("DAI", amounts[1]); console.log("MKR", 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 test/UniswapV2/exercises/UniswapV2Swap.test.sol:UniswapV2Test --fork-url $FORK_URL --match-path test/UniswapV2/exercises/UniswapV2Swap.test.sol ``` ### 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 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