diff --git a/package.json b/package.json index 97774bc..0e3f0f9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ghost-dao-interface", "private": true, - "version": "0.8.4", + "version": "0.8.5", "type": "module", "scripts": { "dev": "vite", diff --git a/src/containers/Bridge/BridgeIn.jsx b/src/containers/Bridge/BridgeIn.jsx index 4014c72..393edc7 100644 --- a/src/containers/Bridge/BridgeIn.jsx +++ b/src/containers/Bridge/BridgeIn.jsx @@ -47,11 +47,8 @@ import { networkAvgBlockSpeed } from "../../constants"; const DECIMALS_PREFIX = "bridge-in-decimals"; -const BridgeStatusCard = ({ totalValidators, pendingValidators, tapestryDraft }) => { +const BridgeStatusCard = ({ progressPercent, totalValidators, pendingValidators, tapestryDraft }) => { const theme = useTheme(); - const progressPercent = totalValidators > 0 - ? (pendingValidators / totalValidators) * 100 - : 0; return ( @@ -91,6 +88,7 @@ const BridgeIn = ({ chainId, address, config, connect }) => { const { casperApi, client, account } = useUnstableProvider(); const { gatekeepedApy } = useGatekeeperApy(chainId); + const [msSinceLastBlock, setMsSinceLastBlock] = useState(0); const [nextEraIn, setNextEraIn] = useState(null); const [settingsOpen, setSettingsOpen] = useState(false); const [isPending, setIsPending] = useState(false); @@ -164,6 +162,38 @@ const BridgeIn = ({ chainId, address, config, connect }) => { } ); + const { data: attestationDelay } = useSWR( + casperApi ? ['attestationDelay', casperApi] : null, + () => casperApi.constants.GhostWeaver.AttestationDelay(), + ); + + const { data: weavingDelay } = useSWR( + casperApi ? ['weavingDelay', casperApi] : null, + () => casperApi.constants.GhostWeaver.WeavingDelay(), + ); + + const { data: currentBlockNumber } = useSWR( + casperApi ? ['currentBlockNumber', casperApi] : null, + () => casperApi.query.System.Number.getValue(), + { + refreshInterval: 12000, + revalidateOnFocus: true + } + ); + + const { data: nextConsensusAttempt, error } = useSWR( + casperApi ? ['nextConsensusAttempt', casperApi] : null, + async () => { + const key = BigInt(chainId); + const res = await casperApi.query.GhostWeaver.NextConsensusAttemptBlocks.getValue(key); + return res; + }, + { + refreshInterval: 12000, + revalidateOnFocus: true + } + ); + const { data: networkAttestations } = useSWR( casperApi ? ['networkAttestations', casperApi, chainId] : null, async () => { @@ -358,6 +388,36 @@ const BridgeIn = ({ chainId, address, config, connect }) => { } } + useEffect(() => { + setMsSinceLastBlock(0); + + const startTime = Date.now(); + const interval = setInterval(() => { + setMsSinceLastBlock(Date.now() - startTime); + }, 500); + + return () => clearInterval(interval); + }, [currentBlockNumber]); + + const progressPercent = useMemo(() => { + if (!nextConsensusAttempt || !attestationDelay || !weavingDelay) return 5; + + const total = tapestryDraft ? weavingDelay : attestationDelay; + const end = tapestryDraft ? tapestryDraft.until : nextConsensusAttempt; + + const remaining = end - currentBlockNumber; + const passed = Number(total) - remaining; + const progress = passed * 100 / Number(total); + + // TODO + const virtualBlocksPassed = Math.min(msSinceLastBlock / 12000, 1); + const smoothBlocksPassed = passed + virtualBlocksPassed; + + const smoothProgress = (smoothBlocksPassed * 100) / Number(total); + + return Math.min(Math.max(smoothProgress, 3), 100); + }, [currentBlockNumber, tapestryDraft, nextConsensusAttempt, msSinceLastBlock, attestationDelay, weavingDelay]); + return ( @@ -412,6 +472,7 @@ const BridgeIn = ({ chainId, address, config, connect }) => {