xiaoyong931011
2023-03-24 a4634d64a5ace604bb137c6c92d28a948e2b8cf9
src/main/java/cc/mrbird/febs/mall/chain/service/UsdtErc20UpdateService.java
@@ -10,6 +10,7 @@
import org.springframework.stereotype.Service;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.methods.request.EthFilter;
@@ -63,13 +64,13 @@
    }
    //private static String blockchainNode = "http://114.55.250.231:8545";
    private static String blockchainNode = "http://120.55.86.146:8545";
    private static String blockchainNode = "https://bsc-dataseed1.ninicoin.io";
    private static String contractAddr = "0xdac17f958d2ee523a2206206994597c13d831ec7";
//    private static String contractAddr = "0xdac17f958d2ee523a2206206994597c13d831ec7";
    private static String contractAddr = "0x55d398326f99059ff775485246999027b3197955";
    // 操作账号
    private static String privateKey = "4576fafdd215f52051c60e04618ef8997fbc5cee8413d3b34d210e296e6e9a3d";
    private static String privateKey = "7761233246f68ab3d35eba04c2fbc794714652d2acc15ee5b27ecd1c4005d279";
    @Resource
@@ -84,13 +85,16 @@
        }
        // 获取最新区块
        String string = redisUtils.getString(ETH_USDT_BLOCK_NUM);
//        String string = "24317595";
        if(string==null){
            string = "11957825";
            string = "24317595";
        }
        BigInteger blockNum = new BigInteger(string);
        Credentials credentials = Credentials.create(privateKey);
        EthUsdtContract contract = EthUsdtContract.load(contractAddr, getInstance(), credentials, getStaticGasProvider());
        EthFilter filter = getFilter(blockNum);
        EthFilter filter = getFilter(blockNum,blockNum,contractAddr);
        Map<String,BigInteger> map = new HashMap<String,BigInteger>();
        map.put("blockNum",blockNum);
        contract.transferEventFlowable(filter).subscribe(e->{
@@ -127,6 +131,25 @@
            return new EthFilter(DefaultBlockParameterName.EARLIEST,
                    DefaultBlockParameterName.LATEST, contractAddr);
        }
    }
    private static EthFilter getFilter(BigInteger startBlock, BigInteger endBlock, String contractAddress) {
        DefaultBlockParameter startParameterName = null;
        DefaultBlockParameter endParameterName = null;
        if (startBlock != null) {
            startParameterName = new DefaultBlockParameterNumber(startBlock);
        } else {
            startParameterName = DefaultBlockParameterName.EARLIEST;
        }
        if (endBlock != null) {
            endParameterName = new DefaultBlockParameterNumber(endBlock);
        } else {
            endParameterName = DefaultBlockParameterName.LATEST;
        }
        return new EthFilter(startParameterName, endParameterName, contractAddress);
    }
}