Skip to content
LogoLogo

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 {  } 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 { ,  } from 'viem/nonce'
 
const  = ({
  : ()
})

Integration with Local Accounts

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

// @filename: config.ts
import { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
 
export const client = createWalletClient({
  chain: mainnet,
  transport: http(),
})
// @filename: example.ts
// ---cut---
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 { ,  } from 'viem/nonce'
 
const  = ({
  : () 
})