Skip to content

Mining & block production

TEVM supports three block-production modes controlled by ZEVM’s native miner: automining (default), manual mining, and interval mining.

Mode Configuration When blocks are produced Best for
Automine (default) { mining: { auto: true } } or omitted Immediately after each submitted transaction Fast scripts, simple unit tests, interactive flows
Manual { mining: { auto: false } } Only when you call tevmMine(), client.mine(), or evm_mine Batching multiple transactions in one block, txpool inspection
Interval { mining: { auto: false, interval: 2 } } Automatically every N whole seconds on a background Zig thread Local dev servers simulating live block cadence

Disable automining to queue multiple transactions in the mempool and commit them into one block:

manual-mining.ts
import { createMemoryClient, parseEther, PREFUNDED_ACCOUNTS } from 'tevm'
const [alice, bob, charlie] = PREFUNDED_ACCOUNTS
const client = createMemoryClient({
mining: { auto: false },
})
try {
const tx1 = await client.tevmCall({
from: alice.address,
to: bob.address,
value: parseEther('1'),
addToMempool: true,
})
const tx2 = await client.tevmCall({
from: alice.address,
to: charlie.address,
value: parseEther('2'),
addToMempool: true,
})
// Neither receipt exists before mining
console.log(await client.request({ method: 'eth_getTransactionReceipt', params: [tx1.txHash!] }))
// null
// Mine 1 block containing both pending transactions
await client.tevmMine({ blocks: 1 })
const block = await client.getBlock({ blockNumber: 1n })
console.log('Mined tx count:', block.transactions.length) // 2
console.log('Hashes:', [tx1.txHash, tx2.txHash])
} finally {
await client.tevmClose()
}

Mine multiple blocks with timestamp spacing

Section titled “Mine multiple blocks with timestamp spacing”

Pass blocks and interval (seconds between consecutive blocks) to tevmMine:

// Mine 5 blocks spaced 12 seconds apart
await client.tevmMine({ blocks: 5, interval: 12 })

To advance time explicitly before the next block, use viem’s Anvil test actions (increaseTime / setNextBlockTimestamp):

// Jump 1 hour forward, then mine 1 block
await client.increaseTime({ seconds: 3600 })
await client.tevmMine({ blocks: 1 })
// Or pin the exact Unix timestamp of the next block
await client.setNextBlockTimestamp({ timestamp: 1_800_000_000n })
await client.tevmMine({ blocks: 1 })

Set mining: { auto: false, interval: 2 } to mine a block every 2 seconds on ZEVM’s background thread. Attach a 'block' listener on client.transport.tevm.events to react whenever chain height advances:

interval-mining.ts
import { createMemoryClient } from 'tevm'
const client = createMemoryClient({
mining: { auto: false, interval: 2 },
})
try {
await client.tevmReady()
await new Promise<void>((resolve) => {
client.transport.tevm.events.on('block', (block) => {
console.log('Observed block:', (block as { number: string }).number)
resolve()
})
})
} finally {
// Stops the background interval thread and releases native state
await client.tevmClose()
}

Switch modes on a running client via standard Anvil/EVM JSON-RPC methods:

// Turn automining off at runtime
await client.setAutomine(false)
// Check current automine state
const isAuto = await client.getAutomine()
// Enable 3-second interval mining (pass 0 to stop)
await client.setIntervalMining({ interval: 3 })