4

I want to calculate fees for historical EIP-1559 transactions on Astar EVM. For this, I need to use the GasPrice formula:

GasPrice = BaseFee + MaxPriorityFeePerGas < MaxFeePerGas ? 
        BaseFee + MaxPriorityFeePerGas : 
        MaxFeePerGas;

MaxPriorityFeePerGas and MaxFeePerGas values are available in transaction data and are set by the user but BaseFee value is not. As far as I know this value is set by the system and is dynamic. Is there a way to retrieve this BaseFee value for historical blocks? Is this information stored in the blockchain and can it be read directly?

Solution:

In order to get baseFee value using the Ethereum API see @gluneau answer.

When it comes to the Substrate API: In the Astar blockchain, there exists a BaseFee pallet, which stores the baseFee value for each block. We can access its storage to fetch the baseFee value for any specific block.

For instance, with the Substrate Sidecar API, we can use the following path to obtain the baseFeePerGas value for block number 1300000:

/pallets/base-fee/storage/BaseFeePerGas?at=1300000

1 Answer 1

2

Here is a method to get historical baseFeePerGas

const { ethers } = require("ethers");

async function getBaseFee(blockNumber) {
    const rpc = "https://evm.astar.network"
    let provider = new ethers.providers.JsonRpcProvider(rpc);
    let block = await provider.getBlock(blockNumber);
    return block.baseFeePerGas;
}

let blockNumber = 1300000;  // replace with the block number you're interested in
getBaseFee(blockNumber)
    .then(baseFee => console.log(`Base fee for block ${blockNumber}: ${baseFee}`))
    .catch(err => console.log(err));

I've made also a replit you can play with.

1
  • Thank you for your answer. While your suggestion to use the Ethereum API to fetch the base fee is valid, I was specifically interested in achieving the same via the Substrate API. After doing some additional research, I have found the answer. I will update my question with the solution.
    – pjozvtg
    Commented Jun 6, 2023 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.