0

当我尝试从区块链下载事件时:

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

我得到了例外:

    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) 在 rx.Observable.subscribe(Observable.java:10238) ... 还有 3 个

如何解决这个问题并下载 NewContract 事件?

4

1 回答 1

1

infura 现在支持 mainnet、ropsten 和 rinkeby 上的 websocket 端点,允许您监听事件。但是,web3j 直到现在才支持 websockets,因此您在连接到 infura 时不能在 web3 中使用过滤器。

于 2018-07-16T10:41:20.560 回答