Everything you ever need to know about TWAP Oracles

Jun 6
10
min of reading

Time-Weighted Average Price, this is what aTWAP oracle is based on. Understanding their operation and potentialvulnerabilities is important knowledge for auditors. Let's dive into thenuances of TWAP oracles, illustrating their function and highlighting theconcerns that can arise, as evidenced by the recent exploit of Compound V2.

How TWAP Oracles Work

TWAP oracles are designed to provide a morestable and manipulation-resistant measure of asset prices over time. Unlikesimple spot prices, which capture the price at a specific moment, TWAPcalculates the average price of an asset over a specified period. Thismechanism is often used by different protocols to prevent manipulation attacks.

The core idea behind a TWAP oracle is toaccumulate price values over time, which requires tracking the cumulative priceat various intervals. This is typically achieved by recording the cumulativeprice each time an event (like a swap) occurs, which affects the price. Thecalculation involves updating a cumulativePrice variable by adding the productof the current price and the time elapsed since the last update.

When you need to determine the averageprice between two points in time, the process involves fetching the cumulativeprices at these moments, calculating the difference between them, and thendividing this by the time elapsed between the two points. The formula lookssomething like this:

This method smooths out price fluctuationsover the specified period, providing a more consistent price metric that's lesssusceptible to momentary market manipulation.

The Problem with TWAP Oracles

Despite their benefits, TWAP oracles arenot without their vulnerabilities. Two main issues stand out:

Lag in Reflecting Real-Time Prices: BecauseTWAPs are based on historical data, there's an inherent delay in reflectingcurrent market conditions. This can be problematic in volatile markets whereprices change rapidly.

Potential for Manipulation: Although TWAPoracles are designed to be more resistant to price manipulation, they are notimmune. The mechanism that triggers the TWAP update can be exploited. Forinstance, if the oracle updates at fixed intervals, an attacker can executetrades that artificially inflate or deflate the price right before an update.This manipulation affects the cumulative price calculation, leading to a skewedTWAP.

The recent exploit of Compound V2underscores the limited use-case of TWAP oracles. Not only due to theirinherently incorrect price reflection but also by manipulating the price usedby the TWAP oracle, attackers can create favorable conditions which can finallylead to sophisticated exploits.

Read the original article

Related articles