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 Modes

Mining controls when pending transactions become part of the canonical chain. tevm@1.0.0-rc.151 supports manual, automatic, and interval mining.

Configure a Mode

import { createMemoryClient } from 'tevm'
 
const manualClient = createMemoryClient({
  miningConfig: { type: 'manual' },
})
 
const autoClient = createMemoryClient({
  miningConfig: { type: 'auto' },
})
 
const intervalClient = createMemoryClient({
  miningConfig: {
    type: 'interval',
    blockTime: 12, // seconds
  },
})
  • Manual keeps submitted transactions in the txpool until a mine action runs. Use it for deterministic tests.
  • Auto mines a block after each submitted transaction. Use it for fast local development.
  • Interval mines on a timer. blockTime is measured in seconds; 0 disables the timer while preserving manual mining.

Gas-threshold mining is not a supported rc.151 mining mode.

Submit and Mine Manually

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

interval on tevmMine is the number of seconds added between the timestamps of blocks created by that call. It is separate from the interval mining policy's blockTime.

Tevm and Viem Mining Actions

The memory client exposes both APIs:

import { createMemoryClient } from 'tevm'
 
const client = createMemoryClient()
 
// Tevm action
await client.tevmMine({ blockCount: 2, interval: 1 })
 
// viem's Anvil-compatible test action
await client.mine({ blocks: 2, interval: 1 })

Do not mix the parameter names: Tevm uses blockCount; viem uses blocks.

Change the Policy at Runtime

The low-level node owns the current policy and the interval timer.

import { createTevmNode } from 'tevm'
 
const node = createTevmNode({
  miningConfig: { type: 'manual' },
})
 
node.setMiningConfig({ type: 'interval', blockTime: 5 })
node.setMiningConfig({ type: 'auto' })
node.setMiningConfig({ type: 'manual' })
 
await node.close()

Call node.close() when an application is finished with an interval-mining node so its timer is stopped.

Observe Mining

The Tevm mine action accepts execution callbacks. Each callback must call next?.() to continue.

import { createMemoryClient } from 'tevm'
 
const client = createMemoryClient()
const mined: bigint[] = []
 
await client.tevmMine({
  blockCount: 2,
  onBlock(block, next) {
    mined.push(block.header.number)
    next?.()
  },
})
 
console.log(mined)

For application-level results, prefer viem queries such as getBlock, getTransaction, getTransactionReceipt, and getLogs after mining.

Related

Transaction Pool · Receipts and Logs · Call API