1

I am using the following code to listen/watch contract events(web3j):

Event TRANSFER_EVENT = new Event("Transfer", Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true){}, new TypeReference<Address>(true){}, new TypeReference<Uint256>(false){}));
String TRANSFER_EVENT_HASH = EventEncoder.encode(TRANSFER_EVENT);

EthFilter filterProxy = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contractAddress);  
filterProxy.addSingleTopic(TRANSFER_EVENT_HASH);

Disposable subscription = web3j.ethLogFlowable(filterProxy).subscribe(log -> {
                // address indexed _arg1
                Address arg1 = (Address) FunctionReturnDecoder.decodeIndexedValue(log.getTopics().get(1), new TypeReference<Address>() {});
                // bytes32 indexed _arg2
                Address arg2 = (Address) FunctionReturnDecoder.decodeIndexedValue(log.getTopics().get(2), new TypeReference<Address>() {});
                // uint8 _arg3
                Uint256 arg3 = (Uint256) FunctionReturnDecoder.decodeIndexedValue(log.getData(), new TypeReference<Uint256>() {});
                
        },Throwable::printStackTrace);

It's working fine and can capture all the events.

But how to stop the subscription? Tried subscription.dispose() and CountDownLatch, both doesn't work.

1 Answer 1

1

I think I found the solution if unsubscribe does not work in the docs:

final Disposable disposable = events.subscribe(...)
disposable.dispose();

here is a link to where I found it: https://docs.web3j.io/4.8.7/getting_started/pub_sub/

3
  • There is no unsubscribe() method for this subscription.
    – Jeff
    Commented Dec 6, 2022 at 5:39
  • I have edited my original answer, sorry I initially thought this was web3.js but I believe I found your answer which is the .dispose() function Commented Dec 6, 2022 at 15:08
  • dispose() also can't stop the subscription.
    – Jeff
    Commented Dec 13, 2022 at 7:20

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.