
Understanding Events in Solidity: A Practical Guide
Explore the concept of events in Solidity, from defining and pushing events to subscribed listeners to illustrating them with examples like blind auctions. Learn about event invocation and logging, and discover how events can be useful for tracking transactions and communicating significant milestones in smart contracts.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Events in Solidity B. Ramamurthy
Learning outcomes Explain the concept of events: defining an event and pushing an event to any subscribed listeners. - Illustrate the events using the blind auction example. Logging Events for 2
Event definition and invocation event nameOfEvent( parameters); Example: event votingCompleted(); // for ballot - //event definition event votingCompleted(); //event invocation emit votingCompleted(); 3
Blind Auction event AuctionEnded(address winner, uint highestBid); Definition - emit AuctionEnded(highestBidder, highestBid); pushing an event 4
Event invocation Invoking an event is by the name of the event and any parameters. In the function Vote when the stage changes to Done, we indicate that by invoking votingCompleted event. emit votingCompleted(); - In the case of Ballot, we will push this event at the end of the voting period. In the case of Blind Auction, we push the event when the auction ends? Is it useful to push events for every phase? Let s do that. 5
Event Logging An event is pushed as opposed to regular function call that is invoked or pulled to get action performed. Typically event feature is used to indicate to a client application (user interface or a transaction monitor) that a significant milestone has been reached. These applications can listen to the events pushed to:: 1. Track transactions 2. Receive results through arguments of the events 3. Initiate a pull request to receive information from the smart contract. - Event handlers are written in app.js and they get the logs from the block header. 6
Lets explore it first in Remix For Blind auction, we will do these: 1. Define an event for AuctionEnded, and push it in auctionEnd function. 2. Then observe its operation in Remix console 3. Lets add one more event: event RevealPhase(bytes32 x); if (x == Phase.Reveal) emit RevealPhase ("reveal phase");
Where are these events stored? They are logged in the logs of the block header. if(result.receipt.status != '0x01') alert("Transfer failed"); for (var i = 0; i < result.logs.length; i++) { var log = result.logs[i]; var singularText = "coins were"; if(log.args.amount == 1){ singularText = "coin was"; } // Look for the event Sent // Notification if (log.event == "Sent") { var text = 'Coin transfer: ' + log.args.amount + " " +singularText + ' sent from ' + log.args.from + ' to ' + log.args.to + '.'; jQuery('#showmessage_text').html(text); jQuery('#show_event').animate({'right':'10px'}); setTimeout(function(){jQuery('#show_event').animate({'right':'-410px'},500)}, 15000); break; } }
Summary Lets add events to Blind Auction For announcing the phases : Bidding and Reveal Lets now examine the ASK.sol