CenturionDEX
Launch App

Oracles

Last modified:

The oracle problem

Smart contracts that integrate with DeFi protocols need reliable on-chain price data. Many ad-hoc oracle designs have been deployed on Ethereum with varying degrees of decentralization and security, and several have been exploited. See this analysis for examples.

CenturionDEX v2 provides building blocks for highly decentralized, manipulation-resistant on-chain price oracles.

How it works

Every pair measures the market price at the beginning of each block, before any trades execute. This price is set by the last transaction in the previous block (whether a mint, swap, or burn), making it expensive to manipulate: an attacker must make a bad trade at the end of one block with no guarantee of arbitraging it back in the next. This requires "selfishly" mining consecutive blocks — an attack that has not been observed in practice.

To further increase manipulation cost, V2 accumulates this end-of-block price into a single cumulative price variable in the core contract, weighted by time. This variable represents the sum of the pair's price for every second in the contract's entire history.

External contracts read this accumulator at two points in time and divide the difference by the interval to compute a time-weighted average price (TWAP).

TWAPs can be used directly or as inputs to moving averages (EMAs, SMAs).

Key properties:

See the whitepaper for additional nuances around manipulation resistance.

Manipulation resistance

The cost of manipulating the TWAP for a given period can be estimated as the amount lost to arbitrage and fees every block over the entire period. For large pools and long time windows, this cost typically exceeds the value at stake, making the attack impractical.

Network congestion and other factors can affect attack costs. See Security for more.

Building an oracle

See Building an Oracle in the developer guides, and the example 24-hour TWAP implementation.