getTransaction vs getTransactionReceipt
Asked Answered
S

1

10

What is the difference between two methods web3.eth.getTransaction and web3.eth.getTransactionReceipt in the web3 library? I tried to read this documentation https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#gettransactionreceipt but the difference is not clear to me.

Sickle answered 26/4, 2021 at 8:32 Comment(0)
S
18

The receipt is available only for mined transactions. But because of this, it includes a few more properties:

  • status - successful or reverted
  • gasUsed - amount of gas used by this tx alone
  • cumulativeGasUsed - amount of gas used by this tx and its internal transactions
  • logs - list of event logs that the transaction produced

The regular getTransaction allows you to get details (such as from, to, data and value) for transactions that are not yet mined. Possible use case: You got only the transactionHash from an external source and need to find out the recipient and don't know whether the transaction has been mined yet.

So they both can be used in different cases.

Selfstyled answered 26/4, 2021 at 9:2 Comment(2)
I also thought this, but according to the documentation getTransaction also gives transactionIndex in the block, which should be available only when the transaction is included in the block, so I was confused.Darrickdarrill
@AnhDũngLê Yes, but transactionIndex in getTransaction() is nullable. So if the transaction is pending, getTransaction() returns the tx with index null, but getTransactionReceipt() returns null for the whole tx. If the transaction is mined, both return a object with the index value in the transactionIndex field.Selfstyled

© 2022 - 2024 — McMap. All rights reserved.