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

@@ -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);