Address Lookup
The ENS Protocol aims to make it easy to use Ethereum. It does this by providing a simple way to use human-readable names instead of long machine-readable addresses.
Forward Lookup
The goal here is to take a name, such as nick.eth
, and convert it to an address, such as 0x225f137127d9067788314bc7fcc1f36746a3c3B5
.
The simplest thing you can do is start with a name, and resolve it to an address. We call this a "forward lookup". Think of places where users can enter names, such as sending transactions, chatting, etc.
Note that all dot-separated strings should be treated as potential ENS names, since ENS supports many TLDs. A common mistake is to only treat strings that end in .eth
as ENS names.
import { useAccount, useEnsName, useEnsAvatar } from "wagmi";
export const Name = () => {
const { data: ensName } = useEnsAddress({
address: "luc.eth", // The name to lookup
chainId: 1, // The chainId to lookup on
});
return <div>{ensName || address}</div>;
};
To learn what happens under the hood when you do a forward lookup, read the resolution section.
Multi-Chain
ENS Names aren't just limited to storing Ethereum addresses. Any blockchain address (BTC, LTC, SOL, etc.) can be queried by SLIP-0044 coin type or a value derived from an EVM Chain ID (specified in ENSIP-11). This includes Ethereum L2 networks such as OP Mainnet and Base.
For EVM Chains besides Mainnet Ethereum, always use its ENSIP-11 coin type, irrespective of being included in SLIP-0044 (like Ether Classic).
The standardization of multichain addresses was first introduced in ENSIP-9, and also EIP-2304.
import { useAccount, useEnsAddress, useEnsAvatar, useEnsName } from 'wagmi';
import { formatAddress } from '@ens-tools/format';
export const MyAddresses = () => {
const { data: name } = useEnsName({ address: "0x225f137127d9067788314bc7fcc1f36746a3c3B5" });
// SLIP-0044 Coin Types
const { data: bitcoin } = useEnsAddress({ name, coinType: 0 });
const { data: litecoin } = useEnsAddress({ name, coinType: 2 });
const { data: solana } = useEnsAddress({ name, coinType: 501 });
// EVM Chain IDs (see ENSIP-9)
const { data: optimism } = useEnsAddress({ name, coinType: 2147483658 });
const { data: base } = useEnsAddress({ name, coinType: 2147492101 });
const { data: polygon } = useEnsAddress({ name, coinType: 2147483658 });
const { data: arbitrum } = useEnsAddress({ name, coinType: 2147525809 });
return <div>{JSON.stringify({ name, bitcoin, litecoin, solana, optimism, base, polygon, arbitrum })}</div>;
};
Network | Coin Type |
---|---|
Bitcoin | 0 |
Litecoin | 2 |
Dogecoin | 3 |
Ethereum | 60 |
Solana | 501 |
OP Mainnet | 2147483658 |
Polygon | 2147483785 |
Base | 2147492101 |
Arbitrum One | 2147525809 |
ENS resolvers store all addresses in bytes, which may have to be encoded to their respective address formats. To do this, we recommend using the @ensdomains/address-encoder package.