Skip to content

Commit

Permalink
fix: cache list of resolved addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Mar 13, 2024
1 parent 671aa1b commit 998e3df
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions apps/ui/src/helpers/stamp.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import { formatAddress } from './utils';

const resolvedAddresses = new Map<string, string>();

export async function getNames(addresses: string[]): Promise<Record<string, string>> {
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);
Expand Down

0 comments on commit 998e3df

Please sign in to comment.