KKSU
2024-05-16 c268540881fe493bc40e76ba82793a63c0897c91
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
package cc.mrbird.febs.job;
 
import cc.mrbird.febs.common.contants.AppContants;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.dapp.chain.ChainEnum;
import cc.mrbird.febs.dapp.chain.ChainService;
import cc.mrbird.febs.dapp.chain.ContractEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.math.BigInteger;
 
@Slf4j
@Component
@ConditionalOnProperty(prefix = "system", name = "chain-listener", havingValue = "true")
//public class ChainListenerJob implements ApplicationRunner {
public class ChainListenerJob{
 
    @Autowired
    private ContractEventService bscCoinContractEvent;
 
    @Autowired
    private ContractEventService bscUsdtContractEvent;
 
    @Autowired
    private RedisUtils redisUtils;
 
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void chainBlockUpdate() {
        log.info("最新区块更新");
        BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_GFA.name()).blockNumber();
 
        redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM, blockNumber);
    }
 
    @Scheduled(cron = "0/2 * * * * ? ")
    public void chainIncrementBlock() {
        Object newestBlockObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM);
        BigInteger newestBlock;
        if (newestBlockObj == null) {
            newestBlock = ChainService.getInstance(ChainEnum.BSC_GFA.name()).blockNumber();
        } else {
            newestBlock = (BigInteger) newestBlockObj;
        }
 
        Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM);
        BigInteger toIncrement;
        if (incrementObj == null) {
            toIncrement = newestBlock;
        } else {
            BigInteger incrementBlock = (BigInteger) incrementObj;
 
            // 最新区块小于增加区块
            if (newestBlock.compareTo(incrementBlock) <= 0) {
                return;
            }
            toIncrement = incrementBlock.add(BigInteger.ONE);
        }
 
        redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM, toIncrement);
    }
 
//    @Override
//    public void run(ApplicationArguments args) throws Exception {
//        long start = System.currentTimeMillis();
//        log.info("区块链监听开始启动");
//        Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM);
//        BigInteger newest = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
//        BigInteger block;
//        if (incrementObj == null) {
//            block = newest;
//        } else {
//            block = (BigInteger) incrementObj;
//        }
//
//        BigInteger section = BigInteger.valueOf(5000);
//        while (newest.subtract(block).compareTo(section) > -1) {
//            BigInteger end = block.add(section);
//            log.info("监听:[{} - {}]", block, end);
////            ChainService.contractEventListener(block, end, bscUsdtContractEvent, ChainEnum.BSC_USDT.name());
//            /**
//             * 检测团队收益,质押数量的20%到一个钱包a,
//             * 监控A钱包,
//             */
////            ChainService.contractEventListener(block, end, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
//            ChainService.coinRewardEventListener(block, end, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
//
//            block = block.add(section);
//            if (block.compareTo(newest) > 0) {
//                block = newest;
//            }
//        }
////        ChainService.contractEventListener(block, bscUsdtContractEvent, ChainEnum.BSC_USDT.name());
////        ChainService.contractEventListener(block, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
//
//        long end = System.currentTimeMillis();
//        log.info("区块链监听启动完成, 消耗时间{}", end - start);
//    }
 
//    @Override
//    public void run(ApplicationArguments args) throws Exception {
//        long start = System.currentTimeMillis();
//        log.info("区块链监听开始启动");
//
//        Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM);
//        BigInteger newest = ChainService.getInstance(ChainEnum.BSC_GFA.name()).blockNumber();
////        Object incrementObj = BigInteger.valueOf(39780699);
////        BigInteger newest = BigInteger.valueOf(39780739);
//        BigInteger block;
//        if (incrementObj == null) {
//            block = newest;
//        } else {
//            block = (BigInteger) incrementObj;
//        }
//
//        BigInteger section = BigInteger.valueOf(5000);
//        BigInteger subtract = newest.subtract(block);
//        log.info("监听:[{} - {} - {}]", newest,block,newest.subtract(block).compareTo(section) > -1);
//        while (newest.subtract(block).compareTo(section) > -1) {
//            BigInteger end = block.add(section);
//            log.info("监听:[{} - {}]", block, end);
//            ChainService.coinRewardEventListener(block, end, bscCoinContractEvent, ChainEnum.BSC_GFA.name());
//
//            block = block.add(section);
//            if (block.compareTo(newest) > 0) {
//                block = newest;
//            }
//        }
//
//        ChainService.coinRewardEventListener(block, null, bscCoinContractEvent, ChainEnum.BSC_GFA.name());
//
//        long end = System.currentTimeMillis();
//        log.info("区块链监听启动完成, 消耗时间{}", end - start);
//    }
 
}