1/5
### Exercise 3 Solution Let's go over the solutions for how to call `exactOutputSingle` on the swap router v2 contract. First, we'll type ```javascript router.exactOutputSingle ``` and then we'll check back in the interface to see what parameter we need to prepare to call the function `exactOutputSingle`. Okay, here is the function `exactOutputSingle`, and we need to prepare this parameter. We'll copy and paste that parameter. ```javascript ISwapRouter.exactOutputSingleParams({ tokenIn: tokenOut: fee: recipient: amountOut: amountInMaximum: sqrtPriceLimitX96: }) ``` We'll need to import this struct called `exactOutputSingleParams`. This is from `ISwapRouter.exactOutputSingleParams`. Token in will be DAI, token out will be WETH, and fee will be 3000. Or we can use the constant that is defined in this contract: ```javascript POOL_FEE ``` Recipient will be this contract: ```javascript address(this) ``` Amount out, in this exercise, we want to get out 0.1 WETH. WETH has 18 decimals, so 0.1 WETH will be 0.1 * 1e18. ```javascript 0.1 * 1e18 ``` Amount in maximum, we are willing to spend up to 1000 DAI, so that is ```javascript 1000 * 1e18 ``` and square root price limit X96 will be set to zero. Okay, let's try executing the test. Set the fork URL as an environment variable. Then execute the test: ```bash forge test --fork-url $FORK_URL --match-path test/uniswap-v3/exercises/UniswapV3Swap.test.sol -vvv ``` Okay, and our test passed. To get 0.1 WETH, we had to spend about 300 DAI.
A detailed walkthrough of how to call exactOutputSingle on the Uniswap V3 router contract to execute a swap. The lesson covers how to define the necessary parameters for the function call, including token in, token out, fee, recipient, amount in maximum, and sqrtPriceLimitX96.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Concentrated liquidity and derive its equations
Uniswap V3 math
How to calculate the spot price of tokens
Single and multi position swapping
Factory contract architecture
How to calculate liquidity requirements
Uniswap V3 fee algorithm
Flash loans
TWAP price oracle
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
DeFi Developer
$75,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 May 15, 2025
DeFi Developer
Uniswap V3Duration: 3min
Duration: 35min
Duration: 25min
Duration: 22min
Duration: 1h 43min
Duration: 11min
Duration: 1h 03min
Duration: 12min
Duration: 51min
Duration: 41min
Duration: 10min
Duration: 7min
Course Overview
About the course
Concentrated liquidity and derive its equations
Uniswap V3 math
How to calculate the spot price of tokens
Single and multi position swapping
Factory contract architecture
How to calculate liquidity requirements
Uniswap V3 fee algorithm
Flash loans
TWAP price oracle
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
DeFi Developer
$75,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 May 15, 2025