What are events in Solidity?

Overview

In SolidityAn object-oriented programming language, when an event is triggered, it keeps the parameters supplied in the transaction logs as an inheritable part of the contract.

What are events?

Events are member contracts that can be inherited and triggered or emitted to store transaction log arguments, which can be accessed through an address on the blockchain.

Events declaration syntax

event <eventName>(parameters) ;

Code example

The following code shows the usage of the emit to trigger a simple event.

pragma solidity ^0.5.0;
contract sample {
event Save(address indexed _from, bytes32 indexed _id, uint _value);
function save(bytes32 _id) public payable {
emit Save(msg.sender, _id, msg.value);
}
}

Code explanation

In the provided code example, we demonstrate a simple event (i.e., Save).

  • In line 3, we instantiate a contract sample.

  • In line 4, we create our event Save.

  • In line 5, we create a function to trigger the event using the emit keyword in line 6.

Simple diagram of Solidity events

The Solidity events can be visualized using the figure below.

Simple diagram of Solidity events

Free Resources