There are several options for achieving this, but the most efficient way is probably using the eth_getBlockByNumber
method. With this method, you can get the details for a specific block by giving it a block number or a tag like "latest" or "pending" block.
The response holds an array with all of the transaction hashes in the block. At this point, it's easy just to pick the first 20.
Here is an example of the call in cURL:
curl --request POST \
--url YOUR_NODE \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": [
"pending",
false
]
}
'
You can find more examples and explanations in the Chainstack API docs eth_getBlockByNumber.
And here is an example in ethers.js
// Note this is V6 of ethers
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);
const eth_getBlockByNumber = async () => {
const blockByNumber = await provider.send("eth_getBlockByNumber", ["pending", false]);
const transactions = blockByNumber.transactions;
const first20Transactions = transactions.slice(0, 20);
//console.log("Transactions array:", transactions);
console.log("First 20 transactions:", first20Transactions);
};
eth_getBlockByNumber();
Now you have an array named first20Transactions
with the first 20 transaction hashes.