TWAP Oracle

Powering robust protocols through highly decentralized, manipulation-resistant on-chain price oracles using TWAPs.
Degenhive is a powerful tool on SUI that empowers developers to construct highly decentralized, resistant on-chain price oracles. These are essential for the development of sturdy, reliable protocols. This ability to track and ensure price integrity is instrumental in meeting many of the requirements for creating robust protocols.
The way Degenhive operates is by calculating cumulative prices for coin balances in LiquidityPool Objects, a method first introduced by Uniswap V2 here. These calculated values can be used by external contracts to trace precise time-weighted average prices (TWAPs) across any time span.
To construct a TWAP, one simply needs to read the cumulative price from a LiquidityPool Object at the start and finish of the desired interval. The change in the cumulative price can then be divided by the duration of the interval, yielding the TWAP for that period.
These TWAPs can be used directly or as the foundation for moving averages (EMAs and SMAs), depending on the requirements of the protocol.
Here are a few notes to remember when working with TWAPs:
  1. 1.
    The sampling frequency should align with the length of the TWAP. For example, a 10-minute TWAP requires sampling once every 10 minutes, whereas a 1-week TWAP requires sampling once every week.
  2. 2.
    The cost of manipulating a simple TWAP rises proportionally with the liquidity on Uniswap and the length of time over which the average is taken.
  3. 3.
    Estimating the cost of an attack is relatively straightforward. For instance, to move the price 5% on a 1-hour TWAP is approximately equal to the loss due to arbitrage and fees when moving the price 5% every block for an hour.
While the use of Uniswap V2 as an oracle has its benefits, it also has some complexities that should be considered, especially in terms of manipulation resistance. More in-depth information about these nuances is provided in the whitepaper. Additional resources including oracle-focused developer guides and documentation will be released soon to provide further guidance.