Administrator
7 days ago d936551a55d8b7e0d75c888dc1e6ac16eca6c1d3
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
package cc.mrbird.febs.mall.quartz;
 
import cc.mrbird.febs.mall.controller.dependentStation.TokenviewWebhookService;
import cc.mrbird.febs.mall.controller.dependentStation.utils.OkHttpUtil2;
import cc.mrbird.febs.mall.controller.dependentStation.utils.Trc20TokenviewContentModel;
import cc.mrbird.febs.mall.controller.dependentStation.utils.Trc20TokenviewModel;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Component
@ConditionalOnProperty(prefix = "system", name = "job", havingValue = "true")
public class ChatTrc20ChargeOkLinkTask {
 
    @Resource
    private TokenviewWebhookService tokenviewWebhookService;
 
    /**
     * 30分钟(毫秒)
     */
    private final static long TIME_INTERVAL = 300000 * 6;
 
    private final static String TRON_API_KEY = "X8Np5zbDuhmG6cntbhLu";
 
    private static Map<String, String> REQUEST_HEADER = new HashMap<>();
    static {
        REQUEST_HEADER.put("TRON-PRO-API-KEY", TRON_API_KEY);
    }
 
    /**
     * 每分钟轮询 Tokenview API,查询 TRC20-USDT 充值记录
     * 充值匹配逻辑委托给 {@link TokenviewWebhookService}
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void recharge() {
        String receiveAddress = tokenviewWebhookService.getReceiveAddress();
        if (receiveAddress == null) {
            log.error("请先配置系统地址");
            return;
        }
 
        String url = "https://services.tokenview.io/vipapi/trx/address/tokentrans/"
                + receiveAddress + "/TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t/1/10";
 
        long endTime = System.currentTimeMillis();
        long startTime = endTime - TIME_INTERVAL;
 
        log.info("自动充值定时任务 startTime={}, endTime={}", startTime, endTime);
 
        Map<String, String> param = new HashMap<>();
        param.put("timestampStart", startTime + "");
        param.put("timestampEnd", endTime + "");
        param.put("toAddress", receiveAddress);
        param.put("apikey", TRON_API_KEY);
 
        long start = System.currentTimeMillis();
        byte[] bytes = OkHttpUtil2.doGetSingle(url, REQUEST_HEADER, param, "application/json");
 
        if (ObjectUtil.isEmpty(bytes)) {
            log.error("查询链上数据返回为空, param={}", param);
            return;
        }
 
        String response = new String(bytes, StandardCharsets.UTF_8);
        log.info("查询到的充值记录:{}", response);
 
        Trc20TokenviewModel model = JSONObject.parseObject(response, Trc20TokenviewModel.class);
        List<Trc20TokenviewContentModel> tokenTransfers = model.getData();
        if (CollUtil.isEmpty(tokenTransfers)) {
            return;
        }
 
        log.info("查询耗时:{}ms, 记录数:{}, 时间区间 [{}, {}]",
                System.currentTimeMillis() - start, tokenTransfers.size(), startTime, endTime);
 
        for (Trc20TokenviewContentModel transfer : tokenTransfers) {
            log.info("链上时间: {}", transfer.getTime());
            String result = tokenviewWebhookService.processPollTransaction(
                    transfer.getTxid(),
                    transfer.getValue(),
                    receiveAddress
            );
            log.info("轮询处理结果: {}", result);
        }
    }
 
    public static void main(String[] args) {
        String receiveAddress = "TExto1UjtFcXKw5QdJDRqtx7wTQ15D37GD";
        String url = "https://services.tokenview.io/vipapi/trx/address/tokentrans/"
                + receiveAddress + "/TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t/1/10";
 
        long endTime = System.currentTimeMillis();
        long startTime = endTime - TIME_INTERVAL;
 
        System.out.println(new Date() + " 自动充值定时任务 " + startTime + " " + endTime);
 
        Map<String, String> param = new HashMap<>();
        param.put("timestampStart", startTime + "");
        param.put("timestampEnd", endTime + "");
        param.put("toAddress", receiveAddress);
        param.put("apikey", TRON_API_KEY);
 
        byte[] bytes = OkHttpUtil2.doGetSingle(url, REQUEST_HEADER, param, "application/json");
 
        if (bytes == null) {
            System.out.println("查询链上数据返回为空, param=" + param);
            return;
        }
 
        String s = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(s);
        System.exit(0);
    }
}