I used web3.eth.estimateGas to calculate the fee for a transaction with data. My code below:
var web3 = new Web3('https://bsc-dataseed1.binance.org:443');
var esGas;
web3.eth.estimateGas({
"from" : 'my address',
"nonce" : 1,
"to" : 'contract address',
"data" : 'data'
}).then((result) => {
esGas = result; // always 81332 but Metamask is 89465
});
var gasPrice;
web3.eth.getGasPrice(function(e, r){
gasPrice = r; // always 5 GWei like Metamask
});
With command below, the result is always 0.00040666 while with same 'data'
But I used Metamask, its result is always Max amount: 0.00044732 BNB
web3.utils.fromWei(gasPrice.toString(), 'ether') * esGas; // 0.00040666
Can anyone help me explain? And how to calculate like Metamask? Or my calculation is safe to send?
Many thanks