Skip to content

Commit

Permalink
refactro allowance calculation in FeeEstimation
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Jun 17, 2024
1 parent e19b46e commit e949c23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
21 changes: 15 additions & 6 deletions src/custom/api/useEvmFeeEstimation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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';
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(() => {
Expand All @@ -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 };
}
23 changes: 4 additions & 19 deletions src/pages/IndexPage/Layouts/BridgeRedeemLayout/FeesEstimation.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,7 +10,6 @@ import {
addFeesAndStorageCost,
useMassaFeeEstimation,
} from '@/custom/api/useMassaFeeEstimation';
import useEvmToken from '@/custom/bridge/useEvmToken';
import {
useEvmChainValidation,
useGetChainValidationContext,
Expand Down Expand Up @@ -48,28 +47,14 @@ export function FeesEstimation() {

const massaNetwork = getMassaNetwork();

const { allowance } = useEvmToken();

const { estimateClaimFees, estimateLockFees, estimateApproveFees } =
useEvmFeeEstimation();
const { estimateClaimFees, estimateLockFees } = useEvmFeeEstimation();

const { data: balanceData } = useBalance({
address,
});

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();
Expand All @@ -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 (
Expand Down

0 comments on commit e949c23

Please sign in to comment.