Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Why events communicate contract activity in Blockchain / Solidity - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why events communicate contract activity
Smart Contract Executes
↓
Event Triggered in Contract
↓
Event Stored in Blockchain Logs
↓
External Listeners Detect Event
↓
Applications React to Event
When a smart contract runs, it triggers events that are saved in blockchain logs. External apps watch these events to know what happened.
Execution Sample
Blockchain / Solidity
event Transfer(address indexed from, address indexed to, uint amount);
function send(address to, uint amount) public {
  // transfer logic
  emit Transfer(msg.sender, to, amount);
}
This code emits a Transfer event when tokens are sent, signaling the transfer details.
Execution Table
StepActionEvent Emitted?Event DataEffect
1send() called with to=0xB, amount=100NoN/AStart transfer logic
2Transfer tokens from msg.sender to toNoN/ATokens moved internally
3emit Transfer(msg.sender, to, amount)Yes{from: msg.sender, to: 0xB, amount: 100}Event logged on blockchain
4External apps detect Transfer eventYes{from: msg.sender, to: 0xB, amount: 100}Apps update UI or trigger actions
5Function endsNoN/ATransfer complete
💡 Function ends after emitting event and completing transfer
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
toundefined0xB0xB0xB0xB
amountundefined100100100100
event Transfernot emittednot emittednot emittedemittedemitted
Key Moments - 2 Insights
Why do we emit events instead of just changing variables?
Events are stored in blockchain logs and can be easily detected by external apps, unlike internal variables which are not visible outside the contract. See step 3 and 4 in the execution_table.
Does emitting an event change the contract state?
No, emitting an event only logs information externally. The contract state changes happen separately, as shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the Transfer event emitted?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Event Emitted?' column in execution_table rows
According to variable_tracker, what is the value of 'amount' after Step 1?
A100
B0xB
Cundefined
Dnot emitted
💡 Hint
Look at the 'amount' row and 'After Step 1' column in variable_tracker
If the event was not emitted, what would external apps miss?
AThe internal variable changes
BThe token transfer details
CThe function call itself
DNothing, apps get all info anyway
💡 Hint
Refer to execution_table steps 3 and 4 about event logging and detection
Concept Snapshot
Smart contracts emit events to log important actions.
Events are stored in blockchain logs, visible to external apps.
Apps listen to events to react or update UI.
Emitting events does not change contract state.
Events help communicate contract activity clearly.
Full Transcript
When a smart contract runs a function like send(), it performs internal logic such as transferring tokens. During this process, it emits an event like Transfer with details of the transfer. This event is saved in the blockchain logs, which external applications watch. These apps detect the event and update their state or user interface accordingly. Emitting events is important because internal variables are not visible outside the contract, so events provide a clear way to communicate contract activity. The event emission does not change the contract's internal state but acts as a message for outside listeners.

Practice

(1/5)
1. Why do smart contracts use events to communicate activity?
easy
A. To send messages about contract actions to external listeners
B. To store large amounts of data permanently on the blockchain
C. To increase the gas cost of contract execution
D. To prevent users from interacting with the contract

Solution

  1. Step 1: Understand the purpose of events in smart contracts

    Events are designed to notify external systems about what happens inside the contract without storing data permanently on-chain.
  2. Step 2: Identify the correct use of events

    Events send messages to external listeners, like user interfaces or monitoring tools, about contract actions.
  3. Final Answer:

    To send messages about contract actions to external listeners -> Option A
  4. Quick Check:

    Events notify external systems = C [OK]
Hint: Events notify outside world about contract actions [OK]
Common Mistakes:
  • Thinking events store large data permanently
  • Believing events increase gas cost unnecessarily
  • Confusing events with access control mechanisms
2. Which of the following is the correct syntax to declare an event in Solidity?
easy
A. event Transfer function(address from, address to, uint amount);
B. event Transfer(address indexed from, address indexed to, uint amount);
C. event Transfer {address from, address to, uint amount};
D. event Transfer = (address from, address to, uint amount);

Solution

  1. Step 1: Recall Solidity event declaration syntax

    Events are declared with the keyword 'event', followed by the event name and parameters in parentheses, ending with a semicolon.
  2. Step 2: Identify the correct syntax among options

    event Transfer(address indexed from, address indexed to, uint amount); matches the correct Solidity syntax with 'indexed' keywords for filtering.
  3. Final Answer:

    event Transfer(address indexed from, address indexed to, uint amount); -> Option B
  4. Quick Check:

    Correct event syntax uses parentheses and semicolon = D [OK]
Hint: Events use 'event Name(params);' syntax in Solidity [OK]
Common Mistakes:
  • Using '=' instead of parentheses
  • Using curly braces instead of parentheses
  • Adding 'function' keyword in event declaration
3. Given the Solidity event and function below, what will be emitted when transfer(0x123, 100) is called?
event Transfer(address indexed from, address indexed to, uint amount);

function transfer(address to, uint amount) public {
    emit Transfer(msg.sender, to, amount);
}
medium
A. No event emitted because emit keyword is missing
B. Transfer event with from = 0x123, to = caller address, amount = 100
C. Transfer event with from = caller address, to = 0x123, amount = 100
D. Transfer event with from = 0x0, to = 0x123, amount = 0

Solution

  1. Step 1: Understand the function call and event emission

    The function emits a Transfer event with 'msg.sender' as 'from', 'to' as the passed address, and 'amount' as passed.
  2. Step 2: Apply the call parameters

    Calling transfer(0x123, 100) sets 'to' to 0x123 and 'amount' to 100; 'msg.sender' is the caller's address.
  3. Final Answer:

    Transfer event with from = caller address, to = 0x123, amount = 100 -> Option C
  4. Quick Check:

    emit Transfer(msg.sender, to, amount) = A [OK]
Hint: emit uses caller as from, passed args as to and amount [OK]
Common Mistakes:
  • Confusing 'from' and 'to' parameters
  • Forgetting 'emit' keyword
  • Assuming default zero addresses
4. Identify the error in the following Solidity code snippet that prevents the event from being emitted:
event Deposit(address indexed sender, uint amount);

function deposit(uint amount) public {
    Deposit(msg.sender, amount);
}
medium
A. Event parameters are in wrong order
B. Function must be marked as 'payable' to emit events
C. Event name should be lowercase
D. Missing 'emit' keyword before the event call

Solution

  1. Step 1: Review event emission syntax in Solidity

    Since Solidity 0.4.21, events must be emitted using the 'emit' keyword.
  2. Step 2: Check the code for missing 'emit'

    The code calls 'Deposit(msg.sender, amount);' without 'emit', so the event won't be emitted.
  3. Final Answer:

    Missing 'emit' keyword before the event call -> Option D
  4. Quick Check:

    Events require 'emit' keyword = B [OK]
Hint: Always use 'emit' before event calls in Solidity [OK]
Common Mistakes:
  • Omitting 'emit' keyword
  • Assuming event names are case-insensitive
  • Thinking 'payable' is required for events
5. You want to track token transfers in your contract efficiently. Which approach best uses events to minimize gas costs and allow external monitoring?
hard
A. Emit an event with indexed sender and receiver addresses and the amount transferred
B. Store all transfer details in a public array inside the contract
C. Use a mapping to save transfer history and emit no events
D. Emit events without indexed parameters to save gas

Solution

  1. Step 1: Understand gas cost and event indexing

    Indexed event parameters allow efficient filtering by external tools and cost less gas than storing data on-chain.
  2. Step 2: Evaluate options for tracking transfers

    Emitting events with indexed sender and receiver is efficient and allows external monitoring without high storage costs.
  3. Final Answer:

    Emit an event with indexed sender and receiver addresses and the amount transferred -> Option A
  4. Quick Check:

    Indexed events minimize gas and enable monitoring = A [OK]
Hint: Use indexed event params for efficient tracking [OK]
Common Mistakes:
  • Storing all data on-chain increases gas costs
  • Not indexing event parameters reduces filtering ability
  • Avoiding events leads to poor external monitoring
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard