| | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import io.reactivex.Flowable; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.data.repository.query.ParameterOutOfBoundsException; |
| | | import org.web3j.crypto.Credentials; |
| | |
| | | * @param startBlock 开始区块 |
| | | */ |
| | | public static void contractEventListener(BigInteger startBlock, ContractEventService event, String type) { |
| | | contractEventListener(startBlock, null, event, type); |
| | | } |
| | | |
| | | public static void contractEventListener(BigInteger startBlock, BigInteger endBlock, ContractEventService event, String type) { |
| | | ChainEnum chain = ChainEnum.getValueByName(type); |
| | | assert chain != null; |
| | | |
| | | EthUsdtContract contract = contract(chain.getPrivateKey(), chain.getContractAddress(), chain.getUrl()); |
| | | EthFilter filter = getFilter(startBlock, chain.getContractAddress()); |
| | | EthFilter filter = getFilter(startBlock, endBlock, chain.getContractAddress()); |
| | | |
| | | contract.transferEventFlowable(filter).subscribe(e -> { |
| | | Flowable<EthUsdtContract.TransferEventResponse> eventFlowable = contract.transferEventFlowable(filter); |
| | | eventFlowable.subscribe(e -> { |
| | | event.compile(e); |
| | | }, error -> { |
| | | log.error("--->", error); |
| | | log.error("合约监听启动报错", error); |
| | | }); |
| | | } |
| | | |
| | | |
| | | private static EthUsdtContract contract(String privateKey, String contractAddress, String url) { |
| | | Credentials credentials = Credentials.create(privateKey); |
| | | return EthUsdtContract.load(contractAddress, Web3j.build(new HttpService(url)), credentials, new StaticGasProvider(BigInteger.valueOf(4500000L), BigInteger.valueOf(200000L))); |
| | | } |
| | | |
| | | // 18097238 18098663 |
| | | private static EthFilter getFilter(BigInteger startBlock, String contractAddress) { |
| | | return getFilter(startBlock, null, contractAddress); |
| | | } |
| | |
| | | assert chain != null; |
| | | |
| | | EthUsdtContract contract = contract(chain.getPrivateKey(), chain.getContractAddress(), chain.getUrl()); |
| | | EthFilter filter = getFilter(new BigInteger("18097238"), new BigInteger("18098663"), chain.getContractAddress()); |
| | | EthFilter filter = getFilter(new BigInteger("18097238"), chain.getContractAddress()); |
| | | |
| | | contract.transferEventFlowable(filter).subscribe(e -> { |
| | | System.out.println(1); |