#!/usr/bin/env python3
"""
  python scripts/verify_okx_funding.py

打印 OKX_API_KEY 前 8 位便于与 Binance 控制台核对(不含 Secret).用于服务器自检.
"""
import os
import sys

BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE)


def load_env(path):
    if not os.path.exists(path):
        return
    for line in open(path, "r", encoding="utf-8", errors="ignore"):
        line = line.strip()
        if not line or line.startswith("#") or "=" not in line:
            continue
        k, v = line.split("=", 1)
        k = k.strip().lstrip("\ufeff")
        if k.replace("_", "").isalnum():
            os.environ[k] = v.strip().strip('"').strip("'")


def main():
    load_env(os.path.join(BASE, ".env"))
    k = (os.getenv("OKX_API_KEY") or "").strip()
    s = (os.getenv("OKX_API_SECRET") or "").strip()
    if not k or "REPLACE" in k.upper():
        print("WARN: OKX_API_KEY 为空或仍像占位符,请核对 .env")
    if not s or "REPLACE" in s.upper():
        print("WARN: OKX_API_SECRET 为空或仍像占位符,请核对 .env")
    print("OKX_API_KEY prefix (8 chars):", (k[:8] + "…") if len(k) > 8 else "(short)")

    import app as mod  # noqa: E402

    mod.ensure_markets_loaded()
    fu = mod._fetch_okx_funding_usdt()
    print(">>> _fetch_okx_funding_usdt() =", fu)
    try:
        sw = mod._fetch_okx_swap_usdt_total()
        print(">>> _fetch_okx_swap_usdt_total() (合约账户) =", sw)
        sf = mod._fetch_okx_swap_usdt_free()
        print(">>> _fetch_okx_swap_usdt_free() (合约可用) =", sf)
    except Exception as e:
        print(">>> swap balance fetch error:", e)


if __name__ == "__main__":
    main()
