0
0
Blockchain / Solidityprogramming~30 mins

Why events communicate contract activity in Blockchain / Solidity - See It in Action

Choose your learning style9 modes available
Why Events Communicate Contract Activity
📖 Scenario: Imagine you have a smart contract on the blockchain that tracks simple transactions. You want to know when someone sends money or performs an action without constantly checking the contract's state. Events help by sending messages that something happened.
🎯 Goal: You will create a simple Solidity contract that uses an event to announce when a user sends money. This shows how events communicate contract activity clearly and efficiently.
📋 What You'll Learn
Create a Solidity contract named SimpleBank
Declare an event named DepositMade with parameters address account and uint amount
Create a deposit function that accepts Ether and emits the DepositMade event
Add a public mapping balances to track user balances
Emit the event inside the deposit function after updating the balance
💡 Why This Matters
🌍 Real World
Events are used in blockchain apps to notify wallets, user interfaces, and other contracts about changes without expensive data reads.
💼 Career
Understanding events is essential for blockchain developers to build interactive and efficient decentralized applications.
Progress0 / 4 steps
1
Create the contract and balances mapping
Write a Solidity contract named SimpleBank and inside it declare a public mapping called balances that maps address to uint.
Blockchain / Solidity
Need a hint?

Start by writing contract SimpleBank { } and inside it add mapping(address => uint) public balances;

2
Declare the DepositMade event
Inside the SimpleBank contract, declare an event named DepositMade that has two parameters: address account and uint amount.
Blockchain / Solidity
Need a hint?

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

3
Create the deposit function and emit the event
Add a public payable function named deposit inside SimpleBank. Inside it, increase the sender's balance in balances by msg.value and then emit the DepositMade event with msg.sender and msg.value.
Blockchain / Solidity
Need a hint?

Remember to mark the function as payable to accept Ether. Use emit to send the event.

4
Test the contract by calling deposit and observe event
Write a comment line that explains that when deposit is called, the DepositMade event will notify listeners about the deposit.
Blockchain / Solidity
Need a hint?

Write a comment starting with // explaining the event's purpose.