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: 2 months 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 (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-20T16:10:20.000Z (over 1 year ago)
- Last Synced: 2025-03-14T12:18:34.622Z (3 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


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);
```