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 _getAddr, { type InternalGetAddrParameters, type InternalGetAddrReturnType, } from './_getAddr.js' import universalWrapper from './universalWrapper.js' export type GetAddressRecordParameters = Prettify< InternalGetAddrParameters & { /** Batch gateway URLs to use for resolving CCIP-read requests. */ gatewayUrls?: string[] } > export type GetAddressRecordReturnType = Prettify const encode = ( client: ClientWithPns, { name, coin, gatewayUrls, }: Omit, ): SimpleTransactionRequest => { const prData = _getAddr.encode(client, { name, coin }) return universalWrapper.encode(client, { name, data: prData.data, gatewayUrls, }) } const decode = async ( client: ClientWithPns, data: Hex | BaseError, passthrough: GenericPassthrough, { coin, strict, gatewayUrls, }: Pick, ): Promise => { const urData = await universalWrapper.decode(client, data, passthrough, { strict, gatewayUrls, }) if (!urData) return null return _getAddr.decode(client, urData.data, { coin, strict }) } type BatchableFunctionObject = GeneratedFunction /** * Gets an address record for a name and specified coin * @param client - {@link ClientWithPns} * @param parameters - {@link GetAddressRecordParameters} * @returns Coin value object, or `null` if not found. {@link GetAddressRecordReturnType} * * @example * import { createPublicClient, http } from 'viem' * import { mainnet } from 'viem/chains' * import { addPnsContracts } from '@pnsdomains/pnsjs' * import { getAddressRecord } from '@pnsdomains/pnsjs/public' * * const client = createPublicClient({ * chain: addPnsContracts(mainnet), * transport: http(), * }) * const result = await getAddressRecord(client, { name: 'pns.pls', coin: 'PLS' }) * // { id: 1028, name: 'PLS , value: '0xFe89cc7aBB2C4183683ab71653C4cdc9B02D44b7' } */ const getAddressRecord = generateFunction({ encode, decode }) as (( client: ClientWithPns, { name, coin, bypassFormat, strict, gatewayUrls }: GetAddressRecordParameters, ) => Promise) & BatchableFunctionObject export default getAddressRecord