/** * useDataBus — Unified DataBus Hook * ================================== * Every data fetch in the frontend routes through this hook. * Calls POST /api/v1/databus/fetch with typed data_type + params. * * Usage: * const { data, isLoading } = useDataBus('token_price', { mint: 'So111...' }); * const { data, isLoading } = useDataBus('wallet_tokens', { address: '0x...', network: 'eth-mainnet' }); * const { data, isLoading } = useDataBus('wallet_labels', { address: '0x...' }); * const { data, isLoading } = useDataBus('scanner', { address: '0x...', chain: 'solana' }); * const { data, isLoading } = useDataBus('rag_search', { query: 'honeypot patterns' }); * const { data, isLoading } = useDataBus('alerts', { limit: 20 }); * const { data, isLoading } = useDataBus('trending', { timeframe: '24h' }); * const { data, isLoading } = useDataBus('market_overview', {}); * const { data, isLoading } = useDataBus('holder_data', { mint: '0x...' }); * const { data, isLoading } = useDataBus('cross_chain', { address: '0x...' }); * const { data, isLoading } = useDataBus('tx_trace', { tx_hash: '0x...' }); * * 14 chains available — see /api/v1/databus/chains for full list. */ import { useQuery } from '@tanstack/react-query'; import { api } from '../services/api'; import { useAppStore } from '../store/appStore'; type DataBusChain = | 'token_price' | 'market_overview' | 'trending' | 'alerts' | 'news' | 'wallet_labels' | 'wallet_tokens' | 'wallet_nfts' | 'token_metadata' | 'scanner' | 'rag_search' | 'holder_data' | 'cross_chain' | 'tx_trace'; interface DataBusParams { // Token mint?: string; token?: string; contract?: string; // Wallet address?: string; wallet?: string; // Chain / Network chain?: string; network?: string; // Search query?: string; collection?: string; // Pagination limit?: number; offset?: number; // Time timeframe?: string; // Transaction tx_hash?: string; // Misc [key: string]: any; } interface DataBusResult { data_type: string; provider: string; cached: boolean; latency_ms: number; result: T; metadata?: { tier: string; credits_remaining?: number; rate_limit_remaining?: number; }; } /** * Core DataBus hook — fetch any chain by type. * staleTime aligns with DataBus cache TTLs (price=5min, holders=1h, trace=24h). */ export function useDataBus( dataType: DataBusChain | null, params: DataBusParams = {}, options?: { enabled?: boolean; refetchInterval?: number; staleTime?: number; forceFresh?: boolean; } ) { const user = useAppStore((state) => state.user); const tier = user?.tier || 'FREE'; return useQuery>({ queryKey: ['databus', dataType, params], queryFn: async () => { if (!dataType) return null; const result = await api.databusFetch(dataType, { ...params, consumer_type: 'authenticated', x402_tier: tier, ...(options?.forceFresh ? { force_fresh: true } : {}), }); return result; }, enabled: !!dataType && (options?.enabled !== false), staleTime: options?.staleTime ?? getDefaultStaleTime(dataType), gcTime: 10 * 60 * 1000, refetchInterval: options?.refetchInterval, }); } /** Smart defaults matching DataBus cache TTLs */ function getDefaultStaleTime(dataType: DataBusChain | null): number { switch (dataType) { case 'token_price': case 'market_overview': case 'trending': return 5 * 60 * 1000; // 5 min case 'holder_data': case 'wallet_tokens': case 'wallet_nfts': case 'token_metadata': case 'cross_chain': return 60 * 60 * 1000; // 1 hour case 'tx_trace': return 24 * 60 * 60 * 1000; // 24 hours case 'alerts': case 'news': return 60 * 1000; // 1 min default: return 2 * 60 * 1000; // 2 min } } /** * Convenience: fetch multiple chains at once. */ export function useDataBusBatch( queries: Array<{ data_type: DataBusChain; params?: DataBusParams }>, options?: { enabled?: boolean; refetchInterval?: number } ) { const user = useAppStore((state) => state.user); const tier = user?.tier || 'FREE'; return useQuery[]>({ queryKey: ['databus', 'batch', queries], queryFn: async () => { const results = await api.databusBatch( queries.map((q) => ({ data_type: q.data_type, params: { ...q.params, consumer_type: 'authenticated', x402_tier: tier }, })) ); return results; }, enabled: options?.enabled !== false && queries.length > 0, staleTime: 60 * 1000, gcTime: 5 * 60 * 1000, refetchInterval: options?.refetchInterval, }); } /** * Fetch available chains + their status. */ export function useDataBusChains() { return useQuery({ queryKey: ['databus', 'chains'], queryFn: () => api.databusChains(), staleTime: 10 * 60 * 1000, gcTime: 30 * 60 * 1000, }); } /** * Health + capacity check. */ export function useDataBusHealth() { return useQuery({ queryKey: ['databus', 'health'], queryFn: () => api.databusHealth(), refetchInterval: 60000, staleTime: 30000, }); }