Skip to content

Commit

Permalink
gas: fetch suggestedBaseFee from etherscan
Browse files Browse the repository at this point in the history
  • Loading branch information
pkova committed Jan 19, 2024
1 parent d3f4612 commit a54eed8
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions src/lib/getSuggestedGasPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,59 @@ export const defaultGasValues = value => ({

const getGasForNetwork = async providerUrl => {
try {
const feeResponse = await fetch(
providerUrl,
{
method: 'get',
cache: 'no-cache',
}
);
const feeJson = await feeResponse.json();
const [feeResponse, baseFeeResponse] = await Promise.all([
fetch(
'https://beaconcha.in/api/v1/execution/gasnow',
{
method: 'get',
cache: 'no-cache',
}
),
fetch(
`${providerUrl}/api` +
'?module=gastracker' +
'&action=gasoracle' +
'&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
{
method: 'GET',
cache: 'no-cache',
}
),
]);

const [feeJson, baseFeeJson] = await Promise.all([
feeResponse.json(),
baseFeeResponse.json(),
]);

const suggestedBaseFeePerGas = Number(baseFeeJson.result.suggestBaseFee);

// Calculations derived from:
// https://www.blocknative.com/blog/eip-1559-fees
return {
fast: {
price: minGas(weiToGwei(feeJson.data.rapid)),
wait: formatWait(15),
maxFeePerGas: weiToGwei(feeJson.data.rapid)
maxFeePerGas: weiToGwei(feeJson.data.rapid),
maxPriorityFeePerGas: feeToInt(
weiToGwei(feeJson.data.rapid) - suggestedBaseFeePerGas
)
},
average: {
price: minGas(weiToGwei(feeJson.data.fast)),
wait: formatWait(60),
maxFeePerGas: weiToGwei(feeJson.data.fast)
maxFeePerGas: weiToGwei(feeJson.data.fast),
maxPriorityFeePerGas: feeToInt(
weiToGwei(feeJson.data.fast) - suggestedBaseFeePerGas
)
},
low: {
price: minGas(weiToGwei(feeJson.data.standard)),
price: minGas(weiToGwei(feeJson.data.slow)),
wait: formatWait(180),
maxFeePerGas: weiToGwei(feeJson.data.standard)
maxFeePerGas: weiToGwei(feeJson.data.slow),
maxPriorityFeePerGas: feeToInt(
weiToGwei(feeJson.data.slow) - suggestedBaseFeePerGas
)
},
};
} catch (e) {
Expand All @@ -82,11 +109,11 @@ const getGasForNetwork = async providerUrl => {
export default async function getSuggestedGasPrice(networkType) {
switch (networkType) {
case NETWORK_TYPES.GOERLI:
return getGasForNetwork('https://beaconcha.in/api/v1/execution/gasnow');
return getGasForNetwork('https://api-goerli.etherscan.io');
case NETWORK_TYPES.OFFLINE:
return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
case NETWORK_TYPES.LOCAL:
default:
return getGasForNetwork('https://beaconcha.in/api/v1/execution/gasnow');
return getGasForNetwork('https://api.etherscan.io');
}
}

0 comments on commit a54eed8

Please sign in to comment.