Article thumbnail

Share

Disable site suggested gas fee in Metamask using web3.js

Profile picture of author
Rowin van Amsterdam
-

In the process of connecting a smart contract to a webapplication using the web3.js library I found out that MetaMask will use a gas fee that is suggested by your website. Even if you didn't define it yourself, because the web3.js library creates it's own values (version 1.7.4). By default the values are so low that the transaction will most likely fail.

The solution could be specifying a custom gas fee, but I was looking for a way to use the integrated gas fee tracker from MetaMask itself. Most of the time the gas fee suggested by MetaMask is good enough and will dynamically adjust to the current network conditions/demand.

You can disable the website suggested gas fee by defining maxPriorityFeePerGas and maxFeePerGas as null. Pass them as parameter to the .send() function and from now on MetaMask will no longer show the "site suggested" gas fee.

const contract = new web3.eth.Contract(contractABI as AbiItem[], contractAddress as string);

export const transfer = async () => {
    await contract.methods
        .transfer(recipient, amount)
        .send(
            from: userAddress, 
            gas: 70000, 
            maxPriorityFeePerGas: null, 
            maxFeePerGas: null
        );
};

This is useful if you don't want to specify a custom gas fee by yourself. Also Checkout the website I quickly build to demonstrate the difference.

➡️ GitHub repository