@zerodev/sdk
Version:
A utility library for working with ERC-4337
78 lines (73 loc) • 2.11 kB
text/typescript
// Copied from: https://github.com/pimlicolabs/permissionless.js/blob/main/packages/permissionless/actions/public/getAccountNonce.ts
import type { Address, Client } from "viem"
import { readContract } from "viem/actions"
import { getAction } from "viem/utils"
export type GetAccountNonceParams = {
address: Address
entryPointAddress: Address
key?: bigint
}
/**
* Returns the nonce of the account with the entry point.
*
* - Docs: https://docs.pimlico.io/permissionless/reference/public-actions/getAccountNonce
*
* @param client {@link client} that you created using viem's createPublicClient.
* @param args {@link GetAccountNonceParams} address, entryPoint & key
* @returns bigint nonce
*
* @example
* import { createPublicClient } from "viem"
* import { getAccountNonce } from "permissionless/actions"
*
* const client = createPublicClient({
* chain: goerli,
* transport: http("https://goerli.infura.io/v3/your-infura-key")
* })
*
* const nonce = await getAccountNonce(client, {
* address,
* entryPoint,
* key
* })
*
* // Return 0n
*/
export const getAccountNonce = async (
client: Client,
args: GetAccountNonceParams
): Promise<bigint> => {
const { address, entryPointAddress, key = BigInt(0) } = args
return await getAction(
client,
readContract,
"readContract"
)({
address: entryPointAddress,
abi: [
{
inputs: [
{
name: "sender",
type: "address"
},
{
name: "key",
type: "uint192"
}
],
name: "getNonce",
outputs: [
{
name: "nonce",
type: "uint256"
}
],
stateMutability: "view",
type: "function"
}
],
functionName: "getNonce",
args: [address, key]
})
}