package com.xcong.excoin; import com.xcong.excoin.modules.blackchain.service.EthUsdtContract; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.web3j.crypto.Credentials; import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.DefaultBlockParameterNumber; import org.web3j.protocol.core.methods.request.EthFilter; import org.web3j.protocol.http.HttpService; import org.web3j.tx.gas.StaticGasProvider; import java.math.BigInteger; import java.util.HashMap; import java.util.Map; /** * @author wzy * @date 2020-11-04 **/ @Slf4j public class FilterTest { public static BigInteger GAS_PRICE = BigInteger.valueOf(45000000000L); //public static final BigInteger GAS_LIMIT = BigInteger.valueOf(4300000L); public static final BigInteger GAS_LIMIT = BigInteger.valueOf(2000000L); private static Web3j web3; private static String blockchainNode = "http://114.55.250.231:8545"; private static String contractAddr = "0xdac17f958d2ee523a2206206994597c13d831ec7"; // 操作账号 private static String privateKey = "4576fafdd215f52051c60e04618ef8997fbc5cee8413d3b34d210e296e6e9a3d"; @Test public void testAA() { BigInteger blockNum = new BigInteger("11189995"); Credentials credentials = Credentials.create(privateKey); EthUsdtContract contract = EthUsdtContract.load(contractAddr, getInstance(), credentials, getStaticGasProvider()); EthFilter filter = getFilter(blockNum); Map map = new HashMap(); map.put("blockNum",blockNum); contract.transferEventFlowable(filter).subscribe(e->{ // System.out.println(1); // Thread.sleep(60); // System.out.println(2); }); } public static void main(String[] args) { BigInteger blockNum = new BigInteger("11190135"); Credentials credentials = Credentials.create(privateKey); EthUsdtContract contract = EthUsdtContract.load(contractAddr, getInstance(), credentials, getStaticGasProvider()); EthFilter filter = getFilter(blockNum); Map map = new HashMap(); map.put("blockNum",blockNum); contract.transferEventFlowable(filter).subscribe(e->{ Thread.sleep(120); }); } private static Web3j getInstance() { if (web3 == null) { HttpService httpService = new HttpService(blockchainNode); web3 = Web3j.build(httpService); } return web3; } private static EthFilter getFilter(BigInteger startBlock) { if (startBlock != null) { EthFilter filter = new EthFilter(new DefaultBlockParameterNumber(startBlock), DefaultBlockParameterName.LATEST, contractAddr); return filter; } else { return new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contractAddr); } } private static StaticGasProvider getStaticGasProvider(){ return new StaticGasProvider(GAS_PRICE,GAS_LIMIT); } }