Understanding eth_getTransactionReceipt in Ethereum: A Complete Guide

ยท

Introduction

The eth_getTransactionReceipt method is a critical Ethereum API function that retrieves transaction details using its hash. It verifies transaction success/failure, checks smart contract execution status, retrieves logs, and confirms contract deployments. This guide explores its parameters, response fields, use cases, and includes practical code examples.


Parameters

| Parameter | Description |
|-----------|-------------|
| hash | The unique transaction hash identifying the transaction. |


Response Fields

| Field | Description |
|-------|-------------|
| blockHash | Block hash where the transaction was included. null if pending. |
| blockNumber | Block number of inclusion. null if pending. |
| contractAddress | Address of newly deployed contract (if applicable); otherwise null. |
| cumulativeGasUsed | Total gas used in the block until this transaction. |
| effectiveGasPrice | Actual gas price paid by the sender. |
| from | Sender's address. |
| gasUsed | Gas consumed by this transaction. |
| logs | Array of log objects emitted by smart contracts. |
| logsBloom | Bloom filter for lightweight log retrieval. |
| status | 1 for success, 0 for failure. |
| to | Recipient's address (null for contract creation). |
| transactionHash | Unique transaction identifier. |
| transactionIndex | Transaction's position in the block. |
| type | Transaction type (0 for transfers, 2 for contract interactions). |

๐Ÿ‘‰ Explore Ethereum development tools


Code Examples

Ethers.js Implementation

const ethers = require("ethers");

// Initialize ChainstackProvider for Ethereum mainnet
const chainstack = new ethers.ChainstackProvider("mainnet");

async function verifyTransaction(transactionHash) {
  const receipt = await chainstack.send("eth_getTransactionReceipt", [transactionHash]);

  if (!receipt) {
    console.log(`Transaction ${transactionHash} is pending...`);
    return;
  }

  if (receipt.status) {
    console.log(`โœ… Transaction successful! Hash: ${transactionHash}`);
    if (receipt.contractAddress) {
      console.log(`๐Ÿ“‘ Contract deployed at: ${receipt.contractAddress}`);
    } else {
      console.log("โ— No contract deployed.");
    }
  } else {
    console.log(`๐Ÿ’ฅ Transaction failed. Hash: ${transactionHash}`);
  }
}

verifyTransaction("0x2761f74e2f45d981a9d7553cbbcfbcc862cae416eb37a820300d4c19516d6fca");

Use Cases

  1. Transaction Verification
    Confirm if a transaction succeeded or failed, critical for dApp user feedback.
  2. Smart Contract Deployment
    Validate contract deployment and retrieve its address.
  3. Log Analysis
    Access emitted logs for debugging or event tracking.

๐Ÿ‘‰ Learn more about Ethereum APIs


FAQs

Q: How do I check if a transaction is pending?

A: If blockHash and blockNumber are null, the transaction is still pending.

Q: What does status: 0 mean?

A: It indicates transaction failure (e.g., out of gas or reverted).

Q: Can I retrieve gas fees from the receipt?

A: Yes, via effectiveGasPrice and gasUsed.

Q: Why is contractAddress null?

A: The transaction wasnโ€™t a contract creation.


Conclusion

eth_getTransactionReceipt is indispensable for Ethereum developers, offering insights into transaction outcomes and contract interactions. Integrate it into your workflows for robust transaction monitoring and smart contract management.