跳转到主要内容

参数

此方法不接受任何参数。

响应

result
string
创世哈希,以 base-58 编码的字符串表示

代码示例

基本请求

curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getGenesisHash"
}'

使用 web3.js

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

const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');

// Get genesis hash
const genesisHash = await connection.getGenesisHash();
console.log('Genesis hash:', genesisHash);

// Verify cluster
async function verifyCluster(expectedHash: string) {
  const actualHash = await connection.getGenesisHash();
  if (actualHash !== expectedHash) {
    throw new Error(`Connected to wrong cluster. Expected ${expectedHash}, got ${actualHash}`);
  }
  console.log('Connected to correct cluster');
}

注意事项

  1. 返回集群的创世哈希
  2. 创世哈希对于每个集群是唯一的
  3. 可用于验证连接到了哪个集群
  4. 哈希以 base-58 编码
  5. 响应是即时的,因为它从当前状态读取

最佳实践

  1. 使用此方法验证集群连接
  2. 缓存结果以减少 RPC 负载
  3. 与已知的不同集群创世哈希进行比较
  4. 结合其他集群验证方法使用
  5. 适当处理网络错误

常见错误

错误码消息解决方案
-32601Method not found验证是否连接到 Solana RPC 节点
-32007Genesis hash unavailable节点可能正在启动或同步中

用例

  1. 集群验证
    const CLUSTER_HASHES = {
      mainnet: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d',
      testnet: '4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY',
      devnet: 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG'
    };
    
    async function verifyClusterConnection(expectedCluster: keyof typeof CLUSTER_HASHES) {
      const genesisHash = await connection.getGenesisHash();
      const expectedHash = CLUSTER_HASHES[expectedCluster];
      
      if (genesisHash !== expectedHash) {
        throw new Error(`Connected to wrong cluster. Expected ${expectedCluster} (${expectedHash}), got ${genesisHash}`);
      }
      
      return {
        cluster: expectedCluster,
        genesisHash,
        verified: true
      };
    }
    
  2. 集群检测
    async function detectCluster() {
      const genesisHash = await connection.getGenesisHash();
      
      for (const [cluster, hash] of Object.entries(CLUSTER_HASHES)) {
        if (hash === genesisHash) {
          return cluster;
        }
      }
      
      throw new Error('Unknown cluster');
    }
    
  3. 网络配置
    interface NetworkConfig {
      cluster: string;
      genesisHash: string;
      rpcEndpoint: string;
      wsEndpoint: string;
    }
    
    async function configureNetwork(endpoint: string): Promise<NetworkConfig> {
      const connection = new Connection(endpoint);
      const genesisHash = await connection.getGenesisHash();
      const cluster = await detectCluster();
      
      return {
        cluster,
        genesisHash,
        rpcEndpoint: endpoint,
        wsEndpoint: endpoint.replace('http', 'ws')
      };
    }