|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | + |
| 3 | +import { ethers } from "ethers"; |
| 4 | +import moment from "moment"; |
| 5 | + |
| 6 | +import { getChainRpcUrl } from "context/Web3Provider"; |
| 7 | + |
| 8 | +export const useBlockByTimestamp = (chainId, timestamp) => { |
| 9 | + const [blockNumber, setBlockNumber] = useState<number | null>(null); |
| 10 | + const [loading, setLoading] = useState(true); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + const provider = new ethers.providers.JsonRpcProvider(getChainRpcUrl("https", chainId)); |
| 14 | + |
| 15 | + const getBlockByTimestamp = async () => { |
| 16 | + const getBlockWrapper = async (block) => { |
| 17 | + const blockData = await provider.getBlock(block); |
| 18 | + return { |
| 19 | + timestamp: blockData.timestamp, |
| 20 | + number: blockData.number, |
| 21 | + }; |
| 22 | + }; |
| 23 | + |
| 24 | + const getBoundaries = async () => { |
| 25 | + const latestBlock = await getBlockWrapper("latest"); |
| 26 | + const firstBlock = await getBlockWrapper(1); |
| 27 | + const blockTime = (latestBlock.timestamp - firstBlock.timestamp) / (latestBlock.number - 1); |
| 28 | + return { latestBlock, firstBlock, blockTime }; |
| 29 | + }; |
| 30 | + |
| 31 | + const findBetter = async (date, predictedBlock, after, blockTime) => { |
| 32 | + if (await isBetterBlock(date, predictedBlock, after)) return predictedBlock.number; |
| 33 | + const difference = date.diff(moment.unix(predictedBlock.timestamp), "seconds"); |
| 34 | + let skip = Math.ceil(difference / (blockTime === 0 ? 1 : blockTime)); |
| 35 | + if (skip === 0) skip = difference < 0 ? -1 : 1; |
| 36 | + const nextPredictedBlock = await getBlockWrapper(predictedBlock.number + skip); |
| 37 | + blockTime = Math.abs( |
| 38 | + (predictedBlock.timestamp - nextPredictedBlock.timestamp) / |
| 39 | + (predictedBlock.number - nextPredictedBlock.number) |
| 40 | + ); |
| 41 | + return findBetter(date, nextPredictedBlock, after, blockTime); |
| 42 | + }; |
| 43 | + |
| 44 | + const isBetterBlock = async (date, predictedBlock, after) => { |
| 45 | + const blockTime = moment.unix(predictedBlock.timestamp); |
| 46 | + if (after) { |
| 47 | + if (blockTime.isBefore(date)) return false; |
| 48 | + const previousBlock = await getBlockWrapper(predictedBlock.number - 1); |
| 49 | + if (blockTime.isSameOrAfter(date) && moment.unix(previousBlock.timestamp).isBefore(date)) return true; |
| 50 | + } else { |
| 51 | + if (blockTime.isSameOrAfter(date)) return false; |
| 52 | + const nextBlock = await getBlockWrapper(predictedBlock.number + 1); |
| 53 | + if (blockTime.isBefore(date) && moment.unix(nextBlock.timestamp).isSameOrAfter(date)) return true; |
| 54 | + } |
| 55 | + return false; |
| 56 | + }; |
| 57 | + |
| 58 | + try { |
| 59 | + const date = moment.utc(timestamp); |
| 60 | + const { latestBlock, firstBlock, blockTime } = await getBoundaries(); |
| 61 | + |
| 62 | + if (date.isBefore(moment.unix(firstBlock.timestamp))) { |
| 63 | + setBlockNumber(firstBlock.number); |
| 64 | + setLoading(false); |
| 65 | + return; |
| 66 | + } |
| 67 | + if (date.isSameOrAfter(moment.unix(latestBlock.timestamp))) { |
| 68 | + setBlockNumber(latestBlock.number); |
| 69 | + setLoading(false); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const predictedBlockNumber = Math.ceil(date.diff(moment.unix(firstBlock.timestamp), "seconds") / blockTime); |
| 74 | + const predictedBlock = await getBlockWrapper(predictedBlockNumber); |
| 75 | + |
| 76 | + const block = await findBetter(date, predictedBlock, true, blockTime); |
| 77 | + setBlockNumber(block); |
| 78 | + } catch (error) { |
| 79 | + console.error(error); |
| 80 | + } finally { |
| 81 | + setLoading(false); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + getBlockByTimestamp(); |
| 86 | + }, [chainId, timestamp]); |
| 87 | + |
| 88 | + return { blockNumber, loading }; |
| 89 | +}; |
0 commit comments