Forking upstream chains
Forking lets you run local transactions, state overrides, and contract deployments against live mainnet or L2 state without broadcasting anything on-chain. Missing accounts, code, and storage slots load lazily on first access; local mutations never leave your process.
Fork at a pinned block
Section titled “Fork at a pinned block”Pass fork.url and pin fork.blockNumber (unsigned safe integer number) for deterministic reads across runs. Pair chainId and common with the target network so viem and ZEVM agree on chain identity and hardfork rules:
import { createMemoryClient, parseAbi } from 'tevm'import { mainnet } from 'tevm/common'
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'const WHALE = '0x47ac0Fb4F2D84898e4D9E7b4DaB3C24507a6D503'
const client = createMemoryClient({ chainId: mainnet.id, common: mainnet, fork: { url: process.env.ETH_RPC_URL ?? 'https://ethereum-rpc.publicnode.com', blockNumber: 20_000_000, },})
try { // Wait eagerly so an invalid or unreachable fork URL fails here await client.tevmReady()
const balance = await client.readContract({ address: USDC, abi: parseAbi(['function balanceOf(address) view returns (uint256)']), functionName: 'balanceOf', args: [WHALE], })
console.log('USDC balance at block 20,000,000:', balance)} finally { await client.tevmClose()}Fork semantics
Section titled “Fork semantics”Understanding three behaviors avoids surprises when writing fork tests:
- Local height starts at
0: ZEVM forks upstream state atfork.blockNumberinto a fresh local chain starting at block0.await client.getBlockNumber()returns0nuntil you mine local blocks (1n,2n, …). - Lazy caching: Accounts, bytecode, and storage slots are fetched from
fork.urlonly on first read and cached in native memory for subsequent calls. - Hardfork rules follow
chainId: WithchainId: 1, ZEVM applies Ethereum mainnet hardfork rules (including Osaka transaction gas cap and EIP-7883 ModExp limits). See Runtime model & hardforks.
Impersonate a live account on a fork
Section titled “Impersonate a live account on a fork”Combine impersonateAccount with tevmDeal (for gas) to execute transactions as any mainnet address — including multisigs, DAOs, or liquidity pools:
import { createMemoryClient, parseAbi, parseEther } from 'tevm'import { mainnet } from 'tevm/common'
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'const WHALE = '0x47ac0Fb4F2D84898e4D9E7b4DaB3C24507a6D503'const RECIPIENT = '0x1111111111111111111111111111111111111111'
const client = createMemoryClient({ chainId: mainnet.id, common: mainnet, fork: { url: process.env.ETH_RPC_URL ?? 'https://ethereum-rpc.publicnode.com', blockNumber: 20_000_000, },})
try { await client.tevmReady() await client.tevmDeal({ address: WHALE, amount: parseEther('5') }) await client.impersonateAccount({ address: WHALE })
const hash = await client.writeContract({ account: WHALE, address: USDC, abi: parseAbi(['function transfer(address,uint256) returns (bool)']), functionName: 'transfer', args: [RECIPIENT, 1_000_000n], // 1 USDC (6 decimals) })
const receipt = await client.getTransactionReceipt({ hash }) console.log('Transfer status:', receipt.status) // 'success'} finally { await client.tevmClose()}Resetting a fork at runtime
Section titled “Resetting a fork at runtime”Re-pin or switch upstream endpoints on a live engine with anvil_reset:
await client.request({ method: 'anvil_reset', params: [ { forking: { jsonRpcUrl: 'https://ethereum-rpc.publicnode.com', blockNumber: 20_500_000, }, }, ],})Handle fork startup errors
Section titled “Handle fork startup errors”If fork.url is unreachable or returns a JSON-RPC error during startup, createZevmEngine releases the native handle automatically and rejects tevmReady() (and any subsequent request) with a NativeRpcError:
import { createMemoryClient, NativeRpcError } from 'tevm'
const client = createMemoryClient({ fork: { url: 'http://127.0.0.1:1' },})
try { await client.tevmReady()} catch (error) { if (error instanceof NativeRpcError) { console.error('Fork init failed:', error.code, error.message) }} finally { await client.tevmClose()}Next steps
Section titled “Next steps”- Accounts, storage & snapshots — rewind forked state between tests without re-fetching upstream RPC data.
- Testing with Vitest — structure fast fork test suites with
snapshot()andrevert(). - Runtime model & hardforks — how
chainIdselects mainnet hardfork schedules.

