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?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
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.
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
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
I have a repo that shows all this in Python using web3.py if you want to see how it works!