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 |
-
Install dependencies
Section titled “Install dependencies”Terminal window pnpm add tevm viem -
Use
Section titled “Use createMemoryClient() (batteries-included)”createMemoryClient()(batteries-included)Pass a default
accountfromPREFUNDED_ACCOUNTSso viem wallet actions (sendTransaction,writeContract,deployContract) work without repeatingaccounton every call:viem-memory-client.ts import { createMemoryClient, parseAbi, parseEther, PREFUNDED_ACCOUNTS } from 'tevm'const [alice, bob] = PREFUNDED_ACCOUNTSconst client = createMemoryClient({ account: alice })try {// 1. Standard viem test action: mutate an account balanceawait 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 balanceconst 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 engineconst 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 withclient.setBalance()is immediately visible toclient.getBalance(),client.tevmGetAccount(), and any attached@tevm/server. -
Compose a minimal tree-shakeable viem client
Section titled “Compose a minimal tree-shakeable viem client”Use
createClientfromviemwithcreateTevmTransport()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()}viem-standalone.ts import { createClient } from 'viem'import { getBalance } from 'viem/actions'import { createTevmTransport } from 'tevm/memory-client'import { tevmDeal } from 'tevm/actions'const client = createClient({cacheTime: 0,transport: createTevmTransport(),})try {const target = '0x1111111111111111111111111111111111111111'await tevmDeal(client, { address: target, amount: 5n * 10n ** 18n })console.log(await getBalance(client, { address: target })) // 5000000000000000000n} finally {await client.transport.tevm.close()} -
Handle contract errors cleanly
Section titled “Handle contract errors cleanly”Viem wraps RPC failures in
ContractFunctionExecutionError(with the underlyingNativeRpcErrorascause), whiletevmCall/tevmContractthrowNativeRpcErrordirectly: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()}
Next steps
Section titled “Next steps”- Memory client reference — complete options and method map for
createMemoryClient. - Testing with Vitest — isolate contract tests with snapshots and fixtures.
- Forking upstream chains — run viem reads and writes against forked mainnet state.

