diff --git a/backend/app/api/hyperliquid.py b/backend/app/api/hyperliquid.py index 057fe30..13c9f4b 100644 --- a/backend/app/api/hyperliquid.py +++ b/backend/app/api/hyperliquid.py @@ -136,7 +136,7 @@ async def get_positions( "size": abs(size), "entry_price": pos["entry_price"], "unrealized_pnl": pos["unrealized_pnl"], - "leverage": pos.get("leverage", {}).get("value", "N/A"), + "leverage": (pos.get("leverage") or {}).get("value", "N/A") if isinstance(pos.get("leverage"), dict) else (pos.get("leverage") or "N/A"), "liquidation_price": pos.get("liquidation_price"), "take_profit": tp_sl.get("take_profit"), "stop_loss": tp_sl.get("stop_loss"), diff --git a/backend/app/crypto_agent/crypto_agent.py b/backend/app/crypto_agent/crypto_agent.py index 6d2603d..50a9adc 100644 --- a/backend/app/crypto_agent/crypto_agent.py +++ b/backend/app/crypto_agent/crypto_agent.py @@ -928,7 +928,11 @@ class CryptoAgent: # 转换持仓格式 position_list = [] for pos in hl_state["positions"]: + if not isinstance(pos, dict): + continue position_data = pos.get("position", {}) + if not isinstance(position_data, dict): + continue coin = position_data.get("coin") size = float(position_data.get("szi", 0)) diff --git a/backend/app/services/hyperliquid_trading_service.py b/backend/app/services/hyperliquid_trading_service.py index bd0a40b..cf2e89d 100644 --- a/backend/app/services/hyperliquid_trading_service.py +++ b/backend/app/services/hyperliquid_trading_service.py @@ -355,6 +355,9 @@ class HyperliquidTradingService: continue order_type = order.get("order_type", {}) + # 防御性检查:确保 order_type 是 dict + if not isinstance(order_type, dict): + continue # 止盈:限价单 if "limit" in order_type and order["price"] > 0: @@ -583,7 +586,7 @@ class HyperliquidTradingService: "size": size, # 正数=多头,负数=空头 "entry_price": float(position_data.get("entryPx", 0)), "unrealized_pnl": float(position_data.get("unrealizedPnl", 0)), - "leverage": position_data.get("leverage", {}).get("value"), + "leverage": position_data.get("leverage", {}).get("value") if isinstance(position_data.get("leverage"), dict) else position_data.get("leverage"), "liquidation_price": position_data.get("liquidationPx"), "stop_loss": tp_sl_prices.get("stop_loss"), "take_profit": tp_sl_prices.get("take_profit"),