UNPKG

@reservoir0x/relay-kit-ui

Version:

Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.

85 lines 3.48 kB
import { useMemo } from 'react'; import {} from '@reservoir0x/relay-kit-hooks'; import { useInternalRelayChains } from '../hooks/index.js'; export const useEnhancedTokensList = (tokenLists, balanceMap, context, multiWalletSupportEnabled, chainId, sortByBalance = true) => { const { chains } = useInternalRelayChains(); const chainCurrencyMap = useMemo(() => { if (!chains) return new Map(); return new Map(chains .filter((chain) => chain?.currency?.address) .map((chain) => [ `${chain.id}:${chain.currency?.address?.toLowerCase()}`, true ])); }, [chains]); return useMemo(() => { if (!tokenLists) return []; const enhancedTokens = tokenLists .map((currency) => { // Validate that all required fields exist if (typeof currency?.chainId !== 'number' || !currency?.address || !currency?.symbol || !currency?.name || typeof currency?.decimals !== 'number') { return null; } // Construct the enhanced token with all required fields const chain = chains?.find((c) => c.id === currency.chainId); const enhancedToken = { chainId: currency.chainId, address: currency.address, symbol: currency.symbol, name: currency.name, decimals: currency.decimals, logoURI: currency.metadata?.logoURI ?? '', verified: currency.metadata?.verified ?? false, vmType: currency.vmType, balance: balanceMap?.[`${currency.chainId}:${currency.address}`], isGasCurrency: currency.chainId !== 1337 && chainCurrencyMap.has(`${currency.chainId}:${currency.address.toLowerCase()}`), chain }; return enhancedToken; }) // Remove any tokens that failed validation .filter((token) => token !== null) // Remove duplicate tokens by chainId:address .filter((token, index, self) => index === self.findIndex((t) => t.chainId === token.chainId && t.address.toLowerCase() === token.address.toLowerCase())) // Filter by chainId if specified .filter((token) => !chainId || token.chainId === chainId) // Filter out non-EVM tokens when multiWallet support is disabled .filter((token) => { if (context === 'from' && !multiWalletSupportEnabled) { return token.vmType === 'evm'; } return true; }) .sort((a, b) => { // Only sort by USD value if sortByBalance is true if (sortByBalance) { const aValueUsd = a.balance?.value_usd ?? 0; const bValueUsd = b.balance?.value_usd ?? 0; if (aValueUsd !== bValueUsd) { return bValueUsd - aValueUsd; } } // Maintain original order if not sorting by balance return 0; }); return enhancedTokens; }, [ tokenLists, balanceMap, context, multiWalletSupportEnabled, chainId, chainCurrencyMap, sortByBalance ]); }; //# sourceMappingURL=useEnhancedTokensList.js.map