Is there any difference between the two?
event NewEvent(address addr) event NewEvent(address indexed addr)
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
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
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.
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.
Indexed arguments will not be stored themselves. You can only search for the values, but it is impossible to retrieve the values themselves.
indexed for event parameters you want to be able to search for!