KKSU
2024-05-09 ee3f68b0d13f8387cd11d54c6348c2c4aa05ba4c
src/main/java/cc/mrbird/febs/job/ChainListenerJob.java
@@ -20,7 +20,8 @@
@Slf4j
@Component
@ConditionalOnProperty(prefix = "system", name = "chain-listener", havingValue = "true")
public class ChainListenerJob implements ApplicationRunner {
//public class ChainListenerJob implements ApplicationRunner {
public class ChainListenerJob{
    @Autowired
    private ContractEventService bscCoinContractEvent;
@@ -34,7 +35,7 @@
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void chainBlockUpdate() {
        log.info("最新区块更新");
        BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
        BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_GFA.name()).blockNumber();
        redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM, blockNumber);
    }
@@ -44,7 +45,7 @@
        Object newestBlockObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM);
        BigInteger newestBlock;
        if (newestBlockObj == null) {
            newestBlock = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
            newestBlock = ChainService.getInstance(ChainEnum.BSC_GFA.name()).blockNumber();
        } else {
            newestBlock = (BigInteger) newestBlockObj;
        }
@@ -66,35 +67,77 @@
        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;
        }
//    @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);
//    }
        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());
            ChainService.contractEventListener(block, end, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
//    @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);
//    }
            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);
    }
}