Cookbook

Runnable recipes for debugging, gas analysis, state diffing, and more.

Worked examples for common mega-evme tasks.

Debug a Failing Transaction

Replay a transaction from MegaETH mainnet with opcode-level tracing to find the revert reason. Replace 0xabc123... with a real transaction hash:

mega-evme replay 0xabc123... \
  --rpc https://mainnet.megaeth.com/rpc \
  --trace --tracer opcode \
  --trace.opcode.enable-return-data \
  --trace.output trace.json

Inspect trace.json for the opcode that triggered REVERT. The return data contains the revert reason (ABI-encoded).

Compare Gas Usage Across Specs

Run the same calldata against different specs to see how gas costs differ:

# Under Rex3 — call WETH.balanceOf
mega-evme tx \
  --fork --fork.rpc https://mainnet.megaeth.com/rpc \
  --sender.balance 1ether \
  --receiver 0x4200000000000000000000000000000000000006 \
  --input 0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266 \
  --spec Rex3

# Under Rex4
mega-evme tx \
  --fork --fork.rpc https://mainnet.megaeth.com/rpc \
  --sender.balance 1ether \
  --receiver 0x4200000000000000000000000000000000000006 \
  --input 0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266 \
  --spec Rex4

Compare the gasUsed in the output to understand the impact of spec changes on your contract.

Deploy and Interact in Two Steps

Deploy a contract, capture the state, then call it:

Test SALT Bucket Impact on Storage Gas

Observe how storage gas costs scale with bucket capacity:

Compare the gasCost of the SSTORE opcode in both traces.

Fork and Patch Storage for Access Control Testing

Override an access-control slot to test a protected function without needing the actual admin key. This example patches WETH's slot 0 (total supply) and reads it back with totalSupply():

The --storage flag sets the slot to 1 before execution, so totalSupply() returns 1 regardless of the live chain value.

Replay With Modified Calldata

Test what would happen if a transaction had been called with different arguments. Replace 0xabc123... with a real transaction hash from MegaETH mainnet:

Fund Multiple Accounts for Multi-Party Testing

Set up balances for several participants in a single command:

Capture a State Diff

See exactly which accounts and storage slots a transaction modifies. WETH.deposit() wraps ETH and updates the caller's balance mapping:

The diff.json file shows pre and post states for all touched accounts.

Investigate Gas Detention Behavior

When a transaction reads volatile data (block environment fields, oracle storage), gas detention caps the remaining compute gas. Call HighPrecisionTimestamp.timestamp() — it reads oracle storage, which triggers detention:

Look for a sharp drop in gas remaining after the oracle SLOAD opcode.

Last updated