0
0
Blockchain / Solidityprogramming~30 mins

Event-driven architecture patterns in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a Simple Event-Driven Blockchain Logger
📖 Scenario: You are creating a simple blockchain event logger. In blockchain systems, events like new blocks or transactions are emitted and other parts of the system listen and react to these events. This helps keep the system organized and responsive.Imagine you want to track when new blocks are added and when transactions happen, and log these events.
🎯 Goal: Build a simple event-driven system that listens for blockchain events and logs messages when new blocks or transactions occur.
📋 What You'll Learn
Create a dictionary called blockchain_events to hold event names and their listeners
Create a variable called new_block with the value 'block_added'
Write a function called emit_event that takes an event name and calls all listeners for that event
Print the log messages when events are emitted
💡 Why This Matters
🌍 Real World
Event-driven architecture is common in blockchain systems to handle asynchronous events like new blocks, transactions, or network changes efficiently.
💼 Career
Understanding event-driven patterns is important for blockchain developers to build scalable and maintainable decentralized applications and services.
Progress0 / 4 steps
1
Create the event listeners dictionary
Create a dictionary called blockchain_events with two keys: 'block_added' and 'transaction_made'. Each key should have an empty list as its value.
Blockchain / Solidity
Need a hint?

Use curly braces {} to create a dictionary. Each key should be a string and the value an empty list [].

2
Define the event name variable
Create a variable called new_block and set it to the string 'block_added'.
Blockchain / Solidity
Need a hint?

Assign the string 'block_added' to the variable new_block.

3
Write the event emitter function
Write a function called emit_event that takes one parameter event_name. Inside the function, use a for loop with variables listener to iterate over blockchain_events[event_name] and call each listener().
Blockchain / Solidity
Need a hint?

Use a for loop to go through each listener in the list for the event and call it like a function.

4
Add listeners and print event logs
Define two functions: log_block_added that prints 'New block added to the blockchain.' and log_transaction_made that prints 'New transaction recorded.'. Add log_block_added to blockchain_events['block_added'] and log_transaction_made to blockchain_events['transaction_made']. Then call emit_event(new_block) and emit_event('transaction_made') to print the logs.
Blockchain / Solidity
Need a hint?

Define functions that print the messages, add them to the correct event lists, then call emit_event with the event names.