DEV Community

Cover image for Events in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Events in Solidity

Events are interfaces with EVM logging functionality that allow developers to track changes in smart contracts.

Solidity events are signals that smart contracts can fire, which cause the arguments to be stored in the transaction's log. Events are crucial for smart contract developers because they allow smart contracts to index variables in order to rebuild the storage stage, help to automatically update the user interface, and allow for testing of specific variables. Events are inheritable members of a contract that store the arguments passed in transaction logs when emitted. These logs are stored on blockchain and are accessible using address of the contract till the contract is present on the blockchain.

Events notify applications about changes made to contracts and applications which can be used to execute dependent logic. Solidity events allow us to easily query “stuff” that happened in blocks and transactions. If you run a blockchain node, you can “listen” for Solidity events and react accordingly. Knowing how to use events in Solidity makes smart contract development easier.

The following is an example of how to declare an event:

event Transfer(address indexed _from, address indexed _to, uint256 _value);
Enter fullscreen mode Exit fullscreen mode

This event will inform the external application that something on the blockchain has changed. The next example creates an event that has two parameters: the address of the sender and a string message:

event Message(address indexed sender, string message);
Enter fullscreen mode Exit fullscreen mode

Solidity event examples include defining and emitting an event for transfers. This code applies to an Ethereum transfer application where the event is triggered on a transfer. Additional code can be added to allow the event, upon triggering, to update the user interface showing that a transfer has taken place. To create an event, it must be defined within contracts as global and called within their functions. Events are declared by using the event keyword followed by an identifier and parameter list ending with a semicolon. The parameter values are used to log information or execute conditional logic.

In Solidity, when an event is emitted, it becomes an inheritable part of the contract and keeps the arguments that were supplied in the transaction logs. For as long as the contract is present on the blockchain, these logs are stored on the blockchain and can be accessed by using the contract's address to navigate to them.

Event generation is not accessible from within contracts, including the contract that generated and emitted the event in the first place. Generally, events are used to tell the caller program about the current state of the contract, which is accomplished through the usage of the logging capability provided by the Ethereum Virtual Machine.

Apps that may be used to execute the dependent logic are notified of changes to the contracts and applications that can be used to notify applications of changes to the contracts. The event keyword can be used to declare a specific event.

A good code example for events in Solidity is as follows:

event MyEvent(uint indexed _value); 

function doSomething() public { 
    emit MyEvent(42); 
}
Enter fullscreen mode Exit fullscreen mode

Events are defined in the contracts as global events that can be called from within the contract's functionalities. Using the event keyword, followed by an identifier and the parameter list, and ending with a semicolon, you can declare events in your application.

The parameter values are used to log information or to execute conditional logic depending on the situation. The information and values associated with it are recorded as part of the transactions that take place within the block. There is no requirement to provide variables; only data types are required instead.

An event can be called from any method by referencing it by name and passing it the necessary parameters to complete the task.

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

contract Event {
    // Event declaration
    // Up to 3 parameters can be indexed.
    // Indexed parameters helps you filter the logs by the indexed parameter
    event Log(address indexed sender, string message);
    event AnotherLog();

    function test() public {
        emit Log(msg.sender, "Hello Neuron!");
        emit Log(msg.sender, "Welcome to Tech Neuron!");
        emit AnotherLog();
    }
}
Enter fullscreen mode Exit fullscreen mode

For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)