32

When I see transactions in Etherscan (e.g., 0x7eb9a7454f9cd2f39a7ea7c6212833673165b35f352127ffb337df866897eb73), I see the field Cumulative Gas Used which isn't present in web3 response.

In general, it is equal to Gas Used but I wonder if it could be different and in case it could be, how it should be calculated.

1 Answer 1

34

First of all, let's understand what is the cumulative gas used.

cumulativeGasUsed: Number - The total amount of gas used when this transaction was executed in the block.

As suggested by JavaScript API. That explanation was not clear to me at all, let's try another one:

cumulativeGasUsed is the sum of gasUsed by this transaction and all preceding transactions in the same block.

Example: http://etherscan.io/txs?block=1402679 We have there 4 transactions. 3 simple sends, and 1 contract creation.

  1. Send, 21000 gasUsed, 21000 cumulativeGasUsed
  2. Send, 21000 gasUsed, 42000 cumulativeGasUsed
  3. Send, 21000 gasUsed, 63000 cumulativeGasUsed
  4. Contract creation, 514474 gasUsed, 577474 cumulativeGasUsed

Now if we calc 577474 - 63000 the result is 514474.

Back to your question about how to calculate it. Simple answer: you just don't bother, cause it doesn't(almost) affect your transaction. But if you're really want to do that here the simple approach. Get the 'pending' block gasUsed web3.eth.getBlock('pending').gasUsed and add to it the estimateGas of your transaction. Most of the time it will result in a cumulativeGasUsed for your transaction. It may change though if someone will submit another transaction in between your estimation and your submit.

I said 'almost' earlier because it may affect your transaction in a way that it will not fit the block gas limit.

I see the field Cumulative Gas Used which isn't present in web3 response.

It is present in web3.eth.getTransactionReceipt(txHash) response.

1
  • 3
    So this is usefull to sum up all the gas used in a block but for a contract developer it has no further value? Commented Jun 26, 2016 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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