diff --git a/src/custom/api/useEvmFeeEstimation.ts b/src/custom/api/useEvmFeeEstimation.ts index 7f43fac4..3d37528e 100644 --- a/src/custom/api/useEvmFeeEstimation.ts +++ b/src/custom/api/useEvmFeeEstimation.ts @@ -1,5 +1,7 @@ import { useCallback, useEffect, useState } from 'react'; import { useEstimateFeesPerGas } from 'wagmi'; +import useEvmToken from '../bridge/useEvmToken'; +import { useOperationStore } from '@/store/operationStore'; const VITE_CLAIM_GAS_COST = import.meta.env.VITE_CLAIM_GAS_COST || '65484'; const VITE_LOCK_GAS_COST = import.meta.env.VITE_LOCK_GAS_COST || '85676'; @@ -7,6 +9,8 @@ const VITE_APPROVE_GAS_COST = import.meta.env.VITE_APPROVE_GAS_COST || '30846'; export function useEvmFeeEstimation() { const { data, refetch } = useEstimateFeesPerGas(); + const { allowance } = useEvmToken(); + const { inputAmount } = useOperationStore(); const [maxFeePerGas, setMaxFeePerGas] = useState(0n); useEffect(() => { @@ -25,12 +29,17 @@ export function useEvmFeeEstimation() { }, [maxFeePerGas]); const estimateLockFees = useCallback((): bigint => { - return BigInt(VITE_LOCK_GAS_COST) * maxFeePerGas; - }, [maxFeePerGas]); + if (!inputAmount) return 0n; - const estimateApproveFees = useCallback((): bigint => { - return BigInt(VITE_APPROVE_GAS_COST) * maxFeePerGas; - }, [maxFeePerGas]); + const lockFees = BigInt(VITE_LOCK_GAS_COST) * maxFeePerGas; + const approveFees = BigInt(VITE_APPROVE_GAS_COST) * maxFeePerGas; + + if (allowance < inputAmount) { + return approveFees + lockFees; + } else { + return lockFees; + } + }, [maxFeePerGas, allowance, inputAmount]); - return { estimateClaimFees, estimateLockFees, estimateApproveFees }; + return { estimateClaimFees, estimateLockFees }; } diff --git a/src/pages/IndexPage/Layouts/BridgeRedeemLayout/FeesEstimation.tsx b/src/pages/IndexPage/Layouts/BridgeRedeemLayout/FeesEstimation.tsx index b4fbb1f4..6a9a8f5f 100644 --- a/src/pages/IndexPage/Layouts/BridgeRedeemLayout/FeesEstimation.tsx +++ b/src/pages/IndexPage/Layouts/BridgeRedeemLayout/FeesEstimation.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect } from 'react'; +import { useEffect } from 'react'; import { MassaLogo, Tooltip, formatAmount } from '@massalabs/react-ui-kit'; import { FiInfo } from 'react-icons/fi'; @@ -10,7 +10,6 @@ import { addFeesAndStorageCost, useMassaFeeEstimation, } from '@/custom/api/useMassaFeeEstimation'; -import useEvmToken from '@/custom/bridge/useEvmToken'; import { useEvmChainValidation, useGetChainValidationContext, @@ -48,10 +47,7 @@ export function FeesEstimation() { const massaNetwork = getMassaNetwork(); - const { allowance } = useEvmToken(); - - const { estimateClaimFees, estimateLockFees, estimateApproveFees } = - useEvmFeeEstimation(); + const { estimateClaimFees, estimateLockFees } = useEvmFeeEstimation(); const { data: balanceData } = useBalance({ address, @@ -59,17 +55,6 @@ export function FeesEstimation() { const { estimateFeesMassa } = useMassaFeeEstimation(); - const getEthBridgeFees = useCallback((): bigint | undefined => { - const lockFees = estimateLockFees(); - if (!inputAmount) return undefined; - if (allowance < inputAmount) { - const approveFees = estimateApproveFees(); - return approveFees + lockFees; - } else { - return lockFees; - } - }, [estimateLockFees, inputAmount, allowance, estimateApproveFees]); - useEffect(() => { if (massaToEvm) { const { feesMAS, storageMAS } = estimateFeesMassa(); @@ -80,17 +65,17 @@ export function FeesEstimation() { } else { setFeesMAS(0n); setStorageMAS(0n); - const ethBridgeFees = getEthBridgeFees(); + const ethBridgeFees = estimateLockFees(); setFeesETH(ethBridgeFees); } }, [ setFeesMAS, setFeesETH, setStorageMAS, - getEthBridgeFees, massaToEvm, estimateClaimFees, estimateFeesMassa, + estimateLockFees, ]); if (