0
0
Blockchain / Solidityprogramming~20 mins

Why events communicate contract activity in Blockchain / Solidity - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Mastery in Smart Contracts
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
AAn event log with author address, oldValue = 0, newValue = 42
BAn event log with author address, oldValue = 42, newValue = 42
CNo event log is created because events are not stored
DA runtime error occurs because events cannot have indexed parameters
Attempts:
2 left
💡 Hint
Events record the state before and after the change and include the sender's address.
🧠 Conceptual
intermediate
1: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?
AEvents automatically update contract storage variables for faster access.
BEvents allow off-chain applications to efficiently track contract state changes without reading all contract storage.
CEvents prevent other contracts from calling the emitting contract.
DEvents encrypt contract data to secure it from public view.
Attempts:
2 left
💡 Hint
Think about how external apps listen to blockchain changes.
🔧 Debug
advanced
2: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;
    }
}
ACompilation error: event parameters must be non-indexed.
BSyntaxError: 'indexed' keyword cannot be used in event parameters.
CNo error; the code compiles and runs correctly emitting the event.
DRuntime error: msg.value is not accessible in non-payable functions.
Attempts:
2 left
💡 Hint
Check the use of 'indexed' and payable function modifiers.
Predict Output
advanced
2: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...
ATwo event logs: one with oldValue 0 newValue 10, and one with oldValue 20 newValue 30
BAll three event logs regardless of author
CNo event logs because filtering by indexed parameters is not supported
DOnly the first event log with oldValue 0 newValue 10
Attempts:
2 left
💡 Hint
Indexed parameters allow filtering events by that value.
🧠 Conceptual
expert
2: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?
AEvents automatically trigger off-chain code execution, while storage changes do not.
BContract storage is private and cannot be read by off-chain systems, so events are the only way to share data.
CWriting to contract storage is slower because it requires consensus, but events do not require consensus.
DEvents are cheaper in gas and provide an efficient, indexed log that off-chain systems can easily monitor without expensive storage reads.
Attempts:
2 left
💡 Hint
Think about gas costs and how off-chain apps listen to blockchain data.