import type { LiFiStep, TokenAmount } from '@lifi/sdk' import type { BoxProps } from '@mui/material' import { Box, Grow, Skeleton, Tooltip } from '@mui/material' import type { FC, PropsWithChildren, ReactElement } from 'react' import { useTranslation } from 'react-i18next' import { useChain } from '../../hooks/useChain.js' import { useToken } from '../../hooks/useToken.js' import { formatTokenAmount, formatTokenPrice } from '../../utils/format.js' import { getPriceImpact } from '../../utils/getPriceImpact.js' import { AvatarBadgedSkeleton } from '../Avatar/Avatar.js' import { SmallAvatar } from '../Avatar/SmallAvatar.js' import { TokenAvatar } from '../Avatar/TokenAvatar.js' import { TextFitter } from '../TextFitter/TextFitter.js' import { TextSecondary, TextSecondaryContainer } from './Token.style.js' interface TokenProps { token: TokenAmount impactToken?: TokenAmount enableImpactTokenTooltip?: boolean step?: LiFiStep stepVisible?: boolean disableDescription?: boolean isLoading?: boolean } export const Token: FC = ({ token, ...other }) => { if (!token.priceUSD || !token.logoURI) { return } return } const TokenFallback: FC = ({ token, isLoading, ...other }) => { const { token: chainToken, isLoading: isLoadingToken } = useToken( token.chainId, token.address ) return ( ) } const TokenBase: FC = ({ token, impactToken, enableImpactTokenTooltip, step, stepVisible, disableDescription, isLoading, ...other }) => { const { t, i18n } = useTranslation() const { chain } = useChain(token?.chainId) if (isLoading) { return ( ) } const tokenAmount = formatTokenAmount(token.amount, token.decimals) const tokenPrice = formatTokenPrice( token.amount, token.priceUSD, token.decimals ) let priceImpact: number | undefined let priceImpactPercent: number | undefined if (impactToken) { priceImpact = getPriceImpact({ fromToken: impactToken, fromAmount: impactToken.amount, toToken: token, toAmount: token.amount, }) priceImpactPercent = priceImpact * 100 } const tokenOnChain = !disableDescription ? ( {t('main.tokenOnChain', { tokenSymbol: token.symbol, chainName: chain?.name, })} ) : null return ( {t('format.tokenAmount', { value: tokenAmount, })} {t('format.currency', { value: tokenPrice, })} {impactToken ? ( ) : null} {impactToken ? ( enableImpactTokenTooltip ? ( {t('format.percent', { value: priceImpact, usePlusSign: true, })} ) : ( {t('format.percent', { value: priceImpact, usePlusSign: true })} ) ) : null} {!disableDescription ? ( ) : null} {!disableDescription && step ? ( {tokenOnChain} ) : ( tokenOnChain )} ) } const TokenStep: FC>> = ({ step, stepVisible, disableDescription, children, }) => { return ( {children as ReactElement} {step?.toolDetails.name[0]} {step?.toolDetails.name} ) } export const TokenSkeleton: FC & BoxProps> = ({ step, disableDescription, ...other }) => { return ( {!step && !disableDescription ? ( ) : null} ) }