交易所 API 改为仅服务器配置:前端去掉密钥、新机示例为空,并防止坏钥反复请求触发 Gate 封 IP。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -35,6 +35,11 @@ import sys
|
||||
if _REPO_ROOT not in sys.path:
|
||||
sys.path.insert(0, _REPO_ROOT)
|
||||
from lib.paths import common_static_dir
|
||||
from lib.exchange.api_credentials_lib import (
|
||||
is_exchange_auth_error,
|
||||
load_markets_public_fallback,
|
||||
normalize_api_credential,
|
||||
)
|
||||
from lib.ai.ai_client import ai_generate, ai_review, ai_short_advice
|
||||
from lib.ai.ai_review_lib import (
|
||||
build_journal_ai_chart_path,
|
||||
@@ -360,9 +365,9 @@ def _promote_legacy_options_api_keys() -> None:
|
||||
|
||||
|
||||
_promote_legacy_options_api_keys()
|
||||
OKX_API_KEY = os.getenv("OKX_API_KEY", "")
|
||||
OKX_API_SECRET = os.getenv("OKX_API_SECRET", "")
|
||||
OKX_API_PASSPHRASE = os.getenv("OKX_API_PASSPHRASE", "")
|
||||
OKX_API_KEY = normalize_api_credential(os.getenv("OKX_API_KEY"))
|
||||
OKX_API_SECRET = normalize_api_credential(os.getenv("OKX_API_SECRET"))
|
||||
OKX_API_PASSPHRASE = normalize_api_credential(os.getenv("OKX_API_PASSPHRASE"))
|
||||
OKX_OPTIONS_ENABLED = os.getenv("OKX_OPTIONS_ENABLED", "false").lower() in ("1", "true", "yes", "on")
|
||||
OKX_OPTIONS_TRADE_BUDGET_USDC = float(os.getenv("OKX_OPTIONS_TRADE_BUDGET_USDC", "10"))
|
||||
OKX_OPTIONS_DEFAULT_UNDERLY = (os.getenv("OKX_OPTIONS_DEFAULT_UNDERLY") or "ETH").strip().upper()
|
||||
@@ -506,6 +511,7 @@ if OKX_API_KEY and OKX_API_SECRET and OKX_API_PASSPHRASE:
|
||||
exchange_options.password = OKX_API_PASSPHRASE
|
||||
|
||||
MARKETS_LOADED = False
|
||||
EXCHANGE_AUTH_DISABLED_MSG = ""
|
||||
ACCOUNT_BALANCE_CACHE = {
|
||||
"updated_at": 0.0,
|
||||
"funding_usdt": None,
|
||||
@@ -2485,6 +2491,8 @@ def enrich_order_item(raw_item, current_capital):
|
||||
|
||||
|
||||
def ensure_okx_live_ready():
|
||||
if EXCHANGE_AUTH_DISABLED_MSG:
|
||||
return False, EXCHANGE_AUTH_DISABLED_MSG
|
||||
if not LIVE_TRADING_ENABLED:
|
||||
return False, "未开启实盘下单(LIVE_TRADING_ENABLED=false)"
|
||||
if not (OKX_API_KEY and OKX_API_SECRET and OKX_API_PASSPHRASE):
|
||||
@@ -2517,6 +2525,23 @@ def order_row_key_signal_type(row):
|
||||
return None
|
||||
|
||||
|
||||
def _disable_private_api_after_auth_error(exc):
|
||||
global EXCHANGE_AUTH_DISABLED_MSG, OKX_API_KEY, OKX_API_SECRET, OKX_API_PASSPHRASE
|
||||
from lib.exchange.api_credentials_lib import strip_ccxt_credentials
|
||||
|
||||
strip_ccxt_credentials(exchange)
|
||||
try:
|
||||
strip_ccxt_credentials(exchange_options)
|
||||
except Exception:
|
||||
pass
|
||||
OKX_API_KEY = ""
|
||||
OKX_API_SECRET = ""
|
||||
OKX_API_PASSPHRASE = ""
|
||||
EXCHANGE_AUTH_DISABLED_MSG = (
|
||||
f"API 鉴权失败,已停止私有请求(请在服务器 .env 修正密钥后重启): {exc}"
|
||||
)
|
||||
|
||||
|
||||
def _extract_usdt_total(balance):
|
||||
usdt_info = balance.get("USDT", {}) if isinstance(balance, dict) else {}
|
||||
total_map = balance.get("total", {}) if isinstance(balance, dict) else {}
|
||||
@@ -2637,8 +2662,9 @@ def get_exchange_capitals(force=False):
|
||||
ACCOUNT_BALANCE_CACHE["funding_usdt"] = funding
|
||||
ACCOUNT_BALANCE_CACHE["trading_usdt"] = trading
|
||||
ACCOUNT_BALANCE_CACHE["updated_at"] = now_ts
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
if is_exchange_auth_error(e):
|
||||
_disable_private_api_after_auth_error(e)
|
||||
return ACCOUNT_BALANCE_CACHE["funding_usdt"], ACCOUNT_BALANCE_CACHE["trading_usdt"]
|
||||
|
||||
|
||||
@@ -2869,7 +2895,14 @@ def build_okx_order_params(direction, reduce_only=False):
|
||||
def ensure_markets_loaded(force=False):
|
||||
global MARKETS_LOADED
|
||||
if force or not MARKETS_LOADED:
|
||||
exchange.load_markets(reload=force)
|
||||
try:
|
||||
exchange.load_markets(reload=force)
|
||||
except Exception as e:
|
||||
if is_exchange_auth_error(e) and (getattr(exchange, "apiKey", None) or getattr(exchange, "secret", None)):
|
||||
_disable_private_api_after_auth_error(e)
|
||||
load_markets_public_fallback(exchange, reload=True)
|
||||
else:
|
||||
raise
|
||||
MARKETS_LOADED = True
|
||||
|
||||
|
||||
@@ -2985,6 +3018,8 @@ def _okx_place_tp_sl_orders(exchange_symbol, direction, amount, stop_loss, take_
|
||||
|
||||
|
||||
def exchange_private_api_configured():
|
||||
if EXCHANGE_AUTH_DISABLED_MSG:
|
||||
return False
|
||||
return bool(OKX_API_KEY and OKX_API_SECRET and OKX_API_PASSPHRASE)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user