# Ens.text

A text record of a name, or none when the record is empty.

## Usage

```ts
import { Effect, Layer, Option } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Ens } from "@ethect/core/ens";

const Names = Ens.layer().pipe(
  Layer.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
  Layer.provide(FetchHttpClient.layer),
);

const program = Effect.gen(function* () {
  const url = yield* Ens.text("vitalik.eth", "url"); // Option.some("https://vitalik.ca")
  const github = yield* Ens.text("vitalik.eth", "com.github");
  return { url: Option.getOrNull(url), github: Option.getOrNull(github) };
});

const records = await program.pipe(Effect.provide(Names), Effect.runPromise);
```

The examples below leave the layer out: see [`Ens.layer`](https://ethect.dev/reference/ens/layer/).

### A profile

Each record is one read. Asked for together, they are
[merged](https://ethect.dev/reference/call/batching/) into one request.

```ts
import { Effect } from "effect";
import { Ens } from "@ethect/core/ens";

const keys = ["description", "url", "com.twitter", "com.github"] as const;

const profile = (name: string) =>
  Effect.all(
    Object.fromEntries(keys.map((key) => [key, Ens.text(name, key)])),
    { concurrency: "unbounded" },
  );
```

## Returns

`Effect<Option<string>, Ens.Error | Transport.Error, Ens.Ens>`

The record, as its owner wrote it, or `Option.none` when it is empty or not there.

## Parameters

### name

- **Type:** `string`

The name, as it was typed. It is normalised by the rules of
[ENSIP-15](https://ethect.dev/reference/ens/#a-name-is-normalised-first) before it is hashed.

### key

- **Type:** `string`

Which record. Any text is a key: [ENSIP-5](https://docs.ens.domains/ensip/5) names the usual
ones.

| Key | Holds |
| --- | --- |
| `avatar` | an image: see [`Ens.avatar`](https://ethect.dev/reference/ens/avatar/), which reads it |
| `description`, `url`, `email`, `location` | what they say |
| `com.twitter`, `com.github`, `org.telegram` | an account on a service, by its reversed domain |

## Behavior

- **One read.** The Universal Resolver finds the resolver of the name and asks it for
  `text(node, key)`.
- **None when there is nothing to find.** No resolver, a resolver without text records, an
  empty record: `Option.none`.
- **A record is what its owner wrote.** It is not checked, trimmed or escaped. A `url` is not
  known to be a URL, and a `com.twitter` is not known to be that person's account. Show it as
  text, never as HTML.
- **The key is not normalised.** `URL` and `url` are two records.

## Errors

| Error | Reason | When |
| --- | --- | --- |
| `EnsError` | `"InvalidName"` | the name does not normalise: nothing was sent |
| `EnsError` | `"Gateway"` | a gateway of an offchain name did not answer |
| `EnsError` | `"Resolver"` | the record is not text, the Universal Resolver reverted, or there is no contract at its address |
| [`RpcError`](https://ethect.dev/reference/errors/rpc-error/) | | the node answered with an error that is not a revert |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/) | | the node could not be reached, or did not answer |

## Under the hood

The `Transport` of [the layer](https://ethect.dev/reference/ens/layer/) is asked, through
[`Call.Reader`](https://ethect.dev/reference/call/reader/).

**`Ens.text("vitalik.eth", "url")`**

- **normalise**: `"vitalik.eth"`. No request
- **encode**: `text(namehash("vitalik.eth"), "url")`, the name as DNS. No request
- **Call.Reader** `eth_call({ to: UniversalResolver, data }, "latest")`: data: `resolveWithGateways(name, text(node, key), ["x-batch-gateway:true"])`
- **decode**: `(bytes result, address resolver)` → the text. No request

**an offchain name: the `eth_call` reverts with `OffchainLookup`**

- **HttpClient** `GET or POST`: to the gateway its resolver names
- **Call.Reader** `eth_call({ to: UniversalResolver, data: the call back }, "latest")`

This is the `eth_call` viem's `getEnsText` sends for the same name, once normalised. See
[offchain names](https://ethect.dev/reference/ens/#offchain-names) for what a gateway is sent.
