10

I'm tinkering with web3j and most of the things that I want to do succeed, however I seem to not be able to listen to events.

I've extended the ballot.sol contract you get with remix by adding an event VoteEnded, which is fired when a call is made to winningProposal and that works in the Remix JavaScript VM.

...
event VoteEnded();
...

function winningProposal() constant returns (uint8 winningProposal) {
    uint256 winningVoteCount = 0;
    for (uint8 proposal = 0; proposal < proposals.length; proposal++)
        if (proposals[proposal].voteCount > winningVoteCount) {
            winningVoteCount = proposals[proposal].voteCount;
            winningProposal = proposal;
        }
    VoteEnded();
}
...

I am able to deploy this contract and vote etc. in Web3j. Then I added a filter to listen to VoteEnded. I did it like:

    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress());
    web3.ethLogObservable(filter).subscribe(new Action1<Log>() {
        @Override    
        public void call(Log log) {
            System.out.println("log.toString(): " +  log.toString());
        }
    });

Which I found here: A problem with filters and events (web3j/TestRPC). SOS! ;)

However this doesn't print anything at all.

What am I doing wrong?

2 Answers 2

8

I know this question is old, but providing an answer here as I came across this issue and couldn't find a solution elsewhere...

This appears to be a bug in Web3j that occurs when you pass in the contract address as part of your filter.

EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress());

does not work, but

EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress().substring(2));

does. It appears '0x' is being inserted at the beginning of the string when the filter is applied. Passing in the substring with the '0x' trimmed causes it to work.

(See also Contract Address Must Be Trimmed In EthFilter)

2
  • 1
    Wasted hours over this one, you just saved me a few more, thanks
    – forhas
    Jun 24, 2018 at 17:50
  • 2
    OUTDATED, address without Ox will give org.web3j.protocol.core.filters.FilterException: Invalid request: invalid argument 0: invalid address at index 0: hex string without 0x prefix Aug 31, 2021 at 7:02
1

Just did this recently, but I think this only works for events displayed at the time that your web page is open not from all blocks:

https://coursetro.com/posts/code/100/Solidity-Events-Tutorial---Using-Web3.js-to-Listen-for-Smart-Contract-Events

   var CoursetroContract = web3.eth.contract(YOUR ABI);

   var Coursetro = CoursetroContract.at('CONTRACT ADDRESS');

   var instructorEvent = Coursetro.Instructor();
   instructorEvent.watch(function(error, result){
        if (!error)
            {
                $("#loader").hide();
                $("#instructor").html(result.args.name + ' (' + result.args.age + ' years old)');
            } else {
                $("#loader").hide();
                console.log(error);
            }
    });

Your Answer

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

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