Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added confirmation-checker #8

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading