on 91efb05 help sign in
bridge/src/lib/getSuggestedGasPrice.js
44 lines 1.3 kB history raw
1import { NETWORK_TYPES } from './network';
2import { DEFAULT_GAS_PRICE_GWEI, MAX_GAS_PRICE_GWEI } from './constants';
4export default async function getSuggestedGasPrice(networkType) {
5 switch (networkType) {
6 case NETWORK_TYPES.ROPSTEN:
7 return 10;
8 case NETWORK_TYPES.OFFLINE:
9 return DEFAULT_GAS_PRICE_GWEI;
10 case NETWORK_TYPES.LOCAL:
11 default:
12 try {
13 const response = await fetch(
14 'https://api.etherscan.io/api' +
15 '?module=gastracker' +
16 '&action=gasoracle' +
17 '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
18 {
19 method: 'GET',
20 cache: 'no-cache',
21 }
22 );
24 const json = await response.json();
26 if (json && json.status !== '1') {
27 console.warn('suggested gas price warning', json.message);
28 }
30 const suggestedGasPrice = parseInt(
31 json && json.result && json.result.ProposeGasPrice
32 );
33 if (isNaN(suggestedGasPrice)) {
34 throw new Error('strange gas price response', json);
35 }
37 // safeguard against obscene suggestions
38 return Math.min(suggestedGasPrice, MAX_GAS_PRICE_GWEI);
39 } catch (e) {
40 console.warn('error fetching gas price', e);
41 return DEFAULT_GAS_PRICE_GWEI;
42 }
43 }