跳转到主要内容

参数

config
object

响应

result
number
当前 slot 编号

代码示例

基本请求

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": "getSlot",
  "params": []
}'

带 Commitment 的请求

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": "getSlot",
  "params": [
    {
      "commitment": "confirmed"
    }
  ]
}'

使用 web3.js

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

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

// Get current slot
const slot = await connection.getSlot();
console.log('Current slot:', slot);

// Get slot with commitment
async function getSlotWithCommitment(
  commitment: 'processed' | 'confirmed' | 'finalized' = 'confirmed'
) {
  const slot = await connection.getSlot(commitment);
  return {
    slot,
    commitment,
    timestamp: Date.now()
  };
}

注意事项

  1. 返回节点的当前 slot
  2. 随着新区块的生产,slot 编号会增加
  3. 可以指定不同的 commitment 级别
  4. 响应是即时的,因为它从当前状态读取
  5. slot 编号用于各种基于时间的操作

最佳实践

  1. 在适当时缓存 slot 编号
  2. 考虑使用 WebSocket 订阅获取实时更新
  3. 适当处理网络错误并重试
  4. 根据需求使用适当的 commitment 级别
  5. 监控 slot 进度以用于时间敏感的操作

常见错误

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

用例

  1. Slot 监控
    interface SlotInfo {
      slot: number;
      commitment: string;
      metadata: {
        timestamp: number;
        previousSlot: number | null;
        slotProgression: number;
      };
    }
    
    class SlotMonitor {
      private previousSlot: number | null = null;
      
      async monitorSlot(
        commitment: 'processed' | 'confirmed' | 'finalized' = 'confirmed',
        interval: number = 1000
      ): Promise<SlotInfo> {
        const slot = await connection.getSlot(commitment);
        const timestamp = Date.now();
        
        const info: SlotInfo = {
          slot,
          commitment,
          metadata: {
            timestamp,
            previousSlot: this.previousSlot,
            slotProgression: this.previousSlot !== null
              ? slot - this.previousSlot
              : 0
          }
        };
        
        this.previousSlot = slot;
        return info;
      }
    }
    
  2. Slot 分析
    interface SlotAnalysis {
      currentSlot: number;
      slotProgression: {
        average: number;
        min: number;
        max: number;
        samples: number;
      };
      timeDistribution: {
        slotsPerSecond: number;
        slotsPerMinute: number;
        slotsPerHour: number;
      };
      metadata: {
        startTime: number;
        duration: number;
      };
    }
    
    async function analyzeSlots(
      commitment: 'processed' | 'confirmed' | 'finalized' = 'confirmed',
      duration: number = 60000
    ): Promise<SlotAnalysis> {
      const startTime = Date.now();
      const startSlot = await connection.getSlot(commitment);
      const progressions: number[] = [];
      let previousSlot = startSlot;
      
      while (Date.now() - startTime < duration) {
        const currentSlot = await connection.getSlot(commitment);
        progressions.push(currentSlot - previousSlot);
        previousSlot = currentSlot;
        await new Promise(resolve => setTimeout(resolve, 1000));
      }
      
      const totalProgression = progressions.reduce((sum, p) => sum + p, 0);
      const durationSeconds = (Date.now() - startTime) / 1000;
      
      return {
        currentSlot: previousSlot,
        slotProgression: {
          average: totalProgression / progressions.length,
          min: Math.min(...progressions),
          max: Math.max(...progressions),
          samples: progressions.length
        },
        timeDistribution: {
          slotsPerSecond: totalProgression / durationSeconds,
          slotsPerMinute: (totalProgression / durationSeconds) * 60,
          slotsPerHour: (totalProgression / durationSeconds) * 3600
        },
        metadata: {
          startTime,
          duration: Date.now() - startTime
        }
      };
    }
    
  3. Slot 同步
    interface SlotSync {
      currentSlot: number;
      targetSlot: number;
      difference: number;
      estimatedTime: number;
      metadata: {
        timestamp: number;
        commitment: string;
      };
    }
    
    class SlotSynchronizer {
      private readonly targetSlotsPerSecond = 2; // Solana's target slot rate
      
      async checkSynchronization(
        commitment: 'processed' | 'confirmed' | 'finalized' = 'confirmed'
      ): Promise<SlotSync> {
        const currentSlot = await connection.getSlot(commitment);
        const targetSlot = Math.floor(
          (Date.now() / 1000) * this.targetSlotsPerSecond
        );
        
        return {
          currentSlot,
          targetSlot,
          difference: currentSlot - targetSlot,
          estimatedTime: Math.abs(currentSlot - targetSlot) / this.targetSlotsPerSecond,
          metadata: {
            timestamp: Date.now(),
            commitment
          }
        };
      }
    }