accounts
Version:
Tempo Accounts SDK
38 lines • 1.68 kB
JavaScript
import { cached } from './kv.js';
/** Default cache TTL in seconds (10 minutes) for tokenlist responses. */
const defaultCacheTtl = 10 * 60;
/** Default base URL for the verified tokenlist service. */
const defaultBaseUrl = 'https://tokenlist.tempo.xyz';
/**
* Fetches the verified tokenlist for `chainId`. Reads through `kv` so
* concurrent callers in the same scope share a single upstream request,
* and so independent handlers (e.g. `Handler.relay` + `Handler.exchange`)
* reuse the same KV cache key.
*
* Returns an empty list on any non-OK response — callers should fall back
* to chain-supplied behavior rather than treating an empty list as fatal.
*
* @param chainId - Chain id to fetch the tokenlist for.
* @param kv - Kv used to cache responses across requests.
* @param options - Options.
* @returns Tokens for the chain.
*/
export async function fetch(chainId, kv, options = {}) {
const { baseUrl = defaultBaseUrl, cacheTtl = defaultCacheTtl, resolver } = options;
return cached(kv, `tokenlist:${chainId}`, async () => (resolver ? resolver(chainId) : fetchUncached(chainId, baseUrl)), { ttl: cacheTtl });
}
/**
* Default fetcher — resolves the verified tokenlist for `chainId` directly
* from `tokenlist.tempo.xyz`. Returns an empty list on any non-OK response.
*/
async function fetchUncached(chainId, baseUrl) {
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,
}));
}
//# sourceMappingURL=tokenlist.js.map