0

是否可以使用 web3J 收听 infura 事件?我正在尝试获取事件,但出现错误

Caused by: org.web3j.protocol.core.filters.FilterException: Invalid request: The method eth_newFilter does not exist/is not available
    at org.web3j.protocol.core.filters.Filter.throwException(Filter.java:172)
    at org.web3j.protocol.core.filters.Filter.run(Filter.java:53)
    at org.web3j.protocol.rx.JsonRpc2_0Rx.run(JsonRpc2_0Rx.java:73)
    at org.web3j.protocol.rx.JsonRpc2_0Rx.lambda$ethLogFlowable$2(JsonRpc2_0Rx.java:65)
    at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
    ... 9 more```

# This is java wrappeer function generated by web3j maven plugin

public Flowable<PunkOfferedEventResponse>         punkOfferedEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
        EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
        filter.addSingleTopic(EventEncoder.encode(PUNKOFFERED_EVENT));
        return punkOfferedEventFlowable(filter);
    }


  public static class PunkOfferedEventResponse {
        public Log log;

        public BigInteger punkIndex;

        public String toAddress;

        public BigInteger minValue;
    } 

# This is the caller which subscribe to punkOfferedEventFlowable andd it should iddeally return events in stream fashion 

Web3j web3j = Web3j.build(new HttpService(""));
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
Credentials credentials = Credentials.create("");
            CryptoPunksMarket contract = CryptoPunksMarket.load("",web3j,credentials,new DefaultGasProvider()); contract.punkOfferedEventFlowable(null)
                 .doOnError(error -> error.printStackTrace())
                 .subscribe(event -> {
                     BigInteger index = event.punkIndex;
                     System.out.println("Index:"+index);
                 }).dispose();   

我在不同的博客中读到使用 web3J 获取过滤事件是不可能的,那么使用 web3J 和 infura 获取过滤事件的替代方法是什么?

4

1 回答 1

0

您必须从日志中读取事件。Web3j 提供了一个方法ethGetLogs,它将一个 ethFilter 对象作为参数并响应一个事件日志列表。

对于您的要求,它将是这样的:

EthFilter ethFilter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress());

ethFilter.addSingleTopic(EventEncoder.encode(CryptoPunksMarket.PUNK_OFFERED_EVENT));

EthLog ethLog = web3j.ethGetLogs(ethFilter).send();
于 2019-12-28T11:52:30.590 回答