Skip to content

Commit

Permalink
Update frontend for nicer error handling from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Oct 7, 2024
1 parent 5d07a8e commit 20f5efa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 53 deletions.
64 changes: 18 additions & 46 deletions src/lib/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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="🧪",
Expand Down

0 comments on commit 20f5efa

Please sign in to comment.