Skip to main content

Use Case & Applications

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.

Explanation

Monitors the product of reserves (k) in AMM pools to ensure it remains constant after transactions using:
  • forkPreTx() / forkPostTx(): Compare reserve product before and after transaction
  • getStateChanges(): Monitor reserve changes throughout transaction callstack
  • 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.

Code Example

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