@t0mcr8se/blockstamp
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i @t0mcr8se/blockstamp

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

18 kB

Total Files

7

Last publish

Collaborators

  • t0mcr8se