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 
 
 
 
 
 
 
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5bf75f
8b7b267
 
 
d5bf75f
 
8b7b267
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
d5bf75f
 
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5bf75f
 
 
 
 
 
 
 
8b7b267
 
d5bf75f
 
 
 
 
 
 
 
 
 
 
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env python3
"""
CoinGecko API Client - REAL DATA ONLY with CACHING and RATE LIMIT PROTECTION
Fetches real cryptocurrency market data from CoinGecko
NO MOCK DATA - All data from live CoinGecko API
ENHANCED: 5-minute mandatory cache, exponential backoff, auto-blacklist on 429
"""

import httpx
import logging
import time
import asyncio
from typing import Dict, Any, List, Optional
from datetime import datetime, timedelta
from fastapi import HTTPException

logger = logging.getLogger(__name__)

# Cache and rate limit management
_cache: Dict[str, Dict[str, Any]] = {}
_last_request_time = 0.0
_min_request_interval = 10.0  # Minimum 10 seconds between requests
_blacklist_until = 0.0  # Blacklist timestamp
_consecutive_429s = 0  # Track consecutive 429 errors


def _get_cache_key(method: str, **kwargs) -> str:
    """Generate cache key from method and parameters"""
    params_str = "_".join(f"{k}={v}" for k, v in sorted(kwargs.items()))
    return f"{method}:{params_str}"


def _get_from_cache(cache_key: str, ttl: int = 300) -> Optional[Any]:
    """Get data from cache if not expired (default 5 min TTL)"""
    global _cache
    if cache_key in _cache:
        cached_data = _cache[cache_key]
        if time.time() - cached_data["timestamp"] < ttl:
            logger.info(f"βœ… CoinGecko: Cache hit for {cache_key}")
            return cached_data["data"]
        else:
            # Expired, remove from cache
            del _cache[cache_key]
    return None


def _set_cache(cache_key: str, data: Any):
    """Set data in cache with current timestamp"""
    global _cache
    _cache[cache_key] = {
        "data": data,
        "timestamp": time.time()
    }


def _check_rate_limit() -> bool:
    """Check if we should rate limit (return True if we should wait)"""
    global _last_request_time, _min_request_interval, _blacklist_until
    
    current_time = time.time()
    
    # Check blacklist
    if current_time < _blacklist_until:
        logger.warning(f"πŸ”΄ CoinGecko: Blacklisted until {datetime.fromtimestamp(_blacklist_until).strftime('%H:%M:%S')}")
        return True
    
    # Check minimum interval
    time_since_last = current_time - _last_request_time
    if time_since_last < _min_request_interval:
        wait_time = _min_request_interval - time_since_last
        logger.warning(f"⏳ CoinGecko: Rate limiting - wait {wait_time:.1f}s")
        return True
    
    return False


async def _wait_for_rate_limit():
    """Wait until rate limit allows next request"""
    global _last_request_time, _min_request_interval
    
    current_time = time.time()
    time_since_last = current_time - _last_request_time
    
    if time_since_last < _min_request_interval:
        wait_time = _min_request_interval - time_since_last
        logger.info(f"⏳ CoinGecko: Waiting {wait_time:.1f}s before next request")
        await asyncio.sleep(wait_time)


def _update_last_request_time():
    """Update the last request timestamp"""
    global _last_request_time
    _last_request_time = time.time()


def _handle_429_error():
    """Handle 429 rate limit error with exponential backoff"""
    global _consecutive_429s, _blacklist_until, _min_request_interval
    
    _consecutive_429s += 1
    
    if _consecutive_429s >= 3:
        # Blacklist for 10 minutes after 3 consecutive 429s
        _blacklist_until = time.time() + 600  # 10 minutes
        logger.error(f"πŸ”΄ CoinGecko: {_consecutive_429s} consecutive 429s - BLACKLISTED for 10 minutes")
    else:
        # Exponential backoff
        backoff_time = min(60 * (2 ** _consecutive_429s), 300)  # Max 5 minutes
        _blacklist_until = time.time() + backoff_time
        logger.warning(f"⚠️ CoinGecko: 429 rate limit - backing off for {backoff_time}s")


def _reset_429_counter():
    """Reset 429 counter on successful request"""
    global _consecutive_429s
    if _consecutive_429s > 0:
        logger.info(f"βœ… CoinGecko: Successful request - resetting 429 counter (was {_consecutive_429s})")
        _consecutive_429s = 0


class CoinGeckoClient:
    """
    Real CoinGecko API Client with CACHING and RATE LIMIT PROTECTION
    Primary source for real-time cryptocurrency market prices
    ENHANCED: 5-minute mandatory cache, exponential backoff, auto-blacklist on 429
    """
    
    def __init__(self):
        self.base_url = "https://api.coingecko.com/api/v3"
        self.timeout = 15.0
        
        # Symbol to CoinGecko ID mapping
        self.symbol_to_id = {
            "BTC": "bitcoin",
            "ETH": "ethereum",
            "BNB": "binancecoin",
            "XRP": "ripple",
            "ADA": "cardano",
            "DOGE": "dogecoin",
            "SOL": "solana",
            "TRX": "tron",
            "DOT": "polkadot",
            "MATIC": "matic-network",
            "LTC": "litecoin",
            "SHIB": "shiba-inu",
            "AVAX": "avalanche-2",
            "UNI": "uniswap",
            "LINK": "chainlink",
            "ATOM": "cosmos",
            "XLM": "stellar",
            "ETC": "ethereum-classic",
            "XMR": "monero",
            "BCH": "bitcoin-cash"
        }
        
        # Reverse mapping
        self.id_to_symbol = {v: k for k, v in self.symbol_to_id.items()}
    
    def _symbol_to_coingecko_id(self, symbol: str) -> str:
        """Convert crypto symbol to CoinGecko coin ID"""
        symbol = symbol.upper().replace("USDT", "").replace("USD", "")
        return self.symbol_to_id.get(symbol, symbol.lower())
    
    def _coingecko_id_to_symbol(self, coin_id: str) -> str:
        """Convert CoinGecko coin ID to symbol"""
        return self.id_to_symbol.get(coin_id, coin_id.upper())
    
    async def get_market_prices(
        self,
        symbols: Optional[List[str]] = None,
        limit: int = 100
    ) -> List[Dict[str, Any]]:
        """
        Fetch REAL market prices from CoinGecko with CACHING and RATE LIMITING
        
        Args:
            symbols: List of crypto symbols (e.g., ["BTC", "ETH"])
            limit: Maximum number of results
        
        Returns:
            List of real market data
        
        ENHANCED: 5-minute mandatory cache, rate limiting, exponential backoff
        """
        # Generate cache key
        cache_key = _get_cache_key("market_prices", symbols=str(symbols), limit=limit)
        
        # Check cache first (5-minute TTL)
        cached_data = _get_from_cache(cache_key, ttl=300)
        if cached_data is not None:
            return cached_data
        
        # Check if blacklisted
        if _check_rate_limit():
            # Return cached data even if expired, or raise error
            if cache_key in _cache:
                logger.warning("πŸ”΄ CoinGecko: Rate limited - returning stale cache")
                return _cache[cache_key]["data"]
            else:
                raise HTTPException(
                    status_code=429,
                    detail="CoinGecko rate limited - no cached data available"
                )
        
        try:
            # Wait for rate limit if needed
            await _wait_for_rate_limit()
            
            async with httpx.AsyncClient(timeout=self.timeout) as client:
                if symbols:
                    # Get specific symbols using /simple/price endpoint
                    coin_ids = [self._symbol_to_coingecko_id(s) for s in symbols]
                    
                    response = await client.get(
                        f"{self.base_url}/simple/price",
                        params={
                            "ids": ",".join(coin_ids),
                            "vs_currencies": "usd",
                            "include_24hr_change": "true",
                            "include_24hr_vol": "true",
                            "include_market_cap": "true"
                        }
                    )
                    response.raise_for_status()
                    data = response.json()
                    
                    # Transform to standard format
                    prices = []
                    for coin_id, coin_data in data.items():
                        symbol = self._coingecko_id_to_symbol(coin_id)
                        prices.append({
                            "symbol": symbol,
                            "name": symbol,  # CoinGecko simple/price doesn't include name
                            "price": coin_data.get("usd", 0),
                            "change24h": coin_data.get("usd_24h_change", 0),
                            "changePercent24h": coin_data.get("usd_24h_change", 0),
                            "volume24h": coin_data.get("usd_24h_vol", 0),
                            "marketCap": coin_data.get("usd_market_cap", 0),
                            "source": "coingecko",
                            "timestamp": int(datetime.utcnow().timestamp() * 1000)
                        })
                    
                    logger.info(f"βœ… CoinGecko: Fetched {len(prices)} real prices for specific symbols")
                    
                    # Update rate limit tracking
                    _update_last_request_time()
                    _reset_429_counter()
                    
                    # Cache the result
                    _set_cache(cache_key, prices)
                    
                    return prices
                
                else:
                    # Get top coins by market cap using /coins/markets endpoint
                    response = await client.get(
                        f"{self.base_url}/coins/markets",
                        params={
                            "vs_currency": "usd",
                            "order": "market_cap_desc",
                            "per_page": min(limit, 250),
                            "page": 1,
                            "sparkline": "false",
                            "price_change_percentage": "24h"
                        }
                    )
                    response.raise_for_status()
                    data = response.json()
                    
                    # Transform to standard format
                    prices = []
                    for coin in data:
                        prices.append({
                            "id": coin.get("id", ""),
                            "symbol": coin.get("symbol", "").upper(),
                            "name": coin.get("name", ""),
                            "image": coin.get("image", ""),  # CoinGecko provides real image URLs
                            "price": coin.get("current_price", 0),
                            "change24h": coin.get("price_change_24h", 0),
                            "changePercent24h": coin.get("price_change_percentage_24h", 0),
                            "volume24h": coin.get("total_volume", 0),
                            "marketCap": coin.get("market_cap", 0),
                            "market_cap_rank": coin.get("market_cap_rank", 0),
                            "circulating_supply": coin.get("circulating_supply", 0),
                            "total_supply": coin.get("total_supply", 0),
                            "max_supply": coin.get("max_supply", 0),
                            "ath": coin.get("ath", 0),
                            "atl": coin.get("atl", 0),
                            "source": "coingecko",
                            "timestamp": int(datetime.utcnow().timestamp() * 1000)
                        })
                    
                    logger.info(f"βœ… CoinGecko: Fetched {len(prices)} real market prices")
                    
                    # Update rate limit tracking
                    _update_last_request_time()
                    _reset_429_counter()
                    
                    # Cache the result
                    _set_cache(cache_key, prices)
                    
                    return prices
        
        except httpx.HTTPStatusError as e:
            if e.response.status_code == 429:
                # Handle 429 specifically
                _handle_429_error()
                
                # Try to return cached data even if expired
                if cache_key in _cache:
                    logger.warning("πŸ”΄ CoinGecko: 429 rate limit - returning stale cache")
                    return _cache[cache_key]["data"]
                
                raise HTTPException(
                    status_code=429,
                    detail="CoinGecko rate limited - please try again later"
                )
            
            logger.error(f"❌ CoinGecko API HTTP error: {e}")
            raise HTTPException(
                status_code=503,
                detail=f"CoinGecko API error: HTTP {e.response.status_code}"
            )
        
        except httpx.HTTPError as e:
            logger.error(f"❌ CoinGecko API HTTP error: {e}")
            raise HTTPException(
                status_code=503,
                detail=f"CoinGecko API temporarily unavailable: {str(e)}"
            )
        except Exception as e:
            logger.error(f"❌ CoinGecko API failed: {e}")
            raise HTTPException(
                status_code=503,
                detail=f"Failed to fetch real market data from CoinGecko: {str(e)}"
            )
    
    async def get_ohlcv(self, symbol: str, days: int = 7) -> Dict[str, Any]:
        """
        Fetch REAL OHLCV (price history) data from CoinGecko with CACHING
        
        Args:
            symbol: Cryptocurrency symbol (e.g., "BTC", "ETH")
            days: Number of days of historical data (1, 7, 14, 30, 90, 180, 365, max)
        
        Returns:
            Dict with OHLCV data
        
        ENHANCED: 5-minute cache, rate limiting
        """
        # Generate cache key
        cache_key = _get_cache_key("ohlcv", symbol=symbol, days=days)
        
        # Check cache first
        cached_data = _get_from_cache(cache_key, ttl=300)
        if cached_data is not None:
            return cached_data
        
        # Check if blacklisted
        if _check_rate_limit():
            if cache_key in _cache:
                logger.warning("πŸ”΄ CoinGecko OHLCV: Rate limited - returning stale cache")
                return _cache[cache_key]["data"]
            else:
                raise HTTPException(
                    status_code=429,
                    detail="CoinGecko rate limited - no cached data available"
                )
        
        try:
            await _wait_for_rate_limit()
            
            coin_id = self._symbol_to_coingecko_id(symbol)
            
            async with httpx.AsyncClient(timeout=self.timeout) as client:
                # Get market chart (OHLC) data
                response = await client.get(
                    f"{self.base_url}/coins/{coin_id}/market_chart",
                    params={
                        "vs_currency": "usd",
                        "days": str(days),
                        "interval": "daily" if days > 1 else "hourly"
                    }
                )
                response.raise_for_status()
                data = response.json()
                
                logger.info(f"βœ… CoinGecko: Fetched {days} days of OHLCV data for {symbol}")
                
                # Update rate limit tracking
                _update_last_request_time()
                _reset_429_counter()
                
                # Cache the result
                _set_cache(cache_key, data)
                
                return data
        
        except httpx.HTTPStatusError as e:
            if e.response.status_code == 429:
                _handle_429_error()
                if cache_key in _cache:
                    logger.warning("πŸ”΄ CoinGecko OHLCV: 429 - returning stale cache")
                    return _cache[cache_key]["data"]
                raise HTTPException(status_code=429, detail="CoinGecko rate limited")
            
            logger.error(f"❌ CoinGecko OHLCV API HTTP error: {e}")
            raise HTTPException(status_code=503, detail=f"CoinGecko OHLCV API error: HTTP {e.response.status_code}")
        
        except httpx.HTTPError as e:
            logger.error(f"❌ CoinGecko OHLCV API HTTP error: {e}")
            raise HTTPException(
                status_code=503,
                detail=f"CoinGecko OHLCV API unavailable: {str(e)}"
            )
        except Exception as e:
            logger.error(f"❌ CoinGecko OHLCV API failed: {e}")
            raise HTTPException(
                status_code=503,
                detail=f"Failed to fetch OHLCV data from CoinGecko: {str(e)}"
            )
    
    async def get_trending_coins(self, limit: int = 10) -> List[Dict[str, Any]]:
        """
        Fetch REAL trending coins from CoinGecko with CACHING
        
        Returns:
            List of real trending coins
        
        ENHANCED: 5-minute cache, rate limiting
        """
        # Generate cache key
        cache_key = _get_cache_key("trending", limit=limit)
        
        # Check cache first
        cached_data = _get_from_cache(cache_key, ttl=300)
        if cached_data is not None:
            return cached_data
        
        # Check if blacklisted
        if _check_rate_limit():
            if cache_key in _cache:
                logger.warning("πŸ”΄ CoinGecko trending: Rate limited - returning stale cache")
                return _cache[cache_key]["data"]
            else:
                raise HTTPException(status_code=429, detail="CoinGecko rate limited")
        
        try:
            await _wait_for_rate_limit()
            
            async with httpx.AsyncClient(timeout=self.timeout) as client:
                # Get trending coins
                response = await client.get(f"{self.base_url}/search/trending")
                response.raise_for_status()
                data = response.json()
                
                trending = []
                coins = data.get("coins", [])[:limit]
                
                # Get price data for trending coins
                if coins:
                    coin_ids = [coin["item"]["id"] for coin in coins]
                    
                    # Fetch current prices
                    price_response = await client.get(
                        f"{self.base_url}/simple/price",
                        params={
                            "ids": ",".join(coin_ids),
                            "vs_currencies": "usd",
                            "include_24hr_change": "true"
                        }
                    )
                    price_response.raise_for_status()
                    price_data = price_response.json()
                    
                    for idx, coin_obj in enumerate(coins):
                        coin = coin_obj["item"]
                        coin_id = coin["id"]
                        prices = price_data.get(coin_id, {})
                        
                        trending.append({
                            "symbol": coin.get("symbol", "").upper(),
                            "name": coin.get("name", ""),
                            "rank": idx + 1,
                            "price": prices.get("usd", 0),
                            "change24h": prices.get("usd_24h_change", 0),
                            "marketCapRank": coin.get("market_cap_rank", 0),
                            "source": "coingecko",
                            "timestamp": int(datetime.utcnow().timestamp() * 1000)
                        })
                
                logger.info(f"βœ… CoinGecko: Fetched {len(trending)} real trending coins")
                
                # Update rate limit tracking
                _update_last_request_time()
                _reset_429_counter()
                
                # Cache the result
                _set_cache(cache_key, trending)
                
                return trending
        
        except httpx.HTTPStatusError as e:
            if e.response.status_code == 429:
                _handle_429_error()
                if cache_key in _cache:
                    logger.warning("πŸ”΄ CoinGecko trending: 429 - returning stale cache")
                    return _cache[cache_key]["data"]
                raise HTTPException(status_code=429, detail="CoinGecko rate limited")
            
            logger.error(f"❌ CoinGecko trending API HTTP error: {e}")
            raise HTTPException(status_code=503, detail=f"CoinGecko trending API error: HTTP {e.response.status_code}")
        
        except httpx.HTTPError as e:
            logger.error(f"❌ CoinGecko trending API HTTP error: {e}")
            raise HTTPException(
                status_code=503,
                detail=f"CoinGecko trending API unavailable: {str(e)}"
            )
        except Exception as e:
            logger.error(f"❌ CoinGecko trending API failed: {e}")
            raise HTTPException(
                status_code=503,
                detail=f"Failed to fetch trending coins: {str(e)}"
            )


# Global instance
coingecko_client = CoinGeckoClient()


__all__ = ["CoinGeckoClient", "coingecko_client"]