0
0
Blockchain / Solidityprogramming~20 mins

Event declaration in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Event Declaration in Solidity
📖 Scenario: You are building a simple smart contract for a blockchain application. You want to notify users when a new user registers by using an event.
🎯 Goal: Create a Solidity contract that declares an event called UserRegistered with two parameters: address userAddress and string userName. Then emit this event when a new user registers.
📋 What You'll Learn
Declare an event named UserRegistered with parameters address userAddress and string userName
Create a public function registerUser that takes a string memory userName as input
Emit the UserRegistered event inside the registerUser function with the sender's address and the userName
Print the event declaration and the function emitting the event
💡 Why This Matters
🌍 Real World
Events in smart contracts are like signals that tell external apps when something important happens, such as a user registering or a transaction completing.
💼 Career
Understanding event declaration and emission is essential for blockchain developers to build interactive and responsive decentralized applications.
Progress0 / 4 steps
1
Declare the UserRegistered event
Declare an event called UserRegistered with parameters address userAddress and string userName inside the contract named UserRegistry.
Blockchain / Solidity
Need a hint?

Use the event keyword followed by the event name and parameters inside the contract.

2
Create the registerUser function
Add a public function called registerUser that takes a string memory userName as input inside the UserRegistry contract.
Blockchain / Solidity
Need a hint?

Define a function with the public visibility and a string memory parameter.

3
Emit the UserRegistered event inside registerUser
Inside the registerUser function, emit the UserRegistered event with msg.sender as userAddress and the input userName.
Blockchain / Solidity
Need a hint?

Use the emit keyword followed by the event name and parameters.

4
Test the event emission
Add a comment line with // Event UserRegistered declared and emitted on user registration to confirm the event is declared and emitted correctly.
Blockchain / Solidity
Need a hint?

Add a comment line to confirm the event is declared and emitted.