# Ens.layer

ENS through its Universal Resolver, on the chain of the transport it is given.

## Usage

```ts
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

The names keep their own transport. The reads of your app go to its chain, and the lookups to
mainnet, in one program.

```ts
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

Read the URL as a secret. It is then in no failure, log or span, and no gateway is told it.

```ts
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

A gateway of an [offchain name](https://ethect.dev/reference/ens/#offchain-names) 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.

```ts
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](https://ethect.dev/reference/ens/#on-a-server-redirects). In a browser there is nothing to do.

### Another resolver

`resolver` replaces the Universal Resolver: for a chain that has one of its own, or for a test
that deploys one.

```ts
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

An avatar on IPFS or Arweave is given as the URL of a gateway. Name yours.

```ts
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

`Layer<Ens.Ens, never, Transport.Transport | HttpClient>`

A layer that provides [the service](https://ethect.dev/reference/ens/#the-service). It needs a
[`Transport`](https://ethect.dev/reference/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

### options.resolver (optional)

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/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)

- **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](https://ethect.dev/reference/ens/#offchain-names): those are
named by the name's resolver.

## 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.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](#on-a-server).
- **The reader is kept too.** The lookups are read with the
  [`Call.Reader`](https://ethect.dev/reference/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(…)`](https://ethect.dev/reference/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`](https://ethect.dev/reference/call/with-block/) 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.

## Under the hood

Two services are asked: the [`Transport`](https://ethect.dev/reference/transport/) the layer was given, through
[`Call.Reader`](https://ethect.dev/reference/call/reader/), and the `HttpClient`.

**`Ens.layer(options)`**

- **your program**: no request

**a lookup**

- **Call.Reader** `eth_call({ to: resolver, data }, "latest")`: through the `Transport`

**an offchain name**

- **HttpClient** `GET or POST`: to the gateway the name's resolver names
- **Call.Reader** `eth_call({ to: resolver, data: the call back }, "latest")`: through the `Transport`

**an avatar that is an NFT**

- **Transport** `eth_chainId`: once for the transport
- **Call.Reader** `eth_call({ to: the contract of the token, data }, "latest")`: through the `Transport`
- **HttpClient** `GET`: of the token's metadata

Each page says which of these its lookup makes: [`address`](https://ethect.dev/reference/ens/address/#under-the-hood),
[`name`](https://ethect.dev/reference/ens/name/#under-the-hood), [`text`](https://ethect.dev/reference/ens/text/#under-the-hood),
[`avatar`](https://ethect.dev/reference/ens/avatar/#under-the-hood).
