Event Module
Theiota::event module provides functionality for emitting custom events that can be tracked off-chain. Events are essential for building indexes, monitoring contract activity, and creating responsive dApps.
Source: crates/iota-framework/packages/iota-framework/sources/event.move
Core Function
emit
Emit a custom event that will be included in the transaction effects:Event data to emit (must have copy + drop)
copy and drop abilities.
Event Properties
Every emitted event automatically includes:- Sender: Transaction sender address
- Type signature: Full type of
T(e.g.,0x123::my_module::ItemPurchased) - Event data: The value of
T - Timestamp: Local to the node
- Transaction digest: The transaction that emitted the event
Basic Usage
Define Event Struct
Create a struct withcopy + drop abilities:
Emit Multiple Events
You can emit multiple events in a single transaction:Event Patterns
Lifecycle Events
Track object creation, updates, and deletion:State Change Events
Track important state changes:Financial Events
Track transactions and payments:Phantom Type Parameters
Use phantom type parameters to make events type-specific:Complete Example: Marketplace
Testing Support
The module provides testing utilities:Best Practices
- Always emit events for important actions: This enables off-chain tracking and indexing
- Include relevant IDs: Always include object IDs so events can be correlated with on-chain data
-
Use descriptive names: Event names should clearly indicate what happened (e.g.,
ItemPurchased, notEvent1) - Keep event data minimal: Include only essential information; additional data can be queried on-chain
- Use phantom types: Use phantom type parameters to make events type-specific for better filtering
- Document event fields: Add comments explaining what each field represents
-
Version your events: If you need to change an event structure, create a new event type (e.g.,
ItemPurchasedV2)