Skip to content

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.

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:

fork-mainnet.ts
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()
}

Understanding three behaviors avoids surprises when writing fork tests:

  1. Local height starts at 0: ZEVM forks upstream state at fork.blockNumber into a fresh local chain starting at block 0. await client.getBlockNumber() returns 0n until you mine local blocks (1n, 2n, …).
  2. Lazy caching: Accounts, bytecode, and storage slots are fetched from fork.url only on first read and cached in native memory for subsequent calls.
  3. Hardfork rules follow chainId: With chainId: 1, ZEVM applies Ethereum mainnet hardfork rules (including Osaka transaction gas cap and EIP-7883 ModExp limits). See Runtime model & hardforks.

Combine impersonateAccount with tevmDeal (for gas) to execute transactions as any mainnet address — including multisigs, DAOs, or liquidity pools:

fork-impersonate.ts
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()
}

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,
},
},
],
})

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()
}