1.0.1 • Published 3 years ago

@t0mcr8se/blockstamp v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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