Skip to content

A web3 tool to get the closest block number to a given timestamp.

License

Notifications You must be signed in to change notification settings

t0mcr8se/block-stamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

blockstamp

Description

blockstamp is a tool that gets the block number that is closest (floor) to a given timestamp using binary search.

Installation

npm i "@t0mcr8se/blockstamp"

Usage

import {getBlockByTimestamp, getParallelBlocks} from "@t0mcr8se/blockstamp"
import Web3 from "web3"

const main = async() =>{
  const fuseWeb3 = new Web3(new Web3.providers.HttpProvider("https://rpc.fuse.io"))
  const sometime = 1622892258
  const fuseBlock = await getBlockByTimestamp(fuseWeb3, sometime)

  console.log(`${sometime} : ${fuseBlock.timestamp} : ${fuseBlock.number}`)

  const bscWeb3 = new Web3(new Web3.providers.HttpProvider("https://bsc-dataseed.binance.org/"))
  // get the block on bsc that coincides with block `fuseBlock` on fuse (parallel block)
  const bscBlock = await getParallelBlocks(fuseWeb3, bscWeb3, fuseBlock.number) 

  console.log(`${bscBlock.timestamp} : ${bscBlock.number}`)
  console.log(`${fuseBlock.timestamp} : ${fuseBlock.number}`)
}
main.then()

Credits

to me)

License

MIT

About

A web3 tool to get the closest block number to a given timestamp.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published