Administrator
2026-06-06 f26b4645c4cd07a570967adc017b10a5cf4b82c7
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
package com.xcong.excoin.modules.gateApi.wsHandler.handler;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.gateApi.GateGridTradeService;
import com.xcong.excoin.modules.gateApi.wsHandler.AbstractPrivateChannelHandler;
import lombok.extern.slf4j.Slf4j;
 
import java.math.BigDecimal;
 
/**
 * 用户私有成交频道处理器(futures.usertrades),接收成交推送并回调 {@link GateGridTradeService#onUserTrade}。
 *
 * @author Administrator
 */
@Slf4j
public class UserTradesChannelHandler extends AbstractPrivateChannelHandler {
 
    private static final String CHANNEL_NAME = "futures.usertrades";
 
    public UserTradesChannelHandler(String apiKey, String apiSecret,
                                     String contract,
                                     GateGridTradeService gridTradeService) {
        super(CHANNEL_NAME, apiKey, apiSecret, contract, gridTradeService);
    }
 
    @Override
    public boolean handleMessage(JSONObject response) {
        if (!CHANNEL_NAME.equals(response.getString("channel"))) {
            return false;
        }
        try {
            JSONArray resultArray = response.getJSONArray("result");
            if (resultArray == null || resultArray.isEmpty()) {
                return true;
            }
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject trade = resultArray.getJSONObject(i);
                String contract = trade.getString("contract");
                if (!getContract().equals(contract)) {
                    continue;
                }
                String id = trade.getString("id");
                String orderId = trade.getString("order_id");
                String size = trade.getString("size");
                BigDecimal price = trade.getBigDecimal("price");
                String role = trade.getString("role");
                BigDecimal fee = trade.getBigDecimal("fee");
                String text = trade.getString("text");
//                log.info("[{}] 成交更新, id:{}, order_id:{}, price:{}, size:{}, role:{}, fee:{}, text:{}",
//                        CHANNEL_NAME, id, orderId, price, size, role, fee, text);
//                if (getGridTradeService() != null) {
//                    getGridTradeService().onUserTrade(contract, orderId, price, size, role, fee);
//                }
            }
        } catch (Exception e) {
            log.error("[{}] 处理数据失败", CHANNEL_NAME, e);
        }
        return true;
    }
}