Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor allowance calc #744

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading