import type { BaseErrorType } from 'viem' import { Actions } from 'viem/tempo' import type { Config } from '../../createConfig.js' import type { ChainIdParameter } from '../../types/properties.js' import type { PartialBy } from '../../types/utils.js' import type { QueryOptions, QueryParameter } from './utils.js' import { filterQueryOptions } from './utils.js' /** * Gets the nonce for an account and nonce key. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempo], * transports: { * [tempo.id]: http(), * }, * }) * * const nonce = await Actions.nonce.getNonce(config, { * account: '0x...', * nonceKey: 1n, * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The nonce value. */ export function getNonce( config: config, parameters: getNonce.Parameters, ): Promise { const { chainId, ...rest } = parameters const client = config.getClient({ chainId }) return Actions.nonce.getNonce(client, rest) } export namespace getNonce { export type Parameters = ChainIdParameter & Actions.nonce.getNonce.Parameters export type ReturnValue = Actions.nonce.getNonce.ReturnValue export type ErrorType = BaseErrorType export function queryKey( parameters: PartialBy, 'account' | 'nonceKey'>, ) { return ['getNonce', filterQueryOptions(parameters)] as const } export type QueryKey = ReturnType< typeof queryKey > export function queryOptions( config: Config, parameters: queryOptions.Parameters, ): queryOptions.ReturnValue { const { query, ...rest } = parameters return { ...query, enabled: Boolean( rest.account && rest.nonceKey !== undefined && (query?.enabled ?? true), ), queryKey: queryKey(rest), async queryFn(context) { const [, { account, nonceKey, ...parameters }] = context.queryKey if (!account) throw new Error('account is required.') if (nonceKey === undefined) throw new Error('nonceKey is required.') return await getNonce(config, { account, nonceKey, ...parameters }) }, } } export declare namespace queryOptions { export type Parameters< config extends Config, selectData = getNonce.ReturnValue, > = PartialBy, 'account' | 'nonceKey'> & QueryParameter< getNonce.ReturnValue, getNonce.ErrorType, selectData, getNonce.QueryKey > export type ReturnValue< config extends Config, selectData = getNonce.ReturnValue, > = QueryOptions< getNonce.ReturnValue, getNonce.ErrorType, selectData, getNonce.QueryKey > } } /** * Watches for nonce incremented events. * * @deprecated This function has been deprecated post-AllegroModerato. It will be removed in a future version. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempo], * transports: { * [tempo.id]: http(), * }, * }) * * const unwatch = Actions.nonce.watchNonceIncremented(config, { * onNonceIncremented: (args, log) => { * console.log('Nonce incremented:', args) * }, * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns A function to unsubscribe from the event. */ export function watchNonceIncremented( config: config, parameters: watchNonceIncremented.Parameters, ): () => void { const { chainId, ...rest } = parameters const client = config.getClient({ chainId }) return Actions.nonce.watchNonceIncremented(client, rest) } export declare namespace watchNonceIncremented { export type Parameters = ChainIdParameter & Actions.nonce.watchNonceIncremented.Parameters }