on master help sign in
lib: get gas prices from etherscan
b84276eFang
src/lib/getSuggestedGasPrice.js+16−5
@@ -11,7 +11,10 @@ export default async function getSuggestedGasPrice(networkType) {
11 default:
11 default:
12 try {
12 try {
13 const response = await fetch(
13 const response = await fetch(
14− 'https://ethgasstation.info/json/ethgasAPI.json',
14+ 'https://api.etherscan.io/api' +
15+ '?module=gastracker' +
16+ '&action=gasoracle' +
17+ '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
15 {
18 {
16 method: 'GET',
19 method: 'GET',
17 cache: 'no-cache',
20 cache: 'no-cache',
@@ -20,13 +23,21 @@ export default async function getSuggestedGasPrice(networkType) {
20
23
21 const json = await response.json();
24 const json = await response.json();
22
25
23− // ethgasstation returns values in floating point, one order of magitude
26+ if (json && json.status !== '1') {
24− // more than gwei. see: https://docs.ethgasstation.info
27+ console.warn('suggested gas price warning', json.message);
25− const suggestedGasPrice = Math.ceil(json.fast / 10); // to gwei
28+ }
26
29
27− // we don't want to charge users more than the gas tank funds
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+ }
36+
37+ // safeguard against obscene suggestions
28 return Math.min(suggestedGasPrice, MAX_GAS_PRICE_GWEI);
38 return Math.min(suggestedGasPrice, MAX_GAS_PRICE_GWEI);
29 } catch (e) {
39 } catch (e) {
40+ console.warn('error fetching gas price', e);
30 return DEFAULT_GAS_PRICE_GWEI;
41 return DEFAULT_GAS_PRICE_GWEI;
31 }
42 }
32 }
43 }