import type { Chain } from 'viem' import { NoChainError, UnsupportedChainError } from '../errors/contracts.js' import { type CheckedChainWithEns, type SupportedChain, addresses, subgraphs, supportedChains, } from './consts.js' type AddEnsContractsOptions = { /** Overrides the default ENS subgraph URL for the chain */ subgraphUrl?: string } /** * Adds ENS contract addresses to the viem chain * @param chain - The viem {@link Chain} object to add the ENS contracts to * @param options.subgraphUrl - Overrides the default ENS subgraph URL * * @example * import { createPublicClient, http } from 'viem' * import { electroneum } from 'viem/chains' * import { addEnsContracts } from '@etn-sc/ensjs' * * const clientWithEns = createPublicClient({ * chain: addEnsContracts(electroneum), * transport: http(), * }) */ export const addEnsContracts = ( chain: TChain, options?: AddEnsContractsOptions, ) => { if (!chain) throw new NoChainError() if (!supportedChains.includes(chain.id as SupportedChain)) throw new UnsupportedChainError({ chainId: chain.id, supportedChains, }) return { ...chain, contracts: { ...chain.contracts, ...addresses[chain.id as SupportedChain], }, subgraphs: { ...subgraphs[chain.id as SupportedChain], ...(options?.subgraphUrl ? { ens: { url: options.subgraphUrl, }, } : {}), }, } as unknown as CheckedChainWithEns }