Skip to content

Commit

Permalink
fix: refactor getTokenConfig function in token service
Browse files Browse the repository at this point in the history
  • Loading branch information
VGau committed Oct 30, 2024
1 parent 2283d72 commit dceefd1
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions bridge-ui/src/services/tokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,33 @@ export async function fetchTokenPrices(
return data;
}

export async function validateTokenURI(url: string): Promise<string> {
try {
await fetch(url);
return url;
} catch (error) {
return "/images/logo/noTokenLogo.svg";
}
}

export async function formatToken(token: Token): Promise<TokenInfo> {
const tokenType = token.symbol === USDC_TYPE ? TokenType.USDC : TokenType.ERC20;

const logoURI = await validateTokenURI(token.logoURI);

return {
name: token.name,
symbol: token.symbol,
decimals: token.decimals,
type: tokenType,
L1: token?.extension?.rootAddress ?? null,
L2: token.address,
UNKNOWN: null,
image: logoURI,
isDefault: true,
};
}

export async function getTokenConfig(): Promise<NetworkTokens> {
const [mainnetTokens, sepoliaTokens] = await Promise.all([
getTokens(NetworkTypes.MAINNET),
Expand All @@ -182,46 +209,12 @@ export async function getTokenConfig(): Promise<NetworkTokens> {

updatedTokensConfig.MAINNET = [
...defaultTokensConfig.MAINNET,
...(await Promise.all(
mainnetTokens.map(async (token: Token): Promise<TokenInfo> => {
const tokenType = token.symbol === USDC_TYPE ? TokenType.USDC : TokenType.ERC20;
try {
await fetch(token.logoURI);
} catch (error) {
token.logoURI = "/images/logo/noTokenLogo.svg";
}

return {
name: token.name,
symbol: token.symbol,
decimals: token.decimals,
type: tokenType,
L1: token?.extension?.rootAddress ?? null,
L2: token.address,
UNKNOWN: null,
image: token.logoURI,
isDefault: true,
};
}),
)),
...(await Promise.all(mainnetTokens.map(async (token: Token): Promise<TokenInfo> => formatToken(token)))),
];

updatedTokensConfig.SEPOLIA = [
...defaultTokensConfig.SEPOLIA,
...sepoliaTokens.map((token: Token): TokenInfo => {
const tokenType = token.symbol === USDC_TYPE ? TokenType.USDC : TokenType.ERC20;
return {
name: token.name,
symbol: token.symbol,
decimals: token.decimals,
type: tokenType,
L1: token?.extension?.rootAddress ?? null,
L2: token.address,
UNKNOWN: null,
image: token.logoURI,
isDefault: true,
};
}),
...(await Promise.all(sepoliaTokens.map((token: Token): Promise<TokenInfo> => formatToken(token)))),
];

return updatedTokensConfig;
Expand Down

0 comments on commit dceefd1

Please sign in to comment.