Sui basic - events
Tags: sui, beginners, tutorial
Published: 2023-11-23 03:36:25

# Events

Events are important for Sui Move smart contracts, as it is the main way for indexers to track actions on-chain. You can understand it as logging on server backends, and indexers as parsers.

Events on Sui are also represented as objects. There are several types of system level events in Sui, including Move event, Publish event, Transfer object event, and so on. For the full list of system event types, please refer to the [Sui Events API page here](https://docs.sui.io/build/event_api).

The event details of a transaction can be viewed on the [Sui Explorer](https://suiexplorer.com/) under the `Events` tab:
![Screenshot 2023-11-16 at 16 30 48](https://github.com/truonggau/sui-tutorial/assets/87189382/b3292df5-1180-4807-b38f-440d916407f3)

## Custom Events

Developers can also define custom events on Sui. We can define a custom event marking when a transcript has been requested in the following way.

```rust
    /// Event marking when a transcript has been requested
    struct TranscriptRequestEvent has copy, drop {
        // The Object ID of the transcript wrapper
        wrapper_id: ID,
        // The requester of the transcript
        requester: address,
        // The intended address of the transcript
        intended_address: address,
    }
```

The type representing an event has the abilities `copy` and `drop`. Event objects aren't representing assets, and we are only interested in the data contained within, so they can be duplicated, and dropped at the end of scopes.

To emit an event in Sui, you just need to use the [`sui::event::emit` method](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/event.md#function-emit).

Let's modify our `request_transcript` method to emit this event:

```rust
    public fun request_transcript(transcript: WrappableTranscript, intended_address: address, ctx: &mut TxContext){
        let folderObject = Folder {
            id: object::new(ctx),
            transcript,
            intended_address
        };
        event::emit(TranscriptRequestEvent {
            wrapper_id: object::uid_to_inner(&folderObject.id),
            requester: tx_context::sender(ctx),
            intended_address,
        });
        //We transfer the wrapped transcript object directly to the intended address
        transfer::transfer(folderObject, intended_address);
    }
```

On the Sui explorer, we can see the event emitted displayed as the following, showing the three data fields that we defined in the `TranscriptRequestEvent` event:
![Screenshot 2023-11-16 at 16 29 52](https://github.com/truonggau/sui-tutorial/assets/87189382/b7378a85-456e-43b3-a3cb-974b6d20cde4)

*Here is example code: [sui](https://github.com/truonggau/sui-tutorial/blob/main/basic_example/basic_example/build/basic_example/sources/transcript_4.move) and [js](https://github.com/truonggau/sui-tutorial/blob/main/basic_example/js/main.js)*

Try out creating, requesting and unpacking transcripts using the Sui CLI client and the Sui explorer to check the result. 

That's the end of basic example, great job!

## Referent

- Part 5: [Capability Design Pattern](https://dev.to/truongnx/sui-basic-capability-design-pattern-22mn)

- Part 7: [Generics](https://dev.to/truongnx/sui-basic-generics-2c64)