Skip to content

Ens.address

The address a name points to, or none when the name has no resolver or no address.

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 address = yield* Ens.address("vitalik.eth");
// Option.some("0xd8da6bf26964af9d7eed9e03e53415d37aa96045")
return Option.getOrUndefined(address);
});
const address = await program.pipe(Effect.provide(Names), Effect.runPromise);

The examples below leave the layer out: see Ens.layer.

The name is normalised for you. Pass what the user wrote.

import { Effect } from "effect";
import { Ens } from "@ethect/core/ens";
// the same lookup as "vitalik.eth": one request, for the same name
const typed = Ens.address(" Vitalik.ETH".trim());
// refused before anything is sent: EnsError "InvalidName"
const refused = Ens.address("vitalik .eth").pipe(
Effect.catchTag("EnsError", (error) => Effect.succeed(error.message)),
// Not a valid name: Invalid label "vitalik ": disallowed character: " " {20}
);

A name can hold one address per chain. chainId asks for the one on an EVM chain.

import { Ens } from "@ethect/core/ens";
const BASE = 8453;
const onBase = Ens.address("jesse.base.eth", { chainId: BASE });
// Without a chain, the address is the one on Ethereum.
const onEthereum = Ens.address("jesse.base.eth");

You never write a coin type: the chain id is turned into the one a resolver knows the chain by (ENSIP-11).

chainId The resolver is asked for coin type
1 60, Ethereum’s
any other, such as 8453 0x80000000 + chainId

A form field takes either. An address is itself, and a name is looked up.

import { Effect, Option } from "effect";
import { Address } from "@ethect/core";
import { Ens } from "@ethect/core/ens";
const recipient = (typed: string) =>
Effect.matchEffect(Address.make(typed), {
onSuccess: (address) => Effect.succeed(Option.some(address)),
// what is not an address may be a name
onFailure: () => Ens.address(typed),
});

Effect<Option<Address.Address>, Ens.Error | Transport.Error, Ens.Ens>

The address, in lowercase, or Option.none when the name has none.

  • Type: string

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

  • Type: number
  • Default: none: the address on Ethereum

The id of an EVM chain: a whole number from 0 to 2,147,483,647. Only EVM chains can be asked for, since their addresses are the twenty bytes an Address.Address is.

  • One read. The Universal Resolver finds the resolver of the name, asks it, and gives its answer: one eth_call.
  • None when there is nothing to find. No resolver, no address record, an empty one, the zero address: Option.none. See no answer is not a failure.
  • An offchain name resolves the same way, with one request to its gateway and one more eth_call. See offchain names.
  • With chainId: 1, the resolver is asked addr(node, 60), not addr(node). The two are the same record in most resolvers.
  • An address a resolver answers bare is read. addr(node, coinType) returns bytes. Some resolvers answer twenty bytes or one word instead.
  • The address is not checksummed. Every Address.Address is lowercase: Address.checksum gives the mixed case, to show it.
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 an address, the Universal Resolver reverted, or there is no contract at its address
EnsError "Resolver" chainId is no chain id, such as -1 or 1.5: nothing was sent
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.address("Vitalik.ETH")

    1. normalise"vitalik.eth". No request
    2. encodeaddr(namehash("vitalik.eth")), and the name as DNS. No request
    3. Call.Readereth_call({ to: UniversalResolver, data }, "latest")data: resolveWithGateways(name, addr(node), ["x-batch-gateway:true"])
    4. decode(bytes result, address resolver) → the address. 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")
Call The resolver is asked
Ens.address(name) addr(bytes32 node)
Ens.address(name, { chainId }) addr(bytes32 node, uint256 coinType), with the coin type of the chain

This is the eth_call viem’s getEnsAddress sends for the same name, once normalised.

Lookups made together are merged, as any reads are: ten names are one eth_call to Multicall3 on the transport of the names. An offchain name among them is still followed: its lookup comes back as the failure of its call alone. Call.batching(false), provided to the layer, sends each lookup on its own.