3

I am using web3py to transfer tokens and ether. I need to track the transaction fees for each transaction. Given a transaction hash, how do I determine the total fee paid?

2

You can get this from the transaction receipt. You can use

web3.eth.getTransactionReceipt(transaction_hash)

It returns a dictionary with the field gasused, combining this with the gasPrice that you used for the transaction you can get the fee cost in Ether.

for details of the receipt see the Documentation

Hope this helps

  • So it means, transaction_fee_in_wei = gasUsed * gasPrice ? – Paras Mar 22 at 9:49
  • Yes, that is correct – Jaime Mar 22 at 10:08
1

The total transaction fee is calculated as (gasPrice * gasUsed). The following example shows how to obtain these values using web3.py:

gas_price = web3.eth.getTransaction(transaction_hash).gasPrice
gas_used = web3.eth.getTransactionReceipt(transaction_hash).gasUsed

transaction_cost = gas_price * gas_used

The value of transaction_cost is denominated in wei.

Related documentation:

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.