Spaces:
Running
Running
Update app/price_fetcher.py
Browse files- app/price_fetcher.py +37 -9
app/price_fetcher.py
CHANGED
@@ -10,9 +10,11 @@ import httpx
|
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
#
|
14 |
-
#
|
15 |
-
#
|
|
|
|
|
16 |
ASSET_CONFIG = {
|
17 |
"BTC": {
|
18 |
"pyth_id": "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415B43",
|
@@ -26,7 +28,36 @@ ASSET_CONFIG = {
|
|
26 |
"pyth_id": "ef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d",
|
27 |
"coingecko_id": "solana",
|
28 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
}
|
|
|
30 |
|
31 |
class PriceFetcher:
|
32 |
PYTH_URL = "https://hermes.pyth.network/v2/updates/price/latest"
|
@@ -41,11 +72,10 @@ class PriceFetcher:
|
|
41 |
pyth_ids = [v['pyth_id'] for v in ASSET_CONFIG.values()]
|
42 |
params = [("ids[]", pid) for pid in pyth_ids]
|
43 |
try:
|
44 |
-
resp = await self.client.get(self.PYTH_URL, params=params, timeout=
|
45 |
resp.raise_for_status()
|
46 |
parsed_data = resp.json().get('parsed', [])
|
47 |
|
48 |
-
# Map the returned prices back to our asset symbols (BTC, ETH, etc.)
|
49 |
id_to_symbol = {v['pyth_id']: k for k, v in ASSET_CONFIG.items()}
|
50 |
return {
|
51 |
id_to_symbol[item['id']]: int(item['price']['price']) / (10 ** abs(int(item['price']['expo'])))
|
@@ -59,11 +89,10 @@ class PriceFetcher:
|
|
59 |
coingecko_ids = ",".join([v['coingecko_id'] for v in ASSET_CONFIG.values()])
|
60 |
params = {"ids": coingecko_ids, "vs_currencies": "usd"}
|
61 |
try:
|
62 |
-
resp = await self.client.get(self.AGGREGATOR_URL, params=params, timeout=
|
63 |
resp.raise_for_status()
|
64 |
data = resp.json()
|
65 |
|
66 |
-
# Map the returned prices back to our asset symbols
|
67 |
id_to_symbol = {v['coingecko_id']: k for k, v in ASSET_CONFIG.items()}
|
68 |
return {id_to_symbol[cg_id]: prices['usd'] for cg_id, prices in data.items()}
|
69 |
except Exception as e:
|
@@ -76,14 +105,13 @@ class PriceFetcher:
|
|
76 |
pyth_prices, agg_prices = await asyncio.gather(pyth_task, agg_task)
|
77 |
|
78 |
async with self._lock:
|
79 |
-
# Combine the results for each asset
|
80 |
for symbol in ASSET_CONFIG.keys():
|
81 |
self._prices[symbol] = {
|
82 |
"pyth": pyth_prices.get(symbol),
|
83 |
"chainlink_agg": agg_prices.get(symbol)
|
84 |
}
|
85 |
|
86 |
-
logger.info(f"✅ Multi-Asset Prices Updated
|
87 |
|
88 |
def get_all_prices(self) -> Dict[str, Dict[str, Optional[float]]]:
|
89 |
return self._prices.copy()
|
|
|
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",
|
|
|
28 |
"pyth_id": "ef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d",
|
29 |
"coingecko_id": "solana",
|
30 |
},
|
31 |
+
"XRP": {
|
32 |
+
"pyth_id": "02a01e69981d314fd8a723be08253181e53b4945b4bf376d15a51980a37330c3",
|
33 |
+
"coingecko_id": "ripple",
|
34 |
+
},
|
35 |
+
"DOGE": {
|
36 |
+
"pyth_id": "042f02faf4229babc62635593855b6a383d6a4a2a1b9b9a7c385a4a50b86a345",
|
37 |
+
"coingecko_id": "dogecoin",
|
38 |
+
},
|
39 |
+
"ADA": {
|
40 |
+
"pyth_id": "34f544985c7943c093b5934963505a767f4749445244a852654c6017b28091ea",
|
41 |
+
"coingecko_id": "cardano",
|
42 |
+
},
|
43 |
+
"AVAX": {
|
44 |
+
"pyth_id": "0x141f2a3c34c8035443a01d64380b52207991b16c14c5145f617eb578a994753c",
|
45 |
+
"coingecko_id": "avalanche-2",
|
46 |
+
},
|
47 |
+
"LINK": {
|
48 |
+
"pyth_id": "0x63f4f4755a5a67c64c781d45763b33a72666a15e6b91c0fbdf3b2f205d5a6b01",
|
49 |
+
"coingecko_id": "chainlink",
|
50 |
+
},
|
51 |
+
"DOT": {
|
52 |
+
"pyth_id": "0x00a896677493a74421b33362a7447785b13612f0e340d418641a33716a5067a3",
|
53 |
+
"coingecko_id": "polkadot",
|
54 |
+
},
|
55 |
+
"MATIC": {
|
56 |
+
"pyth_id": "0x737ac3c13709b45da8128ff9e1058a984f86a048035656111b8a365e4921648a",
|
57 |
+
"coingecko_id": "matic-network",
|
58 |
+
},
|
59 |
}
|
60 |
+
# ====================================================================
|
61 |
|
62 |
class PriceFetcher:
|
63 |
PYTH_URL = "https://hermes.pyth.network/v2/updates/price/latest"
|
|
|
72 |
pyth_ids = [v['pyth_id'] for v in ASSET_CONFIG.values()]
|
73 |
params = [("ids[]", pid) for pid in pyth_ids]
|
74 |
try:
|
75 |
+
resp = await self.client.get(self.PYTH_URL, params=params, timeout=10)
|
76 |
resp.raise_for_status()
|
77 |
parsed_data = resp.json().get('parsed', [])
|
78 |
|
|
|
79 |
id_to_symbol = {v['pyth_id']: k for k, v in ASSET_CONFIG.items()}
|
80 |
return {
|
81 |
id_to_symbol[item['id']]: int(item['price']['price']) / (10 ** abs(int(item['price']['expo'])))
|
|
|
89 |
coingecko_ids = ",".join([v['coingecko_id'] for v in ASSET_CONFIG.values()])
|
90 |
params = {"ids": coingecko_ids, "vs_currencies": "usd"}
|
91 |
try:
|
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:
|
|
|
105 |
pyth_prices, agg_prices = await asyncio.gather(pyth_task, agg_task)
|
106 |
|
107 |
async with self._lock:
|
|
|
108 |
for symbol in ASSET_CONFIG.keys():
|
109 |
self._prices[symbol] = {
|
110 |
"pyth": pyth_prices.get(symbol),
|
111 |
"chainlink_agg": agg_prices.get(symbol)
|
112 |
}
|
113 |
|
114 |
+
logger.info(f"✅ Multi-Asset Prices Updated")
|
115 |
|
116 |
def get_all_prices(self) -> Dict[str, Dict[str, Optional[float]]]:
|
117 |
return self._prices.copy()
|