Skip to content

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);

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));

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));

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.

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),
);

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),
);

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.

  • 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.

  • Type: { ipfs?: string; arweave?: string }
  • Default: https://ipfs.io and https://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.

  • 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.on and 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.Reader there is when the layer is built, with offchain lookups followed on top of it. By default that reader merges the lookups made together into one eth_call. Provide Call.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.withBlock is 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 EnsError that says so.

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

  1. Ens.layer(options)

    1. your programno request
  2. a lookup

    1. Call.Readereth_call({ to: resolver, data }, "latest")through the Transport
  3. an offchain name

    1. HttpClientGET or POSTto the gateway the name’s resolver names
    2. Call.Readereth_call({ to: resolver, data: the call back }, "latest")through the Transport
  4. an avatar that is an NFT

    1. Transporteth_chainIdonce for the transport
    2. Call.Readereth_call({ to: the contract of the token, data }, "latest")through the Transport
    3. HttpClientGETof the token’s metadata

Each page says which of these its lookup makes: address, name, text, avatar.