Ens.layer
ENS through its Universal Resolver, on the chain of the transport it is given.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Ens } from "@ethect/core/ens";
// ENS lives on mainnet, whatever chain the app reads: it takes its own transport.const Names = Ens.layer().pipe( Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")), Layer.provide(FetchHttpClient.layer), // for offchain names and avatars);
const address = await Ens.address("vitalik.eth").pipe(Effect.provide(Names), Effect.runPromise);Beside the transport of your app
Section titled “Beside the transport of your app”The names keep their own transport. The reads of your app go to its chain, and the lookups to mainnet, in one program.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { type Address, Transport } from "@ethect/core";import { Ens } from "@ethect/core/ens";import { Eth } from "@ethect/core/rpc";
// The app reads Base.const Base = Transport.http("https://mainnet.base.org");const Names = Ens.layer().pipe( Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")),);
const App = Layer.merge(Base, Names).pipe(Layer.provide(FetchHttpClient.layer));
const profile = (user: Address.Address) => Effect.gen(function* () { const balance = yield* Eth.getBalance(user, "latest"); // asked of Base const name = yield* Ens.name(user); // asked of mainnet return { balance, name }; }).pipe(Effect.provide(App));A node whose URL holds a key
Section titled “A node whose URL holds a key”Read the URL as a secret. It is then in no failure, log or span, and no gateway is told it.
import { Config, Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Ens } from "@ethect/core/ens";
const Names = Layer.unwrap( Effect.map(Config.Redacted("MAINNET_RPC_URL"), (url) => Ens.layer().pipe(Layer.provide(Transport.http(url))), ),).pipe(Layer.provide(FetchHttpClient.layer));On a server
Section titled “On a server”A gateway of an offchain name is a server the owner of a name chose, and it can answer with a redirect to anywhere. Give the names a client that does not follow one when your program resolves names someone else typed.
import { Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Ens } from "@ethect/core/ens";
const Client = FetchHttpClient.layer.pipe( Layer.provide(Layer.succeed(FetchHttpClient.RequestInit)({ redirect: "error" })),);
const Names = Ens.layer().pipe( Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")), Layer.provide(Client),);See redirects. In a browser there is nothing to do.
Another resolver
Section titled “Another resolver”resolver replaces the Universal Resolver: for a chain that has one of its own, or for a test
that deploys one.
import { Layer } from "effect";import { FetchHttpClient } from "effect/http";import { type Address, Transport } from "@ethect/core";import { Ens } from "@ethect/core/ens";
const namesOn = (url: string, resolver: Address.Address) => Ens.layer({ resolver }).pipe( Layer.provide(Transport.http(url)), Layer.provide(FetchHttpClient.layer), );Your own gateways for avatars
Section titled “Your own gateways for avatars”An avatar on IPFS or Arweave is given as the URL of a gateway. Name yours.
import { Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Ens } from "@ethect/core/ens";
const Names = Ens.layer({ gateways: { ipfs: "https://ipfs.example", arweave: "https://arweave.example" },}).pipe( Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")), Layer.provide(FetchHttpClient.layer),);Returns
Section titled “Returns”Layer<Ens.Ens, never, Transport.Transport | HttpClient>
A layer that provides the service. It needs a
Transport, which becomes the transport of the names, and Effect’s
HttpClient, which the gateways of offchain names and the metadata of NFT avatars are asked
with. It cannot fail to be built.
Parameters
Section titled “Parameters”options.resolver (optional)
Section titled “options.resolver (optional)”- Type:
Address.Address - Default:
0xeeeeeeee14d718c2b47d9923deab1335e144eeee, the Universal Resolver on mainnet
The contract every lookup is read from. It has to answer resolveWithGateways and
reverseWithGateways, as the Universal Resolver does.
options.gateways (optional)
Section titled “options.gateways (optional)”- Type:
{ ipfs?: string; arweave?: string } - Default:
https://ipfs.ioandhttps://arweave.net
Where the avatars kept on IPFS and on Arweave are loaded from. A slash at the end is dropped. These are not the gateways of an offchain name: those are named by the name’s resolver.
Behavior
Section titled “Behavior”- Nothing is sent when the layer is built. The first request is the first lookup.
- The transport is kept. Every lookup reads on the transport the layer was given, wherever
it is called from.
Transport.onand the transport of your program do not change it. - The client is kept, and it is yours. What it adds to a request, it adds for a gateway. Whether it follows a redirect is its own rule: on a server, give one that does not.
- The reader is kept too. The lookups are read with the
Call.Readerthere is when the layer is built, with offchain lookups followed on top of it. By default that reader merges the lookups made together into oneeth_call. ProvideCall.batching(…)to the layer to tune that for the names, or to turn it off. - A lookup is read at the latest block of its own chain. A block set with
Call.withBlockis a block of your app’s chain: it does not apply. - The default resolver was created at block 23,085,558. On another chain there is no
contract at its address: a lookup then fails with an
EnsErrorthat says so.
Under the hood
Section titled “Under the hood”Two services are asked: the Transport the layer was given, through
Call.Reader, and the HttpClient.
Ens.layer(options)- your programno request
a lookup
- Call.Reader
eth_call({ to: resolver, data }, "latest")through theTransport
- Call.Reader
an offchain name
- HttpClient
GET or POSTto the gateway the name’s resolver names - Call.Reader
eth_call({ to: resolver, data: the call back }, "latest")through theTransport
- HttpClient
an avatar that is an NFT
- Transport
eth_chainIdonce for the transport - Call.Reader
eth_call({ to: the contract of the token, data }, "latest")through theTransport - HttpClient
GETof the token’s metadata
- Transport
Each page says which of these its lookup makes: address,
name, text,
avatar.