Skip to content

Commit

Permalink
Cache the registered tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Nov 6, 2024
1 parent 0805993 commit 5e4ee44
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bridge_ui/src/utils/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ export type RegisteredTokenInfo = {
logo?: string
}

let _registeredTokens: RegisteredTokenInfo[] | undefined = undefined

export async function getRegisteredTokens(): Promise<RegisteredTokenInfo[]> {
try {
if (_registeredTokens !== undefined) return _registeredTokens

const response = await fetch(`${EXPLORER_API_SERVER_HOST}/api/stats/tokens`)
if (!response.ok) {
throw new Error(`${i18n.t('Failed to get tokens')}, ${i18n.t('response status')}: ${response.status}`)
Expand All @@ -27,7 +31,7 @@ export async function getRegisteredTokens(): Promise<RegisteredTokenInfo[]> {
if (!Array.isArray(tokenList)) {
throw new Error(i18n.t('Invalid response, expect a token list'))
}
return (tokenList as any[]).map((item) => {
const tokens = (tokenList as any[]).map((item) => {
const symbol = (item['symbol'] as string).toUpperCase()
return {
tokenAddress: item['tokenAddress'],
Expand All @@ -39,6 +43,8 @@ export async function getRegisteredTokens(): Promise<RegisteredTokenInfo[]> {
logo: symbol === 'ALPH' ? alephiumIcon : symbol === 'WETH' ? ethIcon : symbol === 'WBNB' ? bscIcon : undefined
}
})
_registeredTokens = tokens
return tokens
} catch (error) {
console.log(error)
return []
Expand Down

0 comments on commit 5e4ee44

Please sign in to comment.