on 58fbcd2 help sign in
bridge/src/lib/useSuggestedGasPrice.js
40 lines 1.1 kB history raw
1import { useState, useEffect } from 'react';
2import { NETWORK_TYPES } from './network';
3import { DEFAULT_GAS_PRICE_GWEI } from './constants';
5async function getSuggestedGasPrice(networkType) {
6 switch (networkType) {
7 case NETWORK_TYPES.ROPSTEN:
8 return 10;
9 case NETWORK_TYPES.OFFLINE:
10 return DEFAULT_GAS_PRICE_GWEI;
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 );
20 const json = await response.json();
21 return Math.min(json.fast / 10, 20); // to gwei
22 } catch (e) {
23 return DEFAULT_GAS_PRICE_GWEI;
24 }
25 }
28function useSuggestedGasPrice(networkType) {
29 const [gasPrice, _setGasPrice] = useState(DEFAULT_GAS_PRICE_GWEI);
31 useEffect(() => {
32 (async () => {
33 _setGasPrice(await getSuggestedGasPrice(networkType));
34 })();
35 }, [networkType]);
37 return { gasPrice };
40export { getSuggestedGasPrice, useSuggestedGasPrice };