KKSU
2024-05-09 7c93f4f06f148546d4c760cf40194212d78878e0
合约监听
2 files modified
1 files added
137 ■■■■ changed files
src/main/java/cc/mrbird/febs/dapp/chain/EthUsdtContract.java 2 ●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/job/ChainListenerJob.java 73 ●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/job/ChainRunner.java 62 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/dapp/chain/EthUsdtContract.java
@@ -427,7 +427,7 @@
    public Flowable<CoinRewardEventResponse> coinRewardEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
        EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
        filter.addSingleTopic(EventEncoder.encode(TRANSFER_EVENT));
        filter.addSingleTopic(EventEncoder.encode(COINREWARD_EVENT));
        return coinRewardEventFlowable(filter);
    }
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;
@@ -103,40 +104,40 @@
//        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);
    }
//    @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);
//    }
}
src/main/java/cc/mrbird/febs/job/ChainRunner.java
New file
@@ -0,0 +1,62 @@
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.stereotype.Component;
import java.math.BigInteger;
@Slf4j
@Component
public class ChainRunner implements ApplicationRunner {
    @Autowired
    private ContractEventService bscCoinContractEvent;
    @Autowired
    private RedisUtils redisUtils;
    @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);
    }
}