mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
deploy: leporello-js/app@a6e3f1b36f
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
import {ethers} from 'https://unpkg.com/ethers/dist/ethers.js'
|
||||
import { ethers } from "ethers"
|
||||
|
||||
const URL = 'https://rpc.ankr.com/eth'
|
||||
const URL = "https://eth-mainnet.public.blastapi.io"
|
||||
|
||||
const provider = await ethers.getDefaultProvider(URL)
|
||||
|
||||
const latest = await provider.getBlock('latest')
|
||||
const latest = await provider.getBlock("latest")
|
||||
|
||||
/*
|
||||
Find ethereum block by timestamp using binary search
|
||||
*/
|
||||
async function getBlockNumberByTimestamp(timestamp, low = 0, high = latest.number) {
|
||||
if(low + 1 == high) {
|
||||
async function getBlockNumberByTimestamp(
|
||||
timestamp,
|
||||
low = 0,
|
||||
high = latest.number,
|
||||
) {
|
||||
if (low + 1 == high) {
|
||||
return low
|
||||
} else {
|
||||
const mid = Math.floor((low + high) / 2)
|
||||
const midBlock = await provider.getBlock(mid)
|
||||
if(midBlock.timestamp > timestamp) {
|
||||
if (midBlock.timestamp > timestamp) {
|
||||
return getBlockNumberByTimestamp(timestamp, low, mid)
|
||||
} else {
|
||||
return getBlockNumberByTimestamp(timestamp, mid, high)
|
||||
@@ -23,6 +27,6 @@ async function getBlockNumberByTimestamp(timestamp, low = 0, high = latest.numbe
|
||||
}
|
||||
}
|
||||
|
||||
const timestamp = new Date('2019-06-01').getTime()/1000
|
||||
const timestamp = new Date("2019-06-01").getTime() / 1000
|
||||
const blockNumber = await getBlockNumberByTimestamp(timestamp)
|
||||
const block = await provider.getBlock(blockNumber)
|
||||
|
||||
Reference in New Issue
Block a user