'how to get transaction history from blocks via web3?

I have a private chain now and I want to query the transaction histories.I know that web3 has provide those methods web3.eth.blockNumber/web3.eth.getBlockTransactionCount/web3.eth.getTransactionFromBlock. So I can get the transactions history by get latest blocknumber(suppose it's n). Range from n to 0 to call web3.eth.getBlockTransactionCount(i), then get transactions by web3.eth.getTransactionFromBlock. but it's just time wasting and inefficient. so I'm wondering how can I get transaction histories from blocks efficient via web3 or rpc or anyway?



Solution 1:[1]

For each block you can pass true as the second parameter to getBlock:

web3.eth.getBlock(blockHashOrBlockNumber [, returnTransactionObjects] [, callback])

Solution 2:[2]

This code will give Transactions details by Block Number.

async function GetBlocks(BlockNumber){
    Block =await web3.eth.getBlock(BlockNumber)
    Block.transactions.forEach(async(transaction) => {
        let t=await web3.eth.getTransaction(transaction)
        console.log(t)
    })
}
GetBlocks(26154210)

If you have Array of Blocks. e.g. Blocks=[10000,10001,10002,10003...]. then use a for loop like this:-

Blocks.forEach(e => {
    GetBlocks(e)
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eth
Solution 2 aakash4dev