1/5
## Introduction to TWAP in Uniswap v2 In this lesson we'll be taking a look at the Uniswap v2 code to understand how TWAP is calculated. Specifically, we'll be looking at the `UniswapV2Pair.sol` contract. This contract is responsible for holding liquidity for a pair of tokens, and it's within this contract that the code for TWAP is implemented. We'll focus on an internal function called `update`, which is called every time a swap occurs or liquidity is added or removed. This function is responsible for updating the cumulative price of each token in the pair. The code snippet below is the section of the `update` function we'll be focused on in this lesson: ```javascript // NOTE: TWAP is time-weighted average pricing price0CumulativeLast += uint(uq112x112.encode(reserve1).uqdiv(reserve0)) * timeElapsed; price1CumulativeLast += uint(uq112x112.encode(reserve0).uqdiv(reserve1)) * timeElapsed; ``` ## Understanding Time-Weighted Average Price (TWAP) TWAP is a mechanism used to calculate the average price of a token over a certain period. It's used to help mitigate price manipulation by preventing large, sudden trades from impacting the price too significantly. The `update` function we're looking at uses a clever technique involving `uint`s to calculate the spot price of one token in terms of the other, and then multiplies that price by a time elapsed variable to get the time-weighted price. We'll take a closer look at how this works: ## How Uniswap v2 Calculates TWAP 1. The function first calculates the spot price of one token in terms of the other by dividing the reserve of one token by the reserve of the other token. 2. To represent the spot price as decimals (as Solidity doesn't support decimals directly), the function uses a special library, `uq112x112`, which splits the number into two parts. The first 112 bits represent the decimal portion, while the next 112 bits represent the whole number portion. 3. The function then multiplies the spot price by the `timeElapsed` variable, which represents the time passed since the last time the `update` function was called. This process is repeated every time a swap occurs or liquidity is added or removed, effectively accumulating the time-weighted average price. ## Overflow Behavior You might notice the comment `// addition overflow is desired` in the code snippet above. This is unusual! Usually, overflow in code is a bad thing. However, in this case, it's important for calculating the TWAP. The `update` function uses `uint`s for storing the cumulative price, and because `uint`s have a fixed size, they can overflow. When we calculate the difference between two cumulative prices to determine the average price, it's fine if the cumulative prices have overflowed. This is because the overflow is a consistent factor, and it cancels out when we calculate the difference.
A technical walkthrough of the Uniswap V2 code for TWAP - The video lesson walks through the TWAP implementation of the Uniswap V2 code, including an explanation of how the code calculates TWAP and why overflow in the calculations is considered desired.
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