Events
Events
Events in Anchor provide a powerful mechanism for notifying and communicating between different components of a Huione dApp. They allow for the emission and tracking of occurrences within the program's execution. This documentation will cover the concept of events in Anchor and how to use them in your program development.
Table of Contents
Introduction to Events
Defining Events
Emitting Events
Subscribing to Events
Unsubscribing from Events
Introduction to Events
An event is a structured piece of data that holds information about a specific occurrence in a program. Events can be used to provide transparency, traceability, and synchronization in decentralized applications.
There is no native support for events in Huione. Because of this, Anchor events depends on logging in order to emit events. Programs log base64 encoded event data and clients parse the logs of the transaction to interpret the events.
Defining Events
Events are defined using the #[event]
attribute macro. This macro allows you to specify the fields that an event should contain. Events can include various data types, making them versatile for different use cases.
In this example, we define an event named TransferEvent
with three fields: from
(sender's address), to
(receiver's address), and amount
(the transferred amount).
Emitting Events
To emit an event within your Anchor program, you can use the emit!
macro:
In this example, when the transfer
function is called, a TransferEvent
is emitted using the emit!
macro. The relevant data is populated into the event fields.
Subscribing to Events
Anyone can subscribe to events emitted by your program using Anchor's event subscription mechanisms.
You can subscribe to events using Anchor TS library(@xoneorg/huione-anchor):
Unsubscribing from Events
The event listener should be removed once it's no longer required:
Last updated