gas cost estimates for small values
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user