Docs

eth_getTransactionByHash - Get Transactio...

Retrieve detailed information about a transaction by its hash on Centrifuge Mainnet. Essential for transaction tracking and confirmation.

Returns information about a transaction requested by transaction hash. Essential for tracking transaction status and details.

When to Use This Method

eth_getTransactionByHash is crucial for:

  • Transaction Tracking - Monitor transaction status after submission
  • Payment Verification - Confirm payment details and recipients
  • Transaction Explorers - Build transaction detail pages
  • Debugging - Investigate failed or stuck transactions

Common Use Cases

1. Payment Verification System

JavaScript
// Verify payment received
async function verifyPayment(txHash, expectedAmount, expectedRecipient) {
  const tx = await provider.getTransaction(txHash);
  
  if (!tx) {
    return { valid: false, reason: 'Transaction not found' };
  }
  
  // Check recipient
  if (tx.to?.toLowerCase() !== expectedRecipient.toLowerCase()) {
    return { valid: false, reason: 'Wrong recipient' };
  }
  
  // Check amount
  if (tx.value.toString() !== expectedAmount) {
    return { valid: false, reason: 'Wrong amount' };
  }
  
  // Check confirmations
  if (!tx.blockNumber) {
    return { valid: false, reason: 'Transaction pending' };
  }
  
  const receipt = await provider.getTransactionReceipt(txHash);
  if (receipt.status === 0) {
    return { valid: false, reason: 'Transaction failed' };
  }
  
  return {
    valid: true,
    confirmations: tx.confirmations,
    blockNumber: tx.blockNumber
  };
}

2. Transaction History Builder

JavaScript
// Build transaction history for address
async function getAddressTransactions(address, txHashes) {
  const transactions = [];
  
  for (const hash of txHashes) {
    const tx = await provider.getTransaction(hash);
    
    if (!tx) continue;
    
    const isIncoming = tx.to?.toLowerCase() === address.toLowerCase();
    const isOutgoing = tx.from.toLowerCase() === address.toLowerCase();
    
    transactions.push({
      hash: tx.hash,
      type: isIncoming ? 'incoming' : 'outgoing',
      from: tx.from,
      to: tx.to,
      value: ethers.formatEther(tx.value),
      timestamp: tx.blockNumber ? 
        (await provider.getBlock(tx.blockNumber)).timestamp : null,
      status: tx.blockNumber ? 'confirmed' : 'pending',
      gasPrice: tx.gasPrice ? ethers.formatGwei(tx.gasPrice) : null,
      confirmations: tx.confirmations
    });
  }
  
  return transactions.sort((a, b) => b.timestamp - a.timestamp);
}

3. Gas Price Analysis

JavaScript
// Analyze gas usage patterns
async function analyzeGasUsage(txHash) {
  const tx = await provider.getTransaction(txHash);
  
  if (!tx) {
    throw new Error('Transaction not found');
  }
  
  const receipt = await provider.getTransactionReceipt(txHash);
  const block = await provider.getBlock(tx.blockNumber);
  
  return {
    txHash: tx.hash,
    gasLimit: tx.gasLimit.toString(),
    gasUsed: receipt ? receipt.gasUsed.toString() : null,
    gasPrice: tx.gasPrice ? ethers.formatGwei(tx.gasPrice) : null,
    maxFeePerGas: tx.maxFeePerGas ? ethers.formatGwei(tx.maxFeePerGas) : null,
    maxPriorityFee: tx.maxPriorityFeePerGas ? 
      ethers.formatGwei(tx.maxPriorityFeePerGas) : null,
    baseFee: block.baseFeePerGas ? 
      ethers.formatGwei(block.baseFeePerGas) : null,
    efficiency: receipt ? 
      ((Number(receipt.gasUsed) / Number(tx.gasLimit)) * 100).toFixed(2) + '%' : null,
    totalCost: receipt && tx.gasPrice ? 
      ethers.formatEther(receipt.gasUsed * tx.gasPrice) : null
  };
}