简体   繁体   中英

Cant download ethereum Events with web3j

When i try download Events from blockhain:

<!-- language: lang-js -->
    Web3j web3 = Web3j.build(new HttpService("https://rinkeby.infura.io/naqTNN4B2QavbM4vZI3q"));
    Credentials credentials =Credentials.create("94b5486c4657faf6d3834c2efd70d90a3cc746db101de32b997206e0ddb5234e");
    TenderFactory factory = TenderFactory.load("0x16606f347cf1b36c08690b150b96dba2031d6729", web3, credentials, GAS_PRICE, GAS_LIMIT);
    final Event event = new Event("NewContract",
    Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
    }),
    Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
    }));
    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST,
    DefaultBlockParameterName.EARLIEST, factory.getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    Toolkit.getDefaultToolkit().beep();
    web3.ethLogObservable(filter).subscribe(log -> {
        LOG.info("log.getTopics=" + log.getTopics());
    });

I got exeption:

    17:43:05.719 [main] INFO  r.s.tenderchain.Download - contractAddress = 0x16606f347cf1b36c08690b150b96dba2031d6729
    17:43:07.097 [main] DEBUG org.web3j.protocol.http.HttpService - --> POST https://rinkeby.infura.io/naqTNN4B2QavbM4vZI3q http/1.1
    17:43:07.098 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    17:43:07.098 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 243
    17:43:07.102 [main] DEBUG org.web3j.protocol.http.HttpService -
            17:43:07.103 [main] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_newFilter","params":[{"topics":["0xa291e5980c03d23637198189ac50f67cecef9d808328c0420a23fa32fc16084a"],"fromBlock":"earliest","toBlock":"earliest","address":["0x16606f347cf1b36c08690b150b96dba2031d6729"]}],"id":0}
    17:43:07.104 [main] DEBUG org.web3j.protocol.http.HttpService - --> END POST (243-byte body)
    17:43:08.688 [main] DEBUG org.web3j.protocol.http.HttpService - <-- 405 Method Not Allowed https://rinkeby.infura.io/naqTNN4B2QavbM4vZI3q (1582ms)
    17:43:08.689 [main] DEBUG org.web3j.protocol.http.HttpService - Date: Wed, 20 Dec 2017 14:43:08 GMT
    17:43:08.694 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: text/plain; charset=utf-8
    17:43:08.694 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 0
    17:43:08.694 [main] DEBUG org.web3j.protocol.http.HttpService - Connection: keep-alive
    17:43:08.694 [main] DEBUG org.web3j.protocol.http.HttpService - Server: nginx/1.10.3 (Ubuntu)
            17:43:08.695 [main] DEBUG org.web3j.protocol.http.HttpService - Vary: Origin
    17:43:08.698 [main] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (0-byte body)
    Exception in thread "main" rx.exceptions.OnErrorNotImplementedException: Invalid response received: okhttp3.internal.http.RealResponseBody@437e951d
    at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:386)
    at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:383)
    at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44)
    at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:153)
    at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
    at rx.Observable.subscribe(Observable.java:10249)
    at rx.Observable.subscribe(Observable.java:10205)
    at rx.Observable.subscribe(Observable.java:10010)
    at ru.simplex_software.tenderchain.Download.main(Download.java:82)
    Caused by: org.web3j.protocol.exceptions.ClientConnectionException: Invalid response received: okhttp3.internal.http.RealResponseBody@437e951d
    at org.web3j.protocol.http.HttpService.performIO(HttpService.java:116)
    at org.web3j.protocol.Service.send(Service.java:31)
    at org.web3j.protocol.core.Request.send(Request.java:71)
    at org.web3j.protocol.core.filters.LogFilter.sendRequest(LogFilter.java:31)
    at org.web3j.protocol.core.filters.Filter.run(Filter.java:40)

.... org.web3j.protocol.rx.JsonRpc2_0Rx.lambda$ethLogObservable$10(JsonRpc2_0Rx.java:65) at rx.Observable.subscribe(Observable.java:10238) ... 3 more

How to fix this and download NewContract events?

infura supports websocket endpoints on mainnet, ropsten and rinkeby now that allow you to listen to events. However, web3j doesn't support until now websockets so you can't use filters in web3 while connected to infura.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM