Solidity Solidity Interfaces, ABIs, and Events 2 — Questions and Answers
Question 1: What is the purpose of `indexed` parameters in Solidity events?
- Indexed parameters are stored in log topics, enabling fast filtering of events by that value using eth_getLogs (Correct answer)
- Indexed parameters are stored in contract storage for permanent access
- Indexed parameters reduce the gas cost of emitting the event
- Indexed parameters allow events to be called like functions
Correct answer: Indexed parameters are stored in log topics, enabling fast filtering of events by that value using eth_getLogs
Up to 3 parameters per event can be `indexed`; they are stored as log topics (not in the data field) and can be efficiently filtered by Ethereum nodes.
Question 2: How many `indexed` parameters can a non-anonymous Solidity event have?
- 3 (since the first topic is reserved for the event signature hash) (Correct answer)
- 4 (all four EVM log topics)
- 1 (only the first parameter can be indexed)
- Unlimited (all parameters can be indexed)
Correct answer: 3 (since the first topic is reserved for the event signature hash)
EVM logs have 4 topic slots; one is used for the event signature keccak256 hash, leaving 3 for indexed parameters in non-anonymous events.
Question 3: What is the topic0 in an Ethereum event log?
- The keccak256 hash of the event signature (e.g., keccak256('Transfer(address,address,uint256)')) (Correct answer)
- The contract address that emitted the event
- The block number when the event was emitted
- The first indexed parameter of the event
Correct answer: The keccak256 hash of the event signature (e.g., keccak256('Transfer(address,address,uint256)'))
Topic0 is always the keccak256 hash of the event's canonical signature string, used to identify which event type was emitted regardless of which contract emitted it.
Question 4: Can Solidity events be read by other smart contracts during execution?
- No, events are write-only from the EVM's perspective and can only be read off-chain via node APIs (Correct answer)
- Yes, using the eth_getLogs opcode inside a contract
- Yes, but only events from the same contract
- Yes, using the LOGREAD opcode in assembly
Correct answer: No, events are write-only from the EVM's perspective and can only be read off-chain via node APIs
Event logs are stored in the transaction receipt, not in contract storage or memory accessible to the EVM — they are only readable off-chain via JSON-RPC or indexers like The Graph.
Question 5: What does the `emit` keyword do in Solidity and when was it made mandatory?
- It triggers event emission; the keyword was made mandatory in Solidity 0.4.21 to distinguish events from function calls (Correct answer)
- It sends ETH to the event listener addresses
- It writes event data to contract storage
- It broadcasts the event to all connected nodes immediately
Correct answer: It triggers event emission; the keyword was made mandatory in Solidity 0.4.21 to distinguish events from function calls
The `emit` keyword explicitly marks event emission statements, added in 0.4.21 to improve code readability by visually distinguishing events from function calls.
Question 6: What is the gas cost structure for emitting a Solidity event?
- 375 base gas + 375 per topic + 8 gas per byte of non-indexed data (Correct answer)
- 5,000 gas per event regardless of size
- 21,000 gas base + 200 gas per parameter
- Events are free and do not consume gas
Correct answer: 375 base gas + 375 per topic + 8 gas per byte of non-indexed data
Event emission costs 375 gas base, 375 gas per indexed topic, and 8 gas per byte of the non-indexed data payload.
What is the purpose of `indexed` parameters in Solidity events?