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.
What a record can be
Section titled “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 }).
With a fallback
Section titled “With a fallback”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
Section titled “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
Section titled “Parameters”- Type:
string
The name, as it was typed. It is normalised by the rules of ENSIP-15 before it is hashed.
Behavior
Section titled “Behavior”- The record is read as any text record.
Ens.avatar(name)starts withEns.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
HEADrequest first, and givesnullunless 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
imageit names is given. Metadata kept onchain, as adata: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
srcof an image, never as HTML or as a link.
What the metadata of an NFT is asked with
Section titled “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, 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
Section titled “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 |
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 |
Under the hood
Section titled “Under the hood”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.
Ens.avatar(name)- the record
Ens.text(name, "avatar")oneeth_call, asEns.textmakes it - a URI
https,ipfs,ipns,ar,data→ a URL. No request
- the record
the record names a token:
eip155:<chain>/<erc721|erc1155>:<contract>/<id>- Transport
eth_chainIdonce for the transport: to know which chain the names are on - Call.Reader
eth_call({ to: contract, data }, "latest")tokenURI(id), oruri(id)for ERC-1155 - HttpClient
GETof the metadata, unless it is onchain.{id}replaced in an ERC-1155 template - a URIthe image the metadata names → a URL. No request
- Transport
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.