我正在尝试从配置文件中获取所有 ENS 文本记录,但在逻辑或可能的方法方面存在问题...
使用 ethers.js v6,我当前的解决方案是获取所有日志数据并对其进行筛选。唯一的问题是,有太多与事件签名(存储文本记录)匹配的日志数据。最好的方法是使用 ethers.js 获取所有交易,这些交易源自配置文件并在 ENS 结束,然后使用这些块哈希查看日志......但看起来 ethers 没有 getTransactions() 函数或类似的函数.
最好的方法是什么?
当前日志实现:
const publicResolverContractAddress = '0x231b0Ee14048e9dCcD1d247744d114a4EB5E8E63';
const eventTopic = [
'0x448bc014f1536726cf8d54ff3d6481ed3cbc683c2591ca204274009afa09b1a1',
'0xc0e482f545380f3c76a14db8d5717d205abfdc508449ed2befe6d5427a9d45cd',
'0x36c0d6cc28ff7763f02401a2d40c3a72c4dfdd8a6b812dfa978274306592721e'
];
let logs = await provider.getLogs({
address: publicResolverContractAddress,
fromBlock: '0x1024592',
toBlock: '0x107D3F5',
topics: [eventTopic]
})
好消息!这个问题有一个食谱示例: https://docs.ethers.org/v6/cookbook/ens/
坏消息,他们正在采用与您完全相同的模型来遍历所有日志。 为了更有效的方法,您需要使用 ens.js 或 ENS 子图之类的东西。
如果文档发生变化,以下是当前版本:
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"));
})();