Search code examples
ethereumweb3js

Get the latest block number of a contract


How do I get the block number of the latest confirmed transaction in a contract?

Context:

web3's myContract.getPastEvents allows you to set the blockNumber from which you will start fetching the events. But I haven't come across a way to find out the latest blockNumber for a contract.

Any help will be appreciated!


Solution

  • Given the discussion in comments, there's no need to find the most recent block involving the contract; you can simply start from the most recent block, period:

    web3.eth.getBlockNumber(function (err, blockNumber) { ... });