1/5
### Exercise 1: TWAP using a Uniswap V3 Pool In this lesson, we'll create a Time Weighted Average Price (TWAP) using a Uniswap V3 pool. The pool contract, along with token0 and token1, are initialized inside the constructor. Later on in the lesson, we'll call the `getQuoteAtTick` function to calculate the amount of token out based on the amount of token in. The exercise we'll be working on is inside of the function called `getTwapAmountOut`. This function takes in three inputs: `tokenIn`, `amountIn`, and `dt`, which is the time interval to take the TWAP. This function should return the amount of token out that is calculated from the TWAP and the amount in. The first task is to verify that `tokenIn` is either `token0` or `token1`, which were initialized inside of the constructor. Once we verify the `tokenIn`, we'll then assign `tokenOut`. Next, we'll initialize a dynamic array of type `uint32` which will store two elements. At index zero, we'll put the input `dt`, and at index one we'll put zero. This dynamic array will then be passed to the `pool.observe` function which we'll use to get the tick cumlatives. To understand how to call `observe` on the pool contract, we can navigate to the interface for the `IUniswapV3Pool`. Inside this interface, we can see how to call the function `observe`. ```javascript function observe(uint32[] calldata secondsAgos) external view returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s); ``` Moving back to our TWAP contract, once we get two observations, one from 0 seconds ago, and another from `dt` seconds ago, we can calculate the `tickCumulativeDelta`. This value can be calculated by taking the difference of the tick cumulative at 0 minus the tick cumulative at `dt`. Next, we'll calculate the average tick. After we calculate the `tickCumulativeDelta`, we'll divide this by `dt` to calculate the average tick. The next part of the code handles the logic for rounding down the numbers. This works for both positive and negative numbers. The code handles the following logic: If the `tickCumulativeDelta` is less than zero and it does not divide evenly by `dt`, then round down to negative infinity by doing `tick--`. ```javascript if (tickCumulativeDelta < 0 && (tickCumulativeDelta % int56(uint56(dt)) != 0)) { tick--; } ``` The last task is to call the `getQuoteAtTick` function, to return the amount of token out that will come out for amount of token in. ```javascript function getTwapAmountOut(address tokenIn, uint128 amountIn, uint32 dt) external view returns (uint256 amountOut){ ```
A comprehensive guide to UniSwap V3 Twap - Learn how to calculate the amount of token you will receive in a UniSwap V3 pool based on the current price and amount of token in. The lesson covers how to call the pool contract's observe function to get tick cumulatives, how to calculate the tick cumulative delta, how to calculate the average tick, and how to handle rounding down numbers to negative infinity.
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