Spaces:
Sleeping
Sleeping
Commit ·
f2532ee
1
Parent(s): feee3c2
chore(config): move hardcoded configurations from data_fetcher.py to config.py
Browse filesFiles changed (2)
- config.py+39
- core/data_fetcher.py+124-85
123456789101112131415161718192021222324252627282930313233343536373839# config.pyimport os# ------------------ Database Config ------------------DB_DIR = "database"DB_PATH = os.path.join(DB_DIR, "kryptrix.db")# ------------------ Token Config ------------------# Central repository for all tokens the system will track.# Adding a new token here is all that's needed for the fetcher to pick it up.TOKENS = [{"symbol": "BTC","name": "Bitcoin","binance_id": "BTCUSDT","coingecko_id": "bitcoin","uniswap_id": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599" # This is WBTC for Uniswap},{"symbol": "ETH","name": "Ethereum","binance_id": "ETHUSDT","coingecko_id": "ethereum","uniswap_id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" # This is WETH for Uniswap},# Add more tokens here in the future]# ------------------ API & Fetcher Config ------------------MAX_RETRIES = 3RETRY_DELAY = 2 # secondsMAX_CONCURRENT_REQUESTS = 10 # Throttling semaphore limit# API EndpointsBINANCE_API_URL = "https://api.binance.com/api/v3"COINGECKO_API_URL = "https://api.coingecko.com/api/v3"UNISWAP_V3_SUBGRAPH_URL = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3"
4 unmodified lines5678910111213141516171819202122232425262728293031323334353637383940413 unmodified lines454647484950515253545556575859606113 unmodified lines75767778798081828384853 unmodified lines89909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092104 unmodified linesfrom sqlite3 import Errorfrom datetime import datetimeimport jsonimport osfrom typing import Dict, List, Optional# ---------------- Database Config ----------------DB_DIR = "database"DB_PATH = os.path.join(DB_DIR, "kryptrix.db")# ---------------- Token Config ----------------TOKENS = [{"symbol": "BTC","name": "Bitcoin","binance_id": "BTCUSDT","coingecko_id": "bitcoin","uniswap_id": "0x123..."},{"symbol": "ETH","name": "Ethereum","binance_id": "ETHUSDT","coingecko_id": "ethereum","uniswap_id": "0x456..."},]MAX_RETRIES = 3RETRY_DELAY = 2 # secondsMAX_CONCURRENT_REQUESTS = 10 # throttling semaphore# ---------------- Database Helpers ----------------def create_connection(db_path=DB_PATH) -> Optional[sqlite3.Connection]:os.makedirs(os.path.dirname(db_path), exist_ok=True)try:conn = sqlite3.connect(db_path)3 unmodified linesprint(f"[DB ERROR] {e}")return Nonedef insert_price(conn: sqlite3.Connection, token_id: int, price_usd: float, volume_24h: float, raw_data: dict):try:cursor = conn.cursor()cursor.execute("INSERT INTO prices (token_id, price_usd, volume_24h, raw_data) VALUES (?, ?, ?, ?)",(token_id, price_usd, volume_24h, json.dumps(raw_data)))conn.commit()except Error as e:print(f"[DB INSERT ERROR] {e}")def log_api_health(conn: sqlite3.Connection, source: str, status: str, response_time_ms: float = None,error_message: str = None, raw_data: dict = None):try:13 unmodified linesresult = cursor.fetchone()if result:return result[0]raise ValueError(f"Token {symbol} not found in DB.")except Error as e:print(f"[DB GET TOKEN ERROR] {e}")raise# ---------------- Fetch Helpers ----------------async def fetch_with_retry(client: httpx.AsyncClient, url: str, source_name: str, method="GET", payload=None):for attempt in range(1, MAX_RETRIES + 1):3 unmodified linesresp = await client.post(url, json=payload, timeout=10.0)else:resp = await client.get(url, timeout=10.0)resp.raise_for_status()elapsed_ms = (datetime.utcnow() - start_time).total_seconds() * 1000return resp.json(), elapsed_msexcept Exception as e:elapsed_ms = (datetime.utcnow() - start_time).total_seconds() * 1000if attempt < MAX_RETRIES:await asyncio.sleep(RETRY_DELAY)else:return {"error": str(e)}, elapsed_ms# ---------------- Exchange Fetchers ----------------async def fetch_binance(client: httpx.AsyncClient, token: Dict, conn: sqlite3.Connection, semaphore: asyncio.Semaphore):async with semaphore:data, elapsed_ms = await fetch_with_retry(client, f"https://api.binance.com/api/v3/ticker/24hr?symbol={token['binance_id']}", "Binance")try:token_id = get_token_id(conn, token["symbol"])if "error" not in data:insert_price(conn, token_id, float(data["lastPrice"]), float(data["volume"]), data)log_api_health(conn, "Binance", "success", elapsed_ms, None, data)else:log_api_health(conn, "Binance", "error", elapsed_ms, data["error"])except Exception as e:log_api_health(conn, "Binance", "error", elapsed_ms, str(e))return dataasync def fetch_coingecko(client: httpx.AsyncClient, token: Dict, conn: sqlite3.Connection, semaphore: asyncio.Semaphore):async with semaphore:data, elapsed_ms = await fetch_with_retry(client,f"https://api.coingecko.com/api/v3/simple/price?ids={token['coingecko_id']}&vs_currencies=usd&include_24hr_vol=true","CoinGecko")try:token_id = get_token_id(conn, token["symbol"])if "error" not in data and token["coingecko_id"] in data:price = data[token["coingecko_id"]]["usd"]volume = data[token["coingecko_id"]]["usd_24h_vol"]insert_price(conn, token_id, price, volume, data)log_api_health(conn, "CoinGecko", "success", elapsed_ms, None, data)else:log_api_health(conn, "CoinGecko", "error", elapsed_ms, data.get("error", "Missing token data"))except Exception as e:log_api_health(conn, "CoinGecko", "error", elapsed_ms, str(e))return dataasync def fetch_eth_usd(client: httpx.AsyncClient, semaphore: asyncio.Semaphore) -> float:async with semaphore:data, _ = await fetch_with_retry(client, "https://api.binance.com/api/v3/ticker/price?symbol=ETHUSDT", "Binance-ETH")if "price" in data:return float(data["price"])return 2000.0 # fallbackasync def fetch_uniswap_v3(client: httpx.AsyncClient, token: Dict, conn: sqlite3.Connection, eth_usd: float, semaphore: asyncio.Semaphore):async with semaphore:query = {"query": f"""{{pools(first: 1, orderBy: totalValueLockedUSD, orderDirection: desc,where: {{ token0: "{token['uniswap_id'].lower()}" }}) {{token0Pricetoken1PricevolumeUSD}}}}"""}start_time = datetime.utcnow()try:resp = await client.post("https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3",json=query,timeout=10.0)resp.raise_for_status()data = resp.json()elapsed_ms = (datetime.utcnow() - start_time).total_seconds() * 1000token_id = get_token_id(conn, token["symbol"])pools = data.get("data", {}).get("pools", [])if not pools:log_api_health(conn, "UniswapV3", "error", elapsed_ms, "No pools found")return {"error": "No pools found"}pool = pools[0]derived_eth = float(pool.get("token1Price", 0))volume_usd = float(pool.get("volumeUSD", 0))price_usd = derived_eth * eth_usdinsert_price(conn, token_id, price_usd, volume_usd, data)log_api_health(conn, "UniswapV3", "success", elapsed_ms, None, data)return dataexcept Exception as e:elapsed_ms = (datetime.utcnow() - start_time).total_seconds() * 1000log_api_health(conn, "UniswapV3", "error", elapsed_ms, str(e))return {"error": str(e)}# ---------------- Runner ----------------async def fetch_all_prices(tokens: List[Dict] = TOKENS):conn = create_connection()if not conn:print("Failed to connect to DB")returnsemaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)async with httpx.AsyncClient() as client:eth_usd = await fetch_eth_usd(client, semaphore)tasks = []for token in tokens:tasks.append(fetch_binance(client, token, conn, semaphore))tasks.append(fetch_coingecko(client, token, conn, semaphore))tasks.append(fetch_uniswap_v3(client, token, conn, eth_usd, semaphore))results = await asyncio.gather(*tasks)conn.close()return results# ---------------- Main ----------------if __name__ == "__main__":asyncio.run(fetch_all_prices())4 unmodified lines56789101112131415161718192021222324252627282930313233343 unmodified lines383940414243444546474849505152535455565713 unmodified lines7172737475767778798081823 unmodified lines86878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482494 unmodified linesfrom sqlite3 import Errorfrom datetime import datetimeimport jsonfrom typing import Dict, List, Optionalimport sysimport os# Add project root to the Python path to allow importing from configsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))# Import configurations from the central config filefrom config import (DB_PATH,TOKENS,MAX_RETRIES,RETRY_DELAY,MAX_CONCURRENT_REQUESTS,BINANCE_API_URL,COINGECKO_API_URL,UNISWAP_V3_SUBGRAPH_URL,)# ---------------- Database Helpers ----------------def create_connection(db_path=DB_PATH) -> Optional[sqlite3.Connection]:# This function is now slightly redundant with database_setup.py,# but it's fine to keep it here for this module's self-sufficiency.# In a larger app, you might create a shared db connection manager.os.makedirs(os.path.dirname(db_path), exist_ok=True)try:conn = sqlite3.connect(db_path)3 unmodified linesprint(f"[DB ERROR] {e}")return Nonedef insert_price(conn: sqlite3.Connection, token_id: int, source: str, price_usd: float, volume_24h: float, raw_data: dict):# Added 'source' to the prices table to track where the data came fromtry:cursor = conn.cursor()cursor.execute("INSERT INTO prices (token_id, source, price_usd, volume_24h, raw_data) VALUES (?, ?, ?, ?, ?)",(token_id, source, price_usd, volume_24h, json.dumps(raw_data)))conn.commit()except Error as e:print(f"[DB INSERT ERROR] {e}")# The rest of the database helpers (log_api_health, get_token_id) remain the same...def log_api_health(conn: sqlite3.Connection, source: str, status: str, response_time_ms: float = None,error_message: str = None, raw_data: dict = None):try:13 unmodified linesresult = cursor.fetchone()if result:return result[0]raise ValueError(f"Token {symbol} not found in DB. Did you run scripts/seed_db.py?")except Error as e:print(f"[DB GET TOKEN ERROR] {e}")raise# ---------------- Fetch Helpers ----------------async def fetch_with_retry(client: httpx.AsyncClient, url: str, source_name: str, method="GET", payload=None):for attempt in range(1, MAX_RETRIES + 1):3 unmodified linesresp = await client.post(url, json=payload, timeout=10.0)else:resp = await client.get(url, timeout=10.0)resp.raise_for_status()elapsed_ms = (datetime.utcnow() - start_time).total_seconds() * 1000return resp.json(), elapsed_msexcept httpx.HTTPStatusError as e:error_message = f"HTTP Error: {e.response.status_code} for URL: {e.request.url}"print(f"[{source_name} ATTEMPT {attempt}/{MAX_RETRIES}] {error_message}")if attempt == MAX_RETRIES:return {"error": error_message, "raw_response": e.response.text}, (datetime.utcnow() - start_time).total_seconds() * 1000except Exception as e:error_message = str(e)print(f"[{source_name} ATTEMPT {attempt}/{MAX_RETRIES}] General Error: {error_message}")if attempt == MAX_RETRIES:return {"error": error_message}, (datetime.utcnow() - start_time).total_seconds() * 1000await asyncio.sleep(RETRY_DELAY)# ---------------- Exchange Fetchers ----------------async def fetch_binance(client: httpx.AsyncClient, token: Dict, conn: sqlite3.Connection, semaphore: asyncio.Semaphore):async with semaphore:url = f"{BINANCE_API_URL}/ticker/24hr?symbol={token['binance_id']}"data, elapsed_ms = await fetch_with_retry(client, url, "Binance")try:token_id = get_token_id(conn, token["symbol"])if "error" not in data:insert_price(conn, token_id, "Binance", float(data["lastPrice"]), float(data["quoteVolume"]), data)log_api_health(conn, "Binance", "success", elapsed_ms, None, data)else:log_api_health(conn, "Binance", "error", elapsed_ms, data["error"], data.get("raw_response"))except Exception as e:log_api_health(conn, "Binance", "error", elapsed_ms, str(e))return dataasync def fetch_coingecko(client: httpx.AsyncClient, token: Dict, conn: sqlite3.Connection, semaphore: asyncio.Semaphore):async with semaphore:url = f"{COINGECKO_API_URL}/simple/price?ids={token['coingecko_id']}&vs_currencies=usd&include_24hr_vol=true"data, elapsed_ms = await fetch_with_retry(client, url, "CoinGecko")try:token_id = get_token_id(conn, token["symbol"])if "error" not in data and token["coingecko_id"] in data:price_data = data[token["coingecko_id"]]insert_price(conn, token_id, "CoinGecko", price_data["usd"], price_data["usd_24h_vol"], data)log_api_health(conn, "CoinGecko", "success", elapsed_ms, None, data)else:error_msg = data.get("error", "Token data not found in response")log_api_health(conn, "CoinGecko", "error", elapsed_ms, error_msg, data.get("raw_response"))except Exception as e:log_api_health(conn, "CoinGecko", "error", elapsed_ms, str(e))return dataasync def fetch_eth_usd(client: httpx.AsyncClient, semaphore: asyncio.Semaphore) -> float:async with semaphore:url = f"{BINANCE_API_URL}/ticker/price?symbol=ETHUSDT"data, _ = await fetch_with_retry(client, url, "Binance-ETH")if "price" in data:return float(data["price"])return 2000.0 # Fallbackasync def fetch_uniswap_v3(client: httpx.AsyncClient, token: Dict, conn: sqlite3.Connection, eth_usd: float, semaphore: asyncio.Semaphore):async with semaphore:# For Uniswap, we typically need to find pools against WETH or a stablecoin.# This query looks for the token against any other token, ordered by liquidity.# It's a simplification; a robust solution would be more complex.query = {"query": f"""{{pools(first: 5,orderBy: totalValueLockedUSD,orderDirection: desc,where: {{or: [{{ token0: "{token['uniswap_id'].lower()}" }},{{ token1: "{token['uniswap_id'].lower()}" }}]}}) {{token0 {{ symbol, derivedETH }}token1 {{ symbol, derivedETH }}volumeUSDtoken0Pricetoken1Price}}}}"""}data, elapsed_ms = await fetch_with_retry(client, UNISWAP_V3_SUBGRAPH_URL, "UniswapV3", method="POST", payload=query)try:token_id = get_token_id(conn, token["symbol"])if "error" not in data and data.get("data", {}).get("pools"):pools = data["data"]["pools"]# A simple strategy: find the first pool and derive price.# A better strategy would be to check if the pair is with WETH/USDC.pool = pools[0]price_usd = 0if pool['token0']['symbol'].upper() in [token['symbol'], 'W'+token['symbol']]:price_usd = float(pool['token0']['derivedETH']) * eth_usdelse:price_usd = float(pool['token1']['derivedETH']) * eth_usdvolume_usd = float(pool["volumeUSD"])insert_price(conn, token_id, "UniswapV3", price_usd, volume_usd, data)log_api_health(conn, "UniswapV3", "success", elapsed_ms, None, data)else:error_msg = data.get("error", "No pools found or data error")log_api_health(conn, "UniswapV3", "error", elapsed_ms, error_msg, data.get("raw_response"))except Exception as e:log_api_health(conn, "UniswapV3", "error", elapsed_ms, str(e))return data# ---------------- Runner ----------------async def fetch_all_prices(tokens: List[Dict] = TOKENS):conn = create_connection()if not conn:print("Failed to connect to DB. Fetcher cannot run.")returnsemaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)async with httpx.AsyncClient() as client:# Fetch ETH price first as it's a dependency for Uniswapeth_usd = await fetch_eth_usd(client, semaphore)tasks = []for token in tokens:# Add each fetching task to the listif token.get("binance_id"):tasks.append(fetch_binance(client, token, conn, semaphore))if token.get("coingecko_id"):tasks.append(fetch_coingecko(client, token, conn, semaphore))if token.get("uniswap_id") and token['symbol'] != 'ETH': # Don't fetch ETH against itself on Uniswaptasks.append(fetch_uniswap_v3(client, token, conn, eth_usd, semaphore))print(f"Starting to fetch data for {len(tasks)} tasks...")results = await asyncio.gather(*tasks, return_exceptions=True)print("All fetching tasks completed.")conn.close()return results# ---------------- Main ----------------if __name__ == "__main__":print("Running data fetcher as a standalone script...")# Add a column to prices table to track source# This is a one-time migration. A better approach is using a migration tool.try:conn = create_connection()conn.execute("ALTER TABLE prices ADD COLUMN source TEXT;")conn.commit()print("Added 'source' column to 'prices' table.")conn.close()except sqlite3.OperationalError:# Column likely already exists, which is fine.passexcept Exception as e:print(f"Could not alter table: {e}")asyncio.run(fetch_all_prices())print("Data fetcher run complete.")
config.py+39
core/data_fetcher.py+124-85