To check the status of an Ethereum transaction, you can use a transaction hash (also known as a transaction ID) and a blockchain explorer like Etherscan. Additionally, you can use web3.js to programmatically check the status of your transaction. Below, we will discuss the steps to check your transaction status using both methods.
1. **Using a Blockchain Explorer**
Blockchain explorers are online tools that allow you to view detailed information about Ethereum transactions. Etherscan is one of the most popular explorers. Here’s how to check your transaction status:
- Obtain the Transaction Hash: After sending a transaction, you will receive a transaction hash. This is a unique identifier for your transaction.
- Visit Etherscan: Go to Etherscan.
- Search for the Transaction: Paste your transaction hash into the search bar and hit enter.
- View Transaction Details: You will see the transaction status (Pending, Success, or Failed), along with other details such as the block number, gas used, and more.
2. **Using Web3.js to Check Transaction Status**
You can also check the status of your Ethereum transaction programmatically using the web3.js library. Below is a sample code snippet demonstrating how to do this:
javascript
// Import the web3 library
const Web3 = require('web3');
// Connect to an Ethereum node (using Infura in this example)
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'));
// Function to check transaction status
async function checkTransactionStatus(transactionHash) {
try {
const receipt = await web3.eth.getTransactionReceipt(transactionHash);
if (receipt) {
if (receipt.status) {
console.log('Transaction was successful:', receipt);
} else {
console.log('Transaction failed:', receipt);
}
} else {
console.log('Transaction is still pending or does not exist.');
}
} catch (error) {
console.error('Error fetching transaction status:', error.message);
}
}
// Replace with your transaction hash
const transactionHash = 'YOUR_TRANSACTION_HASH';
checkTransactionStatus(transactionHash);
Explanation of the Sample Code:
- Import Web3: The code begins by importing the web3 library to interact with the Ethereum network.
- Connect to Ethereum Node: It connects to an Ethereum node using Infura. Replace
YOUR_INFURA_PROJECT_ID
with your actual Infura project ID. - Check Transaction Status Function: This function takes a transaction hash as an argument and retrieves the transaction receipt. It checks the status of the transaction and logs whether it was successful, failed, or still pending.
3. **Conclusion**
Checking the status of your Ethereum transaction is straightforward. You can use a blockchain explorer like Etherscan for a quick check or utilize web3.js for a programmatic approach. By following these steps, you can easily track and verify your transactions on the Ethereum network.