I've looked through the documentation, but there doesn't seem to be this scenario listed. What I want to do is filter the Ethereum event log of a specific contract by an indexed field with Web3j.

Example solidity code: event someEvent(bytes32 indexed id, bytes value);

While we can get all the events within a specific time period using EthFilter filter = new EthFilter(startBlock, endBlock, contractAddress);, and filter for a specific event type by using the Event class, how do I get all events of a specific type that match an indexed field (for example, where id == {0, 0, 0 ... 0})?

  • 1
    Hi @id0, I have the same question. Have you found the solution?
    – Frank Kong
    Commented Oct 8, 2018 at 9:50

2 Answers 2


From the docs https://docs.web3j.io/filters.html:

enter image description here

Where topics are indexed parameters of a contract event. Hope this helps.

  • 1
    Yeah, found that already; there doesn't seem to be much documentation on how to use the functions .addSingleTopic and .addOptionalTopics. I know you can encode an Event object to filter for a specific event, but need to know how to compile an indexed property filter.
    – id01
    Commented Jun 5, 2018 at 1:33
  • 1
    The addSingleTopic function only allows input of the type "String". I believe it must be encoded by an encoder of some type (as I've seen in the examples) I just don't know which, or what (object(s)) to encode.
    – id01
    Commented Jun 5, 2018 at 16:07

you just listen for the topic named to be a value you're interested in.

For example, I'm listening for a integer uint16 tokenId event Emitted(uint16 indexed tokenId, address owner, uint32 timestamp);

using this code

String topic = String.format("0x%064X", tokenId);

EthFilter filter = new EthFilter(DefaultBlockParameterName.LATEST, 
DefaultBlockParameterName.LATEST, ADDRESS).addSingleTopic(topic);

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.