resolver.getText but returns all of the text records #4321
Replies: 2 comments 8 replies
-
This isn’t something that can be done by querying the contract, but you could easily write an event filter which would query all the text event changes and flatten them into the most recent value for each key. |
Beta Was this translation helpful? Give feedback.
-
Here is a quick example of how this can be done using event filters. It is currently quite inefficient, but I will include the updated version in the cookbook, which will use the (soon to be released) MulticallProvider, which will reduce this to 2 eth_calls and an eth_getLogs. This version is import { ethers } from "ethers";
const abi = [
"event TextChanged(bytes32 indexed nodehash, string indexed _key, string key)"
];
async function getTextRecords(provider, name) {
// Get the resolver for the name
const resolver = await provider.getResolver(name);
const contract = new ethers.Contract(resolver.address, abi, provider);
// Get all the TextChanged logs for the name on its resolver
const logs = await contract.queryFilter(contract.filters.TextChanged(ethers.namehash(name)));
// Get the *unique* keys
const keys = [ ...(new Set(logs.map((log) => log.args.key))) ];
// Get the values for the keys
const values = await Promise.all(keys.map((key) => {
try {
return resolver.getText(key);
} catch (error) { }
return null;
}));
// Return a nice dictionary of the key/value pairs
return keys.reduce((accum, key, index) => {
const value = values[index];
if (value != null) { accum[key] = value; }
return accum;
}, { });
}
(async function() {
const provider = new ethers.InfuraProvider();
console.log(await getTextRecords(provider, "ricmoo.eth"));
})(); |
Beta Was this translation helpful? Give feedback.
-
Interested to see if this exists or if there will be value adding a method to the resolver to return all the text record keys and their associated values. Currently resolver.getText('email') only returns a single key record, but would be great to have a method to return all keys and their values.
What are your thoughts?
Beta Was this translation helpful? Give feedback.
All reactions