4

What is the correct algorithm to calculate gas limit for transaction with data in Ethereum?

1
  • 4
    It's not a duplicate! The attached QA has nothing to do with arbitrary data cost estimation. Commented Feb 16, 2018 at 14:33

1 Answer 1

14

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

5
  • will it be different if I'm sending to a contract address? Commented Jul 19, 2019 at 7:43
  • How I can calculate dataByteLenght of my transaction? Commented Jan 22, 2021 at 19:04
  • 1
    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. Commented 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
    – Yao
    Commented Jan 4, 2023 at 19:30
  • the answer should be outdated as of EIP-1559 release?? Commented Jan 24 at 5:25

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