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!