Skip to content

Commit 108f8af

Browse files
committed
Enhance liquidation curves functionality and type safety
- Introduced a new `PriceData` TypedDict to improve type safety for price-related data structures. - Updated the `aggregate_liquidations` function to utilize the new `PriceData` type, enhancing code clarity and maintainability. - Modified the condition to check for both long and short prices before rendering the liquidation data, ensuring that the UI only displays relevant information when both datasets are available. - Refactored the layout of the long and short position accounts sections for better readability and organization in the Streamlit interface.
1 parent 01f935c commit 108f8af

File tree

1 file changed

+91
-81
lines changed

1 file changed

+91
-81
lines changed

src/page/liquidation_curves.py

Lines changed: 91 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import defaultdict
2+
from typing import List, Tuple, TypedDict
23

34
import numpy as np
45
import plotly.graph_objects as go
@@ -8,6 +9,11 @@
89
from lib.api import fetch_api_data
910

1011

12+
class PriceData(TypedDict):
13+
notional: float
14+
positions: List[Tuple[str, float]]
15+
16+
1117
def plot_liquidation_curves(liquidation_data):
1218
liquidations_long = liquidation_data["liquidations_long"]
1319
liquidations_short = liquidation_data["liquidations_short"]
@@ -27,7 +33,9 @@ def filter_outliers(
2733

2834
def aggregate_liquidations(liquidations):
2935
"""Aggregate liquidations to calculate cumulative notional amounts and track pubkeys with their sizes."""
30-
price_to_data = defaultdict(lambda: {"notional": 0.0, "positions": []})
36+
price_to_data: defaultdict[float, PriceData] = defaultdict(
37+
lambda: {"notional": 0.0, "positions": []}
38+
)
3139
for price, notional, pubkey in liquidations:
3240
price_to_data[price]["notional"] += notional
3341
price_to_data[price]["positions"].append((pubkey, notional))
@@ -127,7 +135,7 @@ def liquidation_curves_page():
127135
liquidation_data = fetch_api_data(
128136
"liquidation",
129137
"liquidation-curve",
130-
params=params,
138+
params=dict(st.query_params),
131139
retry=True,
132140
)
133141
except Exception as e:
@@ -158,84 +166,86 @@ def liquidation_curves_page():
158166
None,
159167
)
160168

161-
if long_prices is None:
169+
if long_prices is None or short_prices is None:
162170
st.write("No liquidation data available")
163171
st.stop()
164-
165-
long_col, short_col = st.columns([1, 1])
166-
167-
with long_col:
168-
st.plotly_chart(long_fig, use_container_width=True)
169-
170-
with st.expander("Long Position Accounts"):
171-
if long_pubkeys and len(long_pubkeys[-1]) > 0:
172-
st.write(f"Total Accounts: {len(long_pubkeys[-1])}")
173-
long_data = []
174-
for i, positions in enumerate(long_pubkeys):
175-
if i > 0:
176-
new_positions = set(positions) - set(long_pubkeys[i - 1])
177-
if new_positions:
178-
for pubkey, size in new_positions:
179-
long_data.append(
180-
{
181-
"Price": f"{long_prices[i]:.2f}",
182-
"Size": f"{size:,.2f}",
183-
"Account": pubkey,
184-
"Link": f"https://app.drift.trade/overview?userAccount={pubkey}",
185-
}
186-
)
187-
if long_data:
188-
st.dataframe(
189-
long_data,
190-
column_config={
191-
"Price": st.column_config.TextColumn("Price"),
192-
"Size": st.column_config.TextColumn("Size"),
193-
"Account": st.column_config.TextColumn(
194-
"Account", width="large"
195-
),
196-
"Link": st.column_config.LinkColumn(
197-
"Link", display_text="View"
198-
),
199-
},
200-
hide_index=True,
201-
)
202-
else:
203-
st.write("No long positions found")
204-
205-
with short_col:
206-
st.plotly_chart(short_fig, use_container_width=True)
207-
208-
with st.expander("Short Position Accounts"):
209-
if short_pubkeys and len(short_pubkeys[-1]) > 0:
210-
st.write(f"Total Accounts: {len(short_pubkeys[-1])}")
211-
short_data = []
212-
for i, positions in enumerate(short_pubkeys):
213-
if i > 0:
214-
new_positions = set(positions) - set(short_pubkeys[i - 1])
215-
if new_positions:
216-
for pubkey, size in new_positions:
217-
short_data.append(
218-
{
219-
"Price": f"{short_prices[i]:.2f}",
220-
"Size": f"{size:,.2f}",
221-
"Account": pubkey,
222-
"Link": f"https://app.drift.trade/overview?userAccount={pubkey}",
223-
}
224-
)
225-
if short_data:
226-
st.dataframe(
227-
short_data,
228-
column_config={
229-
"Price": st.column_config.TextColumn("Price"),
230-
"Size": st.column_config.TextColumn("Size"),
231-
"Account": st.column_config.TextColumn(
232-
"Account", width="large"
233-
),
234-
"Link": st.column_config.LinkColumn(
235-
"Link", display_text="View"
236-
),
237-
},
238-
hide_index=True,
239-
)
240-
else:
241-
st.write("No short positions found")
172+
else:
173+
long_col, short_col = st.columns([1, 1])
174+
175+
with long_col:
176+
st.plotly_chart(long_fig, use_container_width=True)
177+
178+
with st.expander("Long Position Accounts"):
179+
if long_pubkeys and len(long_pubkeys[-1]) > 0:
180+
st.write(f"Total Accounts: {len(long_pubkeys[-1])}")
181+
long_data = []
182+
for i, positions in enumerate(long_pubkeys):
183+
if i > 0:
184+
new_positions = set(positions) - set(long_pubkeys[i - 1])
185+
if new_positions:
186+
for pubkey, size in new_positions:
187+
long_data.append(
188+
{
189+
"Price": f"{long_prices[i]:.2f}",
190+
"Size": f"{size:,.2f}",
191+
"Account": pubkey,
192+
"Link": f"https://app.drift.trade/overview?userAccount={pubkey}",
193+
}
194+
)
195+
if long_data:
196+
st.dataframe(
197+
long_data,
198+
column_config={
199+
"Price": st.column_config.TextColumn("Price"),
200+
"Size": st.column_config.TextColumn("Size"),
201+
"Account": st.column_config.TextColumn(
202+
"Account", width="large"
203+
),
204+
"Link": st.column_config.LinkColumn(
205+
"Link", display_text="View"
206+
),
207+
},
208+
hide_index=True,
209+
)
210+
else:
211+
st.write("No long positions found")
212+
213+
with short_col:
214+
st.plotly_chart(short_fig, use_container_width=True)
215+
216+
with st.expander("Short Position Accounts"):
217+
if short_pubkeys and len(short_pubkeys[-1]) > 0:
218+
st.write(f"Total Accounts: {len(short_pubkeys[-1])}")
219+
short_data = []
220+
for i, positions in enumerate(short_pubkeys):
221+
if i > 0:
222+
new_positions = set(positions) - set(
223+
short_pubkeys[i - 1]
224+
)
225+
if new_positions:
226+
for pubkey, size in new_positions:
227+
short_data.append(
228+
{
229+
"Price": f"{short_prices[i]:.2f}",
230+
"Size": f"{size:,.2f}",
231+
"Account": pubkey,
232+
"Link": f"https://app.drift.trade/overview?userAccount={pubkey}",
233+
}
234+
)
235+
if short_data:
236+
st.dataframe(
237+
short_data,
238+
column_config={
239+
"Price": st.column_config.TextColumn("Price"),
240+
"Size": st.column_config.TextColumn("Size"),
241+
"Account": st.column_config.TextColumn(
242+
"Account", width="large"
243+
),
244+
"Link": st.column_config.LinkColumn(
245+
"Link", display_text="View"
246+
),
247+
},
248+
hide_index=True,
249+
)
250+
else:
251+
st.write("No short positions found")

0 commit comments

Comments
 (0)