I want to get events from block X. I have:

web3js 1.2.6

And I have my own smart contracts I want to listen for. So I have created listener for allEvents like this:

  const event = contract.events.allEvents({
    fromBlock: 0,

And I also have following snippet of code where I am listening for the new events and also for the previous events. So I am specifying fromBlock as described in web3js documentation:

fromBlock - Number|String|BN|BigNumber (optional): The block number (greater than or equal to) from which to get events on. Pre-defined block numbers as "latest",

    .on('data', console.log)
    .on('error', eventError);

When I dont specify fromBlock: 0, it doesnt return anything which is OK. When I set fromBlock to 0 it always return the last block (e.g block 6 - see below) even though there are more blocks before the block number 6:

Object {address: "0x9d13C6D3aFE1721BEef56B55D303B09E021E27ab", blockNumber: 6, transactionHash: "0x51f254852cc897d6d3e40041b3a3e5dd7ec9a5ebddd771d9…", transactionIndex: 0, blockHash: "0x0eaec149917f0517aa99042875313882aa57831b3d02aaf5…", …}

Based on the documentation I assume it should return all events from block number 0. So I should get events in block 0,1,2,3,4...latest.

Do I understand it correctly? Do I need to set some configuration? I want to workaround contract.getPastEvents and I want to set fromBlock within the subscription in listener.

  • It actually depends on which node you're using I believe. For example, infura doesn't throw an exception on block 0, but alchemy does. Just set fromBlock to 1. Feb 23, 2020 at 16:53
  • @goodvibration Thanks a lot for your comment. I tried set it to different numbers. If the latest number is smaller than fromBlock it doesnt return any event. However when I set fromBlock to something smaller than current latest block (let's say 10) it always return the previous one (in this example block 9). I also tried converting to hex with web3.utils.toHex(<fromBlock>) but it didnt help as well Feb 23, 2020 at 18:21
  • Have you tried using a callback function with contract.events.allEvents? Feb 27, 2020 at 0:49
  • 1
    @sfmiller940 yes. didnt work... looks like there is a bug with connection in websocket provider for subscription in general. More details here github.com/ethereum/web3.js/issues/3389 Feb 27, 2020 at 12:23

2 Answers 2


Currently, I am using web3 version 1.2.1, the below code is working for me. Please give toBlock as latest. So that web3 js will give you all the events for that range.

contract.getPastEvents('allEvents', {
    fromBlock: 0,
    toBlock: 'latest'
  • 1
    Thanks. The getPastEvents works fine, but I dont want to use second web3 call with getPastEvents. What I want is to create the subscription with contract.events.allEvents and within this subscription I want to get the previous transactions as stated in documentation with fromBlock Feb 24, 2020 at 6:48

In https://github.com/ethereum/web3.js/issues/3389 it turned out it was a bug. Fixed in web3 1.2.7

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.