@@ -120,13 +120,13 @@ def get_account_health_distribution(request: BackendRequest):
120
120
121
121
122
122
@router .get ("/largest_perp_positions" )
123
- def get_largest_perp_positions (request : BackendRequest , number_of_positions : int , market_index : int = None ):
123
+ def get_largest_perp_positions (request : BackendRequest , market_index : int = None ):
124
124
"""
125
125
Get the largest perp positions by notional value across all users or for a specific market if market_index is provided.
126
126
"""
127
127
vat : Vat = request .state .backend_state .vat
128
128
logger .info (
129
- f"==> [largest_perp_positions] Called with number_of_positions= { number_of_positions } , "
129
+ f"==> [largest_perp_positions] Called with "
130
130
f"market_index={ market_index } , current_pickle={ request .state .backend_state .current_pickle_path } "
131
131
)
132
132
try :
@@ -168,21 +168,17 @@ def get_largest_perp_positions(request: BackendRequest, number_of_positions: int
168
168
position .base_asset_amount / BASE_PRECISION , # Keep original sign for display
169
169
))
170
170
171
- # Sort all positions by value (descending) and take top N
172
- positions = sorted (all_positions , key = lambda x : x [0 ], reverse = True )[: number_of_positions ]
171
+ # Sort all positions by value (descending)
172
+ positions = sorted (all_positions , key = lambda x : x [0 ], reverse = True )
173
173
174
+ # Add summary logging
175
+ total_value_returned = sum (pos [0 ] for pos in positions )
174
176
logger .info (
175
177
f"==> [largest_perp_positions] Stats => total_checked={ total_positions_checked } , "
176
- f"positions_meeting_criteria={ positions_meeting_criteria } , positions_returned={ len (positions )} "
178
+ f"positions_meeting_criteria={ positions_meeting_criteria } , positions_returned={ len (positions )} , "
179
+ f"total_notional_value=${ total_value_returned :,.2f} "
177
180
)
178
181
179
- # Log each position with value and sign
180
- for idx , (value , pubkey , market_idx , amt ) in enumerate (positions , 1 ):
181
- logger .info (
182
- f"==> [largest_perp_positions] Position { idx } : Market Index={ market_idx } , "
183
- f"Value=${ value :,.2f} , Base Asset Amount={ amt :,.2f} , Public Key={ pubkey } "
184
- )
185
-
186
182
data = {
187
183
"Market Index" : [pos [2 ] for pos in positions ],
188
184
"Value" : [f"${ pos [0 ]:,.2f} " for pos in positions ],
@@ -194,17 +190,16 @@ def get_largest_perp_positions(request: BackendRequest, number_of_positions: int
194
190
195
191
196
192
@router .get ("/most_levered_perp_positions_above_1m" )
197
- def get_most_levered_perp_positions_above_1m (request : BackendRequest , number_of_positions : int = 10 , market_index : int = None ):
193
+ def get_most_levered_perp_positions_above_1m (request : BackendRequest , market_index : int = None ):
198
194
"""
199
195
Get the most leveraged perpetual positions with value above $1 million.
200
196
201
197
This endpoint calculates the leverage of each perpetual position with a value
202
- over $1 million and returns the most leveraged positions, limited by number_of_positions .
198
+ over $1 million and returns the most leveraged positions.
203
199
Results can be filtered by market_index if provided.
204
200
205
201
Args:
206
202
request: The backend request object
207
- number_of_positions: Maximum number of positions to return (default: 10)
208
203
market_index: Optional market index to filter by
209
204
210
205
Returns:
@@ -216,7 +211,7 @@ def get_most_levered_perp_positions_above_1m(request: BackendRequest, number_of_
216
211
- Public Key (list[str]): The public keys of the position holders
217
212
"""
218
213
vat : Vat = request .state .backend_state .vat
219
- top_positions : list [tuple [float , str , int , float , float ]] = []
214
+ all_positions : list [tuple [float , str , int , float , float ]] = []
220
215
221
216
for user in vat .users .values ():
222
217
try :
@@ -242,26 +237,21 @@ def get_most_levered_perp_positions_above_1m(request: BackendRequest, number_of_
242
237
) * market_price_ui
243
238
leverage = base_asset_value / total_collateral
244
239
if base_asset_value > 1_000_000 :
245
- heap_item = (
240
+ item = (
246
241
to_financial (base_asset_value ),
247
242
user .user_public_key ,
248
243
position .market_index ,
249
244
position .base_asset_amount / BASE_PRECISION ,
250
245
leverage ,
251
246
)
252
-
253
- if len (top_positions ) < number_of_positions :
254
- heapq .heappush (top_positions , heap_item )
255
- else :
256
- heapq .heappushpop (top_positions , heap_item )
247
+ all_positions .append (item )
257
248
258
249
positions = sorted (
259
- top_positions ,
250
+ all_positions ,
260
251
key = lambda x : x [4 ],
252
+ reverse = True
261
253
)
262
254
263
- positions .reverse ()
264
-
265
255
data = {
266
256
"Market Index" : [pos [2 ] for pos in positions ],
267
257
"Value" : [f"${ pos [0 ]:,.2f} " for pos in positions ],
@@ -274,17 +264,16 @@ def get_most_levered_perp_positions_above_1m(request: BackendRequest, number_of_
274
264
275
265
276
266
@router .get ("/largest_spot_borrows" )
277
- def get_largest_spot_borrows (request : BackendRequest , number_of_positions : int = 10 , market_index : int = None ):
267
+ def get_largest_spot_borrows (request : BackendRequest , market_index : int = None ):
278
268
"""
279
269
Get the largest spot borrowing positions by value.
280
270
281
271
This endpoint retrieves the largest spot borrowing positions across all users,
282
- calculated based on the current market prices. Results can be limited by
283
- number_of_positions and filtered by market_index if provided.
272
+ calculated based on the current market prices. Results can be filtered by
273
+ market_index if provided.
284
274
285
275
Args:
286
276
request: The backend request object
287
- number_of_positions: Maximum number of positions to return (default: 10)
288
277
market_index: Optional market index to filter by
289
278
290
279
Returns:
@@ -295,7 +284,7 @@ def get_largest_spot_borrows(request: BackendRequest, number_of_positions: int =
295
284
- Public Key (list[str]): The public keys of the borrowers
296
285
"""
297
286
vat : Vat = request .state .backend_state .vat
298
- top_borrows : list [tuple [float , str , int , float ]] = []
287
+ all_borrows : list [tuple [float , str , int , float ]] = []
299
288
300
289
for user in vat .users .values ():
301
290
for position in user .get_user_account ().spot_positions :
@@ -312,24 +301,15 @@ def get_largest_spot_borrows(request: BackendRequest, number_of_positions: int =
312
301
borrow_value = (
313
302
position .scaled_balance / SPOT_BALANCE_PRECISION
314
303
) * market_price_ui
315
- heap_item = (
304
+ item = (
316
305
to_financial (borrow_value ),
317
306
user .user_public_key ,
318
307
position .market_index ,
319
308
position .scaled_balance / SPOT_BALANCE_PRECISION ,
320
309
)
310
+ all_borrows .append (item )
321
311
322
- if len (top_borrows ) < number_of_positions :
323
- heapq .heappush (top_borrows , heap_item )
324
- else :
325
- heapq .heappushpop (top_borrows , heap_item )
326
-
327
- borrows = sorted (
328
- (value , pubkey , market_idx , amt )
329
- for value , pubkey , market_idx , amt in top_borrows
330
- )
331
-
332
- borrows .reverse ()
312
+ borrows = sorted (all_borrows , key = lambda x : x [0 ], reverse = True )
333
313
334
314
data = {
335
315
"Market Index" : [pos [2 ] for pos in borrows ],
@@ -342,17 +322,16 @@ def get_largest_spot_borrows(request: BackendRequest, number_of_positions: int =
342
322
343
323
344
324
@router .get ("/most_levered_spot_borrows_above_1m" )
345
- def get_most_levered_spot_borrows_above_1m (request : BackendRequest , number_of_positions : int = 10 , market_index : int = None ):
325
+ def get_most_levered_spot_borrows_above_1m (request : BackendRequest , market_index : int = None ):
346
326
"""
347
327
Get the most leveraged spot borrowing positions with value above $750,000.
348
328
349
329
This endpoint calculates the leverage of each spot borrowing position with a value
350
- over $750,000 and returns the most leveraged positions, limited by number_of_positions .
330
+ over $750,000 and returns the most leveraged positions.
351
331
Results can be filtered by market_index if provided.
352
332
353
333
Args:
354
334
request: The backend request object
355
- number_of_positions: Maximum number of positions to return (default: 10)
356
335
market_index: Optional market index to filter by
357
336
358
337
Returns:
@@ -365,7 +344,7 @@ def get_most_levered_spot_borrows_above_1m(request: BackendRequest, number_of_po
365
344
- Error (list[str]): Error details if any (empty string if no error)
366
345
"""
367
346
vat : Vat = request .state .backend_state .vat
368
- top_borrows : list [tuple [float , str , int , float , float , str ]] = [] # Added error field
347
+ all_borrows : list [tuple [float , str , int , float , float , str ]] = [] # Added error field
369
348
error_positions = [] # Track positions with errors for logging
370
349
371
350
for user in vat .users .values ():
@@ -406,17 +385,16 @@ def get_most_levered_spot_borrows_above_1m(request: BackendRequest, number_of_po
406
385
"error" : position_error
407
386
})
408
387
409
- # If we don't have enough items yet, add this one with error
410
- if len (top_borrows ) < number_of_positions :
411
- heap_item = (
412
- borrow_value , # Will be sorted last due to 0 value
413
- user .user_public_key ,
414
- position .market_index ,
415
- scaled_balance ,
416
- leverage ,
417
- position_error ,
418
- )
419
- heapq .heappush (top_borrows , heap_item )
388
+ # Add this one with error
389
+ item = (
390
+ borrow_value , # Will be sorted last due to 0 value
391
+ user .user_public_key ,
392
+ position .market_index ,
393
+ scaled_balance ,
394
+ leverage ,
395
+ position_error ,
396
+ )
397
+ all_borrows .append (item )
420
398
else :
421
399
try :
422
400
market_price_ui = market_price .price / PRICE_PRECISION
@@ -432,19 +410,15 @@ def get_most_levered_spot_borrows_above_1m(request: BackendRequest, number_of_po
432
410
position_error = "Zero collateral"
433
411
434
412
if borrow_value > 750_000 :
435
- heap_item = (
413
+ item = (
436
414
to_financial (borrow_value ),
437
415
user .user_public_key ,
438
416
position .market_index ,
439
417
position .scaled_balance / SPOT_BALANCE_PRECISION ,
440
418
leverage ,
441
419
position_error , # Empty string if no error
442
420
)
443
-
444
- if len (top_borrows ) < number_of_positions :
445
- heapq .heappush (top_borrows , heap_item )
446
- else :
447
- heapq .heappushpop (top_borrows , heap_item )
421
+ all_borrows .append (item )
448
422
except Exception as e :
449
423
calc_error = f"Calculation error: { str (e )} "
450
424
position_error = calc_error if not position_error else f"{ position_error } ; { calc_error } "
@@ -465,7 +439,7 @@ def get_most_levered_spot_borrows_above_1m(request: BackendRequest, number_of_po
465
439
logger .warning (f"Found { len (error_positions )} positions with errors: { error_positions } " )
466
440
467
441
positions = sorted (
468
- top_borrows ,
442
+ all_borrows ,
469
443
key = lambda x : x [4 ] if not x [5 ] else float ('-inf' ), # Sort error positions first
470
444
reverse = True ,
471
445
)
0 commit comments