This example demonstrates how to define and emit events in Neo N3 smart contracts. Events are an essential mechanism for communicating state changes and important occurrences from smart contracts to external applications and services, enabling efficient monitoring and reactive behavior.
- Event declaration syntax
- Event emission with parameters
- Custom event naming
- Multiple event types
- Various parameter data types
The SampleEvent contract demonstrates several key aspects of Neo N3 events:
The example showcases two different event declarations:
- An event with custom display name and multiple parameters of different types
- A standard event with byte array and numeric parameters
The example demonstrates events with various parameter types:
- Byte arrays (binary data)
- String values
- BigInteger numbers
The contract shows proper event emission syntax, where:
- Events are triggered with appropriate parameters
- Parameters are passed in the correct order
- Different event types can be emitted in the same transaction
Events in Neo N3 are implemented as notifications through the Runtime.Notify mechanism:
- Declaration: Events are declared using the
eventkeyword with an Action delegate - Display Name: Optional custom names can be assigned using the
[DisplayName]attribute - Emission: Events are triggered by invoking them like methods with appropriate parameters
- Subscription: External applications can subscribe to contract events using the Neo RPC API
Events are used for:
- Notifying external systems about state changes
- Creating audit trails and transaction history
- Enabling event-driven architectures
- Supporting off-chain indexing and analytics
- Building reactive user interfaces
The example demonstrates recommended practices:
- Meaningful event names
- Properly typed parameters
- Efficient parameter ordering (smaller data first)
- Clear separation of different event types
Events emitted by this contract can be captured by:
- RPC clients subscribing to notifications
- Block explorers monitoring contract activity
- Application backends implementing event listeners
- Frontend applications using websocket connections
This example teaches:
- How to properly declare events in Neo N3 contracts
- The syntax for emitting events with multiple parameters
- How to customize event names for better readability
- Best practices for organizing different event types
- Parameter type handling for events
Events provide a critical communication channel between on-chain smart contracts and off-chain applications, enabling developers to build responsive and data-rich blockchain applications.