Skip to content
LogoLogo

Get Nonce

nonce.get

Gets the nonce for an account and nonce key.

Nonce key 0 is reserved for protocol nonces. Use keys greater than 0 for 2D nonces.

Usage

import {  } from './viem.config'
 
const  = await ..({
  : 1n,
})
 
.('Nonce:', )
Nonce: 42n

Recipes

Prepare a Transaction on a Dedicated Lane

Read the lane's current nonce and pass both nonce and nonceKey to transaction.prepare, since the node does not fill keyed nonces for manually prepared requests.

import {  } from './viem.config'
 
const  = 7n // this worker's payout lane
const  = await ..({  })
 
const {  } = await ..({
  : '0xcafebabe00000000000000000000000000000001',
  : (),
  ,
  : '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
})

Reconcile Dispatched Payouts from a Monitor

Pass account to audit a lane of an account you do not sign for, such as a payout dispatcher's hot wallet: a lane's nonce counts its confirmed transactions.

import {  } from './viem.config'
 
const  = 45n // payouts submitted, from your job queue
 
const  = await ..({
  : '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
  : 7n,
})
 
.(`${ - } payouts pending`)
3 payouts pending

Return Value

type ReturnType = bigint

The nonce value for the account and nonce key.

Parameters

account

  • Type: Account | Address
  • Default: client.account

Account to read the nonce of. Defaults to the Client's account.

nonceKey

  • Type: bigint

Nonce key to read. Key 0 is reserved for protocol nonces.

blockNumber (optional)

  • Type: bigint

Block number to read the state from.

blockOverrides (optional)

  • Type: BlockOverrides

Block overrides to apply to the state.

blockTag (optional)

  • Type: BlockTag

Block tag to read the state from.

stateOverride (optional)

  • Type: StateOverride

State override to apply.