on 368f024 help sign in
bridge/src/lib/getSuggestedGasPrice.js
65 lines 1.8 kB history raw
1import { NETWORK_TYPES } from './network';
2import { DEFAULT_GAS_PRICE_GWEI, MAX_GAS_PRICE_GWEI } from './constants';
3import { formatWait } from 'components/L2/Dropdowns/FeeDropdown';
5// ethgasstation returns values in floating point, one order of magitude
6// more than gwei. see: https://docs.ethgasstation.info
7// we don't want to charge users more than the gas tank funds
8const minGas = gas => Math.min(Math.ceil(gas / 10), MAX_GAS_PRICE_GWEI);
10export const defaultGasValues = value => ({
11 fast: {
12 price: value,
13 wait: '1',
14 },
15 average: {
16 price: value,
17 wait: '1',
18 },
19 low: {
20 price: value,
21 wait: '1',
22 },
23});
25export default async function getSuggestedGasPrice(networkType) {
26 switch (networkType) {
27 case NETWORK_TYPES.ROPSTEN:
28 return defaultGasValues(10);
29 case NETWORK_TYPES.OFFLINE:
30 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
31 case NETWORK_TYPES.LOCAL:
32 default:
33 try {
34 const response = await fetch(
35 'https://api.etherscan.io/api' +
36 '?module=gastracker' +
37 '&action=gasoracle' +
38 '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
39 {
40 method: 'GET',
41 cache: 'no-cache',
42 }
43 );
45 const json = await response.json();
47 return {
48 fast: {
49 price: minGas(json.fast),
50 wait: formatWait(json.fastWait),
51 },
52 average: {
53 price: minGas(json.average),
54 wait: formatWait(json.avgWait),
55 },
56 low: {
57 price: minGas(json.safeLow),
58 wait: formatWait(json.safeLowWait),
59 },
60 };
61 } catch (e) {
62 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
63 }
64 }