8

Context: The indexed keyword is used for Event fields when we want them to be easily searchable using the JSON-RPC method eth_newFilter. This was the best way to offchain index the interactions with your smart contract, until subgraphs.

Since a subgraph does not distinguish between indexed Event fields and non-indexed Event fields, this leads me to believe that it is no longer necessary to add the indexed keyword to any of my events. Is this true?

If it is true, then this is a benefit for the entire Ethereum community because it means we no longer need to use the LOG1, LOG2, LOG3, and LOG4 opcodes, and can always use the gas-optimal LOG0 opcode for our Events.

2 Answers 2

4

In events, any indexed parameter is emitted as is (reference parameters are hashed). Non indexed parameters are ABI encoded and stored in data log.

The advantage of subgraphs is that you can write scripts that can decide what to do with events when they find a matching event.

So even if the parameter is not indexed, you can simply ABI decode the data part.

when they find a matching event

This is the key part. The subgraphs you write cannot react to your events if they can't find them. So in order for the subgraph to detect your event, you should at the least emit a non anonymous event given you don't have the contract address. This way, all the parameters can be ABI encoded in data and our subgraph/any script can decode it.

But if you have the contract address, you can also declare it as anonymous and still find the event and decode the values emitted.

4
  • 3
    This answer is good, but it doesn't answer my original question about "whether indexed is ever worthwhile when using subgraphs".
    – almel
    Sep 1, 2022 at 19:59
  • From a pure data fetching perspective, subgraphs are faster since they index and store events on their DB for dApps to consume. Filtering plainly through event filters takes a lot of time.
    – Pranay
    Sep 2, 2022 at 20:02
  • 2
    can you respond to my specific question about use of the "indexed" keyword? I'm not asking about the tradeoffs between using filtering vs. indexing.
    – almel
    Sep 5, 2022 at 19:51
  • I guess it's your own choice for indexed events, as if anyone wants to build an app on top of your contract and index events with js tools and not a subgraph, it wont be able to filter ;)
    – CP3c0
    Nov 9, 2022 at 18:04
2

To answer your actual question here. I would still recommend using the indexed keyword, since subgraphs are just one blockchain indexing technique. There could be other techniques right now or in the future, that benefit from it.

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.