# Ens.address

The address a name points to, or none when the name has no resolver or no address.

## 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 address = yield* Ens.address("vitalik.eth");
  // Option.some("0xd8da6bf26964af9d7eed9e03e53415d37aa96045")
  return Option.getOrUndefined(address);
});

const address = 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 name as it was typed

The name is normalised for you. Pass what the user wrote.

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

// the same lookup as "vitalik.eth": one request, for the same name
const typed = Ens.address("  Vitalik.ETH".trim());

// refused before anything is sent: EnsError "InvalidName"
const refused = Ens.address("vitalik .eth").pipe(
  Effect.catchTag("EnsError", (error) => Effect.succeed(error.message)),
  // Not a valid name: Invalid label "vitalik ": disallowed character: " " {20}
);
```

### The address on another chain

A name can hold one address per chain. `chainId` asks for the one on an EVM chain.

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

const BASE = 8453;

const onBase = Ens.address("jesse.base.eth", { chainId: BASE });

// Without a chain, the address is the one on Ethereum.
const onEthereum = Ens.address("jesse.base.eth");
```

You never write a coin type: the chain id is turned into the one a resolver knows the chain
by ([ENSIP-11](https://docs.ens.domains/ensip/11)).

| `chainId` | The resolver is asked for coin type |
| --- | --- |
| `1` | `60`, Ethereum's |
| any other, such as `8453` | `0x80000000 + chainId` |

### A name or an address

A form field takes either. An address is itself, and a name is looked up.

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

const recipient = (typed: string) =>
  Effect.matchEffect(Address.make(typed), {
    onSuccess: (address) => Effect.succeed(Option.some(address)),
    // what is not an address may be a name
    onFailure: () => Ens.address(typed),
  });
```

## Returns

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

The [address](https://ethect.dev/reference/primitives/address/), in lowercase, or `Option.none` when the name
has none.

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

### options.chainId (optional)

- **Type:** `number`
- **Default:** none: the address on Ethereum

The id of an EVM chain: a whole number from 0 to 2,147,483,647. Only EVM chains can be asked
for, since their addresses are the twenty bytes an `Address.Address` is.

## Behavior

- **One read.** The Universal Resolver finds the resolver of the name, asks it, and gives its
  answer: one `eth_call`.
- **None when there is nothing to find.** No resolver, no address record, an empty one, the
  zero address: `Option.none`. See
  [no answer is not a failure](https://ethect.dev/reference/ens/#no-answer-is-not-a-failure).
- **An offchain name resolves the same way**, with one request to its gateway and one more
  `eth_call`. See [offchain names](https://ethect.dev/reference/ens/#offchain-names).
- **With `chainId: 1`, the resolver is asked `addr(node, 60)`**, not `addr(node)`. The two are
  the same record in most resolvers.
- **An address a resolver answers bare is read.** `addr(node, coinType)` returns `bytes`. Some
  resolvers answer twenty bytes or one word instead.
- **The address is not checksummed.** Every `Address.Address` is lowercase:
  [`Address.checksum`](https://ethect.dev/reference/primitives/address/) gives the mixed case, to show it.

## 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 an address, the Universal Resolver reverted, 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.address("Vitalik.ETH")`**

- **normalise**: `"vitalik.eth"`. No request
- **encode**: `addr(namehash("vitalik.eth"))`, and the name as DNS. No request
- **Call.Reader** `eth_call({ to: UniversalResolver, data }, "latest")`: data: `resolveWithGateways(name, addr(node), ["x-batch-gateway:true"])`
- **decode**: `(bytes result, address resolver)` → the address. 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")`

| Call | The resolver is asked |
| --- | --- |
| `Ens.address(name)` | `addr(bytes32 node)` |
| `Ens.address(name, { chainId })` | `addr(bytes32 node, uint256 coinType)`, with the coin type of the chain |

This is the `eth_call` viem's `getEnsAddress` sends for the same name, once normalised.

Lookups made together are merged, as any reads are: ten names are one `eth_call` to Multicall3
on the transport of the names. An offchain name among them is still followed: its lookup comes
back as the failure of its call alone. [`Call.batching(false)`](https://ethect.dev/reference/call/batching/),
provided to the layer, sends each lookup on its own.
