import type { TokenAmount } from '@lifi/sdk'; import { ExpandLess, ExpandMore } from '@mui/icons-material'; import { Box, Collapse, Typography } from '@mui/material'; import type { MouseEventHandler } from 'react'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useAccount } from '../../hooks/useAccount.js'; import { useWidgetConfig } from '../../providers/WidgetProvider/WidgetProvider.js'; import type { CardProps } from '../Card/Card.js'; import { Card } from '../Card/Card.js'; import { CardIconButton } from '../Card/CardIconButton.js'; import { CardLabel, CardLabelTypography } from '../Card/CardLabel.js'; import { StepActions } from '../StepActions/StepActions.js'; import { Token } from '../Token/Token.js'; import { TokenContainer } from './RouteCard.style.js'; import { RouteCardEssentials } from './RouteCardEssentials.js'; // import { RouteCardEssentialsExpanded } from './RouteCardEssentialsExpanded.js'; import type { RouteCardProps } from './types.js'; import { TokenPnLTypography } from '../TokenRate/TokenRate.style.js'; import axios from 'axios'; import { MOVE_CHAIN_ID } from '../../utils/constants.js'; export const RouteCard: React.FC< RouteCardProps & Omit > = ({ route, active, variant = 'default', expanded: defaulExpanded, ...other }) => { const { account } = useAccount(); const { t } = useTranslation(); const { subvariant } = useWidgetConfig(); const [cardExpanded, setCardExpanded] = useState(defaulExpanded); const handleExpand: MouseEventHandler = (e) => { e.stopPropagation(); setCardExpanded((expanded) => !expanded); }; const token: TokenAmount = subvariant === 'custom' ? { ...route.fromToken, amount: BigInt(route?.fromAmount || 0) } : { ...route.toToken, amount: BigInt(route?.toAmount || 0) }; const impactToken: TokenAmount | undefined = subvariant !== 'custom' ? { ...route.fromToken, amount: BigInt(route?.fromAmount || 0) } : undefined; const tags = route.tags?.filter( (tag) => tag === 'CHEAPEST' || tag === 'FASTEST', ); const [dataPnl, setDataPnL] = useState({ pnl: 0, percent: 0, }); const handleCalPnL = async (data: any, address: string) => { try { const formatAmountFrom = Number(data?.fromAmount) / 10 ** data?.fromToken?.decimals; const formatAmountTo = Number(data?.toAmount) / 10 ** data?.toToken?.decimals; const payload = { owner: address, from: { address: data?.fromToken?.address, symbol: data?.fromToken?.symbol, quantity: formatAmountFrom, price: Number(data?.fromToken?.priceUSD), }, to: { address: data?.toToken?.address, symbol: data?.toToken?.symbol, quantity: formatAmountTo, price: Number(data?.toToken?.priceUSD), }, }; const response = await axios .post( `https://api.getnimbus.io/swap/pnl?chain=${data?.fromChainId}`, payload, ) .then((res) => res?.data?.data); if (response && response?.newRealizedPnL !== 0) { setDataPnL({ pnl: response?.newRealizedPnL - response?.currentRealizedPnL, percent: Number(response?.cost) === 0 ? 0 : (response?.newRealizedPnL - response?.currentRealizedPnL) / Number(response?.cost), }); } } catch (error) { console.error(error); } }; useEffect(() => { if ( route?.fromChainId && route?.fromChainId === MOVE_CHAIN_ID && account?.isConnected ) { handleCalPnL(route, account?.address || ''); } }, [route, account]); const cardContent = ( {subvariant !== 'refuel' && route.tags?.length ? ( {tags?.length ? ( {t(`main.tags.${tags[0].toLowerCase()}` as any)} ) : null} ) : null} {!defaulExpanded ? ( {cardExpanded ? ( ) : ( )} ) : null} {route?.steps?.map((step) => ( ))} {/* */} {route?.fromChainId === MOVE_CHAIN_ID && account?.isConnected && ( 📊 PnL {Math.abs(Number(dataPnl.pnl)) < 0.1 ? ( `< ${Number(dataPnl.pnl) < 0 ? '-' : ''}$0.1` ) : ( <> {t(`format.currency`, { value: Number(dataPnl.pnl), })} )} {/* ( {Math.abs(Number(dataPnl.percent)) < 0.1 ? ( `< ${Number(dataPnl.percent) < 0 ? '-' : ''}0.1%` ) : ( <>{Number(dataPnl.percent).toFixed(2)}% )} ) */} )} ); return subvariant === 'refuel' || variant === 'cardless' ? ( cardContent ) : ( {cardContent} ); };