Skip to main content

When to Use This Pattern

Ensures the constant product (k = x * y) is maintained in AMM pools, preventing unauthorized manipulation of pool reserves and potential flash loan attacks. Critical for Uniswap V2-style AMMs, DEX aggregators, lending protocols using AMM price oracles, and yield farming protocols providing liquidity. The constant product formula is fundamental to many AMM designs - any deviation could indicate a serious security issue or potential exploit.

What This Pattern Checks

Monitors the product of reserves (k) in AMM pools to ensure it remains constant after transactions using:
  • _preTx() / _postTx() with Reshiram snapshot reads: Compare reserve product before and after transaction
  • ph.loadStateAt(): Load reserve slots before and after the transaction
  • Direct validation that k = reserve0 * reserve1 remains unchanged
The assertion verifies both initial/final states and detects potential manipulations during transaction execution. For more information about cheatcodes, see the Cheatcodes Documentation.

Assertion Pattern

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