mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
25 lines
754 B
TypeScript
25 lines
754 B
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import { fetchUTXOs, type UTXO } from '@/lib/bitcoin';
|
|
|
|
/**
|
|
* Hook to fetch and manage Bitcoin balance from UTXOs
|
|
*/
|
|
export function useBitcoinBalance(address: string | null) {
|
|
return useQuery({
|
|
queryKey: ['bitcoin-balance', address],
|
|
queryFn: async (): Promise<{ balance: number; utxos: UTXO[] }> => {
|
|
if (!address) {
|
|
return { balance: 0, utxos: [] };
|
|
}
|
|
|
|
const utxos = await fetchUTXOs(address);
|
|
const balance = utxos.reduce((sum, utxo) => sum + utxo.value, 0);
|
|
|
|
return { balance, utxos };
|
|
},
|
|
enabled: !!address,
|
|
refetchInterval: 30000, // Refetch every 30 seconds
|
|
staleTime: 10000, // Consider data stale after 10 seconds
|
|
});
|
|
}
|