Skip to content

Resolve ENS names

A recipient field that takes vitalik.eth or 0xd8dA…6045, and gives the address to send to.

recipient.ts
import { Effect, Layer, Option } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Ens } from "@ethect/core/ens";
// ENS lives on mainnet, whatever chain your app is on: the names get a node of their own.
export const Names = Ens.layer().pipe(
Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Layer.provide(FetchHttpClient.layer), // for names kept outside mainnet
);
/** What a recipient field holds: an address, or a name to look up. */
export const recipient = (typed: string) =>
Effect.matchEffect(Address.make(typed.trim()), {
// An address is itself: nothing is asked.
onSuccess: (address) => Effect.succeed({ found: true, address } as const),
// What is not an address may be a name.
onFailure: () =>
Ens.address(typed.trim()).pipe(
Effect.map(
Option.match({
onSome: (address) => ({ found: true, address }) as const,
// No answer is not a failure: the name is fine, and points nowhere.
onNone: () => ({ found: false, message: "This name has no address" }) as const,
}),
),
Effect.catchTag("EnsError", (error) =>
Effect.succeed({
found: false,
message: error.reason === "InvalidName" ? error.message : "Try again later",
} as const),
),
),
});
const outcome = await recipient("vitalik.eth").pipe(Effect.provide(Names), Effect.runPromise);
// { found: true, address: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045" }
  • Effect.matchEffect is a try with both branches written: one when Address.make succeeds, one when it fails.
  • Ens.address gives an Option: Option.some(address) or Option.none(), where viem gives an address or null.
  • Names is what the lookups run with. The import is @ethect/core/ens, an entry point of its own: an app that resolves no name ships none of it.
  • The name is passed as it was typed. It is normalised for you: Vitalik.ETH is vitalik.eth. With viem you call normalize first.

ENS lives on mainnet. An app that reads Base still asks mainnet for a name, so the layer takes its own transport and keeps it.

app.ts
import { Effect, Layer, Option } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Ens } from "@ethect/core/ens";
import { Eth } from "@ethect/core/rpc";
import { Names } from "./recipient";
const App = Layer.merge(Transport.http("https://mainnet.base.org"), Names).pipe(
Layer.provide(FetchHttpClient.layer),
);
/** What to show for an account: its name, or its address cut short, in its EIP-55 form. */
export const label = (account: Address.Address) =>
Effect.map(Ens.name(account), (name) =>
Option.getOrElse(name, () => {
const shown = Address.checksum(account); // 0xd8dA…6045, not 0xd8da…6045
return `${shown.slice(0, 6)}…${shown.slice(-4)}`;
}),
);
export const profile = (user: Address.Address) =>
Effect.gen(function* () {
const balance = yield* Eth.getBalance(user, "latest"); // asked of Base
const shown = yield* label(user); // asked of mainnet
return { balance, shown };
}).pipe(Effect.provide(App));
  • Nothing you do to your program changes where the names are read. Not the transport around the call, not Transport.on.
  • Ens.name is checked to point back. Anyone can claim any name for their account. The name is given only when it resolves to that account.
  • Lookups made together are one request. Ten names asked at once are one eth_call.
  • A node with a key in its URL: read the URL with Config.Redacted("MAINNET_RPC_URL"). It is then in no failure and no log.

A lookup fails only when something went wrong. A name that points nowhere is an answer.

Typed The lookup The form says
an address is not made the address
a name with an address gives Option.some(address) the address
a name nobody registered gives Option.none() This name has no address
a name with no address record gives Option.none() This name has no address
vitalik .eth, with a space fails with EnsError "InvalidName": nothing is sent Not a valid name: …
a name whose gateway is down fails with EnsError "Gateway" Try again later
any name, and your node is down fails with TransportError whatever your app says then
  • “No address” and “try again” are not the same message. A gateway that is down says nothing about the name: it may well have an address.
  • A name kept outside mainnet resolves the same way. jesse.base.eth keeps its records on Base: its resolver names a server to ask, called a gateway, and the lookup follows it. That is what the HttpClient is for.
  • A name is never accepted where an address is expected. Tx.send takes an Address.Address: resolving a name is a step you write, as above.

A name can hold one address per chain. Ask with the chain id.

import { Ens } from "@ethect/core/ens";
// The address of the name on Base. Without `chainId`: the one on Ethereum.
const onBase = Ens.address("jesse.base.eth", { chainId: 8453 });

With viem you pass a coinType, which toCoinType(chainId) computes. Here you never write one. Ens.name takes the same option, for the name of an account on that chain.

A gateway is a server the owner of a name chose. It can answer with a redirect to anywhere, an address of your own network included. A server that resolves names someone else typed gives the names a client that follows no redirect.

import { Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Ens } from "@ethect/core/ens";
const NoRedirect = FetchHttpClient.layer.pipe(
Layer.provide(Layer.succeed(FetchHttpClient.RequestInit)({ redirect: "error" })),
);
export const Names = Ens.layer().pipe(
Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Layer.provide(NoRedirect),
);

A gateway that redirects then counts as one that is down. In a browser the page’s own rules apply, and there is nothing to do.

Two services are asked: the Transport the layer was given, and the HttpClient.

  1. Ens.address("Vitalik.ETH")

    1. your programnormalise: "vitalik.eth". No request
    2. mainnet nodeeth_call({ to: UniversalResolver, data })one read: the resolver is found and asked
  2. a name kept outside mainnet: the read reverts with OffchainLookup

    1. gatewayGET or POSTto the URL the name’s resolver gives
    2. mainnet nodeeth_call({ to: UniversalResolver, data })the gateway’s answer, checked on chain
  3. Ens.name(address)

    1. mainnet nodeeth_call({ to: UniversalResolver, data })the name, resolved forward and compared

What a gateway is sent, the bounds on what it may answer, text records and avatars are on Ens. The rules a name is normalised by are ENSIP-15.