From f5f51ddf3ed0fde2cfa538cd9c9e85481bfa54f8 Mon Sep 17 00:00:00 2001 From: Nathan Seva Date: Wed, 5 Jun 2024 11:50:02 -0500 Subject: [PATCH] improve get icon ft (#455) * improve get icon ft * fix and add story --- .../Icons/Svg/FT/FTIcons.stories.tsx | 42 ++++++++++++++++++ src/components/Icons/Svg/FT/tokenIcons.tsx | 43 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/components/Icons/Svg/FT/FTIcons.stories.tsx b/src/components/Icons/Svg/FT/FTIcons.stories.tsx index c39bc88f..267feb9a 100644 --- a/src/components/Icons/Svg/FT/FTIcons.stories.tsx +++ b/src/components/Icons/Svg/FT/FTIcons.stories.tsx @@ -22,6 +22,20 @@ const bscSymbolList = ['DAI', 'BNB', 'USDC', 'WETH', 'USDT']; const ethSymbolList = ['DAI', 'USDC', 'WETH']; +const allSymbols = [ + 'WETH.e', + 'WETH.s', + 'WETH.b', + 'WETH.bt', + 'USDC.e', + 'USDC.s', + 'USDT.b', + 'USDT.bt', + 'DAI.e', + 'tDAI.s', + 'tDAI', +]; + export const _FT = { render: () => ( <> @@ -131,3 +145,31 @@ export const _FT = { ), }; + +export const _FT2 = { + render: () => ( + <> +
+

Guess the origin chain from the symbol

+ +
+ {allSymbols.map((symbol: string, index: number) => { + return
{getAssetIcons(symbol)}
; + })} +
+ +

+ Guess the origin chain from the symbol, bridged +

+ +
+ {allSymbols.map((symbol: string, index: number) => { + return ( +
{getAssetIcons(symbol, undefined, false)}
+ ); + })} +
+
+ + ), +}; diff --git a/src/components/Icons/Svg/FT/tokenIcons.tsx b/src/components/Icons/Svg/FT/tokenIcons.tsx index c888bd51..370af3fb 100644 --- a/src/components/Icons/Svg/FT/tokenIcons.tsx +++ b/src/components/Icons/Svg/FT/tokenIcons.tsx @@ -14,6 +14,27 @@ import { SepoliaBridged, } from '../ChainIcons'; +export const mapSymbolWithoutExtension = { + 'WETH.e': 'WETH', + 'WETH.s': 'WETH', + 'WETH.b': 'WETH', + 'WETH.bt': 'WETH', + 'USDC.e': 'USDC', + 'USDC.s': 'USDC', + 'USDT.b': 'USDT', + 'USDT.bt': 'USDT', + 'DAI.e': 'DAI', + 'tDAI.s': 'DAI', + tDAI: 'DAI', +}; + +export const tokenExtensionToChainId = { + e: mainnet.id, + s: sepolia.id, + b: bsc.id, + bt: bscTestnet.id, +}; + export function createBridgedFt( ChainIcon: React.FC, FtIcon: React.FC, @@ -51,6 +72,23 @@ export function getAssetIcons( isNative = true, size?: number, ): ReactNode { + // retrieve the icon of the token + const extension = getExtension(symbol); + if (!originChainId && extension && extension in tokenExtensionToChainId) { + originChainId = + tokenExtensionToChainId[ + extension as keyof typeof tokenExtensionToChainId + ]; + } + + // remove the extension from the symbol or map to the correct symbol (eg. tDAI -> DAI) + if (symbol in mapSymbolWithoutExtension) { + symbol = + mapSymbolWithoutExtension[ + symbol as keyof typeof mapSymbolWithoutExtension + ]; + } + const icons = { // Native BNB: createNativeFt(BNB, size), @@ -64,6 +102,7 @@ export function getAssetIcons( ...getTokenIcons(isNative, originChainId, size), }; + // return the icon if (symbol in icons) { return icons[symbol as keyof typeof icons]; } else { @@ -110,3 +149,7 @@ function getTokenIcons( } return {}; } + +function getExtension(symbol: string) { + return symbol.split('.').pop(); +}