4

Is there any difference between the two?

event NewEvent(address addr)
event NewEvent(address indexed addr)

2 Answers 2

8

In addition to the answer above, indexed parameters are slightly more expensive than unindexed parameters.

The general formula for event gas cost is:

375 + 375 * numberOfIndexedParameters + numberOfUnindexedBits

So in your example, the cost would be:

event NewEvent(address addr)         // 375 + 375 * 0 + 160 = 535
event NewEvent(address indexed addr) // 375 + 375 * 1 +   0 = 750
0
6

From the docs:

Given an event name and series of event parameters, we split them into two sub-series: those which are indexed and those which are not. Those which are indexed, which may number up to 3, are used alongside the Keccak hash of the event signature to form the topics of the log entry. Those which are not indexed form the byte array of the event.

Indexed events can be sought for efficiently using an event filter. When you want to be able to look up whether something has happened in the past, you should use an indexed event parameter.

An example:

Let's assume you want to proof that sometime in the past, you transfered some ERC-20 token to your friend. In that case, you'll want to find all Transfer event that token emitted in the passed with your address as a sender, and the address of your friend as a recipient. This lookup can be done easily if the sender and recipient addresses of the event were indexed.

Now, there is a catch.

From the docs again:

Indexed arguments will not be stored themselves. You can only search for the values, but it is impossible to retrieve the values themselves.

Only use indexed for event parameters you want to be able to search for!

2
  • Thanks for the answer! I guess even with one parameter, if you want it to be searchable, you need to index it? Aug 14, 2018 at 13:54
  • That's correct!
    – Henk
    Aug 14, 2018 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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