Advanced Functions

Events and Logging

Beginner Level

In Solidity, events are used to log information on the blockchain. They allow smart contracts to communicate with external applications, such as front-end DApps, without storing all data in storage (which is expensive).

  • Declared with the event keyword.
  • Emitted using emit within functions.
  • Logs are stored in transaction logs and do not consume storage.

Example:


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract EventExample {
    event Deposit(address indexed sender, uint256 amount);

    function deposit() public payable {
        emit Deposit(msg.sender, msg.value); // Log the deposit
    }
}
  • indexed allows filtering by that parameter.
  • External apps can listen to these events to update UI or trigger actions.

Continue Learning

Explore more topics in Solidity or browse other tutorials.