import { useState, useEffect } from "react"; import { ethers } from "ethers"; import { ENSReverseCache, ProcessedTransaction } from "./types"; export const useENSCache = ( provider?: ethers.providers.JsonRpcProvider, page?: ProcessedTransaction[] ) => { const [reverseCache, setReverseCache] = useState(); useEffect(() => { if (!provider || !page) { return; } const addrSet = new Set(); for (const tx of page) { if (tx.from) { addrSet.add(tx.from); } if (tx.to) { addrSet.add(tx.to); } } const addresses = Array.from(addrSet); const reverseResolve = async () => { const solvers: Promise[] = []; for (const a of addresses) { solvers.push(provider.lookupAddress(a)); } const results = await Promise.all(solvers); const cache: ENSReverseCache = {}; for (let i = 0; i < results.length; i++) { if (results[i] === null) { continue; } cache[addresses[i]] = results[i]; } setReverseCache(cache); }; reverseResolve(); }, [provider, page]); return reverseCache; };