Accounts, storage & snapshots
TEVM gives you direct control over account state without sending transactions — ideal for setting up test fixtures, injecting runtime bytecode, rewinding state between test cases, and serializing snapshots to disk.
Read and mutate accounts
Section titled “Read and mutate accounts”Use tevmGetAccount, tevmSetAccount, and tevmDeal (or viem’s Anvil test actions setBalance, setCode, setNonce, setStorageAt):
import { createMemoryClient, parseEther, toHex } from 'tevm'
const client = createMemoryClient()const target = '0x0000000000000000000000000000000000000123'
try { await client.tevmSetAccount({ address: target, balance: parseEther('25'), nonce: 7n, // Minimal runtime bytecode: PUSH1 0x2a; PUSH1 0x00; MSTORE; PUSH1 0x20; PUSH1 0x00; RETURN deployedBytecode: '0x602a60005260206000f3', storage: { '0x00': toHex(99n), }, })
const account = await client.tevmGetAccount({ address: target }) console.log({ balance: account.balance, nonce: account.nonce, code: account.deployedBytecode, })
// Quick balance top-up via anvil_setBalance await client.tevmDeal({ address: target, amount: parseEther('100') }) console.log('New balance:', await client.getBalance({ address: target }))} finally { await client.tevmClose()}Storage slot padding
Section titled “Storage slot padding”Storage keys and values in tevmSetAccount({ storage }) accept hex strings up to 32 bytes; TEVM left-pads them to full 32-byte EVM words on the wire automatically.
To read a specific slot back:
const slot0 = await client.getStorageAt({ address: target, slot: '0x0000000000000000000000000000000000000000000000000000000000000000',})Fast rollback with snapshot() and revert()
Section titled “Fast rollback with snapshot() and revert()”Take a snapshot after expensive setup (such as deploying contracts or warming a fork cache), then revert after each test in microseconds:
import { createMemoryClient, parseEther } from 'tevm'
const client = createMemoryClient()const user = '0x1111111111111111111111111111111111111111'
try { await client.tevmDeal({ address: user, amount: parseEther('10') }) const snapId = await client.snapshot()
// Mutate state freely during a test await client.tevmDeal({ address: user, amount: parseEther('999') }) await client.tevmMine({ blocks: 3 }) console.log('During test:', await client.getBalance({ address: user }), await client.getBlockNumber()) // 999000000000000000000n 3n
// Rewind to the exact pre-test state and block height await client.revert({ id: snapId }) console.log('After revert:', await client.getBalance({ address: user }), await client.getBlockNumber()) // 10000000000000000000n 0n} finally { await client.tevmClose()}Portable state blobs (tevmDumpState / tevmLoadState)
Section titled “Portable state blobs (tevmDumpState / tevmLoadState)”Serialize an entire chain’s state to an opaque hex blob (0x...) with tevmDumpState() and hydrate a separate engine with tevmLoadState(blob):
import { createMemoryClient, parseEther } from 'tevm'
const source = createMemoryClient()const target = createMemoryClient()const addr = '0x2222222222222222222222222222222222222222'
try { await source.tevmDeal({ address: addr, amount: parseEther('42') })
// Export opaque ZEVM state blob const stateBlob = await source.tevmDumpState()
// Restore into a completely separate engine await target.tevmLoadState(stateBlob) console.log('Restored balance:', await target.getBalance({ address: addr })) // 42000000000000000000n} finally { await source.tevmClose() await target.tevmClose()}Next steps
Section titled “Next steps”- Testing with Vitest — combine snapshots with Vitest lifecycle hooks.
- Actions reference — exact TypeScript signatures for
tevmSetAccount,tevmGetAccount,tevmDeal,tevmDumpState, andtevmLoadState.

