Skip to content
function useEnsName(): object;

Hook that returns the getEnsName action configured with the current Anvil SDK context.

This is a thin context-binder, not a query hook: it wraps sdk.getEnsName with a stable callback, not cached/fetch state like wagmi’s useEnsName. Reach for it specifically because it is mainnet-pinned via AnvilDependencies.mainnetRpcUrl — wagmi’s useEnsName resolves against the active wagmi chain, which silently returns nothing on Anvil’s non-mainnet protocol chains unless the developer separately configures a mainnet client and passes chainId: 1. If your app already has wagmi configured for mainnet, its useEnsName is a fine, more fully-featured (cached) alternative — this hook exists to avoid that footgun for protocol-chain apps, not to compete with wagmi’s query hooks.

Rejection contract: the returned callback’s promise rejects exactly as the core getEnsName action documents (transport/RPC errors propagate); null is the domain “no primary name set” outcome. Callers own the catch — the binder never swallows errors.

Returns

object

An object whose getEnsName is a stable callback taking GetEnsNameParams and resolving to the primary name, or null when the address has none.

getEnsName

getEnsName: params => Promise<string | null>;

Parameters

ParameterType
paramsGetEnsNameParams

Returns

Promise<string | null>

Throws

Error when rendered outside <AnvilProvider>, by way of useAnvilSDK.

Example

export function PrimaryName({ address }: { address: Address }) {
const { getEnsName } = useEnsName();
const [name, setName] = useState<string | null>();
async function lookup() {
// Resolves against mainnet regardless of the protocol chain. This
// catch folds a transport failure into the same "no name" state the
// SDK's own AddressInput uses. Keep the two apart, as
// `BeneficiaryField` below does, when the UI should offer a retry.
setName(await getEnsName({ address }).catch(() => null));
}
return <button onClick={lookup}>{name ?? 'Look up ENS name'}</button>;
}