1

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

2 Answers 2

1

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"
});
0

const getBlock = async () => {
    const result = await web3.eth.getBlockNumber()
    return web3.utils.hexToNumberString(result)
    
}

const calculated = getBlock().then((res) => { const calculated = Number(res) - 12343; return calculated});
const options =                                 
{
    address: getPair(tokenA, tokenB),
    fromBlock: calculated,
    toBlock: 'latest'

}

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.