参数
此方法不接受任何参数。响应
代码示例
基本请求
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": "getFirstAvailableBlock"
}'
使用 web3.js
import { Connection } from '@solana/web3.js';
const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');
// Get first available block
const firstBlock = await connection.getFirstAvailableBlock();
console.log('First available block:', firstBlock);
// Get block range
async function getAvailableBlockRange() {
const firstBlock = await connection.getFirstAvailableBlock();
const currentSlot = await connection.getSlot();
return {
firstBlock,
currentSlot,
totalBlocks: currentSlot - firstBlock + 1
};
}
注意事项
- 返回可用的最低已确认区块的 slot
- 这对于确定节点的可用历史记录很有用
- 随着节点修剪旧区块,该值可能会变化
- 不同节点可能返回不同的值
- 响应是即时的,因为它从当前状态读取
最佳实践
- 使用此方法确定可用的区块范围
- 查询历史区块时考虑节点修剪
- 适当时缓存结果以减少 RPC 负载
- 处理请求的区块不可用的情况
- 与其他区块方法结合使用
常见错误
| 错误码 | 消息 | 解决方案 |
|---|---|---|
| -32601 | Method not found | 验证是否连接到 Solana RPC 节点 |
用例
-
区块范围验证
async function verifyBlockRange(startSlot: number, endSlot: number) { const firstBlock = await connection.getFirstAvailableBlock(); const currentSlot = await connection.getSlot(); if (startSlot < firstBlock) { throw new Error(`Start slot ${startSlot} is before first available block ${firstBlock}`); } if (endSlot > currentSlot) { throw new Error(`End slot ${endSlot} is after current slot ${currentSlot}`); } return { startSlot, endSlot, available: true }; } -
历史数据可用性
interface BlockAvailability { firstAvailable: number; currentSlot: number; availableRange: number; requestedSlot: number; isAvailable: boolean; } async function checkBlockAvailability(slot: number): Promise<BlockAvailability> { const firstBlock = await connection.getFirstAvailableBlock(); const currentSlot = await connection.getSlot(); return { firstAvailable: firstBlock, currentSlot, availableRange: currentSlot - firstBlock + 1, requestedSlot: slot, isAvailable: slot >= firstBlock && slot <= currentSlot }; } -
区块历史分析
async function analyzeBlockHistory() { const firstBlock = await connection.getFirstAvailableBlock(); const currentSlot = await connection.getSlot(); const blocks = await connection.getBlocks(firstBlock, currentSlot); const metrics = { firstBlock, currentSlot, totalSlots: currentSlot - firstBlock + 1, availableBlocks: blocks.length, missingBlocks: (currentSlot - firstBlock + 1) - blocks.length, availabilityPercentage: (blocks.length / (currentSlot - firstBlock + 1)) * 100 }; return metrics; }