2

How can I load the most recent 10 or 20 transactions from EVM?

I figured out that with the following code I can listen on "pending" transactions. But I want to load all 20 latest transactions no matter which state they have.

  var url = "...some-node-or-alchemy-"
  var provider = new ethers.providers.JsonRpcProvider(url);
  
  provider.on("pending", (tx) => {
    console.log(tx);
  });

How to do that? The documentation is not really helpful. It shows only functions where I can load a transaction from a specific hash. But I don't know the hashes and need to fetch the latest hashes first.

2 Answers 2

3

To retrieve both pending and completed transactions, you can modify the code to add an event listener for the "block" event in addition to the existing "pending" event listener. The "block" event is emitted whenever a new block is added to the blockchain, and it contains information about all the transactions included in that block (completed transactions).

Here's an example of how you could modify the code to listen for both "pending" and "block" events:

var url = "...some-node-or-alchemy-"
var provider = new ethers.providers.JsonRpcProvider(url);

// Listen for new pending transactions
provider.on("pending", (tx) => {
  console.log("New pending transaction:", tx);
});

// Listen for new blocks, and retrieve all transactions in each block
provider.on("block", async (blockNumber) => {
  const block = await provider.getBlock(blockNumber);
  console.log("New block:", block);
  console.log("Transactions:", block.transactions);
});

With this modification, the code will now log information about both pending and completed transactions to the console as they are detected by the provider. When a new block is added to the blockchain, the code will log information about the block itself as well as the list of transactions included in the block, which can be used to retrieve information about completed transactions.

Hope this helps!

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.