import type { BaseError, Hex } from 'viem' import type { ClientWithPns } from '../../contracts/consts.js' import type { GenericPassthrough, Prettify, SimpleTransactionRequest, } from '../../types.js' import { generateFunction, type GeneratedFunction, } from '../../utils/generateFunction.js' import _getAbi, { type InternalGetAbiParameters, type InternalGetAbiReturnType, } from './_getAbi.js' import universalWrapper from './universalWrapper.js' export type GetAbiRecordParameters = Prettify< InternalGetAbiParameters & { /** Batch gateway URLs to use for resolving CCIP-read requests. */ gatewayUrls?: string[] } > export type GetAbiRecordReturnType = Prettify const encode = ( client: ClientWithPns, { name, supportedContentTypes, gatewayUrls, }: Omit, ): SimpleTransactionRequest => { const prData = _getAbi.encode(client, { name, supportedContentTypes }) return universalWrapper.encode(client, { name, data: prData.data, gatewayUrls, }) } const decode = async ( client: ClientWithPns, data: Hex | BaseError, passthrough: GenericPassthrough, { strict, gatewayUrls, }: Pick, ): Promise => { const urData = await universalWrapper.decode(client, data, passthrough, { strict, gatewayUrls, }) if (!urData) return null return _getAbi.decode(client, urData.data, { strict }) } type BatchableFunctionObject = GeneratedFunction /** * Gets the ABI record for a name * @param client - {@link ClientWithPns} * @param parameters - {@link GetAbiRecordParameters} * @returns ABI record for the name, or `null` if not found. {@link GetAbiRecordReturnType} * * @example * import { createPublicClient, http } from 'viem' * import { mainnet } from 'viem/chains' * import { addPnsContracts } from '@pnsdomains/pnsjs' * import { getAbiRecord } from '@pnsdomains/pnsjs/public' * * const client = createPublicClient({ * chain: addPnsContracts(mainnet), * transport: http(), * }) * const result = await getAbiRecord(client, { name: 'pns.pls' }) * // TODO: real example */ const getAbiRecord = generateFunction({ encode, decode }) as (( client: ClientWithPns, { name, strict, gatewayUrls, supportedContentTypes }: GetAbiRecordParameters, ) => Promise) & BatchableFunctionObject export default getAbiRecord