1
1
from collections import defaultdict
2
+ from typing import List , Tuple , TypedDict
2
3
3
4
import numpy as np
4
5
import plotly .graph_objects as go
8
9
from lib .api import fetch_api_data
9
10
10
11
12
+ class PriceData (TypedDict ):
13
+ notional : float
14
+ positions : List [Tuple [str , float ]]
15
+
16
+
11
17
def plot_liquidation_curves (liquidation_data ):
12
18
liquidations_long = liquidation_data ["liquidations_long" ]
13
19
liquidations_short = liquidation_data ["liquidations_short" ]
@@ -27,7 +33,9 @@ def filter_outliers(
27
33
28
34
def aggregate_liquidations (liquidations ):
29
35
"""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
+ )
31
39
for price , notional , pubkey in liquidations :
32
40
price_to_data [price ]["notional" ] += notional
33
41
price_to_data [price ]["positions" ].append ((pubkey , notional ))
@@ -127,7 +135,7 @@ def liquidation_curves_page():
127
135
liquidation_data = fetch_api_data (
128
136
"liquidation" ,
129
137
"liquidation-curve" ,
130
- params = params ,
138
+ params = dict ( st . query_params ) ,
131
139
retry = True ,
132
140
)
133
141
except Exception as e :
@@ -158,84 +166,86 @@ def liquidation_curves_page():
158
166
None ,
159
167
)
160
168
161
- if long_prices is None :
169
+ if long_prices is None or short_prices is None :
162
170
st .write ("No liquidation data available" )
163
171
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