Loading...

What is Events in Solidity & How to Use it [With Example]

What is Events in Solidity & How to Use it [With Example]

Solidity is a high level, object-oriented programming language for writing smart contracts in the Ethereum Blockchain. Smart contracts are used to manipulate the Ethereum Blockchain and govern the behaviour of the accounts within the Ethereum Blockchain.

With Solidity, you can create interesting Web3.0 projects like a crowdfunding system, blind auctions, multi-signature wallets. Solidity keeps receiving many regular updates which means that you have to adapt to the new version as quickly as you can. The most recent Solidity version is 0.8x.

What are Events in Solidity?

As Solidity is similar to languages like Python, let’s take a look at what Events are in Python and co-relate them with Events in Solidity.

Events in Python are basically a class that notifies other classes when a specific event takes place which might be of interest to the other classes. In simple language, an Event is basically raising a flag and alerting others that something worth nothing has taken place.

Events in Python are generally used when there is some UI interaction involved and to help alert the users of the occurrence of an event. A real-life example could be a store is being robbed and the employees pressing the Emergency button to notify the police and the shop owner about the theft. The pressing of the button leads to the triggering of an event where the system then needs to send an alert to the police and the shop owner about the robbery taking place. This chain of events is how events in Python work as well.

Events in Solidity work on the same logic as well but the only difference is that they are on the blockchain and thus work a bit differently. Let’s take a look at how Events work in Solidity.

As said above, Events are used to interact with the UI to make sure that the user is alerted about any events which the user should be notified about, for example, a transaction passing or failing, deposits failing, etc. Events notify a user about the events happening on the Blockchain to ensure ease of use when someone uses dApps.

If the user has to scan the Blockchain themselves to make sure whether their transaction has passed or failed it would have been very inconvenient for the user, instead of this Events do the job of notifying the user whether something has passed or failed by showing it on the UI itself. There are two types of Events in Solidity, one of them is where we can interact with the UI and one of them is called by using the keyword “emit”, which means that the Event will not show up on the UI but will just be stored on the blockchain and will stay there as long as the Smart Contract is up and running on the Blockchain. Events were added in a recent update to Solidity when hackers managed to steal around 3.6m$ from a DAO resulting in a hard fork of the Solidity languages and changes being made to it. Out of these changes, the addition of Events was the most prominent one as they were added to prevent further hacks using the same method.

Let’s see how to create an Event in Solidity

Example of Event in Solidity 

event deposit(
address indexed user
uint256 time
uint256 etherAmount
);
//output: will display the user address, the time of the transaction and the amount of money sent

Now if we emit the Deposit event let’s see what happens:

function deposit() payable {
event deposit(
address indexed user
uint256 time
uint256 etherAmount
);
emit deposit(msg.sender, block.timestamp, msg.value);
};
//output: -

Here, ‘msg. sender’ is the account address sending the Ether, ‘block. timestamp’ is the time when the Ether was sent, ‘msg. value’ is the amount of Ether sent. All of these are global variables and will only be emitted here as they are being told to emit the deposit event.

As Web3.0 has just started to gain traction, many companies are ahead of the curve and have already started to adapt to the change and have started implementing Solidity in their development processes. Some of the top companies using Solidity are:

  • LeewayHertz
  • EngineerBabu
  • TreeHouse Technology Group
  • OpenGeek Slab
  • Altoros
  • AppInvetiv
  • Abes Lab
  • Arc Touch

How you can start learning Solidity via Codedamn?

Codedamn offers a concise learning path to help you get started with writing Smart Contracts in Solidity to help you build multiple projects in the Web3.0 space. You can learn about the Ethereum Blockchain, Solidity, Smart Contracts, MetaMask, Creating your own coin and launching it, ICO(Initial Coin Offering), etc.

You can access the course here

Codedamn’s Solidity related material:

  • Solidity Fundamentals
  1. What is Solidity? 
  2. Remix IDE
  3. Solidity Compilation Steps.
  4. Mainnet vs Testnet 
  • Solidity Programming
  1. Say “Hello World” in Solidity 
  2. Contract Development Environment 
  3. Solidity Sample Program
  4. State Variables 
  5. Local Variables
  6. Functions
  7. Create functions
  8. Pass an argument to the function
  9. View vs Pure 
  10. Constructor
  11. Integers 
  12. Strings 
  13. Storage vs Memory vs Stack on EVM   
  14. If Else 
  15. Booleans 
  16. Fixed Size Array
  17. Dynamic Size Array 
  18. Fixed (byte) Size Array
  19. Dynamic (byte ) Size Array 
  20. Dynamic Array
  21. Loops 
  22. Storage program 
  23. Struct 
  24. Enum 
  25. Mappings 
  26. Mappings Struct  
  27. Global Variables (Special functions and variables)
  • Solidity Advanced Concepts
  1. Inheritance 
  2. Abstract Contracts 
  3. Interface 
  4. Polymorphism 
  5. Visibility
  6. Require 
  7. Modifier 
  8. Payable Function 
  9. Payable Address 
  10. Fallback Function 

There are many practice labs that you can use to try out the recent concepts you have learned along the way!!

Where to write Solidity Code?

You can use Codedamn’s Solidity Playground. PlayGround lets you write and edit Solidity code which you can easily run and compile right in the browser. You just need to type your code and click on the RunCode button on the bottom left of the screen and the output of your code will be displayed in the terminal. You can use the Codedamn Playground to write Smart Contracts for your Web3 projects as well. 

The Playground is free to use. You can access it from here

Codedamn Compiler opens up a docker container in the backend of the website which then uses WebSockets to verify your credentials and then help run the code in the background and display the output to you in the Terminal. In simple terms, it opens a separate PC in the background which compiles your Solidity Code checks for any errors or problems in your code and shows the output to you on your computer in the Terminal of the Codedamn PlayGround. Codedamn playground uses Solc, which has been rated as the best compiler for Solidity. Making use of Solc compiles your code and displays the output in a matter of a few seconds. It produces various outputs ranging from assemblies and simple binaries over an abstract syntax tree to estimations of gas usage. 

Why use Playground Compiler?

Installing a separate code editor for only one specific language can be a hassle. Compiling your code on Codedamn Playground is very easy as it opens up another computer for you that does all the work in the background without making your own Computer Lag and also compiles it faster than any other compiler available anywhere. The ease of use is another crucial factor that ensures that all your files are in one place and are always safe, due to the AutoSave function which saves every line of code you write ensuring that you never lose your work.

 

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: