1/5
# Exercise 4: Calling Exact Output Okay, let's take a look at this exercise. We are going to be calling the `exactOutput` function on `swapRouter02`. We are going to be swapping a maximum of 1000 DAI to obtain exactly 0.01 WBTC. We will swap DAI to WETH and then from WETH to WBTC. We also need to send the WBTC back to this test contract. Also note that WBTC has 8 decimals. Let's take a look at the function declaration `exactOutput`. Inside the interface for `swapRouter02` the function `exactOutput` takes a single parameter, which is a struct. So we need to prepare this struct to call `exactOutput`. The first parameter `path` will encode the tokens to swap and the pools to call. For `exactOutput`, the path must be encoded in the reverse order of the tokens. In this exercise, we will be swapping from DAI to WETH and then from WETH to WBTC. So the path will be WBTC, WETH, and then finally DAI. The `recipient` parameter will be this test contract. The `amountOut` parameter will be 0.01 WBTC. And the `amountInMaximum` will be 1000 DAI. That is the last exercise, to call the function `exactOutput` on `swapRouter02`.
A comprehensive guide to performing an exact output swap on Uniswap V3 - In this lesson, we'll use the exactOutput function on the Uniswap V3 router to perform a series of token swaps. Specifically, we will swap 1000 DAI for WBTC, and then send the WBTC back to our test contract. In the process, we will learn how to use the exactOutput function and how to encode the swap path, paying close attention to the order of the tokens, the recipient address, and the amountOut and amountIn maximum values.
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