Events
Learn about events in Solidity.
We'll cover the following...
Events in Solidity are a means for capturing and logging specific occurrences or state changes within a contract. Events are contracted inheritable components that allow essential information to be communicated to external programs. They are especially important for signaling changes in the state of the contract, allowing external applications to respond appropriately.
Event creation
Events are declared using the event
keyword, followed by an identifier, a parameter list, and a semicolon. The parameters capture relevant values for conditional logic or informational purposes. Events are invoked within the functions of a contract. They are triggered using the emit
keyword, followed by the event name and the necessary arguments.
// SPDX-License-Identifier: MITpragma solidity ^0.8.17;// Defining calling contractcontract Invoker {// Declaring an event named invokedevent Invoked(uint value, string slang);// Defining function to call// getValue and getSlang functionsfunction trigger() public returns (uint,string memory) {uint value = 2;string memory greet = "Hey!";// Emitting an eventemit Invoked(value,greet);return (value,greet);}}