accounts
Version:
Tempo Accounts SDK
52 lines • 1.93 kB
JavaScript
/** Default base URL for the curated tempo tokenlist. */
const defaultBaseUrl = 'https://tokenlist.tempo.xyz';
/** Cache keyed by `${baseUrl}|${chainId}` so concurrent callers share a single fetch. */
const cache = new Map();
/**
* Fetches the curated tokenlist for a given Tempo chain. Concurrent calls
* for the same chain share a single in-flight request, and successful
* responses are cached for the lifetime of the process.
*
* Returns an empty list on any non-OK response so callers can fall back
* to chain-supplied behavior rather than treating a fetch failure as fatal.
*/
export async function fetch(options) {
const { chainId, baseUrl = defaultBaseUrl } = options;
const key = `${baseUrl}|${chainId}`;
const existing = cache.get(key);
if (existing)
return existing;
const pending = (async () => {
try {
const response = await globalThis.fetch(`${baseUrl}/list/${chainId}`);
if (!response.ok)
return [];
const data = (await response.json());
return data.tokens.map(({ logoURI, ...token }) => ({
...token,
logoUri: token.logoUri ?? logoURI,
}));
}
catch {
return [];
}
})();
cache.set(key, pending);
// Drop failed/empty results so the next caller retries.
pending.then((tokens) => {
if (tokens.length === 0)
cache.delete(key);
});
return pending;
}
/**
* Resolves a token symbol (case-insensitive) against the curated tokenlist
* for a given chain. Returns the token entry, or `undefined` if no match.
*/
export async function resolveSymbol(options) {
const { symbol, ...rest } = options;
const tokens = await fetch(rest);
const lowered = symbol.toLowerCase();
return tokens.find((token) => token.symbol.toLowerCase() === lowered);
}
//# sourceMappingURL=Tokenlist.js.map