Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions components/Treasury/TreasuryVoteTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,34 @@ import { MobileVoteCards } from "./Views/MobileVoteTable";
// Module-level cache to avoid repeated ENS lookups across renders/navigations.
// Bound size keeps memory usage predictable during long sessions.
const ENS_CACHE_MAX_ENTRIES = 2000;
const ensCache = new Map<string, string>();
const ENS_CACHE_TTL_MS = 30 * 60 * 1000; // 30 minutes
const ensCache = new Map<
string,
{ nameOrAddress: string; timestamp: number }
>();
const ensLookupInFlight = new Map<string, Promise<string>>();

const getCachedEns = (address: string) => {
if (!ensCache.has(address)) return undefined;
const cached = ensCache.get(address)!;

if (Date.now() - cached.timestamp > ENS_CACHE_TTL_MS) {
ensCache.delete(address);
return undefined;
}

// Refresh insertion order so frequently used addresses stay cached.
ensCache.delete(address);
ensCache.set(address, cached);

return cached;
return cached.nameOrAddress;
};

const setCachedEns = (address: string, ensName: string) => {
if (ensCache.has(address)) {
ensCache.delete(address);
}
ensCache.set(address, ensName);
ensCache.set(address, { nameOrAddress: ensName, timestamp: Date.now() });

if (ensCache.size > ENS_CACHE_MAX_ENTRIES) {
const oldestKey = ensCache.keys().next().value;
Expand Down Expand Up @@ -107,12 +116,11 @@ const useVotes = (proposalId: string) => {
const uniqueVoters = Array.from(
new Set(treasuryVotesData?.treasuryVotes?.map((v) => v.voter.id) ?? [])
);
const localEnsCache: { [address: string]: string } = {};

await Promise.all(
uniqueVoters.map(async (address) => {
try {
localEnsCache[address] = await resolveEnsName(address);
await resolveEnsName(address.toLowerCase());
} catch (e) {
console.warn(`Failed to fetch ENS for ${address}`, e);
}
Expand All @@ -125,7 +133,7 @@ const useVotes = (proposalId: string) => {
.sort((a, b) => b.timestamp - a.timestamp);

const latestEvent = events[0];
const ensName = localEnsCache[vote.voter.id] ?? "";
const ensName = getCachedEns(vote.voter.id.toLowerCase()) ?? "";

return {
...vote,
Expand Down