Skip to content

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.

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

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.

  • 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
  • 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 url is not known to be a URL, and a com.twitter is not known to be that person’s account. Show it as text, never as HTML.
  • The key is not normalised. URL and url are two records.
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

The Transport of the layer is asked, through Call.Reader.

  1. Ens.text("vitalik.eth", "url")

    1. normalise"vitalik.eth". No request
    2. encodetext(namehash("vitalik.eth"), "url"), the name as DNS. No request
    3. Call.Readereth_call({ to: UniversalResolver, data }, "latest")data: resolveWithGateways(name, text(node, key), ["x-batch-gateway:true"])
    4. decode(bytes result, address resolver) → the text. No request
  2. an offchain name: the eth_call reverts with OffchainLookup

    1. HttpClientGET or POSTto the gateway its resolver names
    2. Call.Readereth_call({ to: UniversalResolver, data: the call back }, "latest")

This is the eth_call viem’s getEnsText sends for the same name, once normalised. See offchain names for what a gateway is sent.