What are Events in Solidity?

24 Jun, 2022
Solidity Events are the same as events in any other programming language. An event is an inheritable member of the contract, which stores the arguments passed in the transaction logs when emitted. Generally, events are used to inform the calling application about the current state of the contract, with the help of the logging facility of EVM. Events notify the applications about the change made to the contracts and applications which can be used to execute the dependent logic.
Events are defined within the contracts as global and called within their functions. Events are declared by using the event keyword, followed by an identifier and the parameter list, and ends with a semicolon. The parameter values are used to log the information or for executing the conditional logic. Its information and values are saved as part of the transactions inside the block. There is no need of providing variables, only datatypes are sufficient. An event can be called from any method by using its name and passing the required parameters.
event <eventName>(parameters) ;    
javascript
// Solidity program to demonstrate// creating an eventpragma solidity ^0.4.21; // Creating a contractcontract eventExample {     // Declaring state variables    uint256 public value = 0;     // Declaring an event    event Increment(address owner);       // Defining a function for logging event    function getValue(uint _a, uint _b) public {        emit Increment(msg.sender);        value = _a + _b;    }}
Output: 
Result in the console: 
Javascript
logs    [    {        "from": "0xd9145CCE52D386f254917e481eB44e9943F39138",        "topic": "0xfc3a67c9f0b5967ae4041ed898b05ec1fa49d2a3c22336247201d71be6f97120",        "event": "Increment",        "args": {            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",            "owner": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4"        }    }]
Index in events: We can also add an index to our event. On adding the different fields to our event, we can add an index to them it helps to access them later but of course, it’s going to cost some more gas!
IMP: We can add atmost 3 indexes in one event. 
Code: 
Solidity
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0;  contract IndexEvents {     event NewTrade(        uint256 indexed date,        address from,        address indexed to,        uint256 indexed amount    );     function trade(address to, uint256 amount) external  {        emit NewTrade(block.timestamp, msg.sender, to,amount);    }}
Output in the console:  adding the output in the console
Javascript
logs    [    {        "from": "0xcD6a42782d230D7c13A74ddec5dD140e55499Df9",        "topic": "0xa6b5ddd331f9dc412a8c258207b1c66f53c1740c72628d9913aafcb6b28d8f73",        "event": "NewTrade",        "args": {            "0": "1655406115",            "1": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",            "2": "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2",            "3": "1234",            "date": "1655406115",            "from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",            "to": "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2",            "amount": "1234"        }    }]
shivangi019717
Solidity-Events
Blockchain
Solidity
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.