Ens.text
A text record of a name, or none when the record is empty.
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 url = yield* Ens.text("vitalik.eth", "url"); // Option.some("https://vitalik.ca") const github = yield* Ens.text("vitalik.eth", "com.github"); return { url: Option.getOrNull(url), github: Option.getOrNull(github) };});
const records = await program.pipe(Effect.provide(Names), Effect.runPromise);The examples below leave the layer out: see Ens.layer.
A profile
Section titled “A profile”Each record is one read. Asked for together, they are merged into one request.
import { Effect } from "effect";import { Ens } from "@ethect/core/ens";
const keys = ["description", "url", "com.twitter", "com.github"] as const;
const profile = (name: string) => Effect.all( Object.fromEntries(keys.map((key) => [key, Ens.text(name, key)])), { concurrency: "unbounded" }, );Returns
Section titled “Returns”Effect<Option<string>, Ens.Error | Transport.Error, Ens.Ens>
The record, as its owner wrote it, or Option.none when it is empty or not there.
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.
- Type:
string
Which record. Any text is a key: ENSIP-5 names the usual ones.
| Key | Holds |
|---|---|
avatar |
an image: see Ens.avatar, which reads it |
description, url, email, location |
what they say |
com.twitter, com.github, org.telegram |
an account on a service, by its reversed domain |
Behavior
Section titled “Behavior”- One read. The Universal Resolver finds the resolver of the name and asks it for
text(node, key). - None when there is nothing to find. No resolver, a resolver without text records, an
empty record:
Option.none. - A record is what its owner wrote. It is not checked, trimmed or escaped. A
urlis not known to be a URL, and acom.twitteris not known to be that person’s account. Show it as text, never as HTML. - The key is not normalised.
URLandurlare two records.
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 |
EnsError |
"Resolver" |
the record is not text, the Universal Resolver reverted, or there is no contract at its address |
RpcError |
the node answered with an error that is not a revert | |
TransportError |
the node could not be reached, or did not answer |
Under the hood
Section titled “Under the hood”The Transport of the layer is asked, through
Call.Reader.
Ens.text("vitalik.eth", "url")- normalise
"vitalik.eth". No request - encode
text(namehash("vitalik.eth"), "url"), the name as DNS. No request - Call.Reader
eth_call({ to: UniversalResolver, data }, "latest")data:resolveWithGateways(name, text(node, key), ["x-batch-gateway:true"]) - decode
(bytes result, address resolver)→ the text. No request
- normalise
an offchain name: the
eth_callreverts withOffchainLookup- HttpClient
GET or POSTto the gateway its resolver names - Call.Reader
eth_call({ to: UniversalResolver, data: the call back }, "latest")
- HttpClient
This is the eth_call viem’s getEnsText sends for the same name, once normalised. See
offchain names for what a gateway is sent.