This article explores the fundamentals of Solidity events, how to define and emit events in your Solidity code, and some best practices for implementing them in your code.
Solidity Events – what are they?
A Solidity Event is the same as an event in another programming language.
A contract’s event is an inheritable member that stores the arguments passed when it is emitted in the transaction log.
In general, events are used to inform a calling application about the current status of a contract, using the logging facility of the EVM. Whenever the contracts or applications are changed, they notify applications which can be used to execute dependent logic.
Create An Event
An event is defined within a contract as global, and is then called by a function within that contract.
Declaring events involves using the event keyword, followed by an identifier and parameter list, and ending with a semicolon.
Values of the parameters are used for logging information or executing conditional logic.
event <eventName>(parameters) ;
The given program shows the basic use of solidity events:
Correspondingly, the sum of two variables can be calculated in the following way taking the solidity events into use:
Index in events
It is also possible to add an index to our event.
The different fields in our event can be indexed, which makes it easier to access them later, but it is going to cost some extra gas.
The given examples shows the process of indexing different field used in an event:
This code defines a Solidity contract called SolidityTest which includes an event called mrx_MyTrade.
The mrx_MyTrade event has four parameters:
- mrx_data is an unsigned integer of 256 bits that is indexed.
- sender is an Ethereum address that represents the account that initiated the trade. It is not indexed.
- receiver is an Ethereum address that represents the account that received the trade. It is indexed.
- transaction_amount is an unsigned integer of 256 bits that represents the amount of the trade. It is indexed.
The ample_trade function is a public function that takes two parameters:
- receiver is the Ethereum address of the account that will receive the trade.
- transaction_amount is an unsigned integer of 256 bits that represents the amount of the trade.
Within the trade function, the mrx_MyTrade event is emitted using the emit keyword, which triggers the event and logs the values of its parameters.
The block.timestamp value is passed as the mrx_data parameter, which is not indexed.
The msg.sender value is passed as the sender parameter, which is not indexed. The receiver parameter is passed as the receiver parameter, which is indexed. The transaction_amount parameter is passed as the transaction_amount parameter, which is indexed.
By emitting this event within the trade function, the smart contract will log the trade details and the involved addresses and amounts for future reference.
The indexed parameters of an event can be used to filter events and quickly retrieve relevant data, making them a useful tool for tracking transactions and events on the Ethereum blockchain.
Solidity events are an integral part of the Solidity programming language used to develop smart contracts on the Ethereum blockchain.
The events allow smart contracts to emit and log events, which can be used by external applications to track blockchain state and trigger actions in response to specific events.
There are different use cases for Solidity events, and they can be customized depending on the needs of the users.
Additionally, they make auditing smart contracts transactions easy, which provides transparency and accountability.
The Ethereum blockchain is full of powerful decentralized applications built with Solidity events, and they are also very easy to use.