Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Cookbook: Forking live networks

Forking lets you run real mainnet, Optimism, Arbitrum, or Base contracts locally against pinned state — then modify balances, impersonate holders, and trace failures without risking real funds.

How ZEVM Forking Works
LOCAL NODE.JS PROCESS (HEIGHT STARTS AT 0)Your Clientreads & writestevmSetAccounttevmCall / mine1. Local Write OverlayAccounts, code, storage, mined blocksChecked first on every read · Never sent upstream2. Lazy Read-Through CacheMiss? Fetch once at pinned blockNumberHit? Served locally at native speedwritereadSEPARATE PROCESS / RPCUpstream JSON-RPCMainnet, Optimism, Base,or a separate local servereth_getBalance / Codeeth_getStorageAtSync HTTP in native code:do not fork a server on thesame JS event loop.cache miss
ZEVM starts a fresh local chain at block 0 backed by lazy upstream state reads. Writes stay in memory and never touch the upstream RPC.

Read and simulate against live mainnet USDC

Set MAINNET_RPC_URL in your environment before running:

import { createMemoryClient, parseAbi, parseEther } from 'tevm'
import { mainnet } from 'tevm/common'
 
const rpcUrl = process.env.MAINNET_RPC_URL ?? 'https://ethereum-rpc.publicnode.com'
 
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
const WHALE = '0x47ac0Fb4F2D84898e4D9E7b4DaB3C24507a6D503'
const RECIPIENT = '0x1111111111111111111111111111111111111111'
 
const erc20Abi = parseAbi([
  'function balanceOf(address) view returns (uint256)',
  'function transfer(address to, uint256 amount) returns (bool)',
])
 
const client = createMemoryClient({
  chainId: mainnet.id,
  common: mainnet,
  fork: {
    url: rpcUrl,
    blockNumber: 20_000_000,
  },
})
 
try {
  await client.tevmReady()
 
  // 1. Read live USDC balance at block 20,000,000 (cached after first fetch)
  const whaleBalance = await client.readContract({
    address: USDC,
    abi: erc20Abi,
    functionName: 'balanceOf',
    args: [WHALE],
  })
  console.log('Whale USDC:', whaleBalance)
 
  // 2. Fund whale with gas, impersonate, and transfer locally
  await client.setBalance({ address: WHALE, value: parseEther('1') })
  await client.impersonateAccount({ address: WHALE })
 
  await client.writeContract({
    account: WHALE,
    address: USDC,
    abi: erc20Abi,
    functionName: 'transfer',
    args: [RECIPIENT, 1_000_000n], // 1 USDC
  })
 
  const received = await client.readContract({
    address: USDC,
    abi: erc20Abi,
    functionName: 'balanceOf',
    args: [RECIPIENT],
  })
  console.log('Recipient local USDC:', received) // 1000000n
} finally {
  await client.tevmClose()
}

Forking an L2 (Optimism, Base, Arbitrum)

Pass the chain's id and viem common preset alongside its RPC URL:

import { createMemoryClient } from 'tevm'
import { base, optimism } from 'tevm/common'
 
const opClient = createMemoryClient({
  chainId: optimism.id,
  common: optimism,
  fork: {
    url: process.env.OPTIMISM_RPC_URL ?? 'https://mainnet.optimism.io',
  },
})
 
const baseClient = createMemoryClient({
  chainId: base.id,
  common: base,
  fork: {
    url: process.env.BASE_RPC_URL ?? 'https://mainnet.base.org',
  },
})

Testing forks against a local mock upstream

Run your local upstream server in a child process with node:child_process:

import { fork } from 'node:child_process'
import { once } from 'node:events'
import { createMemoryClient } from 'tevm'
 
const upstream = fork('./upstream-server.mjs', { stdio: ['ignore', 'ignore', 'inherit', 'ipc'] })
try {
  const [{ url }] = (await once(upstream, 'message')) as [{ url: string }]
 
  const client = createMemoryClient({ fork: { url, blockNumber: 1 } })
  try {
    await client.tevmReady()
    // Reads lazily from the child process without deadlocking
    console.log(await client.getBlockNumber()) // 0n (local chain starts at 0)
  } finally {
    await client.tevmClose()
  }
} finally {
  const exited = once(upstream, 'exit')
  upstream.kill('SIGTERM')
  await exited
}

Next: Forking guide · Accounts, storage & snapshots