import { useWalletBalance } from '@features/wallet' import type { Chain } from '@features/wallet' import { TOKENS, type Token } from './tokens' const NATIVE_TICKER: Record = { BTC: 'BTC', ETH: 'ETH', SOL: 'SOL', TRX: 'TRX', BSC: 'BNB', } const NATIVE_NAME: Record = { BTC: 'Bitcoin', ETH: 'Ethereum', SOL: 'Solana', TRX: 'Tron', BSC: 'BNB', } const DEFAULT_TOKEN_COLOR = '#2A2D3A' function formatUsd(value: number | null | undefined): string { if (value == null) return '$—' return `$${value.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}` } function formatPrice(value: number | null | undefined): string { if (value == null) return '$—' if (value >= 1) { return `$${value.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}` } return `$${value.toLocaleString('en-US', { minimumFractionDigits: 4, maximumFractionDigits: 6 })}` } function lookupStatic(ticker: string): Token | undefined { return TOKENS.find((t) => t.ticker === ticker) } export function useChainTokenRows(chain: Chain) { const { data, isLoading } = useWalletBalance(chain) if (!data) return { rows: [] as Token[], isLoading } const nativeTicker = NATIVE_TICKER[chain] const nativeStatic = lookupStatic(nativeTicker) const nativeRow: Token = { ticker: nativeTicker, name: NATIVE_NAME[chain], logo: nativeStatic?.logo, color: nativeStatic?.color ?? DEFAULT_TOKEN_COLOR, price: formatPrice(data.native.usdPrice), change: 0, bal: data.native.formatted, usd: formatUsd(data.native.usdValue), fav: false, } const tokenRows: Token[] = Object.entries(data.tokens).map(([symbol, amount]) => { const staticToken = lookupStatic(symbol) return { ticker: symbol, name: staticToken?.name ?? symbol, logo: staticToken?.logo, color: staticToken?.color ?? DEFAULT_TOKEN_COLOR, price: formatPrice(amount.usdPrice), change: 0, bal: amount.formatted, usd: formatUsd(amount.usdValue), fav: false, } }) return { rows: [nativeRow, ...tokenRows], isLoading } }