Consider the following Solidity contract snippet. What will be the output in the transaction logs after calling setValue(42)?
pragma solidity ^0.8.0;
contract Test {
event ValueChanged(address indexed author, uint oldValue, uint newValue);
uint public value;
function setValue(uint _value) public {
uint old = value;
value = _value;
emit ValueChanged(msg.sender, old, _value);
}
}Remember that emit triggers the event and oldValue is the previous stored value.
The event ValueChanged is emitted with the caller's address, the old value (initially 0), and the new value (42). Indexed parameters are allowed and help filtering logs.
Choose the correct statement about declaring events in Solidity.
Think about how events help external tools filter logs.
Events can have up to three parameters marked as indexed to enable efficient filtering by external applications. Events are not accessible by contracts and are stored in transaction logs.
Examine the following event declaration. Why does it fail to compile?
event Transfer(address from, address to, uint amount) indexed;Check where the indexed keyword is allowed in event declarations.
The indexed keyword can only be applied to individual parameters, not to the entire event declaration. The syntax here is invalid.
Given this Solidity contract, what will be the content of the event log after calling recordAction(7, 3)?
pragma solidity ^0.8.0;
contract Logger {
event ActionRecorded(address indexed user, uint indexed actionId, uint value);
function recordAction(uint actionId, uint value) public {
emit ActionRecorded(msg.sender, actionId, value);
}
}Remember that indexed parameters appear in topics and non-indexed in data.
The event ActionRecorded logs the caller's address and actionId as indexed parameters, and value as non-indexed data. The values match the function call arguments.
Consider this event declaration:
event DataEvent(uint indexed id, address indexed sender, string message, uint indexed timestamp);
How many parameters are indexed, and is this declaration valid?
Recall the maximum number of indexed parameters allowed and restrictions on data types.
Solidity allows up to three indexed parameters per event. The string parameter cannot be indexed, but the other three parameters are indexed correctly. This declaration is valid.