Solidity Events

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.

Syntax

event <eventName>(parameters) ;

The given program shows the basic use of solidity events:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{event mrx_Event(string ample);function get_String(string memory _mrx) public {emit mrx_Event(_mrx);}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Correspondingly, the sum of two variables can be calculated in the following way taking the solidity events into use:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{event mrx_Event_Sum(uint mrx,uint ample,uint sum); function get_Sum(uint _mrx,uint _ample) public returns(uint){uint _sum=_mrx+_ample;emit mrx_Event_Sum(_mrx,_ample,_sum);return _sum;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

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:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{event mrx_MyTrade( uint256 indexed mrx_data, address sender, address indexed receiver, uint256 indexed transaction_amount ); function ample_trade(address receiver, uint256 transaction_amount) external { emit mrx_MyTrade(block.timestamp, msg.sender, receiver,transaction_amount); } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example Explanation

This code defines a Solidity contract called SolidityTest which includes an event called mrx_MyTrade.

The mrx_MyTrade event has four parameters:

  1. mrx_data is an unsigned integer of 256 bits that is indexed.
  2. sender is an Ethereum address that represents the account that initiated the trade. It is not indexed.
  3. receiver is an Ethereum address that represents the account that received the trade. It is indexed.
  4. 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:

  1. receiver is the Ethereum address of the account that will receive the trade.
  2. 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.


Conclusion

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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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