Skip to content

Ens.name

The primary name of an address, only if it resolves back to that address.

import { Effect, Layer, Option } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Ens } from "@ethect/core/ens";
const Names = Ens.layer().pipe(
Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Layer.provide(FetchHttpClient.layer),
);
const USER = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const program = Effect.gen(function* () {
const name = yield* Ens.name(USER); // Option.some("vitalik.eth")
// what to show for an account: its name, or its address
return Option.getOrElse(name, () => `${USER.slice(0, 6)}…${USER.slice(-4)}`);
});
const shown = await program.pipe(Effect.provide(Names), Effect.runPromise);

The examples below leave the layer out: see Ens.layer.

An account can have a primary name per chain (ENSIP-19). chainId asks for the one on an EVM chain.

import { type Address } from "@ethect/core";
import { Ens } from "@ethect/core/ens";
const BASE = 8453;
const onBase = (user: Address.Address) => Ens.name(user, { chainId: BASE });

Each lookup is one read. Made together, they are merged into one request.

import { Effect } from "effect";
import { type Address } from "@ethect/core";
import { Ens } from "@ethect/core/ens";
const namesOf = (users: ReadonlyArray<Address.Address>) =>
Effect.forEach(users, (user) => Ens.name(user), { concurrency: "unbounded" });

Effect<Option<string>, Ens.Error | Transport.Error, Ens.Ens>

The name, normalised, or Option.none when the address has no primary name that holds.

The account. A name is not taken here: Ens.address gives the address of one.

  • Type: number
  • Default: 1, Ethereum

The id of an EVM chain: a whole number from 0 to 2,147,483,647. It is turned into the coin type a resolver knows the chain by: 60 for Ethereum, 0x80000000 + chainId for any other (ENSIP-11).

  • Anyone can claim any name. A primary name is what an account says its name is. So the Universal Resolver resolves the name forward, and gives it only when it points back to the account. One read does both.
  • A name that is not normalised is no name. An account can claim Vitalik.eth, which is not the name vitalik.eth and resolves to whoever registered it. The name is given only when it is its own normalised form. viem gives it as it is.
  • None when nothing holds. No primary name, one that does not point back, one that has no resolver, one that is not normalised: Option.none.
Error Reason When
EnsError "Gateway" a gateway of an offchain name did not answer
EnsError "Resolver" the Universal Resolver reverted, answered what does not decode, or there is no contract at its address
EnsError "Resolver" chainId is no chain id, such as -1 or 1.5: nothing was sent
RpcError the node answered with an error that is not a revert
TransportError the node could not be reached, or did not answer

The Transport of the layer is asked, through Call.Reader.

  1. Ens.name(address)

    1. Call.Readereth_call({ to: UniversalResolver, data }, "latest")data: reverseWithGateways(address, 60, ["x-batch-gateway:true"]). 60 is Ethereum’s coin type: another chain’s is 0x80000000 + chainId. The contract reads the primary name, resolves it, and compares
    2. decode(string name, address resolver, address reverseResolver). No request
    3. normalisethe name is kept when it is its own normal form. No request
  2. a name kept on another chain: the eth_call reverts with OffchainLookup

    1. HttpClientGET or POSTto the gateway the resolver names
    2. Call.Readereth_call({ to: UniversalResolver, data: the call back }, "latest")

This is the eth_call viem’s getEnsName sends for the same address. See offchain names for what a gateway is sent.