Administrator
5 hours ago afc858458cbdde1d5f3f2cfce6d056656bf75c16
src/main/java/com/xcong/excoin/modules/okxApi/OkxRestClient.java
@@ -44,6 +44,80 @@
        return isSuccess(result, "设置杠杆");
    }
    public boolean cancelAllOrders(String instId) {
        JSONObject params = new JSONObject();
        params.put("instType", "SWAP");
        params.put("instId", instId);
        JSONObject result = post("/api/v5/trade/cancel-all-orders", params);
        return isSuccess(result, "撤销所有委托单");
    }
    public boolean closeAllPositions(String instId, String mgnMode) {
        JSONObject params = new JSONObject();
        params.put("instType", "SWAP");
        params.put("instId", instId);
        params.put("mgnMode", mgnMode);
        JSONObject result = post("/api/v5/trade/close-all-positions", params);
        return isSuccess(result, "平仓ETH");
    }
    public Long fetchInstIdCode(String instType, String instId) {
        String path = "/api/v5/account/instruments?instType=" + instType + "&instId=" + instId;
        JSONObject result = get(path);
        if (result == null || !"0".equals(result.getString("code"))) {
            log.error("[REST] 获取instIdCode失败, code:{}, msg:{}",
                    result != null ? result.getString("code") : "null",
                    result != null ? result.getString("msg") : "no response");
            return null;
        }
        com.alibaba.fastjson.JSONArray data = result.getJSONArray("data");
        if (data == null || data.isEmpty()) {
            log.error("[REST] 获取instIdCode失败: data为空");
            return null;
        }
        JSONObject first = data.getJSONObject(0);
        Long instIdCode = first.getLong("instIdCode");
        log.info("[REST] 获取instIdCode成功, instId:{}, instIdCode:{}", instId, instIdCode);
        return instIdCode;
    }
    private JSONObject get(String path) {
        HttpURLConnection conn = null;
        try {
            String timestamp = OkxWsUtil.getIso8601Timestamp();
            String sign = OkxWsUtil.signRest(timestamp, "GET", path, "", secretKey);
            URL url = new URL(baseUrl + path);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(15000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("OK-ACCESS-KEY", apiKey);
            conn.setRequestProperty("OK-ACCESS-SIGN", sign);
            conn.setRequestProperty("OK-ACCESS-TIMESTAMP", timestamp);
            conn.setRequestProperty("OK-ACCESS-PASSPHRASE", passphrase);
            if (isSimulate) {
                conn.setRequestProperty("x-simulated-trading", "1");
            }
            int code = conn.getResponseCode();
            String response = readResponse(conn);
            if (code < 200 || code >= 300) {
                log.error("[REST] GET {} → HTTP {} body:{}", path, code, response);
            }
            return JSON.parseObject(response);
        } catch (Exception e) {
            log.error("[REST] GET {} 失败: {}", path, e.getMessage());
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
    private JSONObject post(String path, JSONObject body) {
        HttpURLConnection conn = null;
        try {
@@ -73,7 +147,9 @@
            int code = conn.getResponseCode();
            String response = readResponse(conn);
            log.info("[REST] POST {} → HTTP {} body:{}", path, code, response);
            if (code < 200 || code >= 300) {
                log.error("[REST] POST {} → HTTP {} body:{}", path, code, response);
            }
            return JSON.parseObject(response);
        } catch (Exception e) {