Skip to main content
Cheatcodes are native Rust functions exposed to assertions through PhEVM. They provide state snapshots, call traces, storage changes, and transaction logs.

Main Capabilities

Cheatcodes enable functionality that would be impossible or impractical with standard Solidity:
  • Snapshot state access: Access blockchain state before or after a transaction or call with explicit ForkId snapshots
  • Call trace inspection: Inspect call traces and execution flow in ways not possible with standard EVM tools
  • State change tracking: Monitor specific storage slots and track how they change during transaction execution
  • Triggering assertions: Define precise conditions for when assertions should execute with onFnCall and onTxEnd triggers
  • Cumulative flow tracking: Register circuit-breaker triggers that collect ERC20 inflows or outflows into one bucket per block and evaluate the buckets over rolling windows

Why Cheatcodes Exist

Standard Solidity cannot read earlier transaction snapshots or call traces. PhEVM cheatcodes expose this execution context so assertions can compare states and inspect calls. The implementation uses native precompiles. These docs use the term cheatcodes for the assertion APIs.

When to Use Cheatcodes

Use cheatcodes when a policy needs execution history or state from another snapshot. See Transaction Data APIs for examples.

Snapshot Read Guidance

Read only the snapshots needed by the policy. Specify whether “pre-state” and “post-state” refer to the transaction or the matched call; a transaction-end comparison can miss an intermediate change that was later restored.
Choose the assertion specification supported by your target deployment. Reshiram (V2) helpers require V2 support in the enforcer; a local test passing does not establish deployment compatibility. See Assertion Specifications for shared registration and deployment requirements.
Prefer Reshiram snapshot reads for new assertions. Use explicit PhEvm.ForkId values with ph.loadStateAt() for transaction-wide comparisons, or ph.context() with call-scoped ForkId values inside an onFnCall assertion.

When to Use Pre-State

Read pre-state when the rule compares values across execution or checks that a value remains constant.

When to Skip Pre-State

Read only post-state when the rule depends solely on the final value. See Cheatcodes Reference for API signatures.

Next Steps

Cheatcodes Reference

Complete API reference for all available cheatcodes

Writing Your Own Policies

Define, implement, and test policies for your protocol.

Transaction Data APIs

Choose an observation API by task, snapshot scope, and coverage limits.

Use Cases Book

Real-world assertion examples and implementations