Spaces:
Sleeping
Sleeping
Commit ·
66eb00a
1
Parent(s): 85d5c60
Update Unisawp v3 Subgraph URL
Browse filesFiles changed (3)
35 unmodified lines3637383935 unmodified lines# API EndpointsBINANCE_API_URL = "https://api.binance.com/api/v3"COINGECKO_API_URL = "https://api.coingecko.com/api/v3"UNISWAP_V3_SUBGRAPH_URL = "https://gateway-mainnet.network.thegraph.com/api/35630321521469085351478111953114/subgraphs/id/0x9bde7bf4d5b130c05342e71850165c27514a683a"35 unmodified lines3637383935 unmodified lines# 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.studio.thegraph.com/query/48211/uniswap-v3/0.0.5"
146 unmodified lines14714814915015115215315415519 unmodified lines1751761771781791801811821831841851861871881891901911921931941951961971981997 unmodified lines207208209210211212213214215216217218219220221222223224225146 unmodified linesasync 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"""{{19 unmodified lines}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))7 unmodified linesreturnsemaphore = 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)146 unmodified lines14714814915015115219 unmodified lines1721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012027 unmodified lines210211212213214215216217218219220221222223224225226146 unmodified linesasync def fetch_uniswap_v3(client: httpx.AsyncClient, token: Dict, conn: sqlite3.Connection, eth_usd: float, semaphore: asyncio.Semaphore):async with semaphore:query = {"query": f"""{{19 unmodified lines}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"]if not pools:# This handles cases where the API returns an empty pool listraise ValueError(f"No pools found for {token['symbol']} in Uniswap response.")pool = pools[0]price_usd = 0.0if pool['token0']['symbol'].upper() in [token['symbol'], 'W'+token['symbol']]:price_usd = float(pool['token0']['derivedETH']) * eth_usdelif pool['token1']['symbol'].upper() in [token['symbol'], 'W'+token['symbol']]:price_usd = float(pool['token1']['derivedETH']) * eth_usdelse:raise ValueError("Could not determine price from pool.")volume_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("errors", [{}])[0].get("message", f"No pools found or data error for {token['symbol']}")log_api_health(conn, "UniswapV3", "error", elapsed_ms, error_msg, data)except Exception as e:log_api_health(conn, "UniswapV3", "error", elapsed_ms, str(e))7 unmodified linesreturnsemaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)async with httpx.AsyncClient(follow_redirects=True) as client:eth_usd = await fetch_eth_usd(client, semaphore)tasks = []for token in tokens:if 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':tasks.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)
__pycache__/config.cpython-313.pyc
config.py+1-1
core/data_fetcher.py+15-14