Files
CurrenciCombo/src/hooks/useLiveChain.ts
Devin AI 007c79d7a9 feat(portal): wire DashboardPage to live Chain-138 RPC + SolaceScan Explorer
- Add services/{http,chain138,explorer,proxmox,dbisCore} + hooks/{useLiveChain,useOnChainBalances}
- Add BackendStatusBar + LiveNetworkPanel components on DashboardPage
- Overlay on-chain META balance on account rows carrying a walletAddress
- Normalize EIP-55 checksum in chain138.getNativeBalance so hand-typed
  sample custody addresses (e.g. 0x742d35Cc...bD38) don't silently drop
  out of the balance map
- Default RPC: https://rpc.d-bis.org (user-preferred gateway)
- proxmox.ts stays mocked (CF-Access, needs BFF); dbisCore.ts stays
  mocked (no public deployment yet)

Co-Authored-By: Nakamoto, S <defi@defi-oracle.io>
2026-04-19 00:33:46 +00:00

55 lines
2.1 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from 'react';
import { getChainHealth, getLatestBlock, type ChainHealth, type LatestBlock } from '../services/chain138';
import { getExplorerStats, type ExplorerStats } from '../services/explorer';
export interface LiveChainState {
health: ChainHealth | null;
latestBlock: LatestBlock | null;
stats: ExplorerStats | null;
loading: boolean;
error: string | null;
lastUpdated: Date | null;
refresh: () => void;
}
/**
* Polls chain-138 RPC + SolaceScan explorer every `pollMs` (default 12s).
* Returns `null` values while loading the first time; never throws.
*/
export function useLiveChain(pollMs = 12_000): LiveChainState {
const [health, setHealth] = useState<ChainHealth | null>(null);
const [latestBlock, setLatestBlock] = useState<LatestBlock | null>(null);
const [stats, setStats] = useState<ExplorerStats | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
const mounted = useRef(true);
const tick = useCallback(async () => {
try {
const [h, b, s] = await Promise.allSettled([getChainHealth(), getLatestBlock(), getExplorerStats()]);
if (!mounted.current) return;
if (h.status === 'fulfilled') setHealth(h.value);
if (b.status === 'fulfilled') setLatestBlock(b.value);
if (s.status === 'fulfilled') setStats(s.value);
const anyError = [h, b, s].find(r => r.status === 'rejected') as PromiseRejectedResult | undefined;
setError(anyError ? String(anyError.reason?.message ?? anyError.reason) : null);
setLastUpdated(new Date());
} catch (e) {
if (!mounted.current) return;
setError(e instanceof Error ? e.message : String(e));
} finally {
if (mounted.current) setLoading(false);
}
}, []);
useEffect(() => {
mounted.current = true;
void tick();
const id = setInterval(tick, pollMs);
return () => { mounted.current = false; clearInterval(id); };
}, [tick, pollMs]);
return { health, latestBlock, stats, loading, error, lastUpdated, refresh: () => { void tick(); } };
}