npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eth-block-datetime

v1.0.3

Published

Utilities for parsing, validating, manipulating, and displaying Ethereum blocks by date

Downloads

11

Readme

Utilities for parsing, validating, manipulating, and displaying Ethereum blocks by date

🏠 Homepage

Install

Npm

npm install eth-block-datetime

Yarn

yarn add eth-block-datetime

Getting Started

import { ethers } from 'ethers'
import EthBlockDatetime from 'eth-block-datetime'

const provider = new ethers.providers.CloudflareProvider()
const ethDatetime = new EthBlockDatetime(
    provider, // [required] rpc provider
)

With block explorer for more efficient queries

const ethDatetime = new EthBlockDatetime(
    provider, // [required] rpc provider
    1, // [optional / required if using blockExplorer client] chainId
    'FREE_ETHERSCAN_APIKEY', // [optional] free or pro etherscan api key corresponding to specified chainId
)

Get block by timestamp

const latestBlock = await ethDatetime.getBlockByTimestamp({ timestamp: 'latest' })
const earliestBlock = await ethDatetime.getBlockByTimestamp({ timestamp: 'earliest' })
const blockFromDateString = await ethDatetime.getBlockByTimestamp({ timestamp: '2016-07-20T13:20:40Z' })
const blockFromDateObject = await ethDatetime.getBlockByTimestamp({ timestamp: new Date('2016-07-20T13:20:40Z') })
const blockDateFromMomentObject = await ethDatetime.getBlockByTimestamp({ timestamp: moment(new Date('2016-07-20T13:20:40Z')) })

Response

{
    datetime: '2021-10-03T16:09:28Z',
    number: 13347220,
    timestamp: 1633277292
}

Get blocks by range

let start = new Date()
start = new Date(startTime.setDate(startTime.getDate() - 7))
const block = await ethDatetime.getBlocksByRange({
    start,
    interval: 'days',
})

Response

[
  {
    datetime: '2021-09-26T16:09:28Z',
    number: 13302379,
    timestamp: 1632672551
  },
  {
    datetime: '2021-09-27T16:09:28Z',
    number: 13308801,
    timestamp: 1632758964
  },
  // ... rest of blocks
]

Usage

Providers

EthersJS

import { ethers } from 'ethers'

Public cloudflare ETH RPC

const provider = new ethers.providers.CloudflareProvider() // to use public cloudflare ETH RPC

Pass in Infura / Alchemy or other Node-as-a-service Eth RPC provider

const provider = new ethers.providers.JsonRpcProvider(process.env.ETH_RPC_URL) // to use supplied RPC url

Web3

import Web3 from 'web3'

Pass in injected browser web3 object

const provider = new Web3(window.ethereum) // to use supplied RPC url

Pass in Infura / Alchemy or other Node-as-a-service Eth RPC provider

const provider = new Web3(new Web3.providers.HttpProvider(process.env.ETH_RPC_URL)) // to use supplied RPC url

Examples

Get block by timestamp

const block = await ethDatetime.getBlockByTimestamp({
    timestamp: 'earliest', // [required] options: earliest, latest, timestamp string, momentJs date object, or javascript date object
    closest: 'after', // [optional] after (default), before [estimation method for block closest to timestamp]
    blockTime: 15, // [optional] override average blocktime for finding closest block
    useBlockExplorer: true, // [optional] true (default if chainId & api key is given)
    includeFullBlock: false, // [optiona] false (default) [specify if full block data should be returned]
})

Block

{
    datetime: '2021-10-03T16:09:28Z',
    number: 13347220,
    timestamp: 1633277292
}

Get blocks by range

let start = new Date()
let end = new Date()
start = new Date(startTime.setDate(startTime.getDate() - 7))
const block = await ethDatetime.getBlocksByRange({
    start, // [required] options: earliest, latest, timestamp string, momentJs date object, or javascript date object
    end, // [optional] latest block (default) options: earliest, latest, timestamp string, momentJs date object, or javascript date object
    interval: 'days', // [required] // seconds, minutes, hours, days, weeks, months, years
    duration: 1, // [optional] 1 (default)
    closest: 'after', // [optional] after (default), before [estimation method for block closest to timestamp] 
    includeFullBlock: false, // [optiona] false (default) [specify if full block data should be returned]
})

Blocks

[
      {
        datetime: '2021-09-26T16:09:28Z',
        number: 13302379,
        timestamp: 1632672551
      },
      {
        datetime: '2021-09-27T16:09:28Z',
        number: 13308801,
        timestamp: 1632758964
      },
      {
        datetime: '2021-09-28T16:09:28Z',
        number: 13315225,
        timestamp: 1632845341
      },
      {
        datetime: '2021-09-29T16:09:28Z',
        number: 13321658,
        timestamp: 1632931728
      },
      {
        datetime: '2021-09-30T16:09:28Z',
        number: 13328031,
        timestamp: 1633018153
      },
      {
        datetime: '2021-10-01T16:09:28Z',
        number: 13334484,
        timestamp: 1633104558
      },
      {
        datetime: '2021-10-02T16:09:28Z',
        number: 13340842,
        timestamp: 1633190949
      },
      {
        datetime: '2021-10-03T16:09:28Z',
        number: 13347220,
        timestamp: 1633277292
      }
]

Run tests

yarn test

Author

Modified and updated https://github.com/monosux/ethereum-block-by-date to add additional customization and options, dynamic blocktime calculations, and integration with public blockexplorers

👤 matmertz25

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 matmertz25. This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator