Ethereum Block By Date
Get Ethereum block number by a given date. Or blocks by a given period duration.
Works with any Ethereum based mainnet or testnet networks.
Works with web3.js and ethers.js
Installation
Use npm:
npm i ethereum-block-by-date
Or yarn:
yarn add ethereum-block-by-date
Usage
Using with Web3.js
const EthDater = require('ethereum-block-by-date');
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider(process.env.PROVIDER));
const dater = new EthDater(
web3
);
Using with Ethers.js
const EthDater = require('ethereum-block-by-date');
const { ethers } = require('ethers');
const provider = new ethers.providers.CloudflareProvider();
const dater = new EthDater(
provider
);
Requests
let block = await dater.getDate(
'2016-07-20T13:20:40Z',
true,
false
);
let blocks = await dater.getEvery(
'weeks',
'2019-09-02T12:00:00Z',
'2019-09-30T12:00:00Z',
1,
true,
false
);
let requests = dater.requests;
Note: if the given date is before the first block date in the blockchain, the script will return 1 as block number. If the given date is in the future, the script will return the last block number in the blockchain.
Moment.js
The package uses moment.js plugin to parse dates. Read more about valid dates and time zones in the plugin's documentation: Moment.js
Examples
Every first block of the year:
let blocks = await dater.getEvery('years', '2016-01-01T00:00:00Z', '2019-01-01T00:00:00Z');
Every last block of the year:
let blocks = await dater.getEvery('years', '2016-01-01T00:00:00Z', '2019-01-01T00:00:00Z', 1, false);
Every first block of every 4 hours of October 10, 2019:
let blocks = await dater.getEvery('hours', '2019-10-10T00:00:00Z', '2019-10-11T00:00:00Z', 4);
Need Help
If you need any help, please contact me via GitHub issues page: GitHub
Donations
If you like my package and you want to support the development or buy me a cup of coffee, you could donate to me via Ethereum: 0x18F54b91f7e19c51fA701E7ed5628fA45441d872
Thanks ❤️