This guide shows you how to use a transaction hash and eth_getTransactionReceipt
to tell whether a transaction succeeded, reverted, or is still pending.
- Node.js 18 or later
- An Alchemy API key
- A transaction hash on Ethereum Mainnet
A transaction receipt exists only after the transaction is mined. If the
method returns null, the transaction is still pending or the hash is
unknown.
Export your API key as an environment variable:
export ALCHEMY_API_KEY="YOUR_API_KEY"Create a file named check-status.mjs and add this code:
const apiKey = process.env.ALCHEMY_API_KEY;
if (!apiKey) {
throw new Error("Set the ALCHEMY_API_KEY environment variable first.");
}
const rpcUrl = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
const txHash =
"0xd488331be3a2f9cdd0f2b351f2b13f5151630aaafd2c2b246f7f3cd7fd0b1dfc";
async function checkTransactionStatus(hash) {
const response = await fetch(rpcUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "eth_getTransactionReceipt",
params: [hash],
}),
});
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
const payload = await response.json();
if (payload.error) {
throw new Error(payload.error.message);
}
const receipt = payload.result;
if (!receipt) {
console.log("Transaction is still pending or the hash is unknown.");
return;
}
if (receipt.status === "0x1") {
console.log("Transaction succeeded.");
return;
}
console.log("Transaction reverted.");
}
await checkTransactionStatus(txHash);Run the file:
node check-status.mjsA successful run prints one of these results:
Transaction succeeded.Transaction reverted.Transaction is still pending or the hash is unknown.Transaction succeeded.means the receipt exists andstatusis0x1Transaction reverted.means the receipt exists andstatusis0x0Transaction is still pending or the hash is unknown.means the receipt isnull