# Ens.avatar

The `avatar` record of a name, turned into a URL an image can be loaded from.

## 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 avatar = yield* Ens.avatar("ens.eth");
  // Option.some("https://i.imgur.com/ga6y0c0.jpg")
  return Option.getOrUndefined(avatar);
});

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

Set what it gives as the `src` of an image. The examples below leave the layer out: see
[`Ens.layer`](https://ethect.dev/reference/ens/layer/).

### What a record can be

| The record | Gives |
| --- | --- |
| `https://example.com/me.png` | the same URL |
| `ipfs://bafkrei…`, a bare content identifier, a URL of any IPFS gateway | `https://ipfs.io/ipfs/bafkrei…` |
| `ipns://example.eth/me.png` | `https://ipfs.io/ipns/example.eth/me.png` |
| `ar://rgW4h3ff…` | `https://arweave.net/rgW4h3ff…` |
| `data:image/png;base64,…` | the same data URI |
| SVG written out, alone or in a `data:` URI | a `data:image/svg+xml;base64,…` URI |
| `eip155:1/erc721:0xbc4c…/42`, or `erc1155` | the image of that token: see below |
| anything else | `Option.none` |

The gateways are those of [`Ens.layer({ gateways })`](https://ethect.dev/reference/ens/layer/#optionsgateways-optional).

### With a fallback

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

const avatarOf = (name: string) =>
  Ens.avatar(name).pipe(
    Effect.map(Option.getOrElse(() => "/placeholder.svg")),
    // a name that is no name has no avatar either
    Effect.catchTag("EnsError", () => Effect.succeed("/placeholder.svg")),
  );
```

## Returns

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

A URL: `https`, `http` or `data`. `Option.none` when the name has no avatar, or when its
record leads to no image.

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

## Behavior

- **The record is read as any text record.** `Ens.avatar(name)` starts with
  [`Ens.text(name, "avatar")`](https://ethect.dev/reference/ens/text/), and fails as it fails.
- **From there, a step that fails gives none.** A record that is no URI, a token that cannot
  be read, metadata that is not there: `Option.none`, not a failure.
- **The URL is not asked for.** Whether an image is behind it is for the page that loads it to
  find out. viem asks it with a `HEAD` request first, and gives `null` unless it answers as an
  image.
- **An NFT is followed** ([ENSIP-12](https://docs.ens.domains/ensip/12)): the URI of the token
  is read from its contract, its metadata is read from that URI, and the `image` it names is
  given. Metadata kept onchain, as a `data:` URI or as JSON, is read without a request.
- **A token of another chain is not read.** The record names a chain. When it is not the chain
  of the names' transport, the avatar is none: the same address is another contract there.
- **The owner of the token is not checked.** A name can show a token it does not hold.
- **The URL comes from a record anyone can write.** Set it as the `src` of an image, never as
  HTML or as a link.

### What the metadata of an NFT is asked with

The URI of a token is chosen by its contract: like a gateway of an
[offchain name](https://ethect.dev/reference/ens/#offchain-names), it is a server someone else chose. It is sent
one `GET`, with nothing added and no trace header, and it is bounded the same way: `https`
only, ten seconds, 10 MiB.

## 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, for the record |
| `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 the read of the record with an error that is not a revert |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/) | | the node could not be reached for the record |

## 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/), and the `HttpClient` when the avatar is an NFT whose
metadata is on a server.

**`Ens.avatar(name)`**

- **the record** `Ens.text(name, "avatar")`: one `eth_call`, as `Ens.text` makes it
- **a URI**: `https`, `ipfs`, `ipns`, `ar`, `data` → a URL. No request

**the record names a token: `eip155:<chain>/<erc721|erc1155>:<contract>/<id>`**

- **Transport** `eth_chainId`: once for the transport: to know which chain the names are on
- **Call.Reader** `eth_call({ to: contract, data }, "latest")`: `tokenURI(id)`, or `uri(id)` for ERC-1155
- **HttpClient** `GET`: of the metadata, unless it is onchain. `{id}` replaced in an ERC-1155 template
- **a URI**: the image the metadata names → a URL. No request

The `eth_call`s are those viem's `getEnsAvatar` sends for the same name. It sends one request
more, the `HEAD` of the URL, and none for the chain.
