Skip to content

Tutorial: Viem

TEVM is built directly on viem v2. Every MemoryClient is a real viem client with publicActions, walletActions, testActions({ mode: 'anvil' }), and tevmViemActions() attached — all routed through one serialized ZevmEngine.

Choose between two setups:

Setup Best for Bundle footprint
createMemoryClient() Application code, scripts, integration tests Batteries-included (public + wallet + test + TEVM actions)
createClient({ transport: createTevmTransport() }) Tree-shaken apps or custom decorator stacks Minimal — import only the actions you call
  1. Terminal window
    pnpm add tevm viem
  2. Use createMemoryClient() (batteries-included)

    Section titled “Use createMemoryClient() (batteries-included)”

    Pass a default account from PREFUNDED_ACCOUNTS so viem wallet actions (sendTransaction, writeContract, deployContract) work without repeating account on every call:

    viem-memory-client.ts
    import { createMemoryClient, parseAbi, parseEther, PREFUNDED_ACCOUNTS } from 'tevm'
    const [alice, bob] = PREFUNDED_ACCOUNTS
    const client = createMemoryClient({ account: alice })
    try {
    // 1. Standard viem test action: mutate an account balance
    await client.setBalance({
    address: bob.address,
    value: parseEther('50'),
    })
    // 2. Standard viem wallet action: send ETH (automined immediately)
    const hash = await client.sendTransaction({
    to: bob.address,
    value: parseEther('2'),
    })
    // 3. Standard viem public actions: inspect receipt and balance
    const receipt = await client.getTransactionReceipt({ hash })
    const bobBalance = await client.getBalance({ address: bob.address })
    console.log({
    status: receipt.status,
    blockNumber: receipt.blockNumber,
    bobBalance,
    })
    // { status: 'success', blockNumber: 1n, bobBalance: 52000000000000000000n }
    // 4. TEVM action: deploy and call a contract on the same shared engine
    const deployed = await client.tevmDeploy({
    bytecode: '0x600a600c600039600a6000f3602a60005260206000f3',
    })
    const answer = await client.readContract({
    address: deployed.createdAddress!,
    abi: parseAbi(['function answer() view returns (uint256)']),
    functionName: 'answer',
    })
    console.log('answer():', answer) // 42n
    } finally {
    await client.tevmClose()
    }

    Because every action layer shares client.transport.tevm, a balance mutated with client.setBalance() is immediately visible to client.getBalance(), client.tevmGetAccount(), and any attached @tevm/server.

  3. Compose a minimal tree-shakeable viem client

    Section titled “Compose a minimal tree-shakeable viem client”

    Use createClient from viem with createTevmTransport() and extend only the action sets you need:

    viem-minimal.ts
    import { createClient, publicActions } from 'viem'
    import { foundry } from 'viem/chains'
    import { createTevmTransport, tevmViemActions } from 'tevm/memory-client'
    const client = createClient({
    chain: foundry,
    cacheTime: 0,
    transport: createTevmTransport({ chainId: foundry.id }),
    })
    .extend(publicActions)
    .extend(tevmViemActions())
    try {
    await client.tevmReady()
    console.log('Block:', await client.getBlockNumber()) // 0n
    } finally {
    await client.tevmClose()
    }
  4. Viem wraps RPC failures in ContractFunctionExecutionError (with the underlying NativeRpcError as cause), while tevmCall / tevmContract throw NativeRpcError directly:

    viem-errors.ts
    import { createMemoryClient, NativeRpcError, parseAbi } from 'tevm'
    const client = createMemoryClient()
    try {
    const target = '0x0000000000000000000000000000000000000999'
    await client.tevmSetAccount({
    address: target,
    deployedBytecode: '0x60006000fd', // REVERT
    })
    await client.tevmContract({
    address: target,
    abi: parseAbi(['function fail() view']),
    functionName: 'fail',
    })
    } catch (error) {
    if (error instanceof NativeRpcError && error.code === 3) {
    console.log('Reverted with data:', error.data)
    } else {
    throw error
    }
    } finally {
    await client.tevmClose()
    }