1

is it possible to get the information about the actual amount of gas that was used for a transaction? I have the transaction hash and I'd like to know the gas that was used by this transaction after the block has been mined. The estimate Gas won't be helpful for me. I'm using web3.py and Ganache

  • 1
    web3.eth.getTransactionReceipt will have the gas used – Raghav Sood Aug 15 '18 at 6:51
3

Wait for the receipt and read the gasUsed value, like so:

from web3.auto import w3

transaction_hash = '0x...'
receipt = w3.eth.waitForTransactionReceipt(transaction_hash)
print(receipt.gasUsed)

Naturally, you may have to set up your Web3 instance with different connection parameters. The auto import is just shorthand for a quick and dirty setup.

If I have multiple transactions in one Block, this will return the gasUsed up to this transactions.

(from a comment on another answer)

It seems you were looking at cumulativeGasUsed, which tracks all gas used in the block. gasUsed will be the gas from only that transaction.

| improve this answer | |
1

after send transaction, you can use Eth.waitForTransactionReceipt to get tx receipt. when you get receipt, "gasUsed" is real gas used in tx.

Eth.waitForTransactionReceipt

| improve this answer | |
  • thank you! but if I have multiple transactions in one Block, this will return the gasUsed up to this transactions. So if I have for example 5 transactions in one Block and I look up the 3rd transaction of the block the gasUsed variable will give me the gas that has been used for the transactions 1 + 2 + 3. So if I only wanna have the gas for the 3rd transaction, do i have to calculate it myself? – Matthias Seidl Aug 15 '18 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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