File size: 7,023 Bytes
f568a4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { useState, useEffect, useCallback, useMemo } from 'react';
import { supabase } from '@/integrations/supabase/client';
import binancePairs from '@/data/binance-pairs.json';

export interface AssetPrice {
  symbol: string;
  base: string;
  quote: string;
  price: number;
  volume: number;
  change: number;
  volatility: number;
  exchange: 'binance' | 'kraken' | 'alpaca' | 'capital';
  dataSource: 'LIVE' | 'CACHED' | 'STALE' | 'NO_DATA';
  lastUpdate: number;
  priceVariance?: number; // Cross-exchange variance
}

export interface AssetPriceListStats {
  totalAssets: number;
  liveAssets: number;
  cachedAssets: number;
  staleAssets: number;
  noDataAssets: number;
  avgVolatility: number;
  topGainer: AssetPrice | null;
  topLoser: AssetPrice | null;
  topVolume: AssetPrice | null;
  dataFreshness: number; // 0-100%
}

export interface UseAssetPriceListResult {
  assets: AssetPrice[];
  stats: AssetPriceListStats;
  isLoading: boolean;
  lastRefresh: Date | null;
  refresh: () => Promise<void>;
  getAsset: (symbol: string) => AssetPrice | undefined;
  searchAssets: (query: string) => AssetPrice[];
  sortAssets: (by: 'symbol' | 'price' | 'change' | 'volume' | 'volatility') => AssetPrice[];
}

const STALE_THRESHOLD_MS = 30000; // 30 seconds
const CACHE_THRESHOLD_MS = 10000; // 10 seconds

export const useAssetPriceList = (): UseAssetPriceListResult => {
  const [assets, setAssets] = useState<AssetPrice[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [lastRefresh, setLastRefresh] = useState<Date | null>(null);

  // Initialize from static binance-pairs.json
  const initialAssets = useMemo<AssetPrice[]>(() => {
    return binancePairs.pairs.map((pair: any) => ({
      symbol: pair.symbol,
      base: pair.base,
      quote: pair.quote,
      price: pair.price || 0,
      volume: pair.volume || 0,
      change: pair.change || 0,
      volatility: pair.volatility || 0,
      exchange: 'binance' as const,
      dataSource: 'CACHED' as const,
      lastUpdate: new Date(binancePairs.timestamp).getTime(),
    }));
  }, []);

  const refresh = useCallback(async () => {
    setIsLoading(true);
    try {
      // Fetch live tickers from edge function
      const { data, error } = await supabase.functions.invoke('fetch-all-tickers', {
        body: { 
          exchanges: ['binance'],
          limit: 200 // Top 200 by volume
        }
      });

      if (error) {
        console.error('[useAssetPriceList] Error fetching tickers:', error);
        // Fall back to initial cached data
        setAssets(initialAssets);
        return;
      }

      const now = Date.now();
      const liveTickerMap = new Map<string, any>();
      
      if (data?.tickers) {
        data.tickers.forEach((ticker: any) => {
          liveTickerMap.set(ticker.symbol, ticker);
        });
      }

      // Merge live data with static pairs
      const mergedAssets: AssetPrice[] = initialAssets.map(asset => {
        const liveTicker = liveTickerMap.get(asset.symbol);
        
        if (liveTicker) {
          const tickerTime = liveTicker.timestamp || now;
          const age = now - tickerTime;
          
          let dataSource: AssetPrice['dataSource'] = 'LIVE';
          if (age > STALE_THRESHOLD_MS) {
            dataSource = 'STALE';
          } else if (age > CACHE_THRESHOLD_MS) {
            dataSource = 'CACHED';
          }

          return {
            ...asset,
            price: liveTicker.price || asset.price,
            volume: liveTicker.volume || asset.volume,
            change: liveTicker.priceChangePercent || asset.change,
            volatility: liveTicker.volatility || asset.volatility,
            dataSource,
            lastUpdate: tickerTime,
          };
        }
        
        return {
          ...asset,
          dataSource: 'NO_DATA' as const,
        };
      });

      // Sort by volume (highest first)
      mergedAssets.sort((a, b) => b.volume - a.volume);
      
      setAssets(mergedAssets);
      setLastRefresh(new Date());
    } catch (error) {
      console.error('[useAssetPriceList] Exception:', error);
      setAssets(initialAssets);
    } finally {
      setIsLoading(false);
    }
  }, [initialAssets]);

  // Initial load and periodic refresh
  useEffect(() => {
    // Set initial cached data immediately
    setAssets(initialAssets);
    
    // Then fetch live data
    refresh();
    
    // Refresh every 30 seconds
    const interval = setInterval(refresh, 30000);
    return () => clearInterval(interval);
  }, [initialAssets, refresh]);

  // Calculate stats
  const stats = useMemo<AssetPriceListStats>(() => {
    const liveAssets = assets.filter(a => a.dataSource === 'LIVE').length;
    const cachedAssets = assets.filter(a => a.dataSource === 'CACHED').length;
    const staleAssets = assets.filter(a => a.dataSource === 'STALE').length;
    const noDataAssets = assets.filter(a => a.dataSource === 'NO_DATA').length;
    
    const avgVolatility = assets.length > 0
      ? assets.reduce((sum, a) => sum + a.volatility, 0) / assets.length
      : 0;

    const sortedByChange = [...assets].sort((a, b) => b.change - a.change);
    const sortedByVolume = [...assets].sort((a, b) => b.volume - a.volume);

    const dataFreshness = assets.length > 0
      ? ((liveAssets + cachedAssets) / assets.length) * 100
      : 0;

    return {
      totalAssets: assets.length,
      liveAssets,
      cachedAssets,
      staleAssets,
      noDataAssets,
      avgVolatility,
      topGainer: sortedByChange[0] || null,
      topLoser: sortedByChange[sortedByChange.length - 1] || null,
      topVolume: sortedByVolume[0] || null,
      dataFreshness,
    };
  }, [assets]);

  const getAsset = useCallback((symbol: string): AssetPrice | undefined => {
    return assets.find(a => a.symbol.toLowerCase() === symbol.toLowerCase());
  }, [assets]);

  const searchAssets = useCallback((query: string): AssetPrice[] => {
    const q = query.toLowerCase();
    return assets.filter(a => 
      a.symbol.toLowerCase().includes(q) ||
      a.base.toLowerCase().includes(q) ||
      a.quote.toLowerCase().includes(q)
    );
  }, [assets]);

  const sortAssets = useCallback((by: 'symbol' | 'price' | 'change' | 'volume' | 'volatility'): AssetPrice[] => {
    return [...assets].sort((a, b) => {
      switch (by) {
        case 'symbol': return a.symbol.localeCompare(b.symbol);
        case 'price': return b.price - a.price;
        case 'change': return b.change - a.change;
        case 'volume': return b.volume - a.volume;
        case 'volatility': return b.volatility - a.volatility;
        default: return 0;
      }
    });
  }, [assets]);

  return {
    assets,
    stats,
    isLoading,
    lastRefresh,
    refresh,
    getAsset,
    searchAssets,
    sortAssets,
  };
};