Administrator
2 hours ago 1278ee2bd43b401489b4377b0eee5259b3d5bbbb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.xcong.excoin.modules.okxApi;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
 
@Slf4j
public class OkxRestClient {
 
    private final String baseUrl;
    private final String apiKey;
    private final String secretKey;
    private final String passphrase;
    private final boolean isSimulate;
 
    public OkxRestClient(String baseUrl, String apiKey, String secretKey, String passphrase, boolean isSimulate) {
        this.baseUrl = baseUrl;
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.passphrase = passphrase;
        this.isSimulate = isSimulate;
    }
 
    public boolean setPositionMode(String posMode) {
        JSONObject params = new JSONObject();
        params.put("posMode", posMode);
        JSONObject result = post("/api/v5/account/set-position-mode", params);
        return isSuccess(result, "设置持仓方式");
    }
 
    public boolean setLeverage(String instId, String lever, String mgnMode) {
        JSONObject params = new JSONObject();
        params.put("instId", instId);
        params.put("lever", lever);
        params.put("mgnMode", mgnMode);
        JSONObject result = post("/api/v5/account/set-leverage", params);
        return isSuccess(result, "设置杠杆");
    }
 
    public String 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);
        String instIdCode = first.getString("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 {
            String bodyStr = body.toJSONString();
            String timestamp = OkxWsUtil.getIso8601Timestamp();
            String sign = OkxWsUtil.signRest(timestamp, "POST", path, bodyStr, secretKey);
 
            URL url = new URL(baseUrl + path);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            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");
            }
 
            try (OutputStream os = conn.getOutputStream()) {
                os.write(bodyStr.getBytes(StandardCharsets.UTF_8));
                os.flush();
            }
 
            int code = conn.getResponseCode();
            String response = readResponse(conn);
            if (code < 200 || code >= 300) {
                log.error("[REST] POST {} → HTTP {} body:{}", path, code, response);
            }
 
            return JSON.parseObject(response);
        } catch (Exception e) {
            log.error("[REST] POST {} 失败: {}", path, e.getMessage());
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
 
    private String readResponse(HttpURLConnection conn) throws Exception {
        StringBuilder sb = new StringBuilder();
        String line;
        if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            }
        } else {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8))) {
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            }
        }
        return sb.toString();
    }
 
    private boolean isSuccess(JSONObject result, String label) {
        if (result == null) {
            log.error("[REST] {}失败: 无响应", label);
            return false;
        }
        String code = result.getString("code");
        if ("0".equals(code)) {
            log.info("[REST] {}成功", label);
            return true;
        }
        if ("59000".equals(code)) {
            log.info("[REST] {}已设置(59000:配置未变更)", label);
            return true;
        }
        log.error("[REST] {}失败, code:{}, msg:{}", label, code, result.getString("msg"));
        return false;
    }
}