Skip to content

Memory client (@tevm/memory-client)

@tevm/memory-client (re-exported from tevm and tevm/memory-client) creates a viem v2 client backed by an in-process ZevmEngine.

  • createMemoryClient(options?: MemoryClientOptions): MemoryClient
  • createTevmTransport(options?: ZevmEngineOptions & { engine?: ZevmEngine }): Transport
  • tevmViemActions() — viem client decorator attaching tevmReady, tevmClose, tevmCall, tevmContract, tevmDeploy, tevmDeal, tevmGetAccount, tevmSetAccount, tevmMine, tevmDumpState, tevmLoadState
  • tevmReady(client): Promise<void>
  • Action re-exports from @tevm/actions: tevmCall, tevmContract, tevmDeal, tevmDeploy, tevmDumpState, tevmGetAccount, tevmLoadState, tevmMine, tevmSetAccount
  • Viem re-exports: createClient, publicActions, walletActions, testActions

Extends ZevmEngineOptions:

Option Type Default Description
chainId number common?.id ?? 31337 Unsigned safe integer chain ID. Throws if it disagrees with common.id.
common Chain (viem) foundry (with chainId) Viem Chain metadata attached as client.chain. With an injected backend or engine and neither common nor chainId, client.chain is undefined.
mining { auto?: boolean; interval?: number } { auto: true } Automine flag and optional background interval in whole seconds (0 disables timer).
fork { url: string; blockNumber?: number } undefined Upstream JSON-RPC endpoint and optional pinned block number (unsigned safe integer).
backend ZevmBackend undefined Custom { rpc, close } JSON-RPC backend. Mutually exclusive with wasm.
wasm boolean | ZevmWasmSource auto outside Node.js Force (true / bytes / WebAssembly.Module) or disable (false) @evmts/zevm/browser WASM reactor. Mutually exclusive with backend.
consoleLog boolean | ConsoleLogHandler false Capture Hardhat Solidity console.log calls. Ignored when engine is supplied directly.
engine ZevmEngine new engine Supply an existing ZevmEngine. client.tevmClose() still closes it.
account Account | Hex undefined Default account for viem wallet actions (sendTransaction, writeContract, deployContract).
name string 'TEVM' Viem client display name.
key string 'tevm' Viem client key.

A MemoryClient exposes five namespaces on one object:

Surface Key methods
Lifecycle tevmReady(), tevmClose(), transport.tevm (ZevmEngine)
TEVM actions tevmCall, tevmContract, tevmDeploy, tevmDeal, tevmGetAccount, tevmSetAccount, tevmMine, tevmDumpState, tevmLoadState
Viem public getBlockNumber, getBlock, getBalance, getCode, getStorageAt, getTransaction, getTransactionReceipt, getLogs, readContract, simulateContract, call, estimateGas, multicall
Viem wallet sendTransaction, writeContract, deployContract, signMessage, signTypedData
Viem test (Anvil) mine, setBalance, setCode, setNonce, setStorageAt, impersonateAccount, stopImpersonatingAccount, snapshot, revert, increaseTime, setNextBlockTimestamp, setAutomine, getAutomine, setIntervalMining, dumpState, loadState

Initialize with explicit chain metadata and default account

Section titled “Initialize with explicit chain metadata and default account”
import { createMemoryClient, PREFUNDED_ACCOUNTS } from 'tevm'
import { optimism } from 'tevm/common'
const client = createMemoryClient({
chainId: optimism.id,
common: optimism,
account: PREFUNDED_ACCOUNTS[0],
})
try {
await client.tevmReady()
console.log(await client.getChainId()) // 10
} finally {
await client.tevmClose()
}
import { createMemoryClient, createZevmEngine } from 'tevm'
const engine = createZevmEngine({ mining: { auto: false } })
const client = createMemoryClient({ engine })
try {
await client.tevmReady()
console.log(await client.getBlockNumber()) // 0n
} finally {
// Also closes `engine`
await client.tevmClose()
}
  • Synchronous TypeError on construction: chainId, mining.interval, and fork.blockNumber must be unsigned safe integers (Number.isSafeInteger(v) && v >= 0). Passing floats, negative numbers, or a chainId that differs from common.id throws immediately.
  • Viem action throws MethodNotFound (-32601): createMemoryClient attaches viem’s full public, wallet, and Anvil action sets, but exact RPC method support is governed by ZEVM. See JSON-RPC reference.
  • Process hangs on exit: You left an open client (especially with interval mining enabled) or an active 'block' listener without calling await client.tevmClose().