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 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 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 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 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); } }