Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k points)

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 a 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());

        }

    });

However, this doesn't print anything at all. What am I doing wrong?

1 Answer

0 votes
by (29.5k points)

Hi, what you need to do is add filter.addSingleTopic(EventEncoder.encode(event)) where event is an instantiated org.web3j.abi.datatypes.Event object.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 17, 2019 in Blockchain by Abhishek_31 (12.7k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...