review modal, new smart contract pointer and network gas fees estimation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { usePublicClient, useWalletClient } from 'wagmi';
|
||||
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
|
||||
import type { AvailableToken } from './usePartyPlanner';
|
||||
@@ -181,12 +181,71 @@ export function useSwapAmounts(
|
||||
};
|
||||
}
|
||||
|
||||
export interface GasEstimate {
|
||||
totalGas: bigint;
|
||||
gasPrice: bigint;
|
||||
estimatedCostEth: string;
|
||||
estimatedCostUsd: string;
|
||||
}
|
||||
|
||||
export function useSwap() {
|
||||
const { data: walletClient } = useWalletClient();
|
||||
const publicClient = usePublicClient();
|
||||
const [isSwapping, setIsSwapping] = useState(false);
|
||||
const [swapHash, setSwapHash] = useState<`0x${string}` | null>(null);
|
||||
const [swapError, setSwapError] = useState<string | null>(null);
|
||||
const [gasEstimate, setGasEstimate] = useState<GasEstimate | null>(null);
|
||||
const [isEstimatingGas, setIsEstimatingGas] = useState(false);
|
||||
|
||||
const estimateGas = useCallback(async () => {
|
||||
if (!publicClient) {
|
||||
console.error('Public client not available');
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
setIsEstimatingGas(true);
|
||||
|
||||
// Get current gas price from the network
|
||||
const gasPrice = await publicClient.getGasPrice();
|
||||
|
||||
// Use fixed, typical gas amounts for AMM operations
|
||||
const approvalGas = 50000n; // ERC20 approval typically uses ~50k gas
|
||||
const swapGas = 150000n; // AMM swap typically uses ~150k gas
|
||||
|
||||
const totalGas = approvalGas + swapGas;
|
||||
const estimatedCostWei = totalGas * gasPrice;
|
||||
const estimatedCostEth = (Number(estimatedCostWei) / 1e18).toFixed(6);
|
||||
|
||||
// Fetch ETH price in USD
|
||||
let estimatedCostUsd = '0.00';
|
||||
try {
|
||||
const response = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd');
|
||||
const data = await response.json();
|
||||
const ethPriceUsd = data.ethereum?.usd || 0;
|
||||
const costInUsd = parseFloat(estimatedCostEth) * ethPriceUsd;
|
||||
estimatedCostUsd = costInUsd.toFixed(2);
|
||||
} catch (priceErr) {
|
||||
console.error('Error fetching ETH price:', priceErr);
|
||||
}
|
||||
|
||||
const estimate: GasEstimate = {
|
||||
totalGas,
|
||||
gasPrice,
|
||||
estimatedCostEth,
|
||||
estimatedCostUsd,
|
||||
};
|
||||
|
||||
setGasEstimate(estimate);
|
||||
console.log('⛽ Gas estimate:', estimate);
|
||||
return estimate;
|
||||
} catch (err) {
|
||||
console.error('Error estimating gas:', err);
|
||||
return null;
|
||||
} finally {
|
||||
setIsEstimatingGas(false);
|
||||
}
|
||||
}, [publicClient]);
|
||||
|
||||
const executeSwap = async (
|
||||
poolAddress: `0x${string}`,
|
||||
@@ -290,8 +349,11 @@ export function useSwap() {
|
||||
|
||||
return {
|
||||
executeSwap,
|
||||
estimateGas,
|
||||
isSwapping,
|
||||
swapHash,
|
||||
swapError,
|
||||
gasEstimate,
|
||||
isEstimatingGas,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user