簡體   English   中英

web3J 與 infura 的兼容性

[英]Comptablity of web3J with infura

是否可以使用 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 獲取過濾事件的替代方法是什么?

您必須從日志中讀取事件。 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM