-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgas.ts
33 lines (28 loc) · 1.13 KB
/
gas.ts
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
import { ethers } from 'ethers';
import BigNumber from './bignumber';
import { getNetworkConfigByType } from './constants/NETWORKS';
import { ETH_NUMBER_OF_DIGITS } from './constants/UNITS';
import memoizee from 'memoizee';
const API_URL = 'https://ethgasstation.info/json/ethgasAPI.json';
const TRANSACTION_SPEED = 'fast';
const GAS_CACHE = 10 * 1000;
const _getCurrentGasPrice = async function (): Promise<BigNumber> {
const gasData = await fetch(API_URL).then(r => r.json());
const gasPriceString = ethers.utils.formatUnits(gasData[TRANSACTION_SPEED] / 10, 'gwei');
return new BigNumber(gasPriceString);
};
const getCurrentGasPrice = memoizee(_getCurrentGasPrice, {
maxAge: GAS_CACHE,
promise: true,
});
export const getGasPrice = async function (network: string): Promise<BigNumber> {
const networkConfig = getNetworkConfigByType(network);
if (networkConfig.gasPrice) {
return new BigNumber(networkConfig.gasPrice).shiftedBy(-ETH_NUMBER_OF_DIGITS);
}
try {
return await getCurrentGasPrice();
} catch (error) {
throw new Error(`Gas data is not available on "${network}" network`);
}
};