From 20f5efad178255e11f612a335d8ace92e1a709bd Mon Sep 17 00:00:00 2001 From: sina <20732540+SinaKhalili@users.noreply.github.com> Date: Mon, 7 Oct 2024 03:28:00 -0700 Subject: [PATCH] Update frontend for nicer error handling from backend --- src/lib/page.py | 64 ++++++++++++++----------------------------------- src/main.py | 14 +++++------ 2 files changed, 25 insertions(+), 53 deletions(-) diff --git a/src/lib/page.py b/src/lib/page.py index def299a..3e646ba 100644 --- a/src/lib/page.py +++ b/src/lib/page.py @@ -32,59 +32,31 @@ def header(): def sidebar(): st.sidebar.header("Options") - - network = st.sidebar.radio("env", ("mainnet-beta", "devnet")) - st.session_state[NETWORK_STATE_KEY] = network - - url = os.getenv("RPC_URL", "") - rpc = st.sidebar.text_input("RPC URL", value=url) - if network == "mainnet-beta" and rpc == "": - rpc = os.getenv("ANCHOR_PROVIDER_URL", "") - st.session_state[RPC_STATE_KEY] = rpc - - if VAT_STATE_KEY not in st.session_state: - st.session_state[VAT_STATE_KEY] = None - st.sidebar.write( - f"Have VAT? {'โœ…' if st.session_state[VAT_STATE_KEY] is not None else 'Not Loaded'} " - ) - - metadata = api("metadata", "", as_json=True) - pickle_file = metadata["pickle_file"] - pickle_file = pickle_file.split("/")[-1] - timestamp = pickle_file.split("-")[1:] - timestamp = datetime.strptime(" ".join(timestamp), "%Y %m %d %H %M %S") - time_ago = datetime.now() - timestamp - time_ago_str = humanize.precisedelta(time_ago, minimum_unit="minutes") - st.sidebar.write(f"Last snapshot taken at: {timestamp} ({time_ago_str} ago)") - - -def needs_rpc_and_vat(page_callable: callable): + try: + metadata = api("metadata", "", as_json=True) + pickle_file = metadata["pickle_file"] + pickle_file = pickle_file.split("/")[-1] + timestamp = pickle_file.split("-")[1:] + timestamp = datetime.strptime(" ".join(timestamp), "%Y %m %d %H %M %S") + time_ago = datetime.now() - timestamp + time_ago_str = humanize.precisedelta(time_ago, minimum_unit="minutes") + st.sidebar.write(f"Last snapshot taken at: {timestamp} ({time_ago_str} ago)") + except Exception as e: + st.sidebar.write("# Unable to reach backend") + + +def needs_backend(page_callable: callable): """ Decorator to add a guard to a page function """ def page_with_guard(): - rpc = st.session_state[RPC_STATE_KEY] - if rpc == "๐Ÿคซ" or rpc == "" or rpc is None: - st.warning("Please enter a Solana RPC URL in the sidebar") + try: + api("metadata", "", as_json=True) + except Exception as e: + st.error("Sorry, unable to reach backend") return - vat = st.session_state[VAT_STATE_KEY] - if vat is None: - drift_client = DriftClient( - AsyncClient(rpc), - Wallet.dummy(), - account_subscription=AccountSubscriptionConfig("cached"), - ) - loop: AbstractEventLoop = asyncio.new_event_loop() - newest_snapshot = load_newest_files(os.getcwd() + "/pickles") - start_load_vat = time.time() - vat = loop.run_until_complete(load_vat(drift_client, newest_snapshot)) - st.session_state["vat"] = vat - print(f"loaded vat in {time.time() - start_load_vat}") - st.session_state[VAT_STATE_KEY] = vat - loop.close() - page_callable() return page_with_guard diff --git a/src/main.py b/src/main.py index d710299..2b73216 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,6 @@ from dotenv import load_dotenv from lib.page import header -from lib.page import needs_rpc_and_vat +from lib.page import needs_backend from lib.page import sidebar from page.asset_liability import asset_liab_matrix_page from page.backend import backend_page @@ -28,37 +28,37 @@ icon=":material/home:", ), st.Page( - orderbook_page, + needs_backend(orderbook_page), url_path="orderbook", title="Orderbook", icon="๐Ÿ“ˆ", ), st.Page( - health_page, + needs_backend(health_page), url_path="health", title="Health", icon="๐Ÿฅ", ), st.Page( - price_shock_page, + needs_backend(price_shock_page), url_path="price-shock", title="Price Shock", icon="๐Ÿ’ธ", ), st.Page( - asset_liab_matrix_page, + needs_backend(asset_liab_matrix_page), url_path="asset-liab-matrix", title="Asset-Liab Matrix", icon="๐Ÿ“Š", ), st.Page( - plot_liquidation_curve, + needs_backend(plot_liquidation_curve), url_path="liquidation-curves", title="Liquidation Curves", icon="๐ŸŒŠ", ), st.Page( - backend_page, + needs_backend(backend_page), url_path="backend", title="Control Backend", icon="๐Ÿงช",