eth_getTransactionReceipt | Ethereum

Recipes
๐Ÿ‘จโ€๐Ÿ’ป
Fetching contract deployment transactions with the Chainstack Covalent SDK
Open Recipe

๐Ÿ“˜

No response in the interactive API

The response to the call might be too big for ReadMe to handle, so if you are not getting it here, copy the CURL example and run in your terminalโ€”this will work.

Ethereum API method that retrieves the receipt of a transaction from a transaction hash. It can be used to verify the success or failure of a transaction, check the execution status of a smart contract, retrieve logs, check if a transaction deployed a smart contract, and more.

๐Ÿ‘

Get you own node endpoint today

Start for free and get your app to production levels immediately. No credit card required.

You can sign up with your GitHub, X, Google, or Microsoft account.

Parameters

  • hash โ€” the hash identifying a transaction

Response

  • Transaction receipt โ€” the receipt object with:
    • blockHash โ€” the block hash. Identifies the block in which the transaction was included. This field is null for transactions that have not yet been included in a block.
    • blockNumber โ€” the number of the block in which the transaction was included. This field is null for transactions that have not yet been included in a block.
    • contractAddress โ€” the address of the contract created by the transaction if it was a contract creation transaction. Otherwise, the value is null.
    • cumulativeGasUsed โ€” the total amount of gas used in the block until this transaction was executed.
    • effectiveGasPrice โ€” the actual value deducted from the sender's account for this transaction.
    • from โ€” the address of the sender who initiated the transaction.
    • gasUsed โ€” the amount of gas used by this specific transaction alone.
    • logs โ€” an array of log objects generated by this transaction, if any. Logs are generated by smart contracts.
    • logsBloom โ€” the bloom filter used by light clients to quickly retrieve logs related to the transaction.
    • status โ€” the success status of the transaction, represented as 1 for success or 0 for failure.
    • to โ€” the address of the recipient of the transaction if it was a transaction to an address. For contract creation transactions, this field is null.
    • transactionHash โ€” the hash that uniquely identifies the transaction.
    • transactionIndex โ€” the index of the transaction within the block.
    • type โ€” the type of the transaction. 0 indicates a regular transfer; 2 indicates a contract creation or smart contract function call.

eth_getTransactionReceipt code examples

๐Ÿ“˜

Learn more about the ChainstackProvider in ethers.js: ethers ChainstackProvider Documentation.

const ethers = require("ethers");

// Create a ChainstackProvider instance for Ethereum mainnet
const chainstack = new ethers.ChainstackProvider("mainnet");

const getTransactionReceipt = async (transactionHash) => {
  const receipt = await chainstack.send("eth_getTransactionReceipt", [
    transactionHash,
  ]);
  console.log(receipt);
};

getTransactionReceipt(
  "0xb528d706507b7e5d8f132389a667b3785cf14480a8f9aa6363f1e674a5a6099e"
);

const { Web3 } = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getReceipt(transactionHash) {
  const receipt = await web3.eth.getTransactionReceipt(transactionHash)
  console.log(receipt)
 }
 
 getReceipt("0xb528d706507b7e5d8f132389a667b3785cf14480a8f9aa6363f1e674a5a6099e")
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 

web3 = Web3(Web3.HTTPProvider(node_url)) 
print(web3.eth.get_transaction_receipt("0xb528d706507b7e5d8f132389a667b3785cf14480a8f9aa6363f1e674a5a6099e"))

Use case

A practical use case for eth_getTransactionReceipt is to verify a transaction's status and whether it was successful or not. This can be especially useful when deploying a contract, as you can use the receipt to confirm that the contract was deployed successfully and has a valid contract address.

Here's an example in ethers and the ChainstackProvider:

const ethers = require("ethers");

// Create a ChainstackProvider instance for Ethereum mainnet
const chainstack = new ethers.ChainstackProvider("mainnet");

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

  // Check if the receipt is not yet available
  if (!receipt) {
    console.log(`Transaction ${transactionHash} is still pending...`);
    return;
  }

  // Check if the transaction was successful
  if (receipt.status) {
    console.log(`โœ…The transaction was successful! \nHash: ${transactionHash}`);

    // Check if the transaction deployed a smart contract
    if (receipt.contractAddress != null) {
      console.log(
        `๐Ÿ“‘The smart contract was deployed at address: ${receipt.contractAddress}`
      );
    } else {
      console.log(`โ—This transaction did not deploy a smart contract`);
    }
  } else {
    console.log(`๐Ÿ’ฅThe transaction failed. \nHash: ${transactionHash}`);
  }
}

verifyTransaction(
  "0x2761f74e2f45d981a9d7553cbbcfbcc862cae416eb37a820300d4c19516d6fca"
);

This script holds an async function called verifyTransaction that takes a transaction hash as input. The function uses the web3.js library to retrieve the transaction receipt of the specified transaction using the eth_getTransactionReceipt method.

The function first checks if the transaction receipt is not yet available, and if so, it logs a message to the console indicating that the transaction is still pending. If the receipt is available, the function checks the status property of the receipt to determine if the transaction was successful.

If the transaction was successful, the function checks if a smart contract was deployed as part of the transaction. The function logs the smart contract's address to the console if a smart contract is deployed. If not, the function logs a message indicating that no smart contract was deployed.

If the transaction was not successful, the function logs a message indicating that the transaction failed, along with the transaction hash.

Try the eth_getTransactionReceipt RPC method yourself

Language
Click Try It! to start a request and see the response here!