参数
响应
当前 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()
};
}
注意事项
- 返回节点的当前 slot
- 随着新区块的生产,slot 编号会增加
- 可以指定不同的 commitment 级别
- 响应是即时的,因为它从当前状态读取
- slot 编号用于各种基于时间的操作
最佳实践
- 在适当时缓存 slot 编号
- 考虑使用 WebSocket 订阅获取实时更新
- 适当处理网络错误并重试
- 根据需求使用适当的 commitment 级别
- 监控 slot 进度以用于时间敏感的操作
常见错误
| 错误码 | 消息 | 解决方案 |
|---|---|---|
| -32601 | Method not found | 验证是否连接到 Solana RPC 节点 |
| -32602 | Invalid params | 检查配置参数 |
| -32007 | Slot information unavailable | 节点可能正在启动或同步中 |
用例
-
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; } } -
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 } }; } -
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 } }; } }