Skip to content

TWAP Window Too Short to Resist Manipulation

SAFE-0201 Oracle / Price Checked automatically by the scanner
1

What goes wrong

A time-weighted average price is computed over a very short window (a few blocks / seconds). On low-liquidity pools the average is still cheap to skew, so the TWAP inherits much of the spot price's manipulability.

2

The vulnerable pattern

proof of concept — how it is exploited
// TWAP over 2 blocks. Attacker skews the pool, holds it high for those
// 2 blocks (multi-block MEV / low competition), then borrows against the
// inflated average before it decays.
3

How to fix it

the pattern that is safe
// use a long, liquidity-appropriate window (e.g. >= 30 min) and a deep pool
uint32 window = 1800; // 30 minutes
price = consult(pool, window);
Longer TWAP windows + deep liquidity resist manipulation.
4

Where this has happened

Largest recorded losses in the same failure class — related, not the same bug:

Check your own contract for this

The SaferICO scanner runs 201 detectors over your Solidity source, SAFE-0201 among them. Paste an address or the source itself — a small per-scan fee, shown before you sign, or unlimited on any plan.

Run the scanner See how it is attacked Read the docs