on c19b2a8 help sign in
bridge/src/lib/getSuggestedGasPrice.js
33 lines 1.0 kB history raw
1import { NETWORK_TYPES } from './network';
2import { DEFAULT_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://ethgasstation.info/json/ethgasAPI.json',
15 {
16 method: 'GET',
17 cache: 'no-cache',
18 }
19 );
21 const json = await response.json();
23 // ethgasstation returns values in floating point, one order of magitude
24 // more than gwei. see: https://docs.ethgasstation.info
25 const suggestedGasPrice = Math.ceil(json.fast / 10); // to gwei
27 // we don't want to charge users more than our default 20 gwei
28 return Math.min(suggestedGasPrice, DEFAULT_GAS_PRICE_GWEI);
29 } catch (e) {
30 return DEFAULT_GAS_PRICE_GWEI;
31 }
32 }