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

Mining & block production

Mining turns pending transactions in the txpool into canonical blocks with receipts and logs. Tevm supports three modes: auto (default), manual, and interval.

Block Production Modes
1. Auto (default)mining: { auto: true }tx1 → Block #1tx2 → Block #2tx3 → Block #32. Manualmining: { auto: false }tx1 queuedtx2 queuedtevmMine() → Block #1 [tx1, tx2]3. Interval (Zig thread)mining: { auto: false, interval: 2 }t = 2s · Block #1t = 4s · Block #2t = 6s · Block #3Always call client.tevmClose() (or engine.close()) to stop interval timers and release native handles.
Automining is on by default. Switch to manual mining for multi-transaction blocks, or interval mining for wall-clock block cadence.

Configuring mining at startup

import { createMemoryClient } from 'tevm'
 
// 1. Auto (default): each transaction mines its own block immediately
const autoClient = createMemoryClient()
 
// 2. Manual: transactions stay in the txpool until you mine explicitly
const manualClient = createMemoryClient({
  mining: { auto: false },
})
 
// 3. Interval: background Zig thread mines a block every N whole seconds
const intervalClient = createMemoryClient({
  mining: { auto: false, interval: 2 },
})
ModeConfigWhen blocks are producedTypical use
Auto{ auto: true } (default)Immediately on every transactionUnit tests, scripts, simple flows
Manual{ auto: false }Only when tevmMine / mine / evm_mine is calledMulti-tx blocks, mempool inspection, exact block timing
Interval{ auto: false, interval: N }Every N whole seconds on a native thread (0 disables timer)Local dev servers, frontend polling, time-dependent apps

Submitting and mining manually

With mining: { auto: false }, submitted transactions wait in the mempool and getTransactionReceipt returns null until you mine:

import { createMemoryClient, parseEther, PREFUNDED_ACCOUNTS } from 'tevm'
 
const client = createMemoryClient({
  mining: { auto: false },
})
 
try {
  const { txHash } = await client.tevmCall({
    from: PREFUNDED_ACCOUNTS[0].address,
    to: '0x1111111111111111111111111111111111111111',
    value: parseEther('1'),
    addToMempool: true,
  })
 
  // Mine 1 block, advancing timestamp by 12 seconds
  await client.tevmMine({ blocks: 1, interval: 12 })
 
  const receipt = await client.getTransactionReceipt({ hash: txHash! })
  console.log(receipt.blockNumber, receipt.status) // 1n 'success'
} finally {
  await client.tevmClose()
}

tevmMine vs. viem mine

Both methods accept the same parameter names and forward to anvil_mine:

// TEVM action (defaults: blocks = 1, interval = 1)
await client.tevmMine({ blocks: 2, interval: 12 })
 
// viem Anvil test action
await client.mine({ blocks: 2, interval: 12 })

Here interval is the timestamp delta in seconds between each mined block in that call — distinct from mining.interval, which sets the background wall-clock timer.

Switching modes at runtime

Toggle automining or interval mining on a running engine through standard Anvil/EVM RPC methods or viem test actions:

import { createMemoryClient } from 'tevm'
 
const client = createMemoryClient()
try {
  // Disable automining
  await client.setAutomine(false)
 
  // Start mining every 5 seconds
  await client.setIntervalMining({ interval: 5 })
 
  // Stop the interval timer (0 disables it)
  await client.setIntervalMining({ interval: 0 })
 
  // Re-enable automining
  await client.setAutomine(true)
} finally {
  await client.tevmClose()
}

Controlling block timestamps

Use viem test actions (or evm_setNextBlockTimestamp / evm_increaseTime) to test timelocks, vesting, and hardfork activation timestamps:

import { createMemoryClient } from 'tevm'
 
const client = createMemoryClient()
try {
  await client.setNextBlockTimestamp({ timestamp: 2_000_000_000n })
  await client.mine({ blocks: 1 })
 
  const block = await client.getBlock({ blockTag: 'latest' })
  console.log(block.timestamp) // 2000000000n
} finally {
  await client.tevmClose()
}

Observing mined blocks

Subscribe to block on client.transport.tevm.events. Listeners fire for both explicit mine() calls and blocks mined autonomously by ZEVM's interval thread:

import { once } from 'node:events'
import { createMemoryClient } from 'tevm'
 
const client = createMemoryClient({
  mining: { auto: false, interval: 1 },
})
 
// The internal block poller is unref'd; keep the event loop alive in a standalone script.
const keepAlive = setTimeout(() => {}, 3_000)
 
try {
  await client.tevmReady()
  const [block] = await once(client.transport.tevm.events, 'block')
  console.log('Interval-mined block:', block.number) // '0x1'
} finally {
  clearTimeout(keepAlive)
  await client.tevmClose()
}

Next: Call, contract & deploy · Engine API · Testing with Vitest