Web3 getting contract transactions history

Viewed 2118

I created simple monitor to check a contract and its latest transactions. I know I can get this information by getting latest block like:

block = await this.web3.eth.getBlock("latest");

and later on check if receiver is the same as contract that I am monitoring. But I would like to get history of that contract for example last 24 hours and see what transactions were made there. Is it possible to do that and if yes how can I achieve that?

1 Answers

You can specify a specific block number instead of the "latest" parameter - extract the number of the last block at some point and then iteratively reduce it by one until the desired depth of history is reached.

However, it would be more efficient to write an application that would read new blocks in resident mode as they appear on the network and write them to some database - in this case, the user could get information much faster and to an arbitrary depth.

At the same time, we must not forget to track possible forks with the cancellation of the corresponding transactions.

Related