Events
Events are emitted during the execution of a transaction. Each Move module can define its own events and choose when to emit the events upon execution of the module. Endless Move supports two form of events: module events and EventHandle events. Module events are the modern event mechanism and shipped in the framework release 1.7. EventHandle events are deprecated and shipped with the original framework. Because of how blockchains work, EventHandle events will likely never be fully removed from Endless.
Module Events
Module events are global event streams identified by a struct type. To define an event struct, add the attribute #[event]
to a normal Move struct that has drop
and store
abilities. For example,
And then create and emit the event:
Examples of coin transfer on testnet is here(https://scan.endless.link/txn/3143636/events?network=testnet) . Indices 0(Withdraw
), 1(Deposit
) are module events of type 0x1::fungible_asset
.
Access in Tests
Events are stored in a separate merkle tree called event accumulator for each transaction. As it is ephemeral and hence independent of the state tree, MoveVM does not have read access to events when executing transaction in production. But in tests, Endless Move supports two native functions that read emitted events for testing and debugging purposes:
API Access
Event-Handle Event
EventHandle is identified by a globally unique value, GUID, and a per-event sequence number and stored within a resource. Each event within a stream has a unique sequence number derived from the EventHandle sequence number.
Assuming that an account GinLHfukKufLYJ757NfyCLpeDvjeNjLPQwc7waco3o7b
had sent coins to another account, the following query could be made to the REST interface: https://rpc-test.endless.link/v1/accounts/GinLHfukKufLYJ757NfyCLpeDvjeNjLPQwc7waco3o7b/events/0x1::fungible_asset::FungibleStore<0x1::endless_coin::EndlessCoin>/owner
. The output would be all WithdrawEvent
s stored on that account, it would look like
Each registered event has a unique key
. The key 0x0000000000000000caa60eb4a01756955ab9b2d1caca52ed
maps to the event 0x1::fungible_asset::FungibleStore<0x1::endless_coin::EndlessCoin>/sent_events
registered on account GinLHfukKufLYJ757NfyCLpeDvjeNjLPQwc7waco3o7b
.
These represent event streams, or a list of events with each entry containing a sequentially increasing sequence_number
beginning at 0
, a type
, and data
. Each event must be defined by some type
. There may be multiple events defined by the same or similar type
s especially when using generics. Events have associated data
. The general principle is to include all data necessary to understand the changes to the underlying resources before and after the execution of the transaction that changed the data and emitted the event.
Migration to Module Events
With the release of module events, EventHandle events are deprecated. To support migration to the module events, projects should emit a module event wherever they currently emit EventHandle events. Once external systems have sufficiently adopted module events, the legacy event may no longer need to be emitted.
Note, the EventHandle events cannot and will not be deleted and hence projects that are unable to upgrade will continue to be able to leverage them.
Last updated