2

I have some web application based on Ethereum. This is how I call some function from a contract:

contract.doSomething.sendTransaction(data, {gasPrice: web3.toWei(10, 'Gwei')}, callback...);

This code works. But I have few questions:

1) Can I change GASPRICE for this transaction in the MetaMask?

2) How does the MetaMask know what GASLIMIT to set?

3) If there is no GASPRICE specified in the transaction code, how does the MetaMask know how to set the GASPRICE?

2 Answers 2

2

According to the following code, this is how metamask generates different gas estimates :

  • Get the details of the 5 most recent blocks.
  • Get the base fee of the latest block.
  • Adjust the base fee according to the high, medium, or low user priority.
  • From each block, collect the priority fee for transactions at the 10th, 20th, and 30th percentiles.
  • Sort the priority fees according to the percentile and get the medians of each of the sorted list of priority fees.
  • Adjust the medians according to the high, medium, or low user priority.
  • Get the gas estimates of your transaction.
  • Calculate the fee using the following formula,
    fee = (gas estimates) * (adjustedBaseFee + adjustedPriorityFeeMedian)

This article from chainstack explains the metamask gas estimation process and also provides a python implementation of the gas estimation process: https://chainstack.com/a-developers-guide-to-the-transactions-in-mempool-code-edition/

1

Metamask performs estimations about how much gas is needed (gas limit). I'd assume it adds some "safety" margin on top of that. Also, the gas estimations are sometimes very inaccurate and/or impossible to perform due to various reasons.

The default gas price is probably taken from a gas price prediction system such as https://ethgasstation.info/ .

You can adjust both of these if needed: https://metamask.zendesk.com/hc/en-us/articles/360015488771-How-to-Adjust-Gas-Price-and-Gas-Limit-

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .