Spaces:
Running
Running
Update app/price_fetcher.py
Browse files- app/price_fetcher.py +16 -12
app/price_fetcher.py
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
A professional-grade, multi-asset, multi-oracle price engine.
|
3 |
This engine dynamically fetches prices for a configured list of assets
|
4 |
from decentralized oracles to detect market dislocations.
|
|
|
5 |
"""
|
6 |
import asyncio
|
7 |
import logging
|
@@ -10,11 +11,7 @@ import httpx
|
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
#
|
14 |
-
# THE "MONEY-SPINNING" UPGRADE IS HERE
|
15 |
-
# ====================================================================
|
16 |
-
# This is our master watchlist. The engine will hunt for opportunities
|
17 |
-
# across all 10 of these high-volume assets simultaneously.
|
18 |
ASSET_CONFIG = {
|
19 |
"BTC": {
|
20 |
"pyth_id": "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415B43",
|
@@ -57,10 +54,15 @@ ASSET_CONFIG = {
|
|
57 |
"coingecko_id": "matic-network",
|
58 |
},
|
59 |
}
|
60 |
-
# ====================================================================
|
61 |
|
62 |
class PriceFetcher:
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
AGGREGATOR_URL = "https://api.coingecko.com/api/v3/simple/price"
|
65 |
|
66 |
def __init__(self, client: httpx.AsyncClient):
|
@@ -70,16 +72,19 @@ class PriceFetcher:
|
|
70 |
|
71 |
async def _fetch_pyth_prices(self) -> Dict[str, float]:
|
72 |
pyth_ids = [v['pyth_id'] for v in ASSET_CONFIG.values()]
|
73 |
-
|
|
|
74 |
try:
|
75 |
resp = await self.client.get(self.PYTH_URL, params=params, timeout=10)
|
76 |
resp.raise_for_status()
|
77 |
-
|
|
|
78 |
|
79 |
-
|
|
|
80 |
return {
|
81 |
id_to_symbol[item['id']]: int(item['price']['price']) / (10 ** abs(int(item['price']['expo'])))
|
82 |
-
for item in
|
83 |
}
|
84 |
except Exception as e:
|
85 |
logger.error(f"❌ Oracle Error (Pyth): {e}")
|
@@ -92,7 +97,6 @@ class PriceFetcher:
|
|
92 |
resp = await self.client.get(self.AGGREGATOR_URL, params=params, timeout=10)
|
93 |
resp.raise_for_status()
|
94 |
data = resp.json()
|
95 |
-
|
96 |
id_to_symbol = {v['coingecko_id']: k for k, v in ASSET_CONFIG.items()}
|
97 |
return {id_to_symbol[cg_id]: prices['usd'] for cg_id, prices in data.items()}
|
98 |
except Exception as e:
|
|
|
2 |
A professional-grade, multi-asset, multi-oracle price engine.
|
3 |
This engine dynamically fetches prices for a configured list of assets
|
4 |
from decentralized oracles to detect market dislocations.
|
5 |
+
v11.1: Corrected Pyth Network API endpoint.
|
6 |
"""
|
7 |
import asyncio
|
8 |
import logging
|
|
|
11 |
|
12 |
logger = logging.getLogger(__name__)
|
13 |
|
14 |
+
# --- CONFIGURATION ---
|
|
|
|
|
|
|
|
|
15 |
ASSET_CONFIG = {
|
16 |
"BTC": {
|
17 |
"pyth_id": "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415B43",
|
|
|
54 |
"coingecko_id": "matic-network",
|
55 |
},
|
56 |
}
|
|
|
57 |
|
58 |
class PriceFetcher:
|
59 |
+
# ====================================================================
|
60 |
+
# THE CRITICAL FIX IS HERE
|
61 |
+
# ====================================================================
|
62 |
+
# Using the new, correct endpoint for Pyth Network price feeds.
|
63 |
+
PYTH_URL = "https://hermes.pyth.network/v2/price_feeds"
|
64 |
+
# ====================================================================
|
65 |
+
|
66 |
AGGREGATOR_URL = "https://api.coingecko.com/api/v3/simple/price"
|
67 |
|
68 |
def __init__(self, client: httpx.AsyncClient):
|
|
|
72 |
|
73 |
async def _fetch_pyth_prices(self) -> Dict[str, float]:
|
74 |
pyth_ids = [v['pyth_id'] for v in ASSET_CONFIG.values()]
|
75 |
+
# The new endpoint requires the '0x' prefix for IDs
|
76 |
+
params = [("ids[]", f"0x{pid}") for pid in pyth_ids]
|
77 |
try:
|
78 |
resp = await self.client.get(self.PYTH_URL, params=params, timeout=10)
|
79 |
resp.raise_for_status()
|
80 |
+
# The new endpoint returns a list directly
|
81 |
+
price_data_list = resp.json()
|
82 |
|
83 |
+
# Map the returned prices back to our asset symbols
|
84 |
+
id_to_symbol = {f"0x{v['pyth_id']}": k for k, v in ASSET_CONFIG.items()}
|
85 |
return {
|
86 |
id_to_symbol[item['id']]: int(item['price']['price']) / (10 ** abs(int(item['price']['expo'])))
|
87 |
+
for item in price_data_list if item['id'] in id_to_symbol
|
88 |
}
|
89 |
except Exception as e:
|
90 |
logger.error(f"❌ Oracle Error (Pyth): {e}")
|
|
|
97 |
resp = await self.client.get(self.AGGREGATOR_URL, params=params, timeout=10)
|
98 |
resp.raise_for_status()
|
99 |
data = resp.json()
|
|
|
100 |
id_to_symbol = {v['coingecko_id']: k for k, v in ASSET_CONFIG.items()}
|
101 |
return {id_to_symbol[cg_id]: prices['usd'] for cg_id, prices in data.items()}
|
102 |
except Exception as e:
|