Events help us know what happens inside a smart contract. They send messages outside the contract so other programs or users can see important actions.
0
0
Why events communicate contract activity in Blockchain / Solidity
Introduction
When you want to tell users that a payment was made.
When you want to record that a new item was added to a list.
When you want to track changes in contract ownership.
When you want to notify external apps about contract updates.
Syntax
Blockchain / Solidity
event EventName(type indexed param1, type param2); // To emit the event inside a function: emit EventName(value1, value2);
Events are declared once and then 'emitted' to send data.
Indexed parameters help filter events when searching logs.
Examples
This event tells who paid and how much.
Blockchain / Solidity
event PaymentMade(address indexed from, uint amount); function pay() public { emit PaymentMade(msg.sender, 100); }
This event announces a new item with its ID and name.
Blockchain / Solidity
event ItemAdded(uint itemId, string name);
function addItem(uint id, string memory name) public {
emit ItemAdded(id, name);
}Sample Program
This contract stores a number and sends an event every time the number changes. The event shows who changed it and the new value.
Blockchain / Solidity
pragma solidity ^0.8.0;
contract SimpleStore {
uint public value;
event ValueChanged(address indexed changer, uint newValue);
function setValue(uint _value) public {
value = _value;
emit ValueChanged(msg.sender, _value);
}
}OutputSuccess
Important Notes
Events do not change contract state; they only log information.
Listening to events is cheaper than reading contract storage repeatedly.
Events help external apps react quickly to contract changes.
Summary
Events send messages from contracts to the outside world.
They help track important actions like payments or updates.
Using events makes your contract easier to monitor and interact with.