Parse OKX spot swap errors into friendly Chinese instead of raw JSON.

Map sCode 51008 to a clear insufficient-balance message for the currency exchange UI.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-13 08:54:53 +08:00
parent 8e492987ec
commit c4bac23c48
2 changed files with 18 additions and 10 deletions
+8 -10
View File
@@ -19,6 +19,7 @@ from lib.options.options_pricing_lib import (
)
_OKX_OPTION_ERR_ZH: dict[str, str] = {
"51008": "资金账户 USDT 可用余额不足",
"51018": "期权账户不能持有净空头头寸",
"51019": "期权买入须使用逐仓模式(全仓模式下不能持有多头净头寸)",
}
@@ -1114,10 +1115,9 @@ def execute_convert(ex: ccxt.okx, quote_id: str) -> dict[str, Any]:
data = (resp or {}).get("data") or []
if data and str(data[0].get("sCode", "0")) == "0":
return {"ok": True, "data": data[0], "raw": resp}
msg = data[0].get("sMsg") if data else str(resp)
return {"ok": False, "msg": msg or "兑换失败", "raw": resp}
return {"ok": False, "msg": _okx_trade_error_message(resp=resp), "raw": resp}
except Exception as e:
return {"ok": False, "msg": str(e)}
return {"ok": False, "msg": _okx_trade_error_message(e)}
def transfer_ccy(
@@ -1133,7 +1133,7 @@ def transfer_ccy(
resp = ex.transfer(str(ccy).upper(), float(amount), from_account, to_account)
return {"ok": True, "data": resp}
except Exception as e:
return {"ok": False, "msg": str(e)}
return {"ok": False, "msg": _okx_trade_error_message(e)}
_OKX_ACCT_CODE = {"funding": "6", "trading": "18", "spot": "18"}
@@ -1199,10 +1199,9 @@ def spot_market_swap_usdt_usdc(
data = (resp or {}).get("data") or []
if data and str(data[0].get("sCode")) == "0":
return {"ok": True, "data": data[0], "raw": resp}
msg = data[0].get("sMsg") if data else str(resp)
return {"ok": False, "msg": msg or "现货兑换失败", "raw": resp}
return {"ok": False, "msg": _okx_trade_error_message(resp=resp), "raw": resp}
except Exception as e:
return {"ok": False, "msg": str(e)}
return {"ok": False, "msg": _okx_trade_error_message(e)}
def transfer_main_sub_account(
@@ -1237,10 +1236,9 @@ def transfer_main_sub_account(
data = (resp or {}).get("data") or []
if data and str(data[0].get("sCode", "0")) == "0":
return {"ok": True, "data": data[0], "raw": resp}
msg = data[0].get("sMsg") if data else str(resp)
return {"ok": False, "msg": msg or "主/子账户划转失败", "raw": resp}
return {"ok": False, "msg": _okx_trade_error_message(resp=resp), "raw": resp}
except Exception as e:
return {"ok": False, "msg": str(e)}
return {"ok": False, "msg": _okx_trade_error_message(e)}
def format_position_row(
+10
View File
@@ -30,6 +30,16 @@ class TestOkxSpotSwap(unittest.TestCase):
self.assertEqual(body["tgtCcy"], "base_ccy")
self.assertEqual(body["side"], "sell")
def test_insufficient_balance_returns_chinese_message(self):
ex = MagicMock()
ex.private_post_trade_order.side_effect = Exception(
'okx {"code":"1","data":[{"sCode":"51008","sMsg":"Order failed. Your available USDT balance is insufficient."}]}'
)
result = spot_market_swap_usdt_usdc(ex, direction="usdt_to_usdc", amount=20)
self.assertFalse(result["ok"])
self.assertEqual(result["msg"], "资金账户 USDT 可用余额不足")
self.assertNotIn("{", result["msg"])
if __name__ == "__main__":
unittest.main()