-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoracle.py
More file actions
228 lines (160 loc) · 5.47 KB
/
oracle.py
File metadata and controls
228 lines (160 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# app/app/oracle.py
from __future__ import annotations
import os
import time
import re
from typing import Optional, Dict, Tuple
import requests
# ============================================================
# Configuration (env)
# ============================================================
GOLDAPI_TOKEN = os.getenv("GOLDAPI_TOKEN", "").strip()
METALPRICEAPI_TOKEN = os.getenv("METALPRICEAPI_TOKEN", "").strip()
GOLDAPI_MIN_PERIOD = int(os.getenv("GOLDAPI_MIN_PERIOD", "300")) # seconds
GOLDAPI_MAX_PERIOD = int(os.getenv("GOLDAPI_MAX_PERIOD", "3600"))
METAL_MIN_PERIOD = int(os.getenv("METALPRICEAPI_MIN_PERIOD", "300"))
METAL_MAX_PERIOD = int(os.getenv("METALPRICEAPI_MAX_PERIOD", "3600"))
KITCO_MIN_PERIOD = int(os.getenv("KITCO_MIN_PERIOD", "300"))
KITCO_MAX_PERIOD = int(os.getenv("KITCO_MAX_PERIOD", "3600"))
HTTP_TIMEOUT = 6.0 # seconds
MIN_GOLD_PRICE = 500.0
MAX_GOLD_PRICE = 10_000.0
# ============================================================
# In-memory cache
# ============================================================
_cache: Dict[str, Tuple[float, float]] = {}
# key -> (price, timestamp)
def _now() -> float:
return time.time()
def _valid_gold_price(p: Optional[float]) -> bool:
return isinstance(p, (int, float)) and MIN_GOLD_PRICE < p < MAX_GOLD_PRICE
def _get_cached(
key: str, min_period: int, max_period: int
) -> Optional[float]:
if key not in _cache:
return None
price, ts = _cache[key]
age = _now() - ts
if age < min_period:
return price
if age > max_period:
return None
return price
def _set_cache(key: str, price: float):
_cache[key] = (price, _now())
# ============================================================
# GoldAPI
# ============================================================
def _gold_from_goldapi() -> Optional[float]:
cached = _get_cached("goldapi", GOLDAPI_MIN_PERIOD, GOLDAPI_MAX_PERIOD)
if cached is not None:
return cached
if not GOLDAPI_TOKEN:
return None
try:
r = requests.get(
"https://www.goldapi.io/api/XAU/USD",
headers={"x-access-token": GOLDAPI_TOKEN},
timeout=HTTP_TIMEOUT,
)
if r.status_code != 200:
return None
price = r.json().get("price")
if not _valid_gold_price(price):
return None
price = float(price)
_set_cache("goldapi", price)
return price
except Exception:
return None
# ============================================================
# MetalPriceAPI
# ============================================================
def _gold_from_metalpriceapi() -> Optional[float]:
cached = _get_cached("metalpriceapi", METAL_MIN_PERIOD, METAL_MAX_PERIOD)
if cached is not None:
return cached
if not METALPRICEAPI_TOKEN:
return None
try:
r = requests.get(
"https://api.metalpriceapi.com/v1/latest",
params={
"api_key": METALPRICEAPI_TOKEN,
"base": "USD",
"currencies": "XAU",
},
timeout=HTTP_TIMEOUT,
)
if r.status_code != 200:
return None
rates = r.json().get("rates", {})
xau_per_usd = rates.get("XAU")
if not isinstance(xau_per_usd, (int, float)) or xau_per_usd <= 0:
return None
usd_per_xau = 1.0 / float(xau_per_usd)
if not _valid_gold_price(usd_per_xau):
return None
_set_cache("metalpriceapi", usd_per_xau)
return usd_per_xau
except Exception:
return None
# ============================================================
# Kitco (HTML scrape, regex-based — mirrors your Node code)
# ============================================================
_KITCO_REGEX = re.compile(
r"Bid</div><div class=\"mb-2 text-right\"><h3 class=\".*?\">"
r"(\d{1,3}(?:,\d{3})*\.\d{2})"
)
def _gold_from_kitco() -> Optional[float]:
cached = _get_cached("kitco", KITCO_MIN_PERIOD, KITCO_MAX_PERIOD)
if cached is not None:
return cached
try:
r = requests.get(
"https://www.kitco.com/charts/gold",
timeout=HTTP_TIMEOUT,
headers={
"User-Agent": "Mozilla/5.0",
"Accept": "text/html",
},
allow_redirects=True,
)
if r.status_code != 200:
return None
m = _KITCO_REGEX.search(r.text)
if not m:
return None
raw = m.group(1).replace(",", "")
price = float(raw) + 0.5 # matches your Node logic
if not _valid_gold_price(price):
return None
_set_cache("kitco", price)
return price
except Exception:
return None
# ============================================================
# Public API
# ============================================================
def get_gold_price_usd_per_oz() -> float:
"""
Resolution order (with caching):
1) GoldAPI
2) MetalPriceAPI
3) Kitco HTML scrape
Hard-fail if all unavailable.
"""
for fn in (
_gold_from_goldapi,
_gold_from_metalpriceapi,
_gold_from_kitco,
):
price = fn()
if price is not None:
return price
raise RuntimeError("Gold oracle failure: no valid source available")
def get_avax_price_usd() -> float:
# Still stubbed — replace with Chainlink/Pyth later
return 35.0
def oracle_timestamp() -> int:
return int(time.time())