Skip to content

Commit

Permalink
added ENS caching & max ENS lookups capped to 5
Browse files Browse the repository at this point in the history
still TODO: only lookup addresses for zones that are visible on the screen
  • Loading branch information
Steedie committed May 15, 2024
1 parent c25d54b commit 4fb509e
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions frontend/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,34 @@ export const decodeString = (value: string): string => {
}
};

export async function lookupENSName(address: string) {
const ensCache: { [address: string]: string | null } = {};
let ensActiveChecks = 0;

export async function lookupENSName(address: string): Promise<string | null> {
// Check if the address is already in the ensCache
if (ensCache.hasOwnProperty(address)) {
return ensCache[address];
}

// Lookup max 5 addresses at a time
if (ensActiveChecks >= 5) {
return null;
}

const ensProvider = ethers.getDefaultProvider('mainnet');
try {
ensActiveChecks++;
ensCache[address] = null;
const name = await ensProvider.lookupAddress(address);
return name || address;
// Cache the result, even if it's null
ensCache[address] = name;
return name;
} catch (error) {
console.error('ENS lookup failed', error);
return address;
ensCache[address] = null;
return null;
} finally {
ensActiveChecks--;
}
}

Expand Down

0 comments on commit 4fb509e

Please sign in to comment.