fix(binance): read funding balance from wallet/balance API

Prefer sapi asset/wallet/balance Funding USDT (matches App), merge get-funding-asset and ccxt as fallbacks, and add optional BINANCE_FUNDING_INCLUDE_SPOT plus a richer verify script.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-04 07:12:28 +08:00
parent dd74770c66
commit b1952dcd6e
3 changed files with 132 additions and 22 deletions
@@ -3,6 +3,7 @@
python scripts/verify_binance_funding.py
打印 BINANCE_API_KEY 前 8 位便于与 Binance 控制台核对(不含 Secret)。用于服务器自检。
对比 App:资产 → 资金账户(Funding) / 现货账户(Spot) / U本位合约。
"""
import os
import sys
@@ -33,19 +34,51 @@ def main():
if not s or "REPLACE" in s.upper():
print("WARN: BINANCE_API_SECRET 为空或仍像占位符,请核对 .env")
print("BINANCE_API_KEY prefix (8 chars):", (k[:8] + "") if len(k) > 8 else "(short)")
print("BINANCE_FUNDING_INCLUDE_SPOT:", os.getenv("BINANCE_FUNDING_INCLUDE_SPOT", "false"))
import app as mod # noqa: E402
mod.ensure_markets_loaded()
fu = mod._fetch_binance_funding_usdt()
print(">>> _fetch_binance_funding_usdt() =", fu)
ccy = getattr(mod, "TRANSFER_CCY", "USDT")
try:
raw = mod.exchange.sapiGetAssetWalletBalance({"quoteAsset": ccy})
print(f"\n>>> sapi/v1/asset/wallet/balance (quoteAsset={ccy}):")
if isinstance(raw, list):
for row in raw:
if isinstance(row, dict):
print(
" ",
row.get("walletName"),
"activate=",
row.get("activate"),
"balance=",
row.get("balance"),
)
else:
print(" ", raw)
except Exception as e:
print(">>> wallet/balance error:", e)
try:
raw = mod.exchange.sapiPostAssetGetFundingAsset({"asset": ccy})
print(f"\n>>> get-funding-asset (asset={ccy}):", raw)
except Exception as e:
print(">>> get-funding-asset error:", e)
fu = mod._fetch_binance_funding_usdt()
print("\n>>> _fetch_binance_funding_usdt() (页顶资金账户) =", fu)
try:
fw = mod._fetch_binance_funding_usdt_from_wallet_overview()
print(">>> _fetch_binance_funding_usdt_from_wallet_overview() =", fw)
sp = mod._fetch_binance_spot_usdt_total()
print(">>> _fetch_binance_spot_usdt_total() =", sp)
sw = mod._fetch_binance_swap_usdt_total()
print(">>> _fetch_binance_swap_usdt_total() (合约账户) =", sw)
sf = mod._fetch_binance_swap_usdt_free()
print(">>> _fetch_binance_swap_usdt_free() (合约可用) =", sf)
except Exception as e:
print(">>> swap balance fetch error:", e)
print(">>> balance fetch error:", e)
if __name__ == "__main__":