From 1d6ebadcb28d48c2e1fa5110467c1eca87cb5e82 Mon Sep 17 00:00:00 2001 From: surbhi Date: Tue, 4 Nov 2025 13:46:45 -0400 Subject: [PATCH] gas cost estimates for small values --- src/components/swap-form.tsx | 21 ++++++++++++--------- src/components/swap-review-modal.tsx | 4 +++- src/hooks/usePartyPool.ts | 7 ++++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/components/swap-form.tsx b/src/components/swap-form.tsx index 6b010f3..a6ab3ad 100644 --- a/src/components/swap-form.tsx +++ b/src/components/swap-form.tsx @@ -111,8 +111,11 @@ export function SwapForm() { return; } - // Convert fromAmount to Wei - const maxAmountIn = parseUnits(fromAmount, selectedFromToken.decimals); + // Calculate maxAmountIn by subtracting fees from user's input + // User's input is the total amount they want to spend (including fees) + // So we subtract the fee to get the actual amount to swap + const userInputInWei = parseUnits(fromAmount, selectedFromToken.decimals); + const maxAmountIn = userInputInWei - bestRoute.fee; // Execute the swap await executeSwap( @@ -339,7 +342,7 @@ export function SwapForm() {
Estimated Gas Cost: - {isEstimatingGas ? 'Calculating...' : gasEstimate ? `$${gasEstimate.estimatedCostUsd}` : '-'} + {isEstimatingGas ? 'Calculating...' : gasEstimate ? (gasEstimate.estimatedCostUsd.startsWith('<') ? gasEstimate.estimatedCostUsd : `$${gasEstimate.estimatedCostUsd}`) : '-'}
@@ -349,17 +352,17 @@ export function SwapForm() { {swapAmounts && swapAmounts.length > 0 && selectedFromToken && fromAmount && ( <>
- Fee: + Total Amount In: - {(Number(swapAmounts[0].fee) / Math.pow(10, selectedFromToken.decimals)).toFixed(6)} {selectedFromToken.symbol} - {' '} - ({((Number(swapAmounts[0].fee) / Number(parseUnits(fromAmount, selectedFromToken.decimals))) * 100).toFixed(2)}%) + {formatUnits(swapAmounts[0].amountIn, selectedFromToken.decimals)} {selectedFromToken.symbol}
- Total Amount In: + Fee: - {((Number(parseUnits(fromAmount, selectedFromToken.decimals)) + Number(swapAmounts[0].fee)) / Math.pow(10, selectedFromToken.decimals)).toFixed(6)} {selectedFromToken.symbol} + {formatUnits(swapAmounts[0].fee, selectedFromToken.decimals)} {selectedFromToken.symbol} + {' '} + ({((Number(swapAmounts[0].fee) / Number(swapAmounts[0].amountIn)) * 100).toFixed(2)}%)
diff --git a/src/components/swap-review-modal.tsx b/src/components/swap-review-modal.tsx index 3746bf4..643bcd5 100644 --- a/src/components/swap-review-modal.tsx +++ b/src/components/swap-review-modal.tsx @@ -117,7 +117,9 @@ export function SwapReviewModal({ {gasEstimate && (
Network Fee - ${gasEstimate.estimatedCostUsd} + + {gasEstimate.estimatedCostUsd.startsWith('<') ? gasEstimate.estimatedCostUsd : `$${gasEstimate.estimatedCostUsd}`} +
)}
diff --git a/src/hooks/usePartyPool.ts b/src/hooks/usePartyPool.ts index dacfd6d..d99141b 100644 --- a/src/hooks/usePartyPool.ts +++ b/src/hooks/usePartyPool.ts @@ -216,7 +216,8 @@ export function useSwap() { const totalGas = approvalGas + swapGas; const estimatedCostWei = totalGas * gasPrice; - const estimatedCostEth = (Number(estimatedCostWei) / 1e18).toFixed(6); + // Use more decimal places for testnets with very low gas prices + const estimatedCostEth = (Number(estimatedCostWei) / 1e18).toFixed(9); // Fetch ETH price in USD let estimatedCostUsd = '0.00'; @@ -225,7 +226,8 @@ export function useSwap() { const data = await response.json(); const ethPriceUsd = data.ethereum?.usd || 0; const costInUsd = parseFloat(estimatedCostEth) * ethPriceUsd; - estimatedCostUsd = costInUsd.toFixed(2); + // Show "< $0.01" for amounts less than a cent, otherwise show 2 decimal places + estimatedCostUsd = costInUsd < 0.01 && costInUsd > 0 ? '< $0.01' : costInUsd.toFixed(2); } catch (priceErr) { console.error('Error fetching ETH price:', priceErr); } @@ -238,7 +240,6 @@ export function useSwap() { }; setGasEstimate(estimate); - console.log('⛽ Gas estimate:', estimate); return estimate; } catch (err) { console.error('Error estimating gas:', err);