Files
leporello-js/docs/examples/ethers/block_by_timestamp.js
Dmitry Vasilev e8e54e9d4a fix example
2023-08-10 12:49:12 +03:00

29 lines
858 B
JavaScript

import {ethers} from 'https://unpkg.com/ethers/dist/ethers.js'
const URL = 'https://rpc.ankr.com/eth'
const provider = await ethers.getDefaultProvider(URL)
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) {
return low
} else {
const mid = Math.floor((low + high) / 2)
const midBlock = await provider.getBlock(mid)
if(midBlock.timestamp > timestamp) {
return getBlockNumberByTimestamp(timestamp, low, mid)
} else {
return getBlockNumberByTimestamp(timestamp, mid, high)
}
}
}
const timestamp = new Date('2019-06-01').getTime()/1000
const blockNumber = await getBlockNumberByTimestamp(timestamp)
const block = await provider.getBlock(blockNumber)