getRecentBlockhash

This method is deprecated. Please use getLatestBlockhash instead, which provides more accurate and reliable blockhash information.

Returns a recent blockhash and its fee schedule. This method will be removed in a future release.

Parameters

config
object

Response

result
object

Migration Guide

To migrate from getRecentBlockhash to getLatestBlockhash:

  1. Replace method calls:
- const { blockhash, feeCalculator } = await connection.getRecentBlockhash();
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
  1. Update fee calculations:
// Old way
const fee = feeCalculator.lamportsPerSignature * numSignatures;

// New way
const fee = await connection.getFeeForMessage(message);

Code Examples

Basic Request (Legacy)

curl https://rpc.orbitflare.com -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getRecentBlockhash",
  "params": []
}'

Request with Commitment (Legacy)

curl https://rpc.orbitflare.com -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getRecentBlockhash",
  "params": [
    {
      "commitment": "confirmed"
    }
  ]
}'

Using web3.js (Legacy)

import { Connection } from '@solana/web3.js';

const connection = new Connection('https://rpc.orbitflare.com');

// Get recent blockhash (deprecated)
const { value: { blockhash, feeCalculator } } = await connection.getRecentBlockhash();
console.log('Blockhash:', blockhash);
console.log('Fee per signature:', feeCalculator.lamportsPerSignature);

Notes

  1. This method is deprecated and will be removed in a future release
  2. Use getLatestBlockhash for new development
  3. The blockhash is valid for a limited time (typically 150 slots)
  4. Different commitment levels can be specified
  5. Consider updating existing code to use the new methods

Best Practices

  1. Migrate to getLatestBlockhash for new development
  2. Use getFeeForMessage for fee calculations
  3. Monitor for blockhash expiration using lastValidBlockHeight
  4. Handle network errors and retry when appropriate
  5. Use appropriate commitment level based on your needs

Common Errors

CodeMessageSolution
-32601Method not foundVerify you’re connected to a Solana RPC node
-32602Invalid paramsCheck config parameters
-32007Blockhash information unavailableNode may be bootstrapping or syncing

Use Cases

  1. Transaction Fee Calculation

    interface TransactionFee {
      blockhash: string;
      feePerSignature: number;
      totalFee: number;
      metadata: {
        timestamp: number;
        slot: number;
      };
    }
    
    async function calculateTransactionFees(
      numSignatures: number
    ): Promise<TransactionFee> {
      const { context, value } = await connection.getRecentBlockhash();
      
      return {
        blockhash: value.blockhash,
        feePerSignature: value.feeCalculator.lamportsPerSignature,
        totalFee: numSignatures * value.feeCalculator.lamportsPerSignature,
        metadata: {
          timestamp: Date.now(),
          slot: context.slot
        }
      };
    }
    
  2. Blockhash Monitoring

    interface BlockhashInfo {
      blockhash: string;
      slot: number;
      feePerSignature: number;
      metadata: {
        timestamp: number;
        age: number;
      };
    }
    
    class BlockhashMonitor {
      private currentBlockhash: string | null = null;
      private currentSlot: number | null = null;
      
      async monitorBlockhash(
        interval: number = 5000
      ): Promise<BlockhashInfo | null> {
        const { context, value } = await connection.getRecentBlockhash();
        
        if (this.currentBlockhash === value.blockhash) {
          return null;
        }
        
        this.currentBlockhash = value.blockhash;
        this.currentSlot = context.slot;
        
        return {
          blockhash: value.blockhash,
          slot: context.slot,
          feePerSignature: value.feeCalculator.lamportsPerSignature,
          metadata: {
            timestamp: Date.now(),
            age: 0
          }
        };
      }
    }
    
  3. Transaction Planning

    interface TransactionPlan {
      blockhash: string;
      feePerSignature: number;
      estimatedFees: {
        single: number;
        batch: number;
      };
      metadata: {
        timestamp: number;
        slot: number;
      };
    }
    
    async function planTransactions(
      numTransactions: number,
      signaturesPerTransaction: number
    ): Promise<TransactionPlan> {
      const { context, value } = await connection.getRecentBlockhash();
      const feePerSignature = value.feeCalculator.lamportsPerSignature;
      
      return {
        blockhash: value.blockhash,
        feePerSignature,
        estimatedFees: {
          single: signaturesPerTransaction * feePerSignature,
          batch: numTransactions * signaturesPerTransaction * feePerSignature
        },
        metadata: {
          timestamp: Date.now(),
          slot: context.slot
        }
      };
    }