Skip to content

Architecture

TEVM is structured as a thin TypeScript host layer over a native Zig (or WebAssembly) Ethereum node. Every host API — MemoryClient, TevmProvider, @tevm/server, @tevm/mcp, and standalone actions — routes through one serialized boundary: ZevmEngine.

Layer Package / repo Owns
EVM interpreter Guillotine Mini (Zig) Opcode execution, hardfork rules, gas accounting, debug_traceCall tracer
Primitives & state Voltaire (Zig) Accounts, bytecode, storage slots, crypto, RLP, precompiles
Node runtime ZEVM (Zig) NodeRuntime, chain, mempool, mining, receipts, filters, snapshots, fork backend, JSON-RPC router
Engine bridge @tevm/node Native Node-API or WASM backend loader, serialized FIFO queue, NativeRpcError, EngineEventEmitter, consoleLog drain
Viem client @tevm/memory-client createMemoryClient, createTevmTransport, tevmViemActions (cacheTime: 0)
Actions @tevm/actions Browser-safe typed RPC helpers (tevmCall, tevmContract, tevmDeploy, tevmSetAccount, …)
Servers @tevm/server HTTP POST/batch, WebSocket subscriptions, Unix IPC framing, Express & Next.js adapters

Each ZevmEngine owns one isolated backend instance. Two engines never share state unless you explicitly pass the same engine into multiple clients.

createZevmEngine(options) (also exported as createTevmNode) wraps either the native Node-API addon, @evmts/zevm/browser WASM reactor, or a custom ZevmBackend with five members:

Member Contract
ready() Resolves once initial fork, chain ID, and mining configuration finishes. Rejects and frees backend state if initialization fails.
request({ method, params }) Enqueues one JSON-RPC call, returns the parsed result, or throws NativeRpcError (code, message, optional data).
rpc(jsonString) Enqueues raw JSON-RPC 2.0 wire text (single request, batch array, or notification) and returns the exact response JSON string, or null for notifications.
events Zero-dependency EngineEventEmitter emitting request, response, block, consoleLog, error, and close.
close() Stops the block-poll timer, releases the backend handle, emits close, and removes all listeners. Idempotent.

Callers may invoke request() and rpc() concurrently with Promise.all. Inside ZevmEngine, an internal FIFO promise queue executes every call against the backend strictly one at a time. This guarantees deterministic state transitions without requiring external locks.

A backend response must be valid JSON-RPC 2.0 containing exactly one of result or error, and error must carry an integer code and a string message. Malformed responses reject as a transport error rather than a NativeRpcError.

When at least one 'block' listener is registered on engine.events, ZevmEngine checks eth_blockNumber after dispatched calls and on an unreferenced 50 ms timer (at most one poll in flight at a time). Removing all 'block' listeners stops the timer immediately. If more than 64 blocks advance between two polls, only the tip block is emitted.

createMemoryClient(options) builds a viem client with cacheTime: 0 and createTevmTransport(options), then extends it with:

  1. viem publicActions (getBlock, getBalance, getLogs, getTransactionReceipt, readContract, …)
  2. viem walletActions (sendTransaction, writeContract, deployContract, …)
  3. viem testActions({ mode: 'anvil' }) (setBalance, setCode, mine, snapshot, revert, increaseTime, …)
  4. tevmViemActions() (tevmReady, tevmClose, tevmCall, tevmContract, tevmDeploy, tevmSetAccount, tevmGetAccount, tevmDeal, tevmMine, tevmDumpState, tevmLoadState)

Every layer dispatches through client.transport.tevm, so state changes made with one surface are immediately visible to all others.

  1. Simulate (eth_call, tevmCall) — executes against current state (plus optional stateOverride or code), discards state writes, and returns rawData. Reverts throw NativeRpcError with code === 3.
  2. Queue (addToMempool: true with mining: { auto: false }) — submits via eth_sendTransaction, leaves the transaction in ZEVM’s txpool, and returns { rawData: '0x', txHash }. Receipts return null until mined.
  3. Commit (addToBlockchain: true, automine, or tevmMine()) — executes pending transactions in Guillotine Mini, updates Voltaire state, appends a canonical block, and indexes receipts and logs.

Native and WebAssembly handles live outside JavaScript’s garbage collector. Always release what you create:

  • MemoryClient: await client.tevmClose() (also closes an externally supplied engine).
  • ZevmEngine: await engine.close().
  • TevmProvider (@tevm/ethers): provider.destroy().
  • createServer / createIpcServer: close the server first, then call await client.tevmClose().

Calling request() or rpc() after close() rejects immediately with Error('engine is closed').