monosux/ethereum-block-by-date

How to get the first block of the day?

Closed this issue · 4 comments

Thanks for the great package. It's been working great. I am curious though how to get the first block of the day? Thanks!

Hi,

I'm glad you like my package!

To get the first block of the day you just need to know the necessary date/time. For example, to get today's first block you can use this code:

let block = await dater.getDate('2020-07-22T00:00:00Z');

Keep in mind the timezones!

How to get every block in the past 24 hours?

To get it you can use a function like that:

const getBlocks = async () => {
    // Get the date/time 24 hours ago
    const yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));

    // Get the block by this time
    const first_block = await dater.getDate(yesterday);

    // Get current block
    const last_block = await web3.eth.getBlockNumber();

    // Get all blocks between first and last
    let blocks = [];
    for (var i = first_block.block; i <= last_block; i++) blocks.push(i);

    // Return them 🖖
    return blocks;
}

@monosux sweet! thanks for the help!

sweet! thanks for the help!

No problem