0
0
Blockchain / Solidityprogramming~20 mins

Emitting events in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Emission Master
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 the following Solidity contract snippet. What will be the output in the transaction logs after calling setValue(42)?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract TestEvent {
    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);
    }
}
AA compilation error due to missing event parameters
BAn event ValueChanged with author as zero address, oldValue 0, newValue 42
CAn event ValueChanged with author as caller address, oldValue 0, newValue 42
DNo event emitted because emit keyword is missing
Attempts:
2 left
💡 Hint
Remember that msg.sender is the caller address and emit triggers the event.
🧠 Conceptual
intermediate
1:30remaining
Why use indexed parameters in Solidity events?
In Solidity events, some parameters can be marked as indexed. What is the main purpose of marking event parameters as indexed?
ATo allow filtering of events by those parameters in the logs
BTo make the event parameters private and hidden from logs
CTo reduce gas cost by not storing those parameters
DTo automatically convert parameters to strings
Attempts:
2 left
💡 Hint
Think about how you can search for events in blockchain explorers.
🔧 Debug
advanced
2:00remaining
What error does this Solidity event code produce?
Examine the following Solidity code snippet. What error will occur when compiling?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract EventTest {
    event DataUpdated(uint indexed id, string indexed data);

    function updateData(uint _id, string memory _data) public {
        emit DataUpdated(_id, _data);
    }
}
ARuntime error: Event parameters mismatch
BCompilation error: Cannot index a string parameter
CNo error, code compiles and runs correctly
DCompilation error: Missing event declaration
Attempts:
2 left
💡 Hint
Check which types can be indexed in Solidity events.
Predict Output
advanced
1:30remaining
What is the number of topics in this emitted event?
Given the Solidity event and function below, how many topics will the emitted event have in the logs?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract TopicCount {
    event MultiIndexed(address indexed sender, uint indexed id, string message);

    function sendMessage(uint _id, string memory _msg) public {
        emit MultiIndexed(msg.sender, _id, _msg);
    }
}
A3 topics: event signature + 2 indexed parameters
B2 topics: event signature + 1 indexed parameter
C1 topic: only event signature
D4 topics: event signature + 3 parameters
Attempts:
2 left
💡 Hint
Remember the event signature is always a topic, plus one topic per indexed parameter.
🚀 Application
expert
2:30remaining
Which option produces the correct event log filtering in web3.js?
You want to listen for the Transfer event from an ERC20 token contract filtering only transfers from address 0xabc.... Which web3.js code snippet correctly sets up this filter?
Acontract.events.Transfer({ from: '0xabc...' }, callback);
Bcontract.events.Transfer({ topics: ['0xabc...'] }, callback);
Ccontract.events.Transfer({ filter: { to: '0xabc...' } }, callback);
Dcontract.events.Transfer({ filter: { from: '0xabc...' } }, callback);
Attempts:
2 left
💡 Hint
Check the correct property name for filtering event parameters in web3.js.