csm-dashboard 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
src/web/routes.py ADDED
@@ -0,0 +1,161 @@
1
+ """API endpoints for the web interface."""
2
+
3
+ from fastapi import APIRouter, HTTPException, Query
4
+
5
+ from ..services.operator_service import OperatorService
6
+
7
+ router = APIRouter()
8
+
9
+
10
+ @router.get("/operator/{identifier}")
11
+ async def get_operator(
12
+ identifier: str,
13
+ detailed: bool = Query(False, description="Include validator status from beacon chain"),
14
+ ):
15
+ """
16
+ Get operator data by address or ID.
17
+
18
+ - If identifier is numeric, treat as operator ID
19
+ - If identifier starts with 0x, treat as Ethereum address
20
+ - Add ?detailed=true to include validator status breakdown
21
+ """
22
+ service = OperatorService()
23
+
24
+ # Determine if this is an ID or address
25
+ if identifier.isdigit():
26
+ operator_id = int(identifier)
27
+ if operator_id < 0 or operator_id > 1_000_000:
28
+ raise HTTPException(status_code=400, detail="Invalid operator ID")
29
+ rewards = await service.get_operator_by_id(operator_id, detailed)
30
+ elif identifier.startswith("0x"):
31
+ rewards = await service.get_operator_by_address(identifier, detailed)
32
+ else:
33
+ raise HTTPException(status_code=400, detail="Invalid identifier format")
34
+
35
+ if rewards is None:
36
+ raise HTTPException(status_code=404, detail="Operator not found")
37
+
38
+ result = {
39
+ "operator_id": rewards.node_operator_id,
40
+ "manager_address": rewards.manager_address,
41
+ "reward_address": rewards.reward_address,
42
+ "rewards": {
43
+ "current_bond_eth": float(rewards.current_bond_eth),
44
+ "required_bond_eth": float(rewards.required_bond_eth),
45
+ "excess_bond_eth": float(rewards.excess_bond_eth),
46
+ "cumulative_rewards_shares": rewards.cumulative_rewards_shares,
47
+ "cumulative_rewards_eth": float(rewards.cumulative_rewards_eth),
48
+ "distributed_shares": rewards.distributed_shares,
49
+ "distributed_eth": float(rewards.distributed_eth),
50
+ "unclaimed_shares": rewards.unclaimed_shares,
51
+ "unclaimed_eth": float(rewards.unclaimed_eth),
52
+ "total_claimable_eth": float(rewards.total_claimable_eth),
53
+ },
54
+ "validators": {
55
+ "total": rewards.total_validators,
56
+ "active": rewards.active_validators,
57
+ "exited": rewards.exited_validators,
58
+ },
59
+ }
60
+
61
+ # Fetch active_since for basic (non-detailed) requests
62
+ # For detailed requests, it's already included in rewards.active_since
63
+ if not detailed and rewards.total_validators > 0:
64
+ active_since = await service.get_operator_active_since(rewards.node_operator_id)
65
+ if active_since:
66
+ result["active_since"] = active_since.isoformat()
67
+
68
+ # Add beacon chain validator details if available
69
+ if rewards.validators_by_status:
70
+ result["validators"]["by_status"] = rewards.validators_by_status
71
+
72
+ if rewards.avg_effectiveness is not None:
73
+ result["performance"] = {
74
+ "avg_effectiveness": round(rewards.avg_effectiveness, 2),
75
+ }
76
+
77
+ if detailed and rewards.validator_details:
78
+ result["validator_details"] = [v.to_dict() for v in rewards.validator_details]
79
+
80
+ # Add APY metrics if available
81
+ if rewards.apy:
82
+ result["apy"] = {
83
+ "historical_reward_apy_28d": rewards.apy.historical_reward_apy_28d,
84
+ "historical_reward_apy_ltd": rewards.apy.historical_reward_apy_ltd,
85
+ "bond_apy": rewards.apy.bond_apy,
86
+ "net_apy_28d": rewards.apy.net_apy_28d,
87
+ "net_apy_ltd": rewards.apy.net_apy_ltd,
88
+ }
89
+
90
+ # Add active_since if available
91
+ if rewards.active_since:
92
+ result["active_since"] = rewards.active_since.isoformat()
93
+
94
+ # Add health status if available
95
+ if rewards.health:
96
+ result["health"] = {
97
+ "bond_healthy": rewards.health.bond_healthy,
98
+ "bond_deficit_eth": float(rewards.health.bond_deficit_eth),
99
+ "stuck_validators_count": rewards.health.stuck_validators_count,
100
+ "slashed_validators_count": rewards.health.slashed_validators_count,
101
+ "validators_at_risk_count": rewards.health.validators_at_risk_count,
102
+ "strikes": {
103
+ "total_validators_with_strikes": rewards.health.strikes.total_validators_with_strikes,
104
+ "validators_at_risk": rewards.health.strikes.validators_at_risk,
105
+ "validators_near_ejection": rewards.health.strikes.validators_near_ejection,
106
+ "total_strikes": rewards.health.strikes.total_strikes,
107
+ "max_strikes": rewards.health.strikes.max_strikes,
108
+ },
109
+ "has_issues": rewards.health.has_issues,
110
+ }
111
+
112
+ return result
113
+
114
+
115
+ @router.get("/operators")
116
+ async def list_operators():
117
+ """List all operators with rewards in the current tree."""
118
+ service = OperatorService()
119
+ operator_ids = await service.get_all_operators_with_rewards()
120
+ return {"count": len(operator_ids), "operator_ids": operator_ids}
121
+
122
+
123
+ @router.get("/operator/{identifier}/strikes")
124
+ async def get_operator_strikes(identifier: str):
125
+ """Get detailed strikes for an operator's validators."""
126
+ service = OperatorService()
127
+
128
+ # Determine if this is an ID or address
129
+ if identifier.isdigit():
130
+ operator_id = int(identifier)
131
+ elif identifier.startswith("0x"):
132
+ operator_id = await service.onchain.find_operator_by_address(identifier)
133
+ if operator_id is None:
134
+ raise HTTPException(status_code=404, detail="Operator not found")
135
+ else:
136
+ raise HTTPException(status_code=400, detail="Invalid identifier format")
137
+
138
+ strikes = await service.get_operator_strikes(operator_id)
139
+
140
+ # Fetch frame dates for tooltip display
141
+ frame_dates = await service.get_recent_frame_dates(6)
142
+
143
+ return {
144
+ "operator_id": operator_id,
145
+ "frame_dates": frame_dates,
146
+ "validators": [
147
+ {
148
+ "pubkey": s.pubkey,
149
+ "strike_count": s.strike_count,
150
+ "strikes": s.strikes,
151
+ "at_ejection_risk": s.at_ejection_risk,
152
+ }
153
+ for s in strikes
154
+ ],
155
+ }
156
+
157
+
158
+ @router.get("/health")
159
+ async def health_check():
160
+ """Health check endpoint."""
161
+ return {"status": "healthy"}