ethers
6.14.3
DOCUMENTATION
Getting Started
Ethereum Basics
Application Programming Interface
Cookbook
Cookbook: ENS Recipes
Get all Text records
React Native
Signing
Migrating from v5
Contributions and Hacking
License and Copyright
Single Page
Documentation »Cookbook »Cookbook: ENS Recipes
 Cookbook: ENS Recipes

Here is a collection of short, but useful examples of working with ENS entries.

 Get all Text records

Here is a short recipe to get all the text records set for an ENS name.

It first queries all TextChanged events on the resolver, and uses a MulticallProvider to batch all the eth_call queries for each key into a single eth_call. As such, you will need to install:

/home/ricmoo> npm install @ethers-ext/provider-multicall

Fetching all ENS text records.
import { ethers } from "ethers"; import { MulticallProvider } from "@ethers-ext/provider-multicall"; async function getTextRecords(_provider, name) { // Prepare a multicall-based provider to batch all the call operations const provider = new MulticallProvider(_provider); // Get the resolver for the given name const resolver = await provider.getResolver(name); // A contract instance; used filter and parse logs const contract = new ethers.Contract(resolver.address, [ "event TextChanged(bytes32 indexed node, string indexed _key, string key)" ], provider); // A filter for the given name const filter = contract.filters.TextChanged(ethers.namehash(name)); // Get the matching logs const logs = await contract.queryFilter(filter); // Filter the *unique* keys const keys = [ ...(new Set(logs.map((log) => log.args.key))) ]; // Get the values for the keys; failures are discarded const values = await Promise.all(keys.map((key) => { try { return resolver.getText(key); } catch (error) { } return null; })); // Return a Map of the key/value pairs return keys.reduce((accum, key, index) => { const value = values[index]; if (value != null) { accum.set(key, value); } return accum; }, new Map()); } // Example usage (async function() { const provider = new ethers.InfuraProvider(); console.log(await getTextRecords(provider, "ricmoo.eth")); })();
← Cookbook
React Native→
The content of this site is licensed under the Creative Commons License. Generated on December 3, 2024, 8:20pm.