Skip to main content

When to Use This Pattern

Prevents TWAP (Time-Weighted Average Price) price manipulation by ensuring oracle prices don’t deviate more than X% from pre-state TWAP prices. Critical for AMMs and DEXs (price discovery), lending protocols (collateral valuation), yield aggregators (rebalancing), options protocols (settlement), and cross-chain bridges (asset pricing). Sudden price deviations could indicate manipulation through flash loan attacks or oracle failures, potentially leading to protocol insolvency or fund extraction.

What This Pattern Checks

Monitors TWAP price changes using a two-stage approach to ensure price stability:
  • _preTx() / _postTx() with Reshiram snapshot reads: Compare post-transaction price against pre-transaction TWAP
  • getStateChangesUint(): Track all price changes during transaction execution
  • registerTxEndTrigger(): Trigger when price storage slot changes
The assertion verifies both the final price and any intermediate price updates against the initial TWAP to detect flash loan attacks and price manipulation throughout the transaction. For more information about cheatcodes, see the Cheatcodes Documentation.

Assertion Pattern

Full examples and mock protocol code are available in credible-std.