-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace fetching with reactqueries for block height fetching (#56)
* feat: replace fetching with reactqueries for block height fetching
- Loading branch information
1 parent
288f36e
commit d8b4f97
Showing
13 changed files
with
217 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
export function useBlockchainHeightQuery(): number | undefined { | ||
const bitcoinBlockchainAPIURL = import.meta.env.VITE_BITCOIN_BLOCKCHAIN_API_URL; | ||
const bitcoinExplorerHeightURL = `${bitcoinBlockchainAPIURL}/blocks/tip/height`; | ||
|
||
async function getBlockchainHeight() { | ||
const response = await fetch(bitcoinExplorerHeightURL); | ||
if (!response.ok) throw new Error('Network response was not ok'); | ||
return response.json(); | ||
} | ||
|
||
const { data: blockHeight } = useQuery('blockHeight', () => getBlockchainHeight(), { | ||
refetchInterval: 10000, | ||
}); | ||
|
||
return blockHeight; | ||
} |
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 |
---|---|---|
@@ -1,63 +1,69 @@ | ||
import { useEffect, useMemo, useRef, useState } from 'react'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { useQuery } from 'react-query'; | ||
|
||
import { Vault, VaultState } from '@models/vault'; | ||
import { BlockchainHeightContext } from '@providers/bitcoin-query-provider'; | ||
|
||
export function useConfirmationChecker(vault?: Vault): number { | ||
const txID = vault?.state === VaultState.FUNDING ? vault?.fundingTX : vault?.closingTX; | ||
const { blockHeight } = useContext(BlockchainHeightContext); | ||
|
||
let txID; | ||
switch (vault?.state) { | ||
case VaultState.FUNDING: | ||
txID = vault?.fundingTX; | ||
break; | ||
case VaultState.CLOSED: | ||
txID = vault?.closingTX; | ||
break; | ||
default: | ||
txID = undefined; | ||
} | ||
|
||
const [blockHeightAtBroadcast, setBlockHeightAtBroadcast] = useState<number | undefined>( | ||
undefined | ||
); | ||
const [transactionProgress, setTransactionProgress] = useState<number>(0); | ||
|
||
const bitcoinBlockchainAPIURL = import.meta.env.VITE_BITCOIN_BLOCKCHAIN_API_URL; | ||
|
||
const bitcoinExplorerTXURL = `${bitcoinBlockchainAPIURL}/tx/${txID}`; | ||
const bitcoinExplorerHeightURL = `${bitcoinBlockchainAPIURL}/blocks/tip/height`; | ||
const fetchInterval = useRef<number | undefined>(undefined); | ||
|
||
const [transactionProgress, setTransactionProgress] = useState(0); | ||
|
||
const memoizedTransactionProgress = useMemo(() => transactionProgress, [transactionProgress]); | ||
|
||
const fetchTransactionDetails = async () => { | ||
if (!txID || (vault?.state && ![VaultState.FUNDING, VaultState.CLOSED].includes(vault.state))) { | ||
clearInterval(fetchInterval.current); | ||
return; | ||
async function fetchTransactionDetails() { | ||
const response = await fetch(bitcoinExplorerTXURL); | ||
if (!response.ok) throw new Error('Network response was not ok'); | ||
const transactionDetails = await response.json(); | ||
return transactionDetails.status.block_height; | ||
} | ||
|
||
const { data: txBlockHeightAtBroadcast } = useQuery( | ||
['transactionDetails', txID], | ||
() => fetchTransactionDetails(), | ||
{ | ||
enabled: | ||
!!txID && | ||
(vault?.state === VaultState.FUNDING || vault?.state === VaultState.CLOSED) && | ||
!blockHeightAtBroadcast, | ||
refetchInterval: 10000, | ||
} | ||
); | ||
|
||
let bitcoinCurrentBlockHeight; | ||
try { | ||
const response = await fetch(bitcoinExplorerHeightURL, { | ||
headers: { Accept: 'application/json' }, | ||
}); | ||
bitcoinCurrentBlockHeight = await response.json(); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
let bitcoinTransactionBlockHeight; | ||
|
||
try { | ||
const response = await fetch(bitcoinExplorerTXURL, { | ||
headers: { Accept: 'application/json' }, | ||
}); | ||
const bitcoinTransactionDetails = await response.json(); | ||
bitcoinTransactionBlockHeight = bitcoinTransactionDetails.status.block_height; | ||
} catch (error) { | ||
console.error(error); | ||
useEffect(() => { | ||
if (txBlockHeightAtBroadcast && typeof txBlockHeightAtBroadcast === 'number') { | ||
setBlockHeightAtBroadcast(txBlockHeightAtBroadcast); | ||
} | ||
}, [txBlockHeightAtBroadcast]); | ||
|
||
const difference = bitcoinCurrentBlockHeight - bitcoinTransactionBlockHeight; | ||
|
||
setTransactionProgress(difference > 0 ? difference : 0); | ||
useEffect(() => { | ||
if ( | ||
(vault?.state != VaultState.FUNDING && vault?.state != VaultState.CLOSED) || | ||
transactionProgress > 6 | ||
) | ||
return; | ||
|
||
if (difference > 6) { | ||
clearInterval(fetchInterval.current); | ||
const blockHeightDifference = (blockHeight as number) - (blockHeightAtBroadcast as number); | ||
if (typeof blockHeightDifference === 'number') { | ||
setTransactionProgress(blockHeightDifference); | ||
} | ||
}; | ||
|
||
fetchTransactionDetails(); | ||
|
||
useEffect(() => { | ||
fetchInterval.current = setInterval(fetchTransactionDetails, 10000) as unknown as number; // Cleanup the interval when the component unmounts | ||
return () => clearInterval(fetchInterval.current); | ||
}, [vault?.state, txID]); | ||
}, [blockHeightAtBroadcast, blockHeight, vault?.state, transactionProgress]); | ||
|
||
return memoizedTransactionProgress; | ||
return transactionProgress; | ||
} |
Oops, something went wrong.