1

I'm using web3j to create a filter to listen to events.

Specifically, I want to listen to events of a specific contract between a certain block number and now.

This is my code:

Web3j web3j = ...
EthFilter filter = new EthFilter(DefaultBlockParameter.valueOf(Numeric.toBigInt("3306085")), DefaultBlockParameterName.LATEST, contractAddr);
web3j.ethLogFlowable(filter).subscribe(log -> onNewEvent(log));

However, when I run that code, I get the following error message:

FilterException: Invalid request: One of the blocks specified in filter (fromBlock, toBlock or blockHash) cannot be found.

The stack trace looks like this:

Exception in thread "main" io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | Invalid request: One of the blocks specified in filter (fromBlock, toBlock or blockHash) cannot be found.
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
    at io.reactivex.internal.subscribers.LambdaSubscriber.onError(LambdaSubscriber.java:79)
    at io.reactivex.internal.operators.flowable.FlowableCreate$BaseEmitter.error(FlowableCreate.java:292)
    [...]
Caused by: org.web3j.protocol.core.filters.FilterException: Invalid request: One of the blocks specified in filter (fromBlock, toBlock or blockHash) cannot be found.

What am I doing wrong?

1 Answer 1

1

I solved the problem. The issue is that Numeric.toBigInt("") expects the block number to be in hex format not in decimal format. So, either pass the block number as hex string or immediately create a BigInteger if your block number string is in decimal format.

Changing the code to this works for me:

Web3j web3j = ...
EthFilter filter = new EthFilter(DefaultBlockParameter.valueOf(new BigInteger("3306085")), DefaultBlockParameterName.LATEST, contractAddr);
web3j.ethLogFlowable(filter).subscribe(log -> onNewEvent(log));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.