-
Notifications
You must be signed in to change notification settings - Fork 3
/
GasPrice.js
45 lines (33 loc) · 1.45 KB
/
GasPrice.js
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
43
44
45
import axios from "axios";
import { ethers, BigNumber } from "ethers";
import { ETHER_SCAN_API_KEY, NETWORKS, POLYGON_SCAN_API_KEY } from "./constants";
const ETHER_API_URL = "https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey=" + ETHER_SCAN_API_KEY;
const POLYGON_API_URL = "https://api.polygonscan.com/api?module=gastracker&action=gasoracle&apikey=" + POLYGON_SCAN_API_KEY;
getGasPrice = async (chainId) => {
let apiURL;
if (chainId == NETWORKS.ethereum.chainId) {
apiURL = ETHER_API_URL;
}
else if (chainId == NETWORKS.polygon.chainId) {
apiURL = POLYGON_API_URL;
}
else {
throw new Error("Unsupported chainId");
}
const resp = await axios.get(apiURL);
const result = resp.data.result;
const proposeGasPriceString = result.ProposeGasPrice.toString();
const suggestBaseFeeString = result.suggestBaseFee.toString();
const proposeGasPriceBigNumber = ethers.utils.parseUnits(proposeGasPriceString, "gwei");
const suggestBaseFeeBigNumber = ethers.utils.parseUnits(suggestBaseFeeString, "gwei");
const maxFeePerGasBigNumber = proposeGasPriceBigNumber.mul(2);
const maxPriorityFeePerGasBigNumber = proposeGasPriceBigNumber.sub(suggestBaseFeeBigNumber);
const gasPrice = {
"maxFeePerGas":maxFeePerGasBigNumber.toHexString(),
"maxPriorityFeePerGas":maxPriorityFeePerGasBigNumber.toHexString()
}
return gasPrice;
}
module.exports = {
getGasPrice
}