Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Memory Client

Install the rc.151 client through the main package:

npm install tevm@1.0.0-rc.151 viem

createMemoryClient returns synchronously and combines a Tevm transport with viem public, wallet, test, and Tevm actions.

Create a Client

import { createMemoryClient } from 'tevm'
 
const client = createMemoryClient({
  miningConfig: { type: 'manual' },
})
 
await client.tevmReady()
console.log(await client.getBlockNumber())

tevmReady() is optional for local clients and useful for eagerly initializing a fork.

Action Groups

  • Public: getBalance, getBlock, getTransaction, getTransactionReceipt, getLogs, readContract
  • Wallet: sendTransaction, writeContract, deployContract, signing actions
  • Test: setBalance, setCode, setStorageAt, mine, snapshot, revert
  • Tevm: tevmCall, tevmContract, tevmDeploy, tevmGetAccount, tevmSetAccount, tevmMine, tevmDumpState, tevmLoadState

State and Mining

import { createMemoryClient, PREFUNDED_ACCOUNTS } from 'tevm'
 
const client = createMemoryClient({
  miningConfig: { type: 'manual' },
})
const recipient = '0x1111111111111111111111111111111111111111'
 
await client.tevmSetAccount({
  address: recipient,
  balance: 10n,
})
 
const account = await client.tevmGetAccount({ address: recipient })
 
const { txHash } = await client.tevmCall({
  from: PREFUNDED_ACCOUNTS[0].address,
  to: recipient,
  value: 1n,
  addToMempool: true,
})
 
if (!txHash) throw new Error('transaction was not added to the txpool')
 
await client.tevmMine({ blockCount: 1 })
const receipt = await client.getTransactionReceipt({ hash: txHash })
 
console.log(account.balance, receipt.status)

Use client.mine({ blocks: 1 }) for the viem test action and client.tevmMine({ blockCount: 1 }) for the Tevm action.

Forking

import { createMemoryClient, http } from 'tevm'
import { mainnet } from 'tevm/common'
 
const client = createMemoryClient({
  common: mainnet,
  fork: {
    transport: http('https://ethereum-rpc.publicnode.com')({}),
    blockTag: 20_000_000n,
  },
})
 
await client.tevmReady()
console.log(await client.getBlockNumber())

Persistence

createSyncStoragePersister is exported from tevm and tevm/sync-storage-persister. Pass the persister through client options when an application needs state hydration and synchronous persistence.

Low-Level Node

The underlying node is available as client.transport.tevm. This is an advanced escape hatch:

import { createMemoryClient } from 'tevm'
 
const client = createMemoryClient()
const node = client.transport.tevm
const vm = await node.getVm()
 
console.log((await vm.blockchain.getCanonicalHeadBlock()).header.number)

Prefer the client actions unless direct VM, txpool, or receipt-manager access is required.