Understanding Events in Smart Contracts
Understanding Events in Smart Contracts
What are events in Smart Contracts?
Events in Smart Contracts can be used to communicate and store data in the transaction logs on the blockchain. It does so with the help of parameters passed inside of it as arguments and are emitted inside the function body to alert users that a particular function has been executed.
Events alert applications and users about the modifications or function executions made inside the contract and may trigger dependent logic.
How to Declare an Event?
Events can be declared in Solidity using the following syntax: event ownerChanged(address indexed _from, address indexed _to);
And events can be emitted using the following syntax: emit ownerChanged(owner, _newOwner);
The event arguments should be of the same type as defined in event creation.
Types of Events
Solidity event parameters are of two types: indexed and non-indexed. These parameters can be placed in the data or topics part. The searchable parameters in events are called “indexed events,” also referred to as “topics.” They allow detecting specific events from a sequence of blocks and searching them on the chain. If there’s no “indexed” keyword, that parameter will be non-indexed.
How to Emit an Event in Solidity?
In the example shown above, we have created an “owner” variable, which is being assigned to the msg.sender inside the constructor during contract deployment. An event called ownerChangedcontains parameters to show the old and the new owner. A function called changeOwnertakes the address of the _newOwnerand emits our event, which we created above with the old and the new owner addresses. The ownervariable is then updated with the address of the _newOwner.

When the function changeOwneris called, the emitted event can be seen in the logs section of the Remix IDE, along with the arguments passed while updating the owner.
Wizards and Dragons: Case Scenario
The Ethereum game Wizards & Dragons have made its smart contract available for public audit.
It can be seen in the following code snippet that the critical functions addAdminand removeAdmindo does not emit any events. This will make the function execution and admin updates difficult to track on the blockchain.
Our product SolidityScan detects missing events on critical functions and performs 100+ vulnerability and code optimization checks.

Best practices in Events in Solidity:
Some events take place for security and transparency, while others let web-based or external applications use contract data to show information.
Events on Critical Functions
It is recommended to emit events for all business-critical functions. This helps frontend and client applications to track the changes happening with the contract’s storage parameters. This also alerts users if any critical resource is updated or deleted.
Indexed Keywords on parameters
Indexed parameters allow us to filter specific data in logs. Although Indexed events are more expensive than non-indexed events, it makes tracking on the chain much easier.
Conclusion:
In this article, we discussed how events could be utilized in solidity smart contracts to their full potential to track and analyze state changes and function updates.
You can rely on SolidityScan to ensure that the proper procedures are followed to achieve top-tier smart contract security. Signup for a free trial at https://solidityscan.com/signup