Challenge - 5 Problems
Event Mastery in Smart Contracts
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Solidity event emission?
Consider this Solidity contract snippet that emits an event when a value is updated. What will be logged in the transaction receipt's event logs after calling
updateValue(42)?Blockchain / Solidity
pragma solidity ^0.8.0;
contract Example {
event ValueChanged(address indexed author, uint256 oldValue, uint256 newValue);
uint256 public value;
function updateValue(uint256 newValue) public {
uint256 old = value;
value = newValue;
emit ValueChanged(msg.sender, old, newValue);
}
}Attempts:
2 left
💡 Hint
Events record the state before and after the change and include the sender's address.
✗ Incorrect
When
updateValue is called, the event ValueChanged is emitted with the sender's address, the old value (initially 0), and the new value (42). Events are stored in transaction logs and can be accessed off-chain.🧠 Conceptual
intermediate1:30remaining
Why do smart contracts use events to communicate activity?
Which of the following best explains why smart contracts emit events to communicate contract activity?
Attempts:
2 left
💡 Hint
Think about how external apps listen to blockchain changes.
✗ Incorrect
Events are stored in transaction logs and can be indexed, allowing off-chain apps to listen and react to contract changes efficiently without expensive storage reads.
🔧 Debug
advanced2:00remaining
Identify the error in this event emission code
This Solidity function is supposed to emit an event when a user deposits Ether. What error will occur when compiling or running this code?
Blockchain / Solidity
pragma solidity ^0.8.0; contract Deposit { event DepositMade(address indexed user, uint amount); function deposit() public payable { emit DepositMade(msg.sender, msg.value); } function getBalance() public view returns (uint) { return address(this).balance; } }
Attempts:
2 left
💡 Hint
Check the use of 'indexed' and payable function modifiers.
✗ Incorrect
The code correctly declares an event with an indexed parameter and emits it inside a payable function. This is valid Solidity syntax and behavior.
❓ Predict Output
advanced2:00remaining
What is the output of this event filter query?
Given a contract that emits this event multiple times, what will be the result of filtering logs for
ValueChanged events where author equals a specific address?Blockchain / Solidity
event ValueChanged(address indexed author, uint256 oldValue, uint256 newValue); // Assume events emitted: // ValueChanged(0xABC..., 0, 10) // ValueChanged(0xDEF..., 10, 20) // ValueChanged(0xABC..., 20, 30) // Filter query: author == 0xABC...
Attempts:
2 left
💡 Hint
Indexed parameters allow filtering events by that value.
✗ Incorrect
Events with indexed parameters can be filtered by those values. Here, filtering by author address 0xABC... returns the two events where that address emitted the event.
🧠 Conceptual
expert2:30remaining
Why are events preferred over contract storage for communicating activity to off-chain systems?
Which reason best explains why blockchain developers prefer emitting events instead of writing data to contract storage to communicate contract activity to off-chain systems?
Attempts:
2 left
💡 Hint
Think about gas costs and how off-chain apps listen to blockchain data.
✗ Incorrect
Events are stored in transaction logs which are cheaper to write than contract storage and can be indexed for fast filtering. Off-chain systems use these logs to track contract activity efficiently.