0

How can the following query be modified to only fetch the past two days worth of events:

const events= await MyContract.getPastEvents("eventName", {
  fromBlock: 0,
  toBlock: "latest"
});

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • My guess is that you have to check when blocks where mined and decide which one to pass as fromBlock. – Maxpeinas Jul 23 '18 at 16:05
  • const events= await MyContract.getPastEvents("EventName", { fromBlock: (await web3.eth.getBlockNumber()) - 12343, // apprx 12343 blocks every 2 days toBlock: "latest" }); – Venkata Gorantla Jul 23 '18 at 18:38
0

There's approximately 12,343 blocks every two days, so finding the current block number and subtracting that amount would give the block number to target. Pass that in as the fromBlock parameter to the getPastEvents function to limit to that range:

const events= await MyContract.getPastEvents("EventName", {
  fromBlock: (await web3.eth.getBlockNumber()) - 12343,
  toBlock: "latest"
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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