hl_getLatestBlockNumber - Get Latest Block Number
Get the highest block number currently available for a HyperCore data stream via the JSON-RPC API.
Return the highest block number currently available for a given stream.
When to Use This Method
hl_getLatestBlockNumber is essential for:
- Synchronization — Determine the current head before fetching batch blocks
- Polling — Check for new blocks at regular intervals
- Catch-up Logic — Calculate how far behind your consumer is
- Health Monitoring — Verify the data pipeline is advancing
Common Use Cases
1. Poll for New Blocks
Check for new data at regular intervals:
async function pollForNewBlocks(callback, intervalMs = 5000) {
let lastSeen = await getLatestBlockNumber();
console.log(`Starting poll from block ${lastSeen}`);
setInterval(async () => {
const latest = await getLatestBlockNumber();
if (latest > lastSeen) {
console.log(`New blocks: ${lastSeen + 1} to ${latest}`);
callback(lastSeen + 1, latest);
lastSeen = latest;
}
}, intervalMs);
}
// Usage
pollForNewBlocks((from, to) => {
console.log(`Processing blocks ${from}–${to}`);
});2. Calculate Consumer Lag
Monitor how far behind a consumer is:
def check_lag(consumer_block: int) -> dict:
latest = get_latest_block_number()
lag = latest - consumer_block
return {
'consumer_block': consumer_block,
'latest_block': latest,
'lag_blocks': lag,
'status': 'healthy' if lag < 100 else 'behind'
}Best Practices
- Poll responsibly — Don't poll more frequently than every few seconds
- Use as a cursor — Combine with
hl_getBatchBlocksfor sequential data retrieval - Monitor lag — Track the difference between your consumer position and the latest block
- Handle stale data — If the latest block number hasn't advanced, the upstream may be paused
Related Methods
- hl_getBatchBlocks — Retrieve fill blocks in a range
Query HyperCore fill data with Dwellir's JSON-RPC API. Get your API key →