Skip to content

Commit

Permalink
Merge pull request #8 from DLC-link/feat/confirmation-checker
Browse files Browse the repository at this point in the history
feat: added confirmation-checker
  • Loading branch information
Polybius93 authored Nov 24, 2023
2 parents 812541c + 49595f1 commit d4b5222
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
21 changes: 18 additions & 3 deletions src/app/components/vault/components/vault-progress-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { Box, Progress, Text, VStack } from "@chakra-ui/react";
import { VaultState } from "@models/vault";

interface VaultProgressBarProps {
confirmedBlocks: number;
vaultState: VaultState;
}

export function VaultProgressBar({
confirmedBlocks,
}: VaultProgressBarProps): React.JSX.Element {
vaultState,
}: VaultProgressBarProps): React.JSX.Element | boolean {
const shouldBeIndeterminate = confirmedBlocks > 6;

if (vaultState === VaultState.CLOSED && confirmedBlocks > 6) return false;
return (
<VStack w={"100%"} alignItems={"end"} position="relative">
<Progress value={50} w={"100%"} h={"25px"} borderRadius={"md"} />
<Progress
isIndeterminate={shouldBeIndeterminate}
value={confirmedBlocks}
max={6}
w={"100%"}
h={"25px"}
borderRadius={"md"}
/>
<Box
display="flex"
position="absolute"
Expand All @@ -21,7 +34,9 @@ export function VaultProgressBar({
h="100%"
>
<Text color={"white"} fontSize={"xs"} fontWeight={800}>
WAITING FOR CONFIRMATIONS: {confirmedBlocks}/6
{shouldBeIndeterminate
? "PROCESSING"
: `WAITING FOR CONFIRMATIONS: ${confirmedBlocks}/6`}
</Text>
</Box>
</VStack>
Expand Down
14 changes: 11 additions & 3 deletions src/app/components/vault/vault-card.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from "react";

import { CustomSkeleton } from "@components/custom-skeleton/custom-skeleton";
import { useConfirmationChecker } from "@hooks/use-confirmation-checker";
import { Vault, VaultState } from "@models/vault";

import { VaultCardLayout } from "./components/vault-card.layout";
Expand All @@ -21,7 +22,11 @@ export function VaultCard({
isSelectable = false,
handleSelect,
}: VaultBoxProps): React.JSX.Element {
const confirmedBlocks = 3;
const confirmations = useConfirmationChecker(
vault?.state === VaultState.FUNDING ? vault?.fundingTX : vault?.closingTX,
vault?.state,
);

const [isExpanded, setIsExpanded] = useState(isSelected ? true : false);

if (!vault) return <CustomSkeleton height={"65px"} />;
Expand All @@ -48,8 +53,11 @@ export function VaultCard({
isExpanded={isExpanded}
/>
)}
{[VaultState.FUNDING, VaultState.CLOSING].includes(vault.state) && (
<VaultProgressBar confirmedBlocks={confirmedBlocks} />
{[VaultState.FUNDING, VaultState.CLOSED].includes(vault.state) && (
<VaultProgressBar
confirmedBlocks={confirmations}
vaultState={vault.state}
/>
)}
</VaultCardLayout>
);
Expand Down
74 changes: 74 additions & 0 deletions src/app/hooks/use-confirmation-checker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useEffect, useMemo, useRef, useState } from "react";

import { VaultState } from "@models/vault";

export function useConfirmationChecker(
txID: string | undefined,
vaultState: VaultState | undefined,
): number {
const bitcoinExplorerTXURL = `https://devnet.dlc.link/electrs/tx/${txID}`;
const bitcoinExplorerHeightURL = `https://devnet.dlc.link/electrs/blocks/tip/height`;
const fetchInterval = useRef<number | undefined>(undefined);

const [transactionProgress, setTransactionProgress] = useState(0);

const memoizedTransactionProgress = useMemo(
() => transactionProgress,
[transactionProgress],
);

const fetchTransactionDetails = async () => {
if (
!txID ||
(vaultState &&
![VaultState.FUNDING, VaultState.CLOSED].includes(vaultState))
) {
clearInterval(fetchInterval.current);
return;
}

let bitcoinCurrentBlockHeight;
try {
const response = await fetch(bitcoinExplorerHeightURL, {
headers: { Accept: "application/json" },
});
bitcoinCurrentBlockHeight = await response.json();
} catch (error) {
console.error(error);

Check warning on line 37 in src/app/hooks/use-confirmation-checker.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
}

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);

Check warning on line 50 in src/app/hooks/use-confirmation-checker.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
}

const difference =
bitcoinCurrentBlockHeight - bitcoinTransactionBlockHeight;

setTransactionProgress(difference);

if (difference > 6) {
clearInterval(fetchInterval.current);
}
};

fetchTransactionDetails();

Check warning on line 63 in src/app/hooks/use-confirmation-checker.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

useEffect(() => {
fetchInterval.current = setInterval(
fetchTransactionDetails,
10000,
) as unknown as number; // Cleanup the interval when the component unmounts
return () => clearInterval(fetchInterval.current);
}, []);

Check warning on line 71 in src/app/hooks/use-confirmation-checker.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

React Hook useEffect has a missing dependency: 'fetchTransactionDetails'. Either include it or remove the dependency array

return memoizedTransactionProgress;
}

0 comments on commit d4b5222

Please sign in to comment.