0
0
Blockchain / Solidityprogramming~10 mins

Event declaration in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event declaration
Start contract
Declare event with name and parameters
Use event in functions to emit logs
Listen to event outside contract
React to event data
End
This flow shows how an event is declared in a contract, emitted during function calls, and then listened to externally.
Execution Sample
Blockchain / Solidity
event Transfer(address indexed from, address indexed to, uint amount);

function send(address to, uint amount) public {
    emit Transfer(msg.sender, to, amount);
}
Declares a Transfer event and emits it inside a send function with sender, receiver, and amount.
Execution Table
StepActionEvent DeclarationEmit EventOutput/Log
1Contract startsTransfer event declared with parameters (from, to, amount)NoNo log
2Function send called with to=0xABC, amount=100Event already declaredEmit Transfer event with (msg.sender, 0xABC, 100)Log created: Transfer(from=caller, to=0xABC, amount=100)
3External listener receives eventNo changeNo emitListener reads Transfer event data
4EndNo changeNo emitNo further logs
💡 Execution stops after event is emitted and listener processes the log.
Variable Tracker
VariableStartAfter send callFinal
Transfer eventDeclared with (from, to, amount)Emitted with (caller, 0xABC, 100)Available for listeners
msg.senderCaller addressUsed as 'from' in eventNo change
toN/A0xABCNo change
amountN/A100No change
Key Moments - 3 Insights
Why do we declare the event before using it?
The event must be declared first (see step 1 in execution_table) so the contract knows its name and parameters before emitting it.
What happens when we emit an event inside a function?
Emitting creates a log entry (step 2) that external listeners can read later (step 3), but it does not change contract state.
Can events be read inside the contract after emitting?
No, events are for external listeners only; inside the contract, they do not store data accessible later.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is logged when the send function is called?
ANo event is logged
BTransfer event with sender, receiver, and amount
COnly the amount is logged
DAn error occurs
💡 Hint
Check row 2 under Output/Log in execution_table
At which step does the external listener receive the event data?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Action column in execution_table for when listener reads event
If the event was not declared before emitting, what would happen?
AThe contract would compile and emit normally
BThe event would be emitted with default parameters
CThe contract would fail to compile
DThe event would be ignored silently
💡 Hint
Refer to key_moments about event declaration necessity
Concept Snapshot
Event declaration syntax:
event EventName(type indexed param1, type param2);

Emit event inside function:
emit EventName(value1, value2);

Events create logs for external listeners.
Must declare event before emitting.
Events do not change contract state.
Full Transcript
In blockchain smart contracts, events are declared with a name and parameters to create logs. The contract declares the event first, then emits it inside functions to record actions like transfers. External listeners watch for these events to react to contract activity. Events do not store data inside the contract but provide a history log. The execution flow starts with event declaration, then function calls emit the event, and listeners receive the event data. This helps track contract actions externally.