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:
@@ -19,6 +19,7 @@ from lib.options.options_pricing_lib import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
_OKX_OPTION_ERR_ZH: dict[str, str] = {
|
_OKX_OPTION_ERR_ZH: dict[str, str] = {
|
||||||
|
"51008": "资金账户 USDT 可用余额不足",
|
||||||
"51018": "期权账户不能持有净空头头寸",
|
"51018": "期权账户不能持有净空头头寸",
|
||||||
"51019": "期权买入须使用逐仓模式(全仓模式下不能持有多头净头寸)",
|
"51019": "期权买入须使用逐仓模式(全仓模式下不能持有多头净头寸)",
|
||||||
}
|
}
|
||||||
@@ -1114,10 +1115,9 @@ def execute_convert(ex: ccxt.okx, quote_id: str) -> dict[str, Any]:
|
|||||||
data = (resp or {}).get("data") or []
|
data = (resp or {}).get("data") or []
|
||||||
if data and str(data[0].get("sCode", "0")) == "0":
|
if data and str(data[0].get("sCode", "0")) == "0":
|
||||||
return {"ok": True, "data": data[0], "raw": resp}
|
return {"ok": True, "data": data[0], "raw": resp}
|
||||||
msg = data[0].get("sMsg") if data else str(resp)
|
return {"ok": False, "msg": _okx_trade_error_message(resp=resp), "raw": resp}
|
||||||
return {"ok": False, "msg": msg or "兑换失败", "raw": resp}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"ok": False, "msg": str(e)}
|
return {"ok": False, "msg": _okx_trade_error_message(e)}
|
||||||
|
|
||||||
|
|
||||||
def transfer_ccy(
|
def transfer_ccy(
|
||||||
@@ -1133,7 +1133,7 @@ def transfer_ccy(
|
|||||||
resp = ex.transfer(str(ccy).upper(), float(amount), from_account, to_account)
|
resp = ex.transfer(str(ccy).upper(), float(amount), from_account, to_account)
|
||||||
return {"ok": True, "data": resp}
|
return {"ok": True, "data": resp}
|
||||||
except Exception as e:
|
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"}
|
_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 []
|
data = (resp or {}).get("data") or []
|
||||||
if data and str(data[0].get("sCode")) == "0":
|
if data and str(data[0].get("sCode")) == "0":
|
||||||
return {"ok": True, "data": data[0], "raw": resp}
|
return {"ok": True, "data": data[0], "raw": resp}
|
||||||
msg = data[0].get("sMsg") if data else str(resp)
|
return {"ok": False, "msg": _okx_trade_error_message(resp=resp), "raw": resp}
|
||||||
return {"ok": False, "msg": msg or "现货兑换失败", "raw": resp}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"ok": False, "msg": str(e)}
|
return {"ok": False, "msg": _okx_trade_error_message(e)}
|
||||||
|
|
||||||
|
|
||||||
def transfer_main_sub_account(
|
def transfer_main_sub_account(
|
||||||
@@ -1237,10 +1236,9 @@ def transfer_main_sub_account(
|
|||||||
data = (resp or {}).get("data") or []
|
data = (resp or {}).get("data") or []
|
||||||
if data and str(data[0].get("sCode", "0")) == "0":
|
if data and str(data[0].get("sCode", "0")) == "0":
|
||||||
return {"ok": True, "data": data[0], "raw": resp}
|
return {"ok": True, "data": data[0], "raw": resp}
|
||||||
msg = data[0].get("sMsg") if data else str(resp)
|
return {"ok": False, "msg": _okx_trade_error_message(resp=resp), "raw": resp}
|
||||||
return {"ok": False, "msg": msg or "主/子账户划转失败", "raw": resp}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"ok": False, "msg": str(e)}
|
return {"ok": False, "msg": _okx_trade_error_message(e)}
|
||||||
|
|
||||||
|
|
||||||
def format_position_row(
|
def format_position_row(
|
||||||
|
|||||||
@@ -30,6 +30,16 @@ class TestOkxSpotSwap(unittest.TestCase):
|
|||||||
self.assertEqual(body["tgtCcy"], "base_ccy")
|
self.assertEqual(body["tgtCcy"], "base_ccy")
|
||||||
self.assertEqual(body["side"], "sell")
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user