Skip to content

Commit

Permalink
reduced reads of celo token list json file (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: LewisB <[email protected]>
  • Loading branch information
krisbitney and L03TJ3 authored Feb 1, 2024
1 parent f5f478c commit 1f03125
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/app/src/hooks/useTokenList.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { useMemo } from 'react';
import { Token } from '@uniswap/sdk-core';
import CeloTokenList from '../models/CeloTokenList.json';
import { SupportedNetwork } from '../models/constants';

const populatedTokenList: Record<string, Token> = populateTokenList();

function populateTokenList(): Record<string, Token> {
const tokenList: Record<string, Token> = {};
const sortedList = CeloTokenList.tokens.sort((a, b) => a.symbol.localeCompare(b.symbol));
sortedList.forEach((token) => {
if (token.chainId !== SupportedNetwork.CELO) {
return;
}
tokenList[token.symbol] = new Token(token.chainId, token.address, token.decimals, token.symbol);
});
return tokenList;
}

export function useToken(symbol: string): Token {
return useTokenList()[symbol];
return populatedTokenList[symbol];
}

export function useTokenList(): Record<string, Token> {
return useMemo(() => {
const tokenList: Record<string, Token> = {};
const sortedList = CeloTokenList.tokens.sort((a, b) => a.symbol.localeCompare(b.symbol));
sortedList.forEach((token) => {
if (token.chainId !== SupportedNetwork.CELO) {
return;
}
tokenList[token.symbol] = new Token(token.chainId, token.address, token.decimals, token.symbol);
});
return tokenList;
}, []);
return populatedTokenList;
}

0 comments on commit 1f03125

Please sign in to comment.