diff --git a/src/components/TradingWidget.tsx b/src/components/TradingWidget.tsx index 232df269..be0c427e 100644 --- a/src/components/TradingWidget.tsx +++ b/src/components/TradingWidget.tsx @@ -28,6 +28,22 @@ function applySlippage(amount: bigint, isBuy: boolean): bigint { const isZapAvailable = ZAP_PLOTLINK !== "0x0000000000000000000000000000000000000000"; +/** Format a raw bigint token amount for display with appropriate precision. */ +function formatTokenAmount(value: bigint, decimals: number): string { + const num = Number(formatUnits(value, decimals)); + if (num === 0) return "0"; + if (num >= 1) { + return num.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 }); + } + if (num >= 0.001) { + return num.toFixed(4); + } + if (num >= 0.000001) { + return num.toFixed(6); + } + return num.toExponential(2); +} + /** Retry a writeContractAsync call once if it fails with a nonce error. */ async function retryOnNonceError(fn: () => Promise): Promise { try { @@ -499,7 +515,7 @@ export function TradingWidget({ tokenAddress }: { tokenAddress: Address }) { {balance !== undefined && (

- Balance: {formatUnits(balance, balanceDecimals)} {balanceLabel} + Balance: {formatTokenAmount(balance, balanceDecimals)} {balanceLabel}

)} {insufficientBalance && ( @@ -548,7 +564,7 @@ export function TradingWidget({ tokenAddress }: { tokenAddress: Address }) { {/* Balance for sell tab and non-zap buy (PLOT direct) */} {(tab === "sell" || !isZapAvailable) && balance !== undefined && (

- Balance: {formatUnits(balance, balanceDecimals)} {balanceLabel} + Balance: {formatTokenAmount(balance, balanceDecimals)} {balanceLabel}

)} {(tab === "sell" || !isZapAvailable) && insufficientBalance && ( @@ -561,7 +577,7 @@ export function TradingWidget({ tokenAddress }: { tokenAddress: Address }) {
Est. cost:{" "} - {formatUnits(zapQuote.fromTokenAmount, estimateDecimals)} {payToken} + {formatTokenAmount(zapQuote.fromTokenAmount, estimateDecimals)} {payToken} (incl. 3% slippage)
@@ -570,7 +586,7 @@ export function TradingWidget({ tokenAddress }: { tokenAddress: Address }) {
{tab === "buy" ? "Max cost" : "Min return"}:{" "} - {formatUnits(applySlippage(estimate, tab === "buy"), 18)} {RESERVE_LABEL} + {formatTokenAmount(applySlippage(estimate, tab === "buy"), 18)} {RESERVE_LABEL} (incl. 3% slippage)