What is the correct algorithm to calculate gas limit for transaction with data in Ethereum?
1 Answer
According to Ethereum Yellow Paper, in order to calculate gas limit for transaction with non-zero data you will need to use the following formula:
gasLimit = Gtransaction + Gtxdatanonzero × dataByteLength
where:
Gtransaction = 21000 gas
Gtxdatanonzero = 68 gas
dataByteLength — your data size in bytes
So, the final formula would look like this:
gasLimit = 21000 + 68 * dataByteLength
.
If your data has 10 bytes, the gas limit would be:
gasLimit = 21000 + 68 * 10 = 21000 + 680 = 21680
-
-
-
Keep in mind if you're sending to a smart contract, that smart contract's internal functions will require additional gas depending on what all they do. Oct 13, 2021 at 4:19
-
What's a reliable way to estimate the gas if we are calling a function on the smart contract? There is a
rpcProvider.estimateGas(txn)
method I can use, but it will fail to estimate the gas sometimes on some complicated functions– YaoJan 4 at 19:30