Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trmid/block-at-timestamp
Ethers Provider extension to quickly find a block close to a target timestamp.
https://github.com/trmid/block-at-timestamp
block blockchain ethers provider query rpc timestamp
Last synced: about 1 month ago
JSON representation
Ethers Provider extension to quickly find a block close to a target timestamp.
- Host: GitHub
- URL: https://github.com/trmid/block-at-timestamp
- Owner: trmid
- License: mit
- Created: 2023-01-22T04:06:09.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-20T16:10:20.000Z (over 1 year ago)
- Last Synced: 2024-11-29T16:42:10.802Z (about 2 months ago)
- Topics: block, blockchain, ethers, provider, query, rpc, timestamp
- Language: TypeScript
- Homepage:
- Size: 26.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# block-at-timestamp
![version](https://img.shields.io/badge/dynamic/json?color=brightgreen&label=npm&logo=npm&query=%24.version&url=https%3A%2F%2Fraw.githubusercontent.com%2Ftrmid%2Fblock-at-timestamp%2Fmain%2Fpackage.json&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fblock-at-timestamp)
![typescript](https://img.shields.io/static/v1?label&logo=typescript&logoColor=white&message=TypeScript&color=blue)
![ethers](https://img.shields.io/static/v1?label&logo=ethereum&logoColor=white&message=ethers.js&color=gray&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fethers)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:
```js
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:
```js
await blockAtTimestamp(provider, timestamp, {
targetRangeSeconds: 60 * 60 // one hour
});
```### Debugging:
To see logs for debugging purposes, you can enable verbose logging with the following:
```js
import { setVerbose } from "block-at-timestamp";setVerbose(true);
```