What is a Transaction Status?

In Ethereum, every transaction submitted to the network has a status that indicates whether it has been successfully mined, is pending, or has failed. Checking the status of a transaction is essential for users and developers to understand the outcome of their operations on the blockchain.

How to Check Transaction Status in Ethers.js

Ethers.js provides a straightforward way to check the status of a transaction using the transaction hash. Once you have the hash, you can use the provider.getTransaction method to retrieve the transaction details, including its status. Below is a complete HTML example demonstrating how to check the status of a transaction using Ethers.js:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ethers.js Transaction Status Example</title>
<script src="https://cdn.jsdelivr.net/npm/ethers@5.7.0/dist/ethers.umd.min.js"></script>
</head>
<body>
<h1>Check Transaction Status</h1>
<input type="text" id="txHashInput" placeholder="Enter transaction hash" />
<button id="checkButton">Check Status</button>
<pre id="statusResult"></pre>

<script>
async function checkTransactionStatus() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const txHash = document.getElementById('txHashInput').value;

try {
// Get transaction details
const transaction = await provider.getTransaction(txHash);

if (transaction) {
// Check if the transaction is confirmed
const receipt = await provider.getTransactionReceipt(txHash);
const status = receipt.status === 1 ? "Success" : "Failed";
document.getElementById('statusResult').innerText =
"Transaction Hash: " + txHash + "\\n" +
"Block Number: " + receipt.blockNumber + "\\n" +
"Status: " + status;
} else {
document.getElementById('statusResult').innerText = "Transaction not found.";
}
} catch (error) {
document.getElementById('statusResult').innerText = "Error: " + error.message;
}
}

document.getElementById('checkButton').onclick = checkTransactionStatus;
</script>
</body>
</html>

How It Works

In the example above:

  1. We create a simple HTML interface that allows the user to input a transaction hash.
  2. The checkTransactionStatus function initializes a connection to the Ethereum network using a Web3 provider.
  3. It retrieves the transaction details using provider.getTransaction with the provided transaction hash.
  4. If the transaction exists, it then fetches the transaction receipt using provider.getTransactionReceipt to check its status.
  5. The status is determined based on the receipt's status property, where a value of 1 indicates success and 0 indicates failure.
  6. The transaction hash, block number, and status are displayed on the webpage, or an error message is shown if the retrieval fails.

Conclusion

Checking the status of a transaction in Ethers.js is a vital part of interacting with the Ethereum blockchain. By using the getTransaction and getTransactionReceipt methods, you can easily determine whether your transactions have been successfully mined or if they have encountered any issues. This functionality enhances the user experience by providing real-time feedback on transaction outcomes.