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.
The name on another chain
Section titled “The name on another chain”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 });Many accounts
Section titled “Many accounts”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" });Returns
Section titled “Returns”Effect<Option<string>, Ens.Error | Transport.Error, Ens.Ens>
The name, normalised, or Option.none when the address has no primary name that holds.
Parameters
Section titled “Parameters”address
Section titled “address”- Type:
Address.Address
The account. A name is not taken here: Ens.address gives the
address of one.
options.chainId (optional)
Section titled “options.chainId (optional)”- 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).
Behavior
Section titled “Behavior”- 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 namevitalik.ethand 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.
Errors
Section titled “Errors”| 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 |
Under the hood
Section titled “Under the hood”The Transport of the layer is asked, through
Call.Reader.
Ens.name(address)- Call.Reader
eth_call({ to: UniversalResolver, data }, "latest")data:reverseWithGateways(address, 60, ["x-batch-gateway:true"]). 60 is Ethereum’s coin type: another chain’s is0x80000000 + chainId. The contract reads the primary name, resolves it, and compares - decode
(string name, address resolver, address reverseResolver). No request - normalisethe name is kept when it is its own normal form. No request
- Call.Reader
a name kept on another chain: the
eth_callreverts withOffchainLookup- HttpClient
GET or POSTto the gateway the resolver names - Call.Reader
eth_call({ to: UniversalResolver, data: the call back }, "latest")
- HttpClient
This is the eth_call viem’s getEnsName sends for the same address. See
offchain names for what a gateway is sent.