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.