There are many questions and answers about estimating the amount of gas consumed. This question is about what to do with this estimate.
What values should we actually provide to gas
and gasPrice
in the send()
options, to make sure transactions go through, but we won't pay too much:
myContract.methods.myMethod(123).send({
from: myAddress,
gas: myGasLimit,
gasPrice: myGasPrice
}, function(error, transactionHash){
...
});
Most people seem to advise to set the gas
option to 10-20% above estimatedGas
(which can be retrieved via the web3 interface).
But what about the case where the estimate is wrong? Because not-used gas is returned, why not set the maximum to something huge? What about the gasPrice
, should we leave that up to the miner or actually set a value?
So far I didn't provide any values for gas
and gasPrice
in the send options, and that resulted in wildly varying amounts of actually paid gas between wallets. In the browser with MetaMask a contract deploy costs 0.000633134 Ether (on Rinkeby) and the same deploy in the CoinbaseWallet costs 0.01268828 Ether (also on Rinkeby). Apparently the gasPrice
at the CoinbaseWallet transaction was 20 GWei compared to 1 GWei for the MetaMask transaction.
Edit: clarification of this question:
To make my question more clear: where the question I link to above is about the limitations of the getEstimate function, I am here asking about best practices after having used this function to get an estimate.
Having an estimate, what values do I provide for the gas
and gasPrice
fields to limit the risk to pay too much and to be picked up in the first upcoming block?
Let's assume here that the method is our own and we know that the estimate is more or less correct (no state changes are possible that make the gas consumption substantially larger).
From the current answers I can conclude: don't set the gas limit too high (so the 10-20%-higher-than-the-estimate-rule sounds good?)
About the gasPrice
: I did some experiments and saw that MetaMask uses the value the dapp sets and CoinBaseWallet ignores it. So maybe letting the wallet (and if possible the end-user) decide is the way to go?