0
0
Blockchain / Solidityprogramming~30 mins

Emitting events in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Emitting Events in a Smart Contract
📖 Scenario: You are creating a simple smart contract on the blockchain that tracks when a user sends a greeting message. You want to record each greeting by emitting an event so that external apps can listen and react to these greetings.
🎯 Goal: Build a Solidity smart contract that emits an event every time a greeting is sent. This event should include the sender's address and the greeting message.
📋 What You'll Learn
Create an event called Greeted with parameters address sender and string message.
Create a public function called sendGreeting that takes a string memory message as input.
Emit the Greeted event inside the sendGreeting function with the sender's address and the message.
Test the contract by calling sendGreeting and ensure the event is emitted.
💡 Why This Matters
🌍 Real World
Events are used in blockchain apps to notify wallets, user interfaces, and other contracts about important actions like transfers, votes, or messages.
💼 Career
Blockchain developers must know how to emit and listen to events to build responsive decentralized applications (dApps) and smart contracts.
Progress0 / 4 steps
1
Create the smart contract and define the Greeted event
Write a Solidity contract named GreetingContract. Inside it, declare an event called Greeted that has two parameters: address sender and string message.
Blockchain / Solidity
Need a hint?

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

2
Add the sendGreeting function with a string memory message parameter
Inside the GreetingContract, add a public function called sendGreeting that takes one parameter: string memory message. The function should be public and payable is not needed.
Blockchain / Solidity
Need a hint?

Define the function with the correct name, parameter type, and visibility.

3
Emit the Greeted event inside sendGreeting
Inside the sendGreeting function, emit the Greeted event. Pass msg.sender as the sender and the message parameter as the message.
Blockchain / Solidity
Need a hint?

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

4
Test by calling sendGreeting and observe the event
Add a comment line that shows how to call sendGreeting with the message "Hello, blockchain!". Then, print a confirmation string "Greeting sent!" using console.log (assume a testing environment that supports it).
Blockchain / Solidity
Need a hint?

Solidity contracts do not print output. Use comments to show the call and a confirmation message.