on cb07dcc help sign in
bridge/src/lib/getSuggestedGasPrice.js
75 lines 2.2 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), 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 etherscanGas = 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 ethgasstationGas = await fetch(
46 'https://ethgasstation.info/json/ethgasAPI.json',
47 {
48 method: 'GET',
49 cache: 'no-cache',
50 }
51 );
53 const etherscanGasJson = await etherscanGas.json();
55 const ethgasstationGasJson = await ethgasstationGas.json();
57 return {
58 fast: {
59 price: minGas(etherscanGasJson.result.FastGasPrice),
60 wait: formatWait(ethgasstationGasJson.fastWait),
61 },
62 average: {
63 price: minGas(etherscanGasJson.result.ProposeGasPrice),
64 wait: formatWait(ethgasstationGasJson.avgWait),
65 },
66 low: {
67 price: minGas(etherscanGasJson.result.SafeGasPrice),
68 wait: formatWait(ethgasstationGasJson.safeLowWait),
69 },
70 };
71 } catch (e) {
72 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
73 }
74 }