date-to-block
v0.3.1
Published
Get blocknumber from date
Downloads
6,295
Readme
Date To Block
This is a simple library for getting a block number based on a date using RPC calls.
The algorithm used is a simple binary search algorithm.
This library only supports viem and you need to provide a viem public client.
Installation
npm install date-to-block
Usage
First create a viem client:
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";
const client = createPublicClient({
chain: mainnet,
transport: http(),
});
Then use date-to-block
to get the block number:
import { dateToBlock } from "date-to-block";
const blockNumber = await dateToBlock(client, new Date("2021-01-01"));
Related work
- date-to-block-eth - Works with Web3.js and Ethers.js
- ethereum-block-by-date - Works with Web3.js and Ethers.js. Can return multible block numbers for a given period.