2

As an example, getPastLogs defines topics parameter as:

topics - Array: An array of values which must each appear in the log entries. The order is important, if you want to leave topics out use null, e.g. [null, '0x12...']. You can also pass an array for each topic with options for that topic e.g. [null, ['option1', 'option2']]

If I mean reading this right, then:

  • ['0x12...'] will filter by 0x12... topic.
  • [null, '0x12...'] will match all topics, and filter by index parameters.

I don't comprehend the [null, ['option1', 'option2']] example though.

Currently, I am doing it as such:

const [syncLogs, swapLogs] = await Promise.all([
  web3.eth.getPastLogs({
    fromBlock: firstBlockNumber,
    toBlock: firstBlockNumber,
    topics: [
      syncToken.signature,
    ],
  }),
  web3.eth.getPastLogs({
    fromBlock: firstBlockNumber,
    toBlock: firstBlockNumber,
    topics: [
      swapToken.signature,
    ],
  }),
]);

Though this results in two distinct JSON-RPC calls.

Is there a way to filter by multiple topics?

1 Answer 1

5

[null, ['option1', 'option2']] means the first topic can be anything, the second topic has to be either 'option1' or 'option2'.

5
  • I am asking if there is a way to filter all logs by multiple first topics.
    – Gajus
    Jun 4, 2021 at 15:21
  • @Gajus Did you try something like [['topic1','topic2']]? In general topics are good enough for simple queries, for more complex you should use an indexer or something more advanced like graphql.
    – Ismael
    Jun 4, 2021 at 15:47
  • I am basically interested in all Sync and Swap events. The approach that I've shared in the original post works, I was simply wondering if I could pull the same data using a single call.
    – Gajus
    Jun 4, 2021 at 16:53
  • 2
    @Gajus Can you try something like topics: [ [syncToken.signature, swapToken.signature] ],? The system underneath uses bloom filters for topics, it has some use cases, but it has its own limitations.
    – Ismael
    Jun 4, 2021 at 17:29
  • Your last suggestion does indeed what I was looking for. Thank you!
    – Gajus
    Jun 5, 2021 at 16:43

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.