import { baseRegistrarAvailableSnippet } from '@ensdomains/ensjs-abi/v1/baseRegistrar' import { type Chain, type GetChainContractAddressErrorType, labelhash, type ReadContractErrorType, } from 'viem' import { readContract } from 'viem/actions' import { getAction } from 'viem/utils' import type { RequireClientContracts } from '../../../../clients/chain.js' import { getChainContractAddress } from '../../../../clients/chain.js' import { UnsupportedNameTypeError } from '../../../../errors/general.js' import type { ErrorType } from '../../../../errors/utils.js' import { ASSERT_NO_TYPE_ERROR } from '../../../../types/internal.js' import { getNameType } from '../../../../utils/name/getNameType.js' export type GetAvailableParameters = { /** Name to check availability for, only compatible for eth 2ld */ name: string } export type GetAvailableReturnType = boolean export type GetAvailableErrorType = | UnsupportedNameTypeError | ReadContractErrorType | GetChainContractAddressErrorType | ErrorType /** * Gets the availability of a name to register on the legacy v1 BaseRegistrar. * * Calls `BaseRegistrarImplementation.available(uint256 id)` where `id` is the * labelhash of the 2LD label as a `uint256`. Use this for the legacy v1 * deployment; for v2, use `getAvailable` from `@ensdomains/ensjs/public/v2`. * * @param client - {@link Client} * @param parameters - {@link GetAvailableParameters} * @returns Availability as boolean. {@link GetAvailableReturnType} * * @example * import { createPublicClient, http } from 'viem' * import { mainnet } from 'viem/chains' * import { addEnsContracts } from '@ensdomains/ensjs' * import { getAvailable } from '@ensdomains/ensjs/public/v1' * * const client = createPublicClient({ * chain: addEnsContracts(mainnet), * transport: http(), * }) * const result = await getAvailable(client, { name: 'ens.eth' }) * // false */ export async function getAvailable( client: RequireClientContracts, { name }: GetAvailableParameters, ): Promise { ASSERT_NO_TYPE_ERROR(client) const labels = name.split('.') const nameType = getNameType(name) if (nameType !== 'eth-2ld') throw new UnsupportedNameTypeError({ nameType, supportedNameTypes: ['eth-2ld'], details: 'Currently only eth-2ld names can be checked for availability', }) const readContractAction = getAction(client, readContract, 'readContract') return readContractAction({ address: getChainContractAddress({ chain: client.chain, contract: 'ensBaseRegistrarImplementation', }), abi: baseRegistrarAvailableSnippet, functionName: 'available', args: [BigInt(labelhash(labels[0]))], }) }