2

When writing to a smart contract, we can specify the priority and fee per gas.

await contract.connect(owner).foo({
  maxFeePerGas: "<YOUR FEE>"
  maxPriorityFeePerGas: "<YOUR PRIORITY FEE>"
});

If we don't specify it, how do these nodes determine those values, and is it possible to know those before making the transaction?

Etherjs has an API called "getFeeData", is that what's being used for the default values?

2 Answers 2

2

yes, ethers.js .getFeeData() returns hardcoded values and they are used as defaults to send a type2 (eip-1559) transaction if the network supports it, otherwise, gasPrice is used with the legacy transaction. Here's the discussion around it and also a relevant code:

async getFeeData(): Promise<FeeData> {
        const { block, gasPrice } = await resolveProperties({
            block: this.getBlock("latest"),
            gasPrice: this.getGasPrice().catch((error) => {
                // @TODO: Why is this now failing on Calaveras?
                //console.log(error);
                return null;
            })
        });

        let maxFeePerGas = null, maxPriorityFeePerGas = null;

        if (block && block.baseFeePerGas) {
            // We may want to compute this more accurately in the future,
            // using the formula "check if the base fee is correct".
            // See: https://eips.ethereum.org/EIPS/eip-1559
            maxPriorityFeePerGas = BigNumber.from("1500000000");
            maxFeePerGas = block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas);
        }

        return { maxFeePerGas, maxPriorityFeePerGas, gasPrice };
    }

I haven't yet tested these defaults on Eth but on Polygon this seems to be underpriced as the latest block baseFee returned by the network is very low and causes transaction to be stuck in pool forever so I use the methods outlined here:

The most guaranteed way to have your transaction included in the block is to specify a maxPriorityFeePerGas field (which is a tip). In this case, Alchemy will look up the pending baseFee and then set the maxFeePerGas field accordingly (to the sum of the base fee and the tip). All you have to do is decide how much tip to provide, which you can get by simply calling the eth_maxPriorityFeePerGas method on Alchemy.

1
  • as it turns out (block && block.baseFeePerGas) may not be a reliable way to tell if network supports eip-1559. for example, Klaytn currently returns baseFeePerGas as 0 so sending transaction immediately fails. For this case I have to manually set the gasPrice override.
    – ihor.eth
    Commented Jul 28, 2022 at 23:39
0

There are two aspects to gas, the amount of gas used and its price.

Before sending a transaction, the client performs estimateGas function of Web3, which simulates the code on a EVM and records the amount of gas used, let's say 100,000 gas.

Now the client checks the network, either queries a 3rd party provider, or an Ethereum node, what's the suggested gas price, it'll result in three levels of price - Safe, Propose and Fast. The Fast is usually the confirmation within 30 sec, let's say it's 30 Gwei.

So 100,000 gas with a 30 Gwei bid, would cost a maximum of 100000 * 30 / 10^9 = 0.003 ETH. Division by 10^9 converts Gwei to Ether units of price.

You can query etherscan.io for suggested gas prices using their API's gastracker module or query your node directly using Web3's Gas Price API.

2
  • 1
    "query your node directly using Web3's Gas Price API." - This refers to gas price, which is for the legacy transaction. I don't see an API for maxFeePerGas or maxPriorityFeePerGas. I can query from third party services, but am wondering if I can query directly from the provider, and add a multiplier if I wanted to increase the bid instead of having to query a separate service.
    – Reginal
    Commented May 19, 2022 at 12:59
  • 1
    good for general knowledge but doesn't really answer the question
    – ihor.eth
    Commented Jul 22, 2022 at 16:46

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.