forked from euler-xyz/liquidation-bot-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
49 lines (37 loc) · 1.62 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Module for handling API routes"""
from flask import Blueprint, make_response, jsonify, request
import math
from .liquidation_bot import logger
from .bot_manager import ChainManager
liquidation = Blueprint("liquidation", __name__)
chain_manager = None
def start_monitor(chain_ids=None):
"""Start monitoring for specified chains, defaults to mainnet if none specified"""
global chain_manager
if chain_ids is None:
chain_ids = [1] # Default to Ethereum mainnet
chain_manager = ChainManager(chain_ids, notify=True)
chain_manager.start()
return chain_manager
@liquidation.route("/allPositions", methods=["GET"])
def get_all_positions():
chain_id = int(request.args.get("chainId", 1)) # Default to mainnet if not specified
if not chain_manager or chain_id not in chain_manager.monitors:
return jsonify({"error": f"Monitor not initialized for chain {chain_id}"}), 500
logger.info("API: Getting all positions for chain %s", chain_id)
monitor = chain_manager.monitors[chain_id]
sorted_accounts = monitor.get_accounts_by_health_score()
response = []
for (address, owner, sub_account, health_score, value_borrowed, vault_name, vault_symbol) in sorted_accounts:
if math.isinf(health_score):
continue
response.append({
"address": owner,
"account_address": address,
"sub_account": sub_account,
"health_score": health_score,
"value_borrowed": value_borrowed,
"vault_name": vault_name,
"vault_symbol": vault_symbol
})
return make_response(jsonify(response))