How to Use getBlocksWithLimit
Learn getBlocksWithLimit use cases, code examples, request parameters, response structure, and tips.
The getBlocksWithLimit
RPC method allows you to retrieve a list of confirmed block slot numbers, starting from a specified slot and returning up to a given limit. This is useful when you need a specific number of subsequent confirmed blocks from a certain point in the ledger.
Common Use Cases
- Fetching a Fixed Number of Subsequent Blocks: Get a defined number of block slots that were confirmed after a particular start slot.
- Paginated Block Exploration: Retrieve blocks in chunks when analyzing a segment of the blockchain.
- Recent Block Monitoring: Get the last N blocks from a known recent slot.
Request Parameters
The getBlocksWithLimit
method takes the following parameters:
start_slot
(u64, required): The first slot to consider (inclusive).limit
(u64, required): The maximum number of block slots to return. The total number of slots queried (fromstart_slot
up tolimit
blocks after it) must not exceed 500,000 slots.commitment
(string, optional): Specifies the commitment level for the query. If omitted, the default commitment of the node is used. This is passed as the sole field in a configuration object as the last parameter.
Response Structure
The result
field of the JSON-RPC response will be an array of u64 integers. Each integer in the array represents a confirmed block slot number, starting from start_slot
up to the specified limit
.
- Example: If
start_slot
is 5 andlimit
is 3, a possible result is[5, 6, 7]
.
Examples
1. Get a Limited Number of Blocks from a Start Slot
This example fetches a list of up to 5 confirmed block slots starting from slot 280000000
.
2. Get Blocks With Limit and a Specific Commitment Level
This example fetches up to 3 blocks starting from slot 290000000
using the confirmed
commitment level.
Developer Tips
- Range Clarification: The
limit
parameter dictates the maximum number of block slots to return. The 500,000 slot constraint applies to the conceptual range scanned (i.e., fromstart_slot
tostart_slot + limit - 1
). Ensure this conceptual range doesn’t exceed 500,000 slots, even if fewer actual blocks are found and returned within that range. - Node Data Availability: Nodes may not retain information for all historical slots. A very old
start_slot
might return fewer blocks than the specifiedlimit
, or an empty array, depending on the node’s ledger retention. - Block Confirmation: This method returns confirmed blocks. The exact set of blocks can vary slightly depending on the chosen
commitment
level and which node you query, especially for very recent slots. - Use Case Specificity:
getBlocksWithLimit
is ideal when you know the starting point and need a fixed number of subsequent blocks. If you need all blocks in a known slot range,getBlocks
might be more appropriate.
This guide provides a clear overview of how to use the getBlocksWithLimit
RPC method to list a specific number of confirmed block slots on the Solana network.