...

How to fix WEB3 metamask gas suggestion not working

by Ballistic Freaks

Share our post:

Share on facebook
Share on linkedin
Share on twitter
Share on email

Metamask gas suggestion not working for your Web3.js contract interface? Here’s how to fix it!

What’s happening here, why is metamask gas suggestion ignored?

We ran into an issue, where mint transactions would fail. This can be solved via increasing gas amount for minting. The problem is that an average user cannot be asked to manually go and change the gas fee to priority. They want to just get on the site, click mint, confirm it and be done with it. So we decided to do just that but were stalled by a strange problem. No matter the gas price we passed, metamask would always display it’s own suggestion. There was no error, no warning. After about a day of trial and error we finally solved it.

It’s very counter intuitive and most likely a bug with the Web3.js library. Not passing null values for the highlighted options would cause the suggested gas price to be ignored and the default metamask gas suggestion would be used. Now we can  set a custom gas amount (triple the default in this case) and rest assured that minting will always succeed!

  // An example contract with mint method
const minter = contract.methods.mint(window.metamaskAccount, amount, referral);

async function estimateGas(method, values, multiplier = 1) {
    let gas 
    try{
        gas = await method.estimateGas(values) * multiplier;
    }catch (exception) {
       // HANDLE ERROR
    }
    return gas
}

const mintData = {
  from: window.metamaskAccount,
  value: price
}

minter.send({...mintData, ...{
  gas: await estimateGas(minter, mintData, 3), // Triple the gas fee to make absolutely sure that minting doesn't run out of gas
  maxPriorityFeePerGas: null, // MAKE SURE TO PASS NULL HERE
  maxFeePerGas: null, // AND HERE
}})
.then((response) => {
 // Handle response
})
.catch((error) => {
  // HANDLE ERROR
});  

Hugry for more Web3 programming content?

Developing with new technologies can be tricky. We have have banged our heads against the wall many many times. Now we share our experience to make sure that other don’t have to suffer the mistakes and pitfalls that we encountered. Check out our socials and consider minting BallisticFreaks NFT’s to help us give back to the community!

Other articles by us:

Elementor #2076

Decouple your code with this one trick! While this applies to javascript, most languages have their own equivalents. Don’t you love when you need to

Read More »

REFER FREAKS

GET 20% BACK

Create a code for your friends to use and get back 10% of every successful transaction that used your code. Same code can be used multiple times!

Code already taken!

Note: Referral code will be tied to the wallet used to create it. After every successful mint, your wallet will automatically receive 20% of mints value

code creation fee:

0.02 ETH

Seraphinite AcceleratorBannerText_Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.