on master help sign in
devex: goerli gas prices use testnet api
commitce0a7bdbe80167f43acb2f4bc687167c58b88781 authortomholford <tomholford@users.noreply.github.com> dateTue 25 Oct 2022, 21:10 (4 years ago) parentb660379
1 file changed+62−58
src/lib/getSuggestedGasPrice.js+62−58
src/lib/getSuggestedGasPrice.js+62−58
@@ -38,70 +38,74 @@ export const defaultGasValues = value => ({
3838 },
3939});
4040
41+const getGasForNetwork = async (providerUrl) => {
42+ try {
43+ const [etherscanGas, ethgasstationGas] = await Promise.all([
44+ fetch(
45+ `${providerUrl}/api` +
46+ '?module=gastracker' +
47+ '&action=gasoracle' +
48+ '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
49+ {
50+ method: 'GET',
51+ cache: 'no-cache',
52+ }
53+ ),
54+ fetch(
55+ 'https://ethgasstation.info/json/ethgasAPI.json',
56+ {
57+ method: 'GET',
58+ cache: 'no-cache',
59+ }
60+ )
61+ ]);
62+
63+ const [etherscanGasJson, ethgasstationGasJson] = await Promise.all([
64+ etherscanGas.json(),
65+ ethgasstationGas.json()
66+ ])
67+
68+ const suggestedBaseFeePerGas = Number(etherscanGasJson.result.suggestBaseFee);
69+
70+ // Calculations derived from:
71+ // https://www.blocknative.com/blog/eip-1559-fees
72+ return {
73+ fast: {
74+ price: minGas(etherscanGasJson.result.FastGasPrice),
75+ wait: formatWait(ethgasstationGasJson.fastWait),
76+ maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, etherscanGasJson.result.FastGasPrice - suggestedBaseFeePerGas),
77+ maxPriorityFeePerGas: feeToInt((etherscanGasJson.result.FastGasPrice - suggestedBaseFeePerGas)),
78+ suggestedBaseFeePerGas
79+ },
80+ average: {
81+ price: minGas(etherscanGasJson.result.ProposeGasPrice),
82+ wait: formatWait(ethgasstationGasJson.avgWait),
83+ maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, etherscanGasJson.result.ProposeGasPrice - suggestedBaseFeePerGas),
84+ maxPriorityFeePerGas: feeToInt((etherscanGasJson.result.ProposeGasPrice - suggestedBaseFeePerGas)),
85+ suggestedBaseFeePerGas
86+ },
87+ low: {
88+ price: minGas(etherscanGasJson.result.SafeGasPrice),
89+ wait: formatWait(ethgasstationGasJson.safeLowWait),
90+ maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, etherscanGasJson.result.SafeGasPrice - suggestedBaseFeePerGas),
91+ maxPriorityFeePerGas: feeToInt((etherscanGasJson.result.SafeGasPrice - suggestedBaseFeePerGas)),
92+ suggestedBaseFeePerGas
93+ },
94+ };
95+ } catch (e) {
96+ console.warn(e);
97+ return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
98+ }
99+}
100+
41101export default async function getSuggestedGasPrice(networkType) {
42102 switch (networkType) {
43103 case NETWORK_TYPES.GOERLI:
44− return defaultGasValues(10);
104+ return getGasForNetwork('https://api-goerli.etherscan.io')
45105 case NETWORK_TYPES.OFFLINE:
46106 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
47107 case NETWORK_TYPES.LOCAL:
48108 default:
49− try {
50− const [etherscanGas, ethgasstationGas] = await Promise.all([
51− fetch(
52− 'https://api.etherscan.io/api' +
53− '?module=gastracker' +
54− '&action=gasoracle' +
55− '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
56− {
57− method: 'GET',
58− cache: 'no-cache',
59− }
60− ),
61− fetch(
62− 'https://ethgasstation.info/json/ethgasAPI.json',
63− {
64− method: 'GET',
65− cache: 'no-cache',
66− }
67− )
68− ]);
69−
70− const [etherscanGasJson, ethgasstationGasJson] = await Promise.all([
71− etherscanGas.json(),
72− ethgasstationGas.json()
73− ])
74−
75− const suggestedBaseFeePerGas = Number(etherscanGasJson.result.suggestBaseFee);
76−
77− // Calculations derived from:
78− // https://www.blocknative.com/blog/eip-1559-fees
79− return {
80− fast: {
81− price: minGas(etherscanGasJson.result.FastGasPrice),
82− wait: formatWait(ethgasstationGasJson.fastWait),
83− maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, etherscanGasJson.result.FastGasPrice - suggestedBaseFeePerGas),
84− maxPriorityFeePerGas: feeToInt((etherscanGasJson.result.FastGasPrice - suggestedBaseFeePerGas)),
85− suggestedBaseFeePerGas
86− },
87− average: {
88− price: minGas(etherscanGasJson.result.ProposeGasPrice),
89− wait: formatWait(ethgasstationGasJson.avgWait),
90− maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, etherscanGasJson.result.ProposeGasPrice - suggestedBaseFeePerGas),
91− maxPriorityFeePerGas: feeToInt((etherscanGasJson.result.ProposeGasPrice - suggestedBaseFeePerGas)),
92− suggestedBaseFeePerGas
93− },
94− low: {
95− price: minGas(etherscanGasJson.result.SafeGasPrice),
96− wait: formatWait(ethgasstationGasJson.safeLowWait),
97− maxFeePerGas: calculateMaxFee(suggestedBaseFeePerGas, etherscanGasJson.result.SafeGasPrice - suggestedBaseFeePerGas),
98− maxPriorityFeePerGas: feeToInt((etherscanGasJson.result.SafeGasPrice - suggestedBaseFeePerGas)),
99− suggestedBaseFeePerGas
100− },
101− };
102− } catch (e) {
103− console.warn(e);
104− return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
105− }
109+ return getGasForNetwork('https://api.etherscan.io/');
106110 }
107111}