Skip to content

createNonceManager

Creates a Nonce Manager for automatic nonce generation

Creates a new Nonce Manager instance to be used with a Local Account. The Nonce Manager is used to automatically manage & generate nonces for transactions.

Import

import { createNonceManager } from 'viem/nonce'

Usage

A Nonce Manager can be instantiated with the createNonceManager function with a provided source.

The example below demonstrates how to create a Nonce Manager with a JSON-RPC source (ie. uses eth_getTransactionCount as the source of truth).

import { createNonceManager, jsonRpc } from 'viem/nonce'
 
const nonceManager = createNonceManager({
  source: jsonRpc()
})

Integration with Local Accounts

A nonceManager can be passed as an option to Local Accounts to automatically manage nonces for transactions.

example.ts
import { privateKeyToAccount, nonceManager } from 'viem/accounts'
import { client } from './config'
 
const account = privateKeyToAccount('0x...', { nonceManager }) 
 
const hashes = await Promise.all([ 
↓ nonce = 0
client.sendTransaction({ account, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: parseEther('0.1'), }),
↓ nonce = 1
client.sendTransaction({ account, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: parseEther('0.2'), }), ])

Return Type

NonceManager

The Nonce Manager.

Parameters

source

  • Type: NonceManagerSource

The source of truth for the Nonce Manager.

Available sources:

  • jsonRpc
import { createNonceManager, jsonRpc } from 'viem/nonce'
 
const nonceManager = createNonceManager({
  source: jsonRpc() 
})