on master help sign in
gas: fetch suggestedBaseFee from etherscan
a54eed8pkova
src/lib/getSuggestedGasPrice.js+41−14
@@ -45,14 +45,32 @@ export const defaultGasValues = value => ({
45
45
46const getGasForNetwork = async providerUrl => {
46const getGasForNetwork = async providerUrl => {
47 try {
47 try {
48− const feeResponse = await fetch(
48+ const [feeResponse, baseFeeResponse] = await Promise.all([
49+ fetch(
50+ 'https://beaconcha.in/api/v1/execution/gasnow',
51+ {
52+ method: 'get',
53+ cache: 'no-cache',
54+ }
55+ ),
56+ fetch(
49− providerUrl,
57+ `${providerUrl}/api` +
58+ '?module=gastracker' +
59+ '&action=gasoracle' +
60+ '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
50− {
61+ {
51− method: 'get',
62+ method: 'GET',
52− cache: 'no-cache',
63+ cache: 'no-cache',
53− }
64+ }
65+ ),
54− );
66+ ]);
67+
68+ const [feeJson, baseFeeJson] = await Promise.all([
55− const feeJson = await feeResponse.json();
69+ feeResponse.json(),
70+ baseFeeResponse.json(),
71+ ]);
72+
73+ const suggestedBaseFeePerGas = Number(baseFeeJson.result.suggestBaseFee);
56
74
57 // Calculations derived from:
75 // Calculations derived from:
58 // https://www.blocknative.com/blog/eip-1559-fees
76 // https://www.blocknative.com/blog/eip-1559-fees
@@ -60,17 +78,26 @@ const getGasForNetwork = async providerUrl => {
60 fast: {
78 fast: {
61 price: minGas(weiToGwei(feeJson.data.rapid)),
79 price: minGas(weiToGwei(feeJson.data.rapid)),
62 wait: formatWait(15),
80 wait: formatWait(15),
63− maxFeePerGas: weiToGwei(feeJson.data.rapid)
81+ maxFeePerGas: weiToGwei(feeJson.data.rapid),
82+ maxPriorityFeePerGas: feeToInt(
83+ weiToGwei(feeJson.data.rapid) - suggestedBaseFeePerGas
84+ )
64 },
85 },
65 average: {
86 average: {
66 price: minGas(weiToGwei(feeJson.data.fast)),
87 price: minGas(weiToGwei(feeJson.data.fast)),
67 wait: formatWait(60),
88 wait: formatWait(60),
68− maxFeePerGas: weiToGwei(feeJson.data.fast)
89+ maxFeePerGas: weiToGwei(feeJson.data.fast),
90+ maxPriorityFeePerGas: feeToInt(
91+ weiToGwei(feeJson.data.fast) - suggestedBaseFeePerGas
92+ )
69 },
93 },
70 low: {
94 low: {
71− price: minGas(weiToGwei(feeJson.data.standard)),
95+ price: minGas(weiToGwei(feeJson.data.slow)),
72 wait: formatWait(180),
96 wait: formatWait(180),
73− maxFeePerGas: weiToGwei(feeJson.data.standard)
97+ maxFeePerGas: weiToGwei(feeJson.data.slow),
98+ maxPriorityFeePerGas: feeToInt(
99+ weiToGwei(feeJson.data.slow) - suggestedBaseFeePerGas
100+ )
74 },
101 },
75 };
102 };
76 } catch (e) {
103 } catch (e) {
@@ -82,11 +109,11 @@ const getGasForNetwork = async providerUrl => {
82export default async function getSuggestedGasPrice(networkType) {
109export default async function getSuggestedGasPrice(networkType) {
83 switch (networkType) {
110 switch (networkType) {
84 case NETWORK_TYPES.GOERLI:
111 case NETWORK_TYPES.GOERLI:
85− return getGasForNetwork('https://beaconcha.in/api/v1/execution/gasnow');
112+ return getGasForNetwork('https://api-goerli.etherscan.io');
86 case NETWORK_TYPES.OFFLINE:
113 case NETWORK_TYPES.OFFLINE:
87 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
114 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
88 case NETWORK_TYPES.LOCAL:
115 case NETWORK_TYPES.LOCAL:
89 default:
116 default:
90− return getGasForNetwork('https://beaconcha.in/api/v1/execution/gasnow');
117+ return getGasForNetwork('https://api.etherscan.io');
91 }
118 }
92}
119}