package com.xcong.excoin.modules.okxNewPrice.okxpi.order.impl;
|
|
import com.xcong.excoin.modules.okxNewPrice.utils.FebsResponse;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.Dto.QuantApiMessage;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.Dto.TradeOrderDto;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.order.ITradeOrderService;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.order.vo.QuantExchangeReturnVo;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.trade.TradeRequestBuy;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.trade.TradeRequestSell;
|
import com.xcong.excoin.modules.okxNewPrice.jiaoyi.IMQService;
|
import lombok.SneakyThrows;
|
import org.json.JSONObject;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import javax.crypto.Mac;
|
import javax.crypto.spec.SecretKeySpec;
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.io.OutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.Base64;
|
|
@Service("oKXTradeOrderServiceImpl")
|
public class OKXTradeOrderServiceImpl implements ITradeOrderService {
|
@Value("${spring.OKEX.baseurl}")
|
private String baseurl;
|
|
@Resource
|
private IMQService imqService;
|
|
@SneakyThrows
|
@Override
|
public FebsResponse QuantExchangeReturnVo(TradeOrderDto tradeOrderDto, QuantApiMessage quantApiMessage) {
|
try {
|
// 构建订单的JSON对象
|
JSONObject jsonBody = new JSONObject();
|
jsonBody.put("instId", "BTC-USDT");
|
jsonBody.put("tdMode", "cash");
|
jsonBody.put("side", "buy");
|
jsonBody.put("ordType", "limit");
|
jsonBody.put("sz", "0.01");
|
jsonBody.put("px", "30000");
|
|
// 发起下单请求
|
String result = postRequest("/api/v5/trade/order", jsonBody.toString(),quantApiMessage);
|
System.out.println("Result: " + result);
|
|
//解析返回数据
|
JSONObject jsonResponse = new JSONObject(result);
|
if (jsonResponse.has("code") && "0".equals(jsonResponse.getString("code"))) {
|
// 订单提交成功
|
JSONObject jSONObject = jsonResponse.getJSONObject("data");
|
String clOrdId = jSONObject.getString("clOrdId");
|
String ordId = jSONObject.getString("ordId");
|
QuantExchangeReturnVo quantExchangeReturnVo = new QuantExchangeReturnVo();
|
quantExchangeReturnVo.setCode("0");
|
quantExchangeReturnVo.setOrdId(ordId);
|
quantExchangeReturnVo.setClOrdId(clOrdId);
|
return new FebsResponse().success().data(quantExchangeReturnVo);
|
} else {
|
String code = jsonResponse.getString("code");
|
String msg = jsonResponse.getString("msg");
|
QuantExchangeReturnVo quantExchangeReturnVo = new QuantExchangeReturnVo();
|
quantExchangeReturnVo.setCode(code);
|
quantExchangeReturnVo.setMessage(msg);
|
return new FebsResponse().fail().data(quantExchangeReturnVo);
|
}
|
|
} catch (Exception e) {
|
return new FebsResponse().fail().message("下单失败");
|
}
|
}
|
|
@Override
|
public void operationBuyMsg(TradeRequestBuy returnVo) {
|
|
imqService.operationBuyMsg(returnVo);
|
}
|
|
@Override
|
public void operationSellMsg(TradeRequestSell returnVo) {
|
|
imqService.operationSellMsg(returnVo);
|
}
|
|
private String postRequest(String endpoint, String body, QuantApiMessage quantApiMessage) throws Exception {
|
URL url = new URL(baseurl + endpoint);
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setDoOutput(true);
|
connection.setRequestMethod("POST");
|
connection.setRequestProperty("Content-Type", "application/json");
|
connection.setRequestProperty("OK-ACCESS-KEY", quantApiMessage.getASecretkey());
|
connection.setRequestProperty("OK-ACCESS-SIGN", generateSignature(body,quantApiMessage));
|
connection.setRequestProperty("OK-ACCESS-TIMESTAMP", getTimestamp());
|
connection.setRequestProperty("OK-ACCESS-PASSPHRASE", quantApiMessage.getPassPhrass());
|
|
try (OutputStream os = connection.getOutputStream()) {
|
os.write(body.getBytes());
|
os.flush();
|
}
|
|
if (connection.getResponseCode() != 200) {
|
throw new RuntimeException("Failed : HTTP Error code : " + connection.getResponseCode());
|
}
|
|
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
|
StringBuilder output = new StringBuilder();
|
String line;
|
while ((line = br.readLine()) != null) {
|
output.append(line);
|
}
|
connection.disconnect();
|
return output.toString();
|
}
|
|
private static String generateSignature(String body, QuantApiMessage quantApiMessage) throws Exception {
|
String preHash = getTimestamp() + "POST" + "/api/v5/order" + body;
|
SecretKeySpec secretKey = new SecretKeySpec(quantApiMessage.getBSecretkey().getBytes(), "HmacSHA256");
|
Mac mac = Mac.getInstance("HmacSHA256");
|
mac.init(secretKey);
|
return Base64.getEncoder().encodeToString(mac.doFinal(preHash.getBytes()));
|
}
|
|
private static String getTimestamp() {
|
return String.valueOf(System.currentTimeMillis() / 1000);
|
}
|
}
|