0
0
Blockchain / Solidityprogramming~20 mins

Event testing in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Testing 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 that emits an event. What will be the output when the triggerEvent function is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract EventTest {
    event NumberSet(uint indexed number);

    function triggerEvent() public {
        emit NumberSet(42);
    }
}
AA compilation error occurs because the event is not declared correctly.
BThe function returns the number 42 as output.
CThe event NumberSet with number 42 is emitted and logged on the blockchain.
DThe event NumberSet is emitted but with number 0 instead of 42.
Attempts:
2 left
💡 Hint

Events in Solidity are used to log information on the blockchain and do not return values from functions.

🧠 Conceptual
intermediate
1:30remaining
Which statement about blockchain event testing is true?

When testing events emitted by smart contracts, which of the following is correct?

AEvents automatically revert the transaction if the event data is incorrect.
BEvents are returned as function return values and can be asserted directly.
CEvents cannot be tested because they are not stored on the blockchain.
DEvents can be tested by checking transaction logs after the transaction is mined.
Attempts:
2 left
💡 Hint

Think about how events are stored and accessed after a transaction.

🔧 Debug
advanced
2:30remaining
Why does this event test fail to detect the emitted event?

Given the following test code snippet for a Solidity contract event, why does the test fail to detect the event?

Blockchain / Solidity
const tx = await contract.triggerEvent();
const receipt = await tx.wait();
const event = receipt.events.find(e => e.event === 'NumberSet');
assert(event.args.number === 42);
AThe event argument is accessed incorrectly; it should be <code>event.args[0]</code> instead of <code>event.args.number</code>.
BThe transaction receipt does not contain events because <code>wait()</code> was not called.
CThe contract does not emit any event named 'NumberSet' because the event is not declared.
DThe event name is case-sensitive and 'NumberSet' does not match the emitted event name.
Attempts:
2 left
💡 Hint

Check the exact spelling and capitalization of the event name in the contract and test.

📝 Syntax
advanced
2:00remaining
Which option correctly declares and emits an event with two parameters in Solidity?

Choose the correct Solidity code snippet that declares an event with two parameters and emits it properly.

A
event DataSet(uint id, string value)
function setData() public {
  emit DataSet(1, "hello");
}
B
event DataSet(uint indexed id, string value);
function setData() public {
  emit DataSet(1, "hello");
}
C
event DataSet(uint id, string value);
function setData() public {
  DataSet(1, "hello");
}
D
event DataSet(uint indexed id, string value);
function setData() public {
  emit DataSet(1);
}
Attempts:
2 left
💡 Hint

Remember that events must be declared with a semicolon and emitted with the emit keyword including all parameters.

🚀 Application
expert
3:00remaining
How many events are emitted and what are their values after this function call?

Given the following Solidity function, how many events are emitted and what are their values when multiEmit() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract MultiEvent {
    event Log(uint indexed id, string message);

    function multiEmit() public {
        for (uint i = 1; i <= 3; i++) {
            emit Log(i, string(abi.encodePacked("Event", uint2str(i))));
        }
    }

    function uint2str(uint _i) internal pure returns (string memory str) {
        if (_i == 0) {
            return "0";
        }
        uint j = _i;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len;
        while (_i != 0) {
            k = k-1;
            uint8 temp = (48 + uint8(_i - _i / 10 * 10));
            bstr[k] = bytes1(temp);
            _i /= 10;
        }
        str = string(bstr);
    }
}
AThree events are emitted with values: (1, "Event1"), (2, "Event2"), (3, "Event3").
BThree events are emitted but all have the same value: (3, "Event3").
CNo events are emitted because string concatenation is invalid.
DOne event is emitted with value: (3, "Event3").
Attempts:
2 left
💡 Hint

Look at the loop and how the event is emitted each iteration with the current index.