Post
Share your knowledge.
Using events for analytics in Sui Move
How do I properly emit events in Sui Move for off-chain indexing? Are there specific patterns or traits I should follow for structured logging?
- Move CLI
- Move
- Smart Contract
Answers
2How to Emit Events in Sui Move
- Define an Event Structure
Create a struct that represents the event payload.
Add the drop ability because events are not stored permanently on-chain; they are emitted and discarded.
module my_project::example {
use sui::event;
/// Define the event structure
struct PurchaseEvent has drop {
buyer: address,
item_id: u64,
price: u64,
}
public fun emit_purchase_event(buyer: address, item_id: u64, price: u64) {
let event = PurchaseEvent { buyer, item_id, price };
event::emit(event);
}
}
event::emit is the standard function for emitting events.
Best Practices for Structured Logging
- Use Strongly Typed Structs
Avoid raw strings or arbitrary maps.
Define fields explicitly for predictable indexing.
- Keep Events Lightweight
Emit only essential information (IDs, addresses, numeric values).
Do not include large data like metadata or JSON.
- Emit Events at Critical Points
Actions like mint, transfer, burn, or state transitions (e.g., auction closed, order executed).
- Version Your Event Structures
If you upgrade a module, consider adding version suffix (e.g., PurchaseEventV2) for backward compatibility.
- Ensure Deterministic Ordering
Events should reflect the order of operations for easy reconstruction of history off-chain.
✅ How to Query Events Off-Chain
Use Sui’s RPC or Indexer API:
sui_getEvents or GraphQL endpoints can query by:
Event type (module + struct name)
Sender address
Transaction digest
Example:
sui_getEvents(filter: { MoveEventType: "0x2::my_project::PurchaseEvent" })
Tools like Sui Indexer, Kafka event streams, or custom indexer can subscribe to these events for analytics.
Security Consideration
Do not rely on events for on-chain logic because they are not part of state.
Only use events for off-chain analytics, notifications, and dashboards.
Do you know the answer?
Please log in and share it.
Move is an executable bytecode language used to implement custom transactions and smart contracts.