How to get a transaction on Ethereum using Web3 JavaScript

In this tutorial, we are going to learn how to get information about an Ethereum transaction using web3 and JavaScript.

In this tutorial, we are going to learn how to get information about an Ethereum transaction using web3 and JavaScript.

Introduction

Note that this also works for all EVM blockchains. Meaning that you can use it to get transactions on the Binance Smart Chain, Fantom network, Polygon network, Avalanche network ... etc

Here you can find a list of all EVM blockchains: https://chainlist.org/

Set up

First, install the web3 library:

npm install web3

and import it that way:

import Web3 from 'web3';

Now, you need a Web3 instance. To create that, you will need a provider which can be an HTTP provider like an Infura URL or another node provider like GetBlock or it can be a wallet that the user connected to your website (through window.ethereum ).

Learn how to connect a wallet to your website here

If you use an HTTP provider, you have to pass a URL that corresponds to the blockchain you want to use. In this guide, we use Infura as it supports Ethereum:

const web3 = new Web3(new Web3.providers.HttpProvider(INFURA_URL_HERE));

Now, you have 2 methods to get a transaction:

  • getTransactionReceipt – this will return information about a completed transaction (a transaction that either failed or passed successfully but that's not pending). It will return null if the transaction is pending.
  • getTransaction – This will return information about a transaction, even if it's pending. The other difference with getTransactionReceipt is that it doesn't have the status property that tells whether or not the transaction was successful.

They both return the same transaction object which we'll see below and they both take the hash of the transaction you want to check as a parameter.

Here is how to use these methods:

const transactionHash = "HASH_OF_THE_TRANSACTION_TO_CHECK"


const transaction = await web3.eth.getTransactionReceipt(transactionHash)

// OR

const transaction = await web3.eth.getTransaction(transactionHash)

The transaction receipt object

This is what getTransactionReceipt returns:

  • status : TRUE if the transaction was successful, FALSE if the EVM reverted the transaction.
  • blockHash : The hash of the block where the transaction was in.
  • blockNumber : The block number where this transaction was in.
  • transactionHash : The hash of the transaction.
  • transactionIndex: The transactions' index position in the block.
  • from : The address of the sender.
  • to : The address of the receiver. It is null when it’s a contract creation transaction.
  • contractAddress : The contract address created, if the transaction was a contract creation, otherwise null.
  • cumulativeGasUsed : The total amount of gas used when the transaction was executed in the block.
  • gasUsed : The amount of gas used by the transaction alone.
  • logs : An array of log objects, that the transaction generated.

The transaction object

This is what getTransaction returns:

  • hash : The hash of the transaction.
  • nonce : The number of transactions made by the sender prior to this one.
  • blockHash : The hash of the block where this transaction was in. null if the transaction is pending.
  • blockNumber : The block number where this transaction was in. null if the transaction is pending.
  • transactionIndex : The transactions index position in the block. null if the transaction is pending.
  • from : The address of the sender.
  • to : The address of the receiver. It is null if it’s a contract creation transaction.
  • value : The value transferred in that transaction. The unit is wei.
  • gasPrice : The gas price provided by the sender in wei.
  • gas : The gas provided by the sender.
  • input : The data sent along with the transaction.

And that's it! πŸ‘

Thanks for reading this article!