Spaces:
Runtime error
Runtime error
gauravlochab
commited on
Commit
·
a691cb9
1
Parent(s):
15f0ff4
refactor: Improve error handling and retries for token price API request
Browse files- app_value_locked.py +46 -30
app_value_locked.py
CHANGED
@@ -5,6 +5,10 @@ from datetime import datetime, timedelta
|
|
5 |
from typing import List, Dict, Optional
|
6 |
import pandas as pd
|
7 |
import time
|
|
|
|
|
|
|
|
|
8 |
|
9 |
ADDRESSES = {
|
10 |
"optimism": {
|
@@ -238,37 +242,49 @@ def get_token_decimals(web3, token_address):
|
|
238 |
])
|
239 |
return token_contract.functions.decimals().call()
|
240 |
|
241 |
-
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
chain_dict = {"optimism": "optimistic-ethereum", "base": "base", "ethereum": "ethereum"}
|
244 |
-
chain_name = chain_dict.get(chain, chain)
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
except requests.exceptions.RequestException as e:
|
266 |
-
logging.error(f"Error fetching token price for {token_address} on attempt {attempt + 1}/{retries}: {e}")
|
267 |
-
if attempt < retries - 1:
|
268 |
-
time.sleep(delay) # Wait before retry
|
269 |
-
else:
|
270 |
-
logging.error(f"Failed to fetch token price after {retries} attempts")
|
271 |
-
return None # Return None if all retry attempts fail
|
272 |
|
273 |
def load_existing_transactions(file_path):
|
274 |
"""Load existing transactions from a CSV file."""
|
|
|
5 |
from typing import List, Dict, Optional
|
6 |
import pandas as pd
|
7 |
import time
|
8 |
+
import requests
|
9 |
+
from requests.adapters import HTTPAdapter
|
10 |
+
from requests.packages.urllib3.util.retry import Retry
|
11 |
+
import time
|
12 |
|
13 |
ADDRESSES = {
|
14 |
"optimism": {
|
|
|
242 |
])
|
243 |
return token_contract.functions.decimals().call()
|
244 |
|
245 |
+
|
246 |
+
|
247 |
+
def requests_retry_session(
|
248 |
+
retries=3,
|
249 |
+
backoff_factor=0.3,
|
250 |
+
status_forcelist=(500, 502, 504),
|
251 |
+
session=None,
|
252 |
+
):
|
253 |
+
session = session or requests.Session()
|
254 |
+
retry = Retry(
|
255 |
+
total=retries,
|
256 |
+
read=retries,
|
257 |
+
connect=retries,
|
258 |
+
backoff_factor=backoff_factor,
|
259 |
+
status_forcelist=status_forcelist,
|
260 |
+
)
|
261 |
+
adapter = HTTPAdapter(max_retries=retry)
|
262 |
+
session.mount('http://', adapter)
|
263 |
+
session.mount('https://', adapter)
|
264 |
+
return session
|
265 |
+
|
266 |
+
def get_token_price_usd(chain, token_address):
|
267 |
chain_dict = {"optimism": "optimistic-ethereum", "base": "base", "ethereum": "ethereum"}
|
268 |
+
chain_name = chain_dict.get(chain, chain)
|
269 |
+
time.sleep(4)
|
270 |
+
url = f"https://api.coingecko.com/api/v3/simple/token_price/{chain_name}?contract_addresses={token_address}&vs_currencies=usd"
|
271 |
+
|
272 |
+
headers = {
|
273 |
+
"accept": "application/json",
|
274 |
+
"x-cg-api-key": "CG-mf5xZnGELpSXeSqmHDLY2nNU"
|
275 |
+
}
|
276 |
+
|
277 |
+
try:
|
278 |
+
response = requests_retry_session().get(url, headers=headers, timeout=10)
|
279 |
+
response.raise_for_status()
|
280 |
+
data = response.json()
|
281 |
+
|
282 |
+
# Extract USD price
|
283 |
+
key = token_address.lower()
|
284 |
+
return data.get(key, {}).get('usd', None)
|
285 |
+
except requests.exceptions.RequestException as e:
|
286 |
+
print(f"An error occurred: {e}")
|
287 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
|
289 |
def load_existing_transactions(file_path):
|
290 |
"""Load existing transactions from a CSV file."""
|