Getting the Status of a Transaction in Web3.js

In Web3.js, you can check the status of a transaction by querying the Ethereum blockchain for the transaction receipt. The receipt contains important information about the transaction, including whether it was successful, the block number in which it was included, and any logs generated by the transaction. Below is a detailed guide on how to get the status of a transaction using Web3.js.

Step-by-Step Guide

  • Install Web3.js: Ensure that Web3.js is installed in your project. You can install it via npm:
  • npm install web3
  • Connect to Ethereum Network: Connect to an Ethereum node using Web3.js. You can use a provider like Infura or connect to a local node.
  • Get the Transaction Receipt: Use the web3.eth.getTransactionReceipt() method to retrieve the transaction receipt using the transaction hash.
  • Check the Status: Inspect the receipt to determine the status of the transaction.

Sample Code

Here’s a simple example of how to get the status of a transaction:

const Web3 = require('web3');

// Connect to the Ethereum network
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545");

// Function to get the status of a transaction
async function getTransactionStatus(transactionHash) {
try {
// Get the transaction receipt
const receipt = await web3.eth.getTransactionReceipt(transactionHash);

if (receipt) {
// Check the status of the transaction
const status = receipt.status ? 'Success' : 'Failure';
console.log(`Transaction Status: ${status}`);
console.log(`Block Number: ${receipt.blockNumber}`);
console.log(`Gas Used: ${receipt.gasUsed}`);
console.log(`Logs: ${JSON.stringify(receipt.logs)}`);
} else {
console.log("Transaction is not yet mined or does not exist.");
}
} catch (error) {
console.error("Error fetching transaction status:", error);
}
}

// Replace with your transaction hash
const transactionHash = '0xYourTransactionHashHere';

getTransactionStatus(transactionHash);

Explanation of the Code

  • Web3 Initialization: A new instance of Web3 is created to connect to the Ethereum network.
  • Async Function: The getTransactionStatus function is defined as asynchronous to handle the promise returned by getTransactionReceipt.
  • Getting the Transaction Receipt: The transaction receipt is retrieved using the web3.eth.getTransactionReceipt(transactionHash) method, where transactionHash is the hash of the transaction you want to check.
  • Checking the Status: The status of the transaction is determined by the receipt.status property:
    • If receipt.status is true, the transaction was successful.
    • If receipt.status is web3.eth.getTransactionReceipt()1, the transaction failed.
  • Logging Additional Information: The block number, gas used, and logs generated by the transaction are also logged for further inspection.
  • Error Handling: A try-catch block is used to handle any potential errors during the process of fetching the transaction status.

Important Notes

  • Make sure that the transaction hash you provide is valid and corresponds to a transaction on the Ethereum network.
  • Transactions can take some time to be mined, so if the transaction is not yet mined, you may receive a message indicating that the transaction does not exist.

Conclusion

Getting the status of a transaction using Web3.js is a straightforward process that allows you to monitor the outcome of your transactions on the Ethereum blockchain. By following the steps outlined above, you can easily retrieve and check the status of any transaction by its hash.