2.7 KiB
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.
:::warning A Nonce Manager can only be used with Local Accounts (ie. Private Key, Mnemonic, etc).
For JSON-RPC Accounts (ie. Browser Extension, WalletConnect, Backend, etc), the Wallet or Backend will manage the nonces. :::
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()
})
:::tip
Viem also exports a default nonceManager instance that you can use directly.
import { nonceManager } from 'viem'
:::
Integration with Local Accounts
A nonceManager can be passed as an option to Local Accounts to automatically manage nonces for transactions.
:::code-group
import { privateKeyToAccount, nonceManager } from 'viem/accounts' // [!code focus]
import { client } from './config'
const account = privateKeyToAccount('0x...', { nonceManager }) // [!code focus]
const hashes = await Promise.all([ // [!code focus]
// @log: ↓ nonce = 0
client.sendTransaction({ // [!code focus]
account, // [!code focus]
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // [!code focus]
value: parseEther('0.1'), // [!code focus]
}), // [!code focus]
// @log: ↓ nonce = 1
client.sendTransaction({ // [!code focus]
account, // [!code focus]
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // [!code focus]
value: parseEther('0.2'), // [!code focus]
}), // [!code focus]
]) // [!code focus]
import { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const client = createWalletClient({
chain: mainnet,
transport: http(),
})
:::
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() // [!code focus]
})