1
1
import pandas as pd
2
2
import streamlit as st
3
3
import logging
4
+ import time
4
5
from typing import Optional
5
6
from driftpy .constants .spot_markets import mainnet_spot_market_configs
6
7
7
8
from lib .api import fetch_api_data
9
+ from src .utils import get_current_slot
8
10
9
11
# Configure logging
10
12
logging .basicConfig (level = logging .INFO )
11
13
logger = logging .getLogger (__name__ )
12
14
15
+ def is_processing (result ):
16
+ """Checks if the API result indicates backend processing."""
17
+ return isinstance (result , dict ) and result .get ("result" ) == "processing"
18
+
19
+ def has_error (result ):
20
+ """Checks if the API result indicates an error."""
21
+ return isinstance (result , dict ) and "error" in result
22
+
13
23
def format_authority (authority : str ) -> str :
14
24
"""Format authority to show first and last 4 chars"""
15
25
return f"{ authority [:4 ]} ...{ authority [- 4 :]} "
@@ -42,6 +52,9 @@ def get_market_symbol(market_index: int) -> str:
42
52
43
53
def deposits_page ():
44
54
try :
55
+ # Explicitly type hint as DataFrame
56
+ df : pd .DataFrame = pd .DataFrame ()
57
+
45
58
params = st .query_params
46
59
market_index = int (params .get ("market_index" , 0 ))
47
60
@@ -87,11 +100,27 @@ def deposits_page():
87
100
retry = True ,
88
101
)
89
102
103
+ if is_processing (result ):
104
+ st .info ("Backend is initializing data. Please wait." )
105
+ with st .spinner ("Auto-refreshing in 10 seconds..." ):
106
+ time .sleep (10 )
107
+ st .rerun ()
108
+ return
109
+
110
+ if has_error (result ):
111
+ st .error (f"An error occurred: { result .get ('error' , 'Unknown error' )} " )
112
+ return
113
+
90
114
if result is None :
91
115
logger .error ("API returned no deposits data" )
92
116
st .error ("No deposits found" )
93
117
return
94
118
119
+ slot = result .get ("slot" , 0 )
120
+ current_slot = get_current_slot ()
121
+ slot_age = current_slot - slot
122
+ st .info (f"Data from slot { slot } , which is { slot_age } slots old." )
123
+
95
124
df = pd .DataFrame (result ["deposits" ])
96
125
if df .empty :
97
126
logger .warning ("Empty deposits dataframe created" )
@@ -104,7 +133,7 @@ def deposits_page():
104
133
105
134
if exclude_vaults :
106
135
original_len = len (df )
107
- df = df [~ df ["authority" ].isin (result ["vaults" ])]
136
+ df = df . loc [~ df ["authority" ].isin (result ["vaults" ])]
108
137
logger .info (f"Excluded { original_len - len (df )} vault entries" )
109
138
110
139
with col1 :
@@ -119,7 +148,7 @@ def deposits_page():
119
148
tabs = st .tabs (["By Position" , "By Authority" ])
120
149
121
150
with tabs [0 ]:
122
- filtered_df = df [df ["balance" ] >= min_balance ]
151
+ filtered_df : pd . DataFrame = df . loc [df ["balance" ] >= min_balance ]. copy ()
123
152
st .write (f"Total deposits value: **${ filtered_df ['value' ].sum ():,.2f} **" )
124
153
st .write (f"Number of depositor user accounts: **{ len (filtered_df ):,} **" )
125
154
@@ -133,7 +162,8 @@ def deposits_page():
133
162
)
134
163
135
164
# Safely map market indices to symbols
136
- filtered_df ["market_index" ] = filtered_df ["market_index" ].map (get_market_symbol )
165
+ if not filtered_df .empty :
166
+ filtered_df ["market_index" ] = filtered_df ["market_index" ].map (get_market_symbol )
137
167
138
168
st .dataframe (
139
169
filtered_df .sort_values ("value" , ascending = False ),
@@ -164,7 +194,7 @@ def deposits_page():
164
194
.agg ({"value" : "sum" , "balance" : "sum" , "user_account" : "count" })
165
195
.reset_index ()
166
196
)
167
- grouped_df = grouped_df [grouped_df ["value" ] >= min_balance ]
197
+ grouped_df : pd . DataFrame = grouped_df . loc [grouped_df ["value" ] >= min_balance ]. copy ()
168
198
st .write (f"Total deposits value: **${ grouped_df ['value' ].sum ():,.2f} **" )
169
199
st .write (f"Total number of authorities with deposits: **{ len (grouped_df ):,} **" )
170
200
grouped_df = grouped_df .rename (columns = {"user_account" : "num_accounts" })
0 commit comments