Skip to main content

Use Case & Applications

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.

Explanation

Monitors TWAP price changes using a two-stage approach to ensure price stability:
  • ph.forkPreTx() / ph.forkPostTx(): Compare post-transaction price against pre-transaction TWAP
  • getStateChangesUint(): Track all price changes during transaction execution
  • registerStorageChangeTrigger(): 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.

Code Example

Full examples with tests available in the Phylax Assertion Examples Repository.