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
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.
after send transaction, you can use Eth.waitForTransactionReceipt to get tx receipt. when you get receipt, "gasUsed" is real gas used in tx.