Resolve ENS names
A recipient field that takes vitalik.eth or 0xd8dA…6045, and gives the address to send
to.
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.matchEffectis atrywith both branches written: one whenAddress.makesucceeds, one when it fails.Ens.addressgives anOption:Option.some(address)orOption.none(), where viem gives an address ornull.Namesis 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.ETHisvitalik.eth. With viem you callnormalizefirst.
A transport of its own
Section titled “A transport of its own”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.
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.nameis 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.
No answer is not a failure
Section titled “No answer is not a failure”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.ethkeeps its records on Base: its resolver names a server to ask, called a gateway, and the lookup follows it. That is what theHttpClientis for. - A name is never accepted where an address is expected.
Tx.sendtakes anAddress.Address: resolving a name is a step you write, as above.
The address on another chain
Section titled “The address on another chain”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.
On a server
Section titled “On a server”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.
Under the hood
Section titled “Under the hood”Two services are asked: the Transport the layer was given, and the HttpClient.
Ens.address("Vitalik.ETH")- your programnormalise:
"vitalik.eth". No request - mainnet node
eth_call({ to: UniversalResolver, data })one read: the resolver is found and asked
- your programnormalise:
a name kept outside mainnet: the read reverts with
OffchainLookup- gateway
GET or POSTto the URL the name’s resolver gives - mainnet node
eth_call({ to: UniversalResolver, data })the gateway’s answer, checked on chain
- gateway
Ens.name(address)- mainnet node
eth_call({ to: UniversalResolver, data })the name, resolved forward and compared
- mainnet node
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.