2

How does metamask come up its suggested gas fees? How does it come up with a certain amount?

What is it optimised for? Should you just use it or should you set your own?

2 Answers 2

2

It uses web3.eth.estimateGas (https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html), which basically runs the transaction locally and returns the amount of gas used. It's pretty good most of the times but it can definitely get it wrong. Not sure in which specific cases though.

4

On the Ethereum network, the gas fee is composed of a base fee and a priority fee.

The base fee is retrieved from the network and is based on the network workload. MetaMask will call the eth_getBlockByNumber method, this gives a lot of information about the block, including the base fee. You can retrieve the base fee also by calling the eth_getGasPrice method.

Then the priority fee is what goes to the miner. Generally, 2 Gwei is considered the minimum needed for a miner to pick it up.

Then the acual ETH value used for gas is calculated by doing:

(base fee + priority fee) * gas limit

The gas limit in 21000 units for a regular transfer on the ETH network, but it can be retrieved from the network using the eth_estimateGas method.

I have a repo that shows all this in Python using web3.py if you want to see how it works!

https://github.com/soos3d/Web3.py-estimate-gas-fees

1
  • this is the better answer, as the question asked about gas fees in general and not about gas limit specifically Sep 29, 2022 at 6:08

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.