-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathuseGetFees.ts
More file actions
42 lines (36 loc) · 1.12 KB
/
useGetFees.ts
File metadata and controls
42 lines (36 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { ethers } from "ethers";
import { useEffect, useState } from "react";
import { formatCurrencyAmount } from "../utils";
import { useGasPrice } from "./apis";
export const useGetFees = (
gasLimit: string,
chainId: number,
decimals: number,
route: any
) => {
const [feesInToken, setFeesInToken] = useState<string>("");
const [feesInUsd, setFeesInUsd] = useState<number>(0);
const gasPrice = useGasPrice(chainId);
useEffect(() => {
const amount =
!!gasLimit &&
!!gasPrice?.data &&
ethers.BigNumber.from(gasLimit)
.mul(ethers.BigNumber.from(gasPrice?.data.normal.gasPrice))
.toString();
if (!!amount && !!decimals) {
const amountInToken = formatCurrencyAmount(amount, decimals).toString();
setFeesInToken(amountInToken);
}
if (route && chainId) {
const totalFeesInUsd = route?.userTxs.reduce((acc, curr) => {
if (curr.chainId === chainId) {
return acc + (curr.gasFees?.feesInUsd ?? 0);
}
return acc;
}, 0);
setFeesInUsd(totalFeesInUsd);
}
}, [gasPrice, gasLimit]);
return { feesInToken, feesInUsd };
};