By Tezos Protocol amendments team
In this blog post, we will discuss a new feature to the existing Michelson language for the Tezos smart contracts. This new feature will allow smart contracts to deliver event messages to off-chain applications by writing event-like data into the transaction receipt in the form of internal operation results. The prominent feature of this is that we define an event emitting instruction, and an event log format that is versatile and expressive.
In addition, having event support in Michelson will avoid manual, duplicating and competing definition of critical parts of event logging systems, so that smart contracts and consumers of events can integrate with each other seamlessly. Historically there has been efforts to emulate this feature using tickets or sending transactions to other smart contracts. This new event logging capability, however, promises to provide a direct, unified and intuitive interface to deliver events with competitive gas prices.
First let us define events in smart contracts on Tezos. Events are data written by smart contracts onto the blockchain metadata that are purely for consumption by off-chain applications. It is different from contract storage in the sense that events can be written by a smart contract, but not read by any smart contract including the one composing it. However, off-chain applications such as indexers like Tzkt or your DApp services can observe block header metadata for those event receipts containing data written by a smart contract.
Let us see one example. A voting smart contract allows addition and removal of quorum members. This contract can use this utility to "push" notification in changes to voters. The DApp off-chain service can process the notification events and actually send notifications to voters' contacts, via email or mobile application push notification service. Event messages in this case will be very likely of the following type expressed in CameLIGO.
How to emit events
Anatomy of events
Events are included in the list of internal operation results in the transaction metadata.
So allow us to present you how to emit events with Tezos contracts. The following code, executed once, will emit two events.
We are happy to roll out event logs in Tezos and we believe that with this feature we can greatly improve the Tezos ecosystem by enabling a variety of applications and modes of interaction between various systems both on-chain and off-chain. We hope that you are enjoying this post and get inspired for new ideas powered by this new feature.