Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event pattern in Substrate

Tags:

substrate

Event pattern in blockchain solutions is well known, could anyone from Substrate's Team link to the pattern in Substrate code?

I just want to understand the pattern in the context of Substrate framework and Rust language.

like image 604
Tomasz Waszczyk Avatar asked Oct 22 '25 22:10

Tomasz Waszczyk


2 Answers

Substrate uses an enum to represent events in the Runtime. Each module can create its own Events enum with the decl_event! macro, and the runtime combines all these seperate objects into a single "outer enum" which captures all possible events that could be emitted from your runtime.

Then, events are simply placed in storage until the beginning of the next block.

Front ends process the event storage item from block to block and can use the information provided in it to notify users when certain actions have occurred.

Here is a walk-through of the code in Substrate that follows an event being emitted and then placed in storage: What is the cost of event storage in substrate?

like image 71
Shawn Tabrizi Avatar answered Oct 25 '25 19:10

Shawn Tabrizi


The concepts of using events in Substrate are similar to those of Ethereum where the pattern was first popularized. Code paths in your on-chain logic (pallet in Substrate, contract in Ethereum) can cause particular events to be emitted. Those events can be subscribed to from offchain for purposes of updating a UI or confirming a transaction executed as expected.

The Substrate Recipes demonstrates how to use events in a Substrate pallet https://substrate.dev/recipes/2-appetizers/4-events.html

like image 28
JoshOrndorff Avatar answered Oct 25 '25 19:10

JoshOrndorff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!