跳转到主要内容

参数

startSlot
number
必填
起始 slot 编号
limit
number
必填
要返回的 slot 领导者数量

响应

result
array
slot 领导者公钥数组(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": "getSlotLeaders",
  "params": [
    1000000,
    10
  ]
}'

使用 web3.js

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

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

// Get slot leaders
const slotLeaders = await connection.getSlotLeaders(1000000, 10);
console.log('Slot leaders:', slotLeaders);

// Get slot leaders with analysis
async function getSlotLeadersWithAnalysis(
  startSlot: number,
  limit: number
) {
  const slotLeaders = await connection.getSlotLeaders(startSlot, limit);
  
  return {
    slotLeaders,
    analysis: {
      uniqueLeaders: new Set(slotLeaders).size,
      leaderDistribution: slotLeaders.reduce((acc, leader) => {
        acc[leader] = (acc[leader] || 0) + 1;
        return acc;
      }, {} as Record<string, number>),
      metadata: {
        startSlot,
        endSlot: startSlot + limit - 1,
        timestamp: Date.now()
      }
    }
  };
}

注意事项

  1. 返回给定 slot 范围的 slot 领导者
  2. slot 领导者负责生产区块
  3. 响应是即时的,因为它从当前状态读取
  4. slot 领导者由网络的领导者计划确定
  5. 范围必须在当前 epoch 内

最佳实践

  1. 根据需求使用适当的起始 slot 和限制数量
  2. 在适当时缓存结果以减少 RPC 负载
  3. 监控领导者计划的变化
  4. 考虑使用 WebSocket 订阅获取实时更新
  5. 适当处理网络错误并重试

常见错误

错误码消息解决方案
-32601Method not found验证是否连接到 Solana RPC 节点
-32602Invalid params检查 startSlot 和 limit 参数
-32007Slot leader information unavailable节点可能正在启动或同步中

用例

  1. 领导者计划分析
    interface LeaderScheduleAnalysis {
      schedule: Array<{
        slot: number;
        leader: string;
      }>;
      distribution: Record<string, number>;
      metadata: {
        startSlot: number;
        endSlot: number;
        uniqueLeaders: number;
      };
    }
    
    async function analyzeLeaderSchedule(
      startSlot: number,
      limit: number
    ): Promise<LeaderScheduleAnalysis> {
      const slotLeaders = await connection.getSlotLeaders(startSlot, limit);
      
      const schedule = slotLeaders.map((leader, index) => ({
        slot: startSlot + index,
        leader
      }));
      
      const distribution = slotLeaders.reduce((acc, leader) => {
        acc[leader] = (acc[leader] || 0) + 1;
        return acc;
      }, {} as Record<string, number>);
      
      return {
        schedule,
        distribution,
        metadata: {
          startSlot,
          endSlot: startSlot + limit - 1,
          uniqueLeaders: Object.keys(distribution).length
        }
      };
    }
    
  2. 领导者计划监控
    interface LeaderScheduleChange {
      slot: number;
      previousLeader: string;
      currentLeader: string;
      metadata: {
        timestamp: number;
      };
    }
    
    class LeaderScheduleMonitor {
      private previousSchedule: Map<number, string> = new Map();
      
      async monitorLeaderSchedule(
        startSlot: number,
        limit: number,
        interval: number = 60000
      ): Promise<LeaderScheduleChange[]> {
        const slotLeaders = await connection.getSlotLeaders(startSlot, limit);
        const changes: LeaderScheduleChange[] = [];
        
        slotLeaders.forEach((leader, index) => {
          const slot = startSlot + index;
          const previousLeader = this.previousSchedule.get(slot);
          
          if (previousLeader && previousLeader !== leader) {
            changes.push({
              slot,
              previousLeader,
              currentLeader: leader,
              metadata: {
                timestamp: Date.now()
              }
            });
          }
          
          this.previousSchedule.set(slot, leader);
        });
        
        return changes;
      }
    }
    
  3. 领导者计划规划
    interface LeaderSchedulePlan {
      currentSlot: number;
      upcomingLeaders: Array<{
        slot: number;
        leader: string;
        estimatedTime: number;
      }>;
      metadata: {
        timestamp: number;
        slotsPerSecond: number;
      };
    }
    
    class LeaderSchedulePlanner {
      private readonly slotsPerSecond = 2; // Solana's target slot rate
      
      async planLeaderSchedule(
        startSlot: number,
        limit: number
      ): Promise<LeaderSchedulePlan> {
        const [slotLeaders, currentSlot] = await Promise.all([
          connection.getSlotLeaders(startSlot, limit),
          connection.getSlot()
        ]);
        
        const upcomingLeaders = slotLeaders.map((leader, index) => {
          const slot = startSlot + index;
          const slotsAhead = slot - currentSlot;
          const estimatedTime = slotsAhead / this.slotsPerSecond;
          
          return {
            slot,
            leader,
            estimatedTime
          };
        });
        
        return {
          currentSlot,
          upcomingLeaders,
          metadata: {
            timestamp: Date.now(),
            slotsPerSecond: this.slotsPerSecond
          }
        };
      }
    }