Home Blockchain What are TWAP Oracles, and How are they different from Uniswap?

What are TWAP Oracles, and How are they different from Uniswap?

by ImmuneBytes
What are TWAP Oracles, and How are they different from Uniswap?

What do you think bridges the gap between Blockchain and the real world? There are third-party services known as Blockchain oracles that are responsible for this. They act as bridges between both of these.?

Indeed, both Blockchains and smart contracts can’t access off-chain data that is present outside the network. However, several contractual agreements can only be executed if you have relevant information from the real world. This is where blockchain oracles come into play. 

Whenever we come to talk about oracles, people often get confused about TWAP oracles and Uniswap. So, this is what we will be discussing in this blog. So, let us get started. 

What are Blockchain Oracles? 

  • Blockchain oracles are responsible for providing a link between off-chain and on-chain data. 
  • They play a pivotal role within the blockchain ecosystem as they broaden the scope of smart contracts to operate.
  • Without them, smart contracts would have limited uses as they wouldn’t be able to access data from outside the network. 
  • These layers query, authenticate, and verify the external data sources, then relay the information. 
  • There are many types of data that can be transmitted by oracles to smart contracts. These data types can include (but are not limited to) price information, the temperature measured by a sensor, and the successful competition of a payment. 

What are TWAP Oracles? 

  • TWAP stands for Time-Weighted Average Price. 
  • This has been widely used in Decentralized Finance and is a reliable average price that can exclude short-term price manipulation. 
  • There is a minor imprecision issue with the TWAP that Uniswap v3 offers. 
  • It is both convenient and easy to use. 

Time-weighted average ticks are an extra feature of Uniswap v3 that contributes immensely to saving a lot of time for a lot of projects. They don’t have to bild their own TWAP implementation. 

Uniswap v3 TWAP Implementation

It is quite simple to understand TWAP oracles. 

The price P gets multiplied by how long it lasts T. This is continuously added to a cumulative value C.

For example, 

  • When the timestamp is 0 and the price of the ETH is 3000, C= 0 (initialization)
  • When the timestamp is 200, and the price of the ETH is 3200, C=600,000+3200*(200-0)=600,000
  • When the timestamp is 250, and the price of the ETH is 3150, C=600,000+3200*(250-200)+760,000

Here, you can see that the time-weighted average price between time (0,250) is (760,000-0)/(250-0)=3,040. 

This means that it satisfies that price 3000 lasts for the time and price 3200 lasts for 3000+3200/5=3,040. 

Interestingly, one thing that you must note here is that Uniswap v3 time-weights the tick instead of the price. 

Let us understand how to get the TWAP. 

How To Get The Time-Weighted Average Price (TWAP)?

Now that we’ve given a fair idea of the algorithm of TWAP, it is time to dwell deeper to know how to get this oracle. 

This is how to get TWAP. Besides this, there are several other things that this snippet includes. These include: 

  • Edge case handling when twapInterval==0; return the current price of the pool
  • A helper function to transform sqrtPrice to price
  • Another thing to notice here in the code is that whenever a pool is initialized, only one slot stores the prices for the TWAP calculation. So, the more slots the pool has, the easier it is to provide TWAPs over longer periods. 

What is TWAP in Uniswap? 

Time Weighted Average Price was added in Uniswap v2 and was improved in Uniswap v3. TWAP on Uniswap v3 calculates the geometric mean of the relative prices of the two assets within a pool. Protocols used TWAP oracle feed as a reference price for on-chain assets. 

Compared to the earlier versions of Uniswap, Uniswap v3 is more capital efficient. Also, it is way more secure and decentralized than the previous versions. 

Why should you use TWAP Oracles? 

Now, it is clear how to calculate and get the TWAP from the Uniswap v3 pool. We can proceed to know the reasons why you should use TWAP oracles. 

The main reason why people use these Oracles is that they are considered to be very challenging for malicious actors to manipulate if we assume that there is enough liquidity in a given pool.

Factors To Measure The Difficulty Of Manipulating Price In A TWAP Oracle. 

The difficulty that hackers face in manipulating the price in a TWAP oracle can be measured using two factors that determine the difficulty of the attack. 

These are: 

  • Funds

The total amount that hackers have to put in the trading pool so that they can move the price in the direction that they want. In case there is deep liquidity in the pool, then it is possible that the amount can easily be in hundreds of millions or even more. However, even that won’t be enough. 

  • Time

The user must select the time window for the desired pricing in order to interact with the TWAP oracles. Now let’s say we request the weighted average price over a five-minute period. Attackers trying to raise the pool’s price to a desired price of X must hold that price for five minutes.


Introduced in Uniswap v2 and then improved in Uniswap v3, TWAP oracles prove to be an appropriate alternative to spot price oracles and off-chain oracles. These are the ones that are less susceptible to attacks. Also, TWAP Oracle stands much stronger against fluctuations and flash loans

Getting TWAPs from Uniswap v3 pools is, undoubtedly, an effortless and joyful experience. 

If there’s something more that you would like to know about TWAP oracles, then you can get in touch with us. ImmuneBytes is a pioneering smart contract auditing company that endeavors to make the crypto world more secure. We work day and night to provide you with the solutions to all your blockchain-related problems.

You may also like