In Solidity, firing or emitting events is a handy tool that you can employ not only to log messages for simple debugging but more importantly Dapps or anything connected to the Ethereum JSON-RPC API can listen to these events and act accordingly. Events can also be indexed so that the event history is searchable later as well.

Here is a very simple example of creating an event.

pragma solidity ^0.6.0;
contract EmitEvent {
event log();
function triggerLog() public {
emit log();
}
}

You first have to declare the event and then give it a name along with the parameters you want to pass around. The above example does not have any parameters. The example below has one parameter.

pragma solidity ^0.6.0;
contract EmitEvent {
event log(string _logmessage);
function triggerLog() public {
emit log("hi");
}
}

The event gets displayed in the logs of the transactions in remix.


Watch the video below for a walkthrough.

Leave a Reply

Your email address will not be published. Required fields are marked *