-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge the develop branch to the master branch, preparation to v2.7.0-…
…rc0 (#515) This merge contains the following set of changes: * [Improvement] Add /metrics endpoint for prometheus support (#512) * [Improvement] Add monitor script for detecting failed AMB messages (#513) * [Improvement] Improve performance of BS requests in ALM (#516) * [Fix] Add pretty error messages for manual execution transaction reverts (#511) * [Fix] Fix resend of stuck pending transactions (#514)
- Loading branch information
Showing
25 changed files
with
593 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useEffect, useState } from 'react' | ||
import { TransactionReceipt } from 'web3-eth' | ||
import { useStateProvider } from '../state/StateProvider' | ||
import { FOREIGN_EXPLORER_API, HOME_EXPLORER_API } from '../config/constants' | ||
import { getClosestBlockByTimestamp } from '../utils/explorer' | ||
|
||
export function useClosestBlock( | ||
searchHome: boolean, | ||
fromHome: boolean, | ||
receipt: Maybe<TransactionReceipt>, | ||
timestamp: number | ||
) { | ||
const { home, foreign } = useStateProvider() | ||
const [blockNumber, setBlockNumber] = useState<number | null>(null) | ||
|
||
useEffect( | ||
() => { | ||
if (!receipt || blockNumber || !timestamp) return | ||
|
||
if (fromHome === searchHome) { | ||
setBlockNumber(receipt.blockNumber) | ||
return | ||
} | ||
|
||
const web3 = searchHome ? home.web3 : foreign.web3 | ||
if (!web3) return | ||
|
||
const getBlock = async () => { | ||
// try to fast-fetch closest block number from the chain explorer | ||
try { | ||
const api = searchHome ? HOME_EXPLORER_API : FOREIGN_EXPLORER_API | ||
setBlockNumber(await getClosestBlockByTimestamp(api, timestamp)) | ||
return | ||
} catch {} | ||
|
||
const lastBlock = await web3.eth.getBlock('latest') | ||
if (lastBlock.timestamp <= timestamp) { | ||
setBlockNumber(lastBlock.number) | ||
return | ||
} | ||
|
||
const oldBlock = await web3.eth.getBlock(Math.max(lastBlock.number - 10000, 1)) | ||
const blockDiff = lastBlock.number - oldBlock.number | ||
const timeDiff = (lastBlock.timestamp as number) - (oldBlock.timestamp as number) | ||
const averageBlockTime = timeDiff / blockDiff | ||
let currentBlock = lastBlock | ||
|
||
let prevBlockDiff = Infinity | ||
while (true) { | ||
const timeDiff = (currentBlock.timestamp as number) - timestamp | ||
const blockDiff = Math.ceil(timeDiff / averageBlockTime) | ||
if (Math.abs(blockDiff) < 5 || Math.abs(blockDiff) >= Math.abs(prevBlockDiff)) { | ||
setBlockNumber(currentBlock.number - blockDiff - 5) | ||
break | ||
} | ||
|
||
prevBlockDiff = blockDiff | ||
currentBlock = await web3.eth.getBlock(currentBlock.number - blockDiff) | ||
} | ||
} | ||
|
||
getBlock() | ||
}, | ||
[blockNumber, foreign.web3, fromHome, home.web3, receipt, searchHome, timestamp] | ||
) | ||
|
||
return blockNumber | ||
} |
Oops, something went wrong.