Merge OKX dual APIs into one OKX_API_* account for perp and options.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-05 21:28:33 +08:00
parent 22e6b68e6d
commit 09a763e47d
26 changed files with 166 additions and 473 deletions
+1 -58
View File
@@ -92,12 +92,10 @@ def _build_cfg(app_module: Any) -> dict[str, Any]:
quote_option_contract,
spot_market_swap_usdt_usdc,
transfer_ccy,
transfer_main_sub_account,
)
return {
"enabled": _env_bool("OKX_OPTIONS_ENABLED", False),
"sub_account_name": (os.getenv("OKX_SUB_ACCOUNT_NAME") or "").strip(),
"get_db": app_module.get_db,
"login_required": app_module.login_required,
"exchange_options": getattr(app_module, "exchange_options", None),
@@ -132,7 +130,6 @@ def _build_cfg(app_module: Any) -> dict[str, Any]:
"execute_convert": execute_convert,
"transfer_ccy": transfer_ccy,
"spot_market_swap_usdt_usdc": spot_market_swap_usdt_usdc,
"transfer_main_sub_account": transfer_main_sub_account,
"options_api_ready": options_api_ready,
"app_module": app_module,
}
@@ -357,13 +354,7 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
if ex is None:
return jsonify({"ok": False, "msg": err})
force = (request.args.get("force") or "").strip().lower() in ("1", "true", "yes")
scope = (request.args.get("scope") or "main").strip().lower()
bal = cfg["fetch_options_balances"](
ex,
force=force,
scope=scope,
sub_acct=cfg.get("sub_account_name") or "",
)
bal = cfg["fetch_options_balances"](ex, force=force, scope="main")
return jsonify({"ok": True, **bal, "trade_budget": cfg["trade_budget"]})
@app.route("/api/options/chain")
@@ -1272,54 +1263,6 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
_mark_balances_stale(cfg)
return jsonify(result)
@app.route("/api/options/cross-transfer", methods=["POST"])
@lr
def api_options_cross_transfer():
ex, err = _require_options_ex(cfg)
if ex is None:
return jsonify({"ok": False, "msg": err})
data = request.get_json(silent=True) or {}
ccy = (data.get("ccy") or "USDT").upper()
direction = (data.get("direction") or "sub_to_main").strip()
from_account = (data.get("from_account") or data.get("account") or "funding").strip()
to_account = (data.get("to_account") or data.get("account") or "funding").strip()
try:
amount = float(data.get("amount"))
except (TypeError, ValueError):
return jsonify({"ok": False, "msg": "数量无效"})
main_to_sub = direction == "main_to_sub"
result = cfg["transfer_main_sub_account"](
ex,
ccy=ccy,
amount=amount,
sub_acct=cfg.get("sub_account_name") or "",
main_to_sub=main_to_sub,
from_account=from_account,
to_account=to_account,
)
if result.get("ok"):
conn = cfg["get_db"]()
try:
init_options_tables(conn)
conn.execute(
"""
INSERT INTO options_transfer_log (ccy, amount, from_account, to_account, status, message)
VALUES (?, ?, ?, ?, 'ok', ?)
""",
(
ccy,
amount,
("main" if main_to_sub else "sub") + ":" + from_account,
("sub" if main_to_sub else "main") + ":" + to_account,
"cross",
),
)
conn.commit()
finally:
conn.close()
_mark_balances_stale(cfg)
return jsonify(result)
@app.route("/api/options/history")
@lr
def api_options_history():