I need help with figuring out the estimated gas for an approval. I'm able to get a number but it doesn't match what's in metamask. I'm a noob to web3 and so I'm not even sure I'm passing the correct data to pass into the methods.

I've deployed two contracts to my local Ganache chain. For my app, I want to display the gas fees on the app instead of just showing them in metamask.

Running this in javascript

myContract.methods.approve(SecondContractAddress, allowance).estimateGas({from: deployerAddress})

gives me a gas fee but when I run

myContract.methods.approve(SecondContractAddress, allowance).send({from: deployerAddress})

it opens metamask and metamask displays a different estimated gas fee.

When I run

myContract.methods.allowance(ownerAddress, spendContractAddress).estimateGas()
                                .then(function (allowance) { console.log("Allowance: " + allowance)})

I get Allowance: 23729

I then run

web3.eth.getBlock('latest').then(function(latestBlock) {
    const blockGas = latestBlock.gasLimit;
    const finalGas = (blockGas * allowance);
    const finalGasInEther = web3.utils.fromWei(finalGas.toString(), 'ether');
    console.log("finalGasInEther: ", finalGasInEther);

The finalGasInEther is different than what's in metamask.

I'm using covalent to get the ETH price via their API.

let ethUSD = response.data.items[0].quote_rate;
const gasAmount = (Number(finalGasInEther) * ethUSD);
console.log('ethUSD', ethUSD);
console.log('gasAmount', gasAmount);

I'm expecting gasAmount to be the estimated gas fee. Any help would be appreciated.

  • Same issue here, the gas I get is about 2-3 times less than what MetaMask estimated. Did you find a solution? Commented Oct 29, 2022 at 22:31
  • I never found a solution.
    – Chris
    Commented Nov 1, 2022 at 18:24

1 Answer 1


I used estimateGas directly (without using web3.eth.getBlock('latest')) and it turns out to be most likely accurate in Eth. And I use the gas price estimating API from Opensea. The gas estimation is very close to what it actually comes out on MetaMask right now.

I called API on the backend (Node.js based) btw, due to my special requirements.

The full process:

  1. Call estimateGas on the frontend with all parameters required.
  2. Call the Opensea gas estimating API to get the current gas price.
  3. Time them up. In my case, I added a little excess to make sure it was covered.

The full code:

const gweiToEth = 1000000000;

// The gas price call happens on the backend, but you can do frontend
.then(r => {
    // Opensea returns gas price in string for some unknown reasons
    const gasPrice = parseInt(r.gasPrice); 
    .estimateGas({ from: yourWallet, value: valueInEth },
    (err, gasAmount) => {
        // This is your estimated gas.
        const theEstGas = gasAmount * gasPrice * gweiToEth;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.