4

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.

Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73
  • 1
    You can find the macro that allows you to define events here: https://github.com/paritytech/substrate/blob/v2.0.0-rc5/frame/support/src/event.rs. Default logic for emitting events is here: https://github.com/paritytech/substrate/blob/v2.0.0-rc5/frame/system/src/lib.rs#L932. – Dan Forbes Aug 13 '20 at 21:05
  • For those who look for an answer I give a link also: https://substrate.dev/recipes/events.html – Tomasz Waszczyk May 15 '21 at 21:39

2 Answers2

6

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?

Shawn Tabrizi
  • 12,206
  • 1
  • 38
  • 69
3

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

JoshOrndorff
  • 1,591
  • 9
  • 19