0

Is it possible to listen to infura events , using web3J ? I am trying to get events , but getting error

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();   

I read in different blogs that it is not possible to get filtered events using web3J , then what are the alternatives available using web3J and infura to get the filtered events ?

  • You can try to use web socket to connect to the network instead of http/https. – tiennv Sep 16 '19 at 14:18
  • Filters are not supported on Infura. You need to fetch each transaction receipt and look at the logs. The alternative is that you run your own node and use that. – Antony Denyer Sep 20 '19 at 18:43

1 Answers1

0

You have to read the events from the logs. Web3j provides a method ethGetLogs, which takes an ethFilter object as parameter and responses a list of event logs.

For your request it would be something like this:

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

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

EthLog ethLog = web3j.ethGetLogs(ethFilter).send();
wero026
  • 1,187
  • 2
  • 11
  • 23