From 566cf952745a4941851d22eb62719aebb8db194c Mon Sep 17 00:00:00 2001 From: sanaica Date: Mon, 23 Mar 2026 21:09:19 +0530 Subject: [PATCH] feat: add asynchronous oracle and market logic utility --- tools/oracle_aggregator.py | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tools/oracle_aggregator.py diff --git a/tools/oracle_aggregator.py b/tools/oracle_aggregator.py new file mode 100644 index 0000000..f0bbe01 --- /dev/null +++ b/tools/oracle_aggregator.py @@ -0,0 +1,63 @@ +import asyncio +import aiohttp +import statistics + +async def fetch_price(session, exchange_name, url, parse_logic): + """Fetches and parses price data from an exchange.""" + try: + async with session.get(url, timeout=5) as response: + if response.status == 200: + data = await response.json() + price = float(parse_logic(data)) + print(f"āœ… [{exchange_name}] Price Received: ${price:,.2f}") + return price + else: + print(f"āš ļø [{exchange_name}] returned status {response.status}") + return None + except Exception as e: + print(f"āŒ [{exchange_name}] Connection Error: {e}") + return None + +async def main(): + print("--- MiniChain Decentralized Oracle Aggregator ---") + print("Fetching global ETH/USD consensus...\n") + + sources = [ + {"name": "Binance", "url": "https://api.binance.com/api/v3/ticker/price?symbol=ETHUSDT", "logic": lambda d: d['price']}, + {"name": "CoinGecko", "url": "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd", "logic": lambda d: d['ethereum']['usd']}, + {"name": "Kraken", "url": "https://api.kraken.com/0/public/Ticker?pair=ETHUSD", "logic": lambda d: d['result']['XETHZUSD']['c'][0]} + ] + + async with aiohttp.ClientSession() as session: + tasks = [fetch_price(session, s['name'], s['url'], s['logic']) for s in sources] + + # This is the missing line! We have to WAIT for the results. + results = await asyncio.gather(*tasks) + + # This line defines 'prices' so the error goes away. + prices = [p for p in results if p is not None] + + if len(prices) >= 2: + final_price = statistics.median(prices) + + # --- AI BRAIN TRIGGER --- + TARGET_PRICE = 2200.00 + print(f"\n[AI BRAIN] Analyzing current price vs Target (${TARGET_PRICE})...") + + if final_price > TARGET_PRICE: + print("šŸš€ RESULT: Price is ABOVE target. Suggesting SELL signal.") + else: + print("šŸ’Ž RESULT: Price is BELOW target. Suggesting BUY/HOLD signal.") + # ------------------------- + + variation = max(prices) - min(prices) + print("\n-------------------------------------------") + print(f"šŸ“Š CONSOLIDATED PRICE: ${final_price:,.2f}") + print(f"šŸ“ˆ Source Variance: ${variation:,.2f}") + print(f"šŸ”— Data Sources: {len(prices)}/3 active") + print("-------------------------------------------") + else: + print("\n🚨 ERROR: Critical Oracle failure. Not enough sources for consensus.") + +if __name__ == "__main__": + asyncio.run(main()) \ No newline at end of file