Quick start
Read an ENS contract with viem in a few minutes, using a typed ABI and the deployed address for your chain from @ensforge/contracts.
By the end of this page you will have read a name's state from the ENSv2 .eth registry with viem, using an ABI and an address from @ensforge/contracts. Install the package first: Installation.
1. Pick an address and an ABI
Deployments.mainnet.v2.contracts.ethRegistry is the address of the registry that holds .eth names. Abis.v2.permissionedRegistry is the ABI to read it with. The ABI is an as const array, so viem infers the function names, arguments and return types.
2. Read the contract
getState takes a label's hash as a number and returns the name's status, expiry, latest owner, token ID and role resource.
Example (Reading a Name's State)
import { Abis, Deployments } from "@ensforge/contracts";
import { createPublicClient, hexToBigInt, http } from "viem";
import { labelhash } from "viem/ens";
import { mainnet } from "viem/chains";
const publicClient = createPublicClient({ chain: mainnet, transport: http() });
const state = await publicClient.readContract({
address: Deployments.mainnet.v2.contracts.ethRegistry,
abi: Abis.v2.permissionedRegistry,
functionName: "getState",
args: [hexToBigInt(labelhash("vitalik"))],
});
state;labelhash("vitalik")hashes the label (the part before.eth), giving its labelhash. Normalise a label before you hash it.statusis the contract'sStatusenum, returned as a number:0(AVAILABLE),1(RESERVED) or2(REGISTERED).- A
.ethname that has not moved from ENSv1 yet reads as1(RESERVED) with a zerolatestOwner, because its owner is still in the ENSv1 registry.vitalik.ethreads this way while it stays on ENSv1. The SDK reads both registries for you: see Get a name's owner, expiry and status.
3. Read another chain
Deployments.sepolia has the same shape. Use Deployments.get({ chainId }) when the chain comes from a variable.
Example (Looking Up a Chain by ID)
import { Deployments } from "@ensforge/contracts";
const profile = Deployments.get({ chainId: 11155111 });
const ethRegistry = profile?.v2?.contracts.ethRegistry;get returns undefined for a chain ensforge does not ship. See Addresses and chains.
Learn more
Was this page helpful?