on master help sign in
Merge pull request #1131 from urbit/pkova/gas-gas

gas: fetch suggestedBaseFee from etherscan

commit8862a81e9d08d2bfae65c2559edfe4f3c902339a authorPyry Kovanen <pkova@users.noreply.github.com> dateFri 19 Jan 2024, 17:30 (3 years ago) parentd3f4612a54eed8
1 file changed+41−14
src/lib/getSuggestedGasPrice.js+41−14
src/lib/getSuggestedGasPrice.js+41−14
@@ -45,14 +45,32 @@ export const defaultGasValues = value => ({
4545
4646const getGasForNetwork = async providerUrl => {
4747 try {
48− const feeResponse = await fetch(
49− providerUrl,
50− {
51− method: 'get',
52− cache: 'no-cache',
53− }
54− );
55− const feeJson = await feeResponse.json();
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(
57+ `${providerUrl}/api` +
58+ '?module=gastracker' +
59+ '&action=gasoracle' +
60+ '&apikey=CG52E4R96W56GIKUI4IJ8CH9EZIXPUW1W8',
61+ {
62+ method: 'GET',
63+ cache: 'no-cache',
64+ }
65+ ),
66+ ]);
67+
68+ const [feeJson, baseFeeJson] = await Promise.all([
69+ feeResponse.json(),
70+ baseFeeResponse.json(),
71+ ]);
72+
73+ const suggestedBaseFeePerGas = Number(baseFeeJson.result.suggestBaseFee);
5674
5775 // Calculations derived from:
5876 // https://www.blocknative.com/blog/eip-1559-fees
@@ -60,17 +78,26 @@ const getGasForNetwork = async providerUrl => {
6078 fast: {
6179 price: minGas(weiToGwei(feeJson.data.rapid)),
6280 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+ )
6485 },
6586 average: {
6687 price: minGas(weiToGwei(feeJson.data.fast)),
6788 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+ )
6993 },
7094 low: {
71− price: minGas(weiToGwei(feeJson.data.standard)),
95+ price: minGas(weiToGwei(feeJson.data.slow)),
7296 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+ )
74101 },
75102 };
76103 } catch (e) {
@@ -82,11 +109,11 @@ const getGasForNetwork = async providerUrl => {
82109export default async function getSuggestedGasPrice(networkType) {
83110 switch (networkType) {
84111 case NETWORK_TYPES.GOERLI:
85− return getGasForNetwork('https://beaconcha.in/api/v1/execution/gasnow');
112+ return getGasForNetwork('https://api-goerli.etherscan.io');
86113 case NETWORK_TYPES.OFFLINE:
87114 return defaultGasValues(DEFAULT_GAS_PRICE_GWEI);
88115 case NETWORK_TYPES.LOCAL:
89116 default:
90− return getGasForNetwork('https://beaconcha.in/api/v1/execution/gasnow');
117+ return getGasForNetwork('https://api.etherscan.io');
91118 }
92119}