import { LocalGasStationRounded } from '@mui/icons-material' import { HelpOutline } from '@mui/icons-material' import type { CardProps } from '@mui/material' import { Box, Collapse, Tooltip, Typography } from '@mui/material' import type { RouteExtended } from '@openocean.finance/widget-sdk' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { useChain } from '../hooks/useChain.js' import { useGasPrice } from '../hooks/useGasPrice.js' import { usePriceImpact } from '../hooks/usePriceImpact.js' import { useToken } from '../hooks/useToken.js' import { useWidgetConfig } from '../providers/WidgetProvider/WidgetProvider.js' import { useFieldValues } from '../stores/form/useFieldValues.js' import { isRouteDone } from '../stores/routes/utils.js' import { useSplitSubvariantStore } from '../stores/settings/useSplitSubvariantStore.js' import { formatTokenAmount, formatTokenPrice } from '../utils/format.js' import { Card } from './Card/Card.js' import { IconTypography } from './IconTypography.js' import { TokenRate } from './TokenRate/TokenRate.js' interface TransactionDetailsProps extends CardProps { route?: RouteExtended } export const TransactionDetails: React.FC = ({ route, ...props }) => { const { t } = useTranslation() const { feeConfig } = useWidgetConfig() const [cardExpanded, setCardExpanded] = useState(true) const [fromChainId] = useFieldValues('fromChain') const [toChainId] = useFieldValues('toChain') const { chain } = useChain(fromChainId) const [state] = useSplitSubvariantStore((storeState) => [storeState.state]) const isBradge = route ? fromChainId !== toChainId : state === 'bridge' const { token } = useToken(fromChainId, chain?.nativeToken?.address) const { gasPrice } = useGasPrice(fromChainId?.toString() || '') if (!route) { route = { fromAmount: '0', toAmount: '0', fromToken: { name: '', address: '', symbol: '', chainId: 1, decimals: 0, priceUSD: '0', }, toToken: { name: '', address: '', symbol: '', chainId: 1, decimals: 0, priceUSD: '0', }, priceImpact: '0', fromAmountMin: 0, estimatedGas: 0, gas: 0, steps: [], } as unknown as RouteExtended } const toggleCard = () => { setCardExpanded((cardExpanded) => !cardExpanded) } let gasCostUSD = 0 const feeCosts: any = [] const combinedFeesUSD = gasCostUSD + feeCosts.reduce((sum: number, fee: any) => sum + fee.costUSD, 0) const feeCostUSD = feeCosts.reduce((sum: number, fee: any) => sum + fee.costUSD, 0) const estimatedGas = (route as any)?.estimatedGas || (route as any)?.data?.estimatedGas const gasCost = (route as any)?.gas || 0 if (token && gasPrice && estimatedGas) { const d = 10 ** token.decimals gasCostUSD = Number(token.priceUSD) * ((Number(gasPrice) * Number(estimatedGas)) / d) } if (token && gasCost) { gasCostUSD = Number(token.priceUSD) * Number(gasCost) } if (isBradge) { gasCostUSD = (route as any).feeCosts?.[0]?.amountUSD || 0 } const { priceImpact } = usePriceImpact(route) const feeCollectionStep = route.steps[0] ? route.steps[0].includedSteps.find( (includedStep) => includedStep.tool === 'feeCollection' ) : 0 let feeAmountUSD = 0 if (feeCollectionStep) { const estimatedFromAmount = BigInt(feeCollectionStep.estimate.fromAmount) - BigInt(feeCollectionStep.estimate.toAmount) feeAmountUSD = formatTokenPrice( estimatedFromAmount, feeCollectionStep.action.fromToken.priceUSD, feeCollectionStep.action.fromToken.decimals ) } const hasGaslessSupport = 12 const showIntegratorFeeCollectionDetails = (feeAmountUSD || Number.isFinite(feeConfig?.fee)) && !hasGaslessSupport if (!Number(route.toAmount)) { return null } return ( {hasGaslessSupport ? t('main.fees.free') : t('format.currency', { value: combinedFeesUSD })} {/* {cardExpanded ? ( ) : ( )} */} {route && (isBradge || route.fromChainId !== 1151111081099710) && ( {!isBradge ? t('main.fees.network') : 'Fee'} {isBradge ? ( ) : undefined} {gasCostUSD ? t('format.currency', { value: gasCostUSD, }) : '--'} )} {feeCosts.length ? ( {t('main.fees.provider')} {feeCostUSD ? t('format.currency', { value: feeCostUSD, }) : '--'} ) : null} {showIntegratorFeeCollectionDetails ? ( {feeConfig?.name ? t('main.fees.integrator', { tool: feeConfig.name }) : t('main.fees.defaultIntegrator')} {feeConfig?.name ? ( {t('format.currency', { value: feeAmountUSD, })} ) : ( {t('format.currency', { value: feeAmountUSD, })} )} ) : null} {t('main.priceImpact')} -0.3 ? 'orange' : 'error.main' : 'green' : '', }} > {priceImpact ? t('format.percent', { value: priceImpact }) : '--'} {!isBradge && ( {!isRouteDone(route) ? ( <> {t('main.maxSlippage')} {t('format.percent', { value: route.steps[0]?.action?.slippage ? route.steps[0].action.slippage / 100 : 0, })} {t('main.minReceived')} {t('format.tokenAmount', { value: formatTokenAmount( typeof route.toAmountMin === 'string' && !route.toAmountMin.includes('e') ? BigInt(Math.floor(Number(route.toAmountMin))) : BigInt(0), route.toToken.decimals ), })}{' '} {route.toToken.symbol} ) : ( <> {t('main.maxSlippage')} -- {t('main.minReceived')} -- )} )} ) }