import type { BaseError, Hex } from 'viem' import type { ClientWithPns } from '../../contracts/consts.js' import type { GenericPassthrough, Prettify, TransactionRequestWithPassthrough, } from '../../types.js' import { generateFunction, type GeneratedFunction, } from '../../utils/generateFunction.js' import _getContentHash, { type InternalGetContentHashParameters, type InternalGetContentHashReturnType, } from './_getContentHash.js' import universalWrapper from './universalWrapper.js' export type GetContentHashRecordParameters = Prettify< InternalGetContentHashParameters & { /** Batch gateway URLs to use for resolving CCIP-read requests. */ gatewayUrls?: string[] } > export type GetContentHashRecordReturnType = Prettify const encode = ( client: ClientWithPns, { name, gatewayUrls }: Omit, ): TransactionRequestWithPassthrough => { const prData = _getContentHash.encode(client, { name }) 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 _getContentHash.decode(client, urData.data, { strict }) } type BatchableFunctionObject = GeneratedFunction /** * Gets the content hash record for a name * @param client - {@link ClientWithPns} * @param parameters - {@link GetContentHashRecordParameters} * @returns Content hash object, or `null` if not found. {@link GetContentHashRecordReturnType} * * @example * import { createPublicClient, http } from 'viem' * import { mainnet } from 'viem/chains' * import { addPnsContracts } from '@pnsdomains/pnsjs' * import { getContentHashRecord } from '@pnsdomains/pnsjs/public' * * const client = createPublicClient({ * chain: addPnsContracts(mainnet), * transport: http(), * }) * const result = await getContentHashRecord(client, { name: 'pns.pls' }) * // { protocolType: 'ipfs', decoded: 'k51qzi5uqu5djdczd6zw0grmo23j2vkj9uzvujencg15s5rlkq0ss4ivll8wqw' } */ const getContentHashRecord = generateFunction({ encode, decode }) as (( client: ClientWithPns, { name, strict, gatewayUrls }: GetContentHashRecordParameters, ) => Promise) & BatchableFunctionObject export default getContentHashRecord