Skip to content

Ens.avatar

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

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.

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 }).

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")),
);

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.

  • Type: string

The name, as it was typed. It is normalised by the rules of ENSIP-15 before it is hashed.

  • The record is read as any text record. Ens.avatar(name) starts with Ens.text(name, "avatar"), 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): 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.

The URI of a token is chosen by its contract: like a gateway of an offchain name, 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.

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 the node answered the read of the record with an error that is not a revert
TransportError the node could not be reached for the record

The Transport of the layer is asked, through Call.Reader, and the HttpClient when the avatar is an NFT whose metadata is on a server.

  1. Ens.avatar(name)

    1. the recordEns.text(name, "avatar")one eth_call, as Ens.text makes it
    2. a URIhttps, ipfs, ipns, ar, data → a URL. No request
  2. the record names a token: eip155:<chain>/<erc721|erc1155>:<contract>/<id>

    1. Transporteth_chainIdonce for the transport: to know which chain the names are on
    2. Call.Readereth_call({ to: contract, data }, "latest")tokenURI(id), or uri(id) for ERC-1155
    3. HttpClientGETof the metadata, unless it is onchain. {id} replaced in an ERC-1155 template
    4. a URIthe image the metadata names → a URL. No request

The eth_calls 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.