-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_checker.py
69 lines (56 loc) · 2.2 KB
/
price_checker.py
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
import logging
from typing import Dict, Optional, List
from datetime import datetime, timedelta
from pycoingecko import CoinGeckoAPI
from config import config
class PriceChecker:
def __init__(self):
self.logger = logging.getLogger(__name__)
self.cg = CoinGeckoAPI()
self.price_cache: Dict[str, Dict] = {}
self.last_update: Optional[datetime] = None
async def get_prices(self, coin_ids: List[str]) -> Dict[str, float]:
"""Get current prices for multiple coins with caching"""
try:
result_prices = {}
# First try to get prices from cache
for coin_id in coin_ids:
if (self.last_update and
datetime.now() - self.last_update <= timedelta(seconds=config.PRICE_CACHE_TIME) and
coin_id in self.price_cache):
result_prices[coin_id] = self.price_cache[coin_id]['usd']
# If we got all prices from cache, return them
if len(result_prices) == len(coin_ids):
return result_prices
# Otherwise get fresh prices for all requested coins
prices = self.cg.get_price(
ids=coin_ids,
vs_currencies='usd'
)
# Update cache
self.price_cache = prices
self.last_update = datetime.now()
# Return all fresh prices
return {coin: data['usd'] for coin, data in prices.items()}
except Exception as e:
self.logger.error(f"Error fetching prices: {e}")
return {}
async def get_price(self, coin_id: str) -> Optional[float]:
"""Get current price for a single coin"""
try:
prices = await self.get_prices([coin_id])
return prices.get(coin_id)
except:
return None
@staticmethod
def format_price(price: float) -> str:
"""Format price with appropriate precision"""
if price < 0.01:
return f"${price:.8f}"
elif price < 1:
return f"${price:.4f}"
elif price < 100:
return f"${price:.2f}"
else:
return f"${price:,.2f}"
price_checker = PriceChecker()