From 27dc43cc1f5e427bbd8b1872873d4f45ec94aa31 Mon Sep 17 00:00:00 2001 From: pharr117 Date: Sat, 21 Oct 2023 18:57:55 -0400 Subject: [PATCH] Add result key check to RPC time requests since some chains do not return the data with result key as the top level --- app.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 32a0726..08676da 100644 --- a/app.py +++ b/app.py @@ -179,9 +179,14 @@ def get_latest_block_height_rpc(rpc_url): response = requests.get(f"{rpc_url}/status", timeout=1) response.raise_for_status() data = response.json() + + if "result" in data.keys(): + data = data["result"] + return int( - data.get("result", {}).get("sync_info", {}).get("latest_block_height", 0) + data.get("sync_info", {}).get("latest_block_height", 0) ) + # RPC endpoints can return a 200 but not JSON (usually an HTML error page due to throttling or some other error) # Catch everything instead of just requests.RequestException except Exception: @@ -194,10 +199,15 @@ def get_block_time_rpc(rpc_url, height): response = requests.get(f"{rpc_url}/block?height={height}", timeout=1) response.raise_for_status() data = response.json() - return data.get("result", {}).get("block", {}).get("header", {}).get("time", "") + + if "result" in data.keys(): + data = data["result"] + + return data.get("block", {}).get("header", {}).get("time", "") # RPC endpoints can return a 200 but not JSON (usually an HTML error page due to throttling or some other error) # Catch everything instead of just requests.RequestException - except Exception: + except Exception as e: + print(e) return None