Skip to content

Quickstart

Embed an isolated Ethereum node directly inside your TypeScript or JavaScript process — no Docker container, background Anvil process, or port management required.

By the end of this tutorial, you will have run a local ETH transfer, deployed and called contract bytecode, caught a structured revert error, and forked an upstream network at a pinned block.

  • Node.js 22.12 or later (Node.js 24 is tested in CI) for the native Node-API addon, or a modern browser/bundler for WebAssembly execution.
  • A package manager (pnpm, npm, yarn, or bun).
  1. Terminal window
    pnpm add tevm viem
  2. Create quickstart.mjs and run it with node quickstart.mjs:

    quickstart.mjs
    import { createMemoryClient, parseEther, PREFUNDED_ACCOUNTS } from 'tevm'
    const client = createMemoryClient({
    mining: { auto: false },
    })
    try {
    const sender = PREFUNDED_ACCOUNTS[0].address
    const recipient = '0x1111111111111111111111111111111111111111'
    // Queue a transfer in the mempool without mining yet
    const { txHash } = await client.tevmCall({
    from: sender,
    to: recipient,
    value: parseEther('1'),
    addToMempool: true,
    })
    // Commit the pending transaction into block 1
    await client.tevmMine({ blocks: 1 })
    // Query the receipt and updated balance using standard viem actions
    const receipt = await client.getTransactionReceipt({ hash: txHash })
    const balance = await client.getBalance({ address: recipient })
    console.log({ status: receipt.status, balance })
    } finally {
    // Always release the native ZEVM handle when finished
    await client.tevmClose()
    }

    Expected output:

    { status: 'success', balance: 1000000000000000000n }

    Here is what happened:

    • createMemoryClient({ mining: { auto: false } }) created an isolated ZEVM chain with chain ID 31337, block height 0, and 10 prefunded accounts holding 10,000 ETH each.
    • tevmCall({ ..., addToMempool: true }) submitted the transfer via eth_sendTransaction and returned its txHash.
    • tevmMine({ blocks: 1 }) ran anvil_mine, committed the state transition, and indexed the receipt.
    • tevmClose() freed the native engine handle.
  3. Use tevmDeploy to deploy creation bytecode and tevmContract for ABI-typed reads and writes:

    deploy.mjs
    import { createMemoryClient, parseAbi } from 'tevm'
    const client = createMemoryClient()
    try {
    // Minimal creation bytecode deploying a contract that returns uint256(42)
    const deployed = await client.tevmDeploy({
    bytecode: '0x600a600c600039600a6000f3602a60005260206000f3',
    })
    const result = await client.tevmContract({
    address: deployed.createdAddress,
    abi: parseAbi(['function answer() view returns (uint256)']),
    functionName: 'answer',
    })
    console.log('Deployed at:', deployed.createdAddress)
    console.log('answer() =>', result.data)
    } finally {
    await client.tevmClose()
    }

    Expected output:

    Deployed at: 0x5fbdb2315678afecb367f032d93f642f64180aa3
    answer() => 42n
  4. When a call reverts, TEVM throws a NativeRpcError with JSON-RPC error code 3 and the raw revert payload on error.data:

    revert.mjs
    import { createMemoryClient, NativeRpcError } from 'tevm'
    const client = createMemoryClient()
    try {
    // Runtime bytecode: PUSH1 0x00; PUSH1 0x00; REVERT
    await client.tevmCall({ code: '0x60006000fd' })
    } catch (error) {
    if (error instanceof NativeRpcError) {
    console.log({ code: error.code, message: error.message, data: error.data })
    // { code: 3, message: 'execution reverted', data: '0x' }
    } else {
    throw error
    }
    } finally {
    await client.tevmClose()
    }
  5. Pass fork.url and pin fork.blockNumber for deterministic reads against live mainnet state. Missing accounts, code, and storage load lazily on cache miss while local writes stay in-process:

    fork.mjs
    import { createMemoryClient } from 'tevm'
    import { mainnet } from 'tevm/common'
    const client = createMemoryClient({
    chainId: mainnet.id,
    common: mainnet,
    fork: {
    url: 'https://ethereum-rpc.publicnode.com',
    blockNumber: 20_000_000,
    },
    })
    try {
    await client.tevmReady()
    const vitalik = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
    const balance = await client.getBalance({ address: vitalik })
    console.log('Forked balance:', balance)
    // Mutate locally — never touches mainnet
    await client.tevmDeal({ address: vitalik, amount: balance + 1n })
    console.log('Local balance:', await client.getBalance({ address: vitalik }))
    } finally {
    await client.tevmClose()
    }