diff --git a/apps/ui/src/helpers/stamp.ts b/apps/ui/src/helpers/stamp.ts index 22d7653de..8ec87f5a1 100644 --- a/apps/ui/src/helpers/stamp.ts +++ b/apps/ui/src/helpers/stamp.ts @@ -1,22 +1,41 @@ import { formatAddress } from './utils'; +const resolvedAddresses = new Map(); + export async function getNames(addresses: string[]): Promise> { try { const inputMapping = Object.fromEntries( addresses.map(address => [address, formatAddress(address)]) ); + const resolvedAddressesKeys = Array.from(resolvedAddresses.keys()); + const unresolvedAddresses = Object.values(inputMapping).filter( + address => !resolvedAddressesKeys.includes(address) + ); + let data: string[] = []; + + if (unresolvedAddresses.length > 0) { + const res = await fetch('https://stamp.fyi', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + method: 'lookup_addresses', + params: unresolvedAddresses + }) + }); + data = (await res.json()).result; - const res = await fetch('https://stamp.fyi', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ method: 'lookup_addresses', params: Object.values(inputMapping) }) - }); - const data = (await res.json()).result; + Object.keys(data).forEach((key: string) => { + resolvedAddresses.set(key, data[key]); + }); + } const entries: any = Object.entries(inputMapping) - .map(([address, formatted]) => [address, data[formatted] || null]) + .map(([address, formatted]) => [ + address, + data[formatted] || resolvedAddresses.get(formatted) || null + ]) .filter(([, name]) => name); return Object.fromEntries(entries);