monosux/ethereum-block-by-date

getEvery by seconds returns duplicates

Closed this issue · 3 comments

const dater = new EthDater(this.web3);
      const startTimeString = this.$route.params.dateString;
      const endTimeString = startTimeString + 1000 * 60; // adds a minute to starting range
        const blocks = await dater.getEvery(
          'seconds',
          moment(startTimeString),
          moment(endTimeString),
          1,
          true
        );

returns an array with 61 blocks instead of 4-5

current work around is to filter the results after getting it. I'm using lodash to do so:
_.uniqBy(blocks, 'block') which returns 4 blocks

Hi @gamalielhere,

That is right. The function dater.getEvery() will return nearest block for every second/minute/day.

So, in your example, you ask for the nearest blocks for timestamps:

2021-12-21T08:55:01
2021-12-21T08:55:02
2021-12-21T08:55:03
...

For the timestamp 2021-12-21T08:55:01 and 2021-12-21T08:55:02, the nearest block is the same, so the function returns the same value.

As I understand you are looking for a function to get all blocks between two dates. There is no such function, but you can do it like this:

    const startBlock = await dater.getDate(startTimeString);
    const endBlock = await dater.getDate(endTimeString);

    const blocks = [];

    for (let i = startBlock.block; i <= endBlock.block; i++) {
        blocks.push(i);
        // or, if you need more info about the block, you can request more info: 
        // blocks.push(await this.web3.eth.getBlock(i));
    }

I hope this information will be helpful for you. Also, your case is interesting, and I am probably going to add the function you need in the plugin in the nearest update.