gas cost estimates for small values

This commit is contained in:
2025-11-04 13:46:45 -04:00
parent ed6426fad7
commit 1d6ebadcb2
3 changed files with 19 additions and 13 deletions

View File

@@ -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() {
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">Estimated Gas Cost:</span>
<span className="font-medium">
{isEstimatingGas ? 'Calculating...' : gasEstimate ? `$${gasEstimate.estimatedCostUsd}` : '-'}
{isEstimatingGas ? 'Calculating...' : gasEstimate ? (gasEstimate.estimatedCostUsd.startsWith('<') ? gasEstimate.estimatedCostUsd : `$${gasEstimate.estimatedCostUsd}`) : '-'}
</span>
</div>
<div className="flex justify-between text-sm">
@@ -349,17 +352,17 @@ export function SwapForm() {
{swapAmounts && swapAmounts.length > 0 && selectedFromToken && fromAmount && (
<>
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">Fee:</span>
<span className="text-muted-foreground">Total Amount In:</span>
<span className="font-medium">
{(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}
</span>
</div>
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">Total Amount In:</span>
<span className="text-muted-foreground">Fee:</span>
<span className="font-medium">
{((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)}%)
</span>
</div>
</>