# Ens.name

The primary name of an address, only if it resolves back to that address.

## Usage

```ts
import { Effect, Layer, Option } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, 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 USER = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const program = Effect.gen(function* () {
  const name = yield* Ens.name(USER); // Option.some("vitalik.eth")
  // what to show for an account: its name, or its address in its EIP-55 form, cut short
  const shown = Address.checksum(USER);
  return Option.getOrElse(name, () => `${shown.slice(0, 6)}…${shown.slice(-4)}`);
});

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

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

### The name on another chain

An account can have a primary name per chain
([ENSIP-19](https://docs.ens.domains/ensip/19)). `chainId` asks for the one on an EVM chain.

```ts
import { type Address } from "@ethect/core";
import { Ens } from "@ethect/core/ens";

const BASE = 8453;

const onBase = (user: Address.Address) => Ens.name(user, { chainId: BASE });
```

### Many accounts

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

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

const namesOf = (users: ReadonlyArray<Address.Address>) =>
  Effect.forEach(users, (user) => Ens.name(user), { concurrency: "unbounded" });
```

## Returns

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

The name, normalised, or `Option.none` when the address has no primary name that holds.

## Parameters

### address

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)

The account. A name is not taken here: [`Ens.address`](https://ethect.dev/reference/ens/address/) gives the
address of one.

### options.chainId (optional)

- **Type:** `number`
- **Default:** `1`, Ethereum

The id of an EVM chain: a whole number from 0 to 2,147,483,647. It is turned into the coin
type a resolver knows the chain by: `60` for Ethereum, `0x80000000 + chainId` for any other
([ENSIP-11](https://docs.ens.domains/ensip/11)).

## Behavior

- **Anyone can claim any name.** A primary name is what an account says its name is. So the
  Universal Resolver resolves the name forward, and gives it only when it points back to the
  account. One read does both.
- **A name that is not normalised is no name.** An account can claim `Vitalik.eth`, which is
  not the name `vitalik.eth` and resolves to whoever registered it. The name is given only
  when it is its own normalised form. viem gives it as it is.
- **None when nothing holds.** No primary name, one that does not point back, one that has no
  resolver, one that is not normalised: `Option.none`.

## Errors

| Error | Reason | When |
| --- | --- | --- |
| `EnsError` | `"Gateway"` | a gateway of an offchain name did not answer |
| `EnsError` | `"Resolver"` | the Universal Resolver reverted, answered what does not decode, or there is no contract at its address |
| `EnsError` | `"Resolver"` | `chainId` is no chain id, such as `-1` or `1.5`: nothing was sent |
| [`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.name(address)`**

- **Call.Reader** `eth_call({ to: UniversalResolver, data }, "latest")`: data: `reverseWithGateways(address, 60, ["x-batch-gateway:true"])`. 60 is Ethereum's coin type: another chain's is `0x80000000 + chainId`. The contract reads the primary name, resolves it, and compares
- **decode**: `(string name, address resolver, address reverseResolver)`. No request
- **normalise**: the name is kept when it is its own normal form. No request

**a name kept on another chain: the `eth_call` reverts with `OffchainLookup`**

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

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