Skip to content

Ethers Provider extension to quickly find a block close to a target timestamp.

License

Notifications You must be signed in to change notification settings

trmid/block-at-timestamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

block-at-timestamp

version typescript ethers

Quickly find a block close to a target timestamp using an RPC URL or ethers Provider.

Installation

Install the package with the following command:

npm i block-at-timestamp

Usage

Example Usage:

import { blockAtTimestamp } from "block-at-timestamp";

let timestamp = 1674420070; // unix timestamp in seconds
let block = await blockAtTimestamp("https://rpc-provider.example/", timestamp);

Optional Target Range:

By default, the algorithm will attempt to find a block within 60 seconds of the target timestamp. If you want a faster result with less accuracy, you can increase the targetRangeSeconds parameter like so:

await blockAtTimestamp(provider, timestamp, {
  targetRangeSeconds: 60 * 60 // one hour
});

Debugging:

To see logs for debugging purposes, you can enable verbose logging with the following:

import { setVerbose } from "block-at-timestamp";

setVerbose(true);

About

Ethers Provider extension to quickly find a block close to a target timestamp.

Topics

Resources

License

Stars

Watchers

Forks