Events

Overview

What are Events?

  • Events provide a logging mechanism for Move smart contracts.
  • Events are emitted during the execution of a transaction. Each Move module can define its own events and choose when to emit the events.

When to use Events?

Use events when the blockchain data changes and you need historical tracking. Ex: tracking the history of your Aptogotchi’s name changes.


Aptogotchi Example

Source Code

1. Import event from the aptos_framework.


use aptos_framework::event;

2. Define your Event struct.


// Event's 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).
#[event]
struct MintAptogotchiEvent has drop, store {
token_name: String,
aptogotchi_name: String,
parts: AptogotchiParts,
}

3. Emit Events.


event::emit<MintAptogotchiEvent>(
MintAptogotchiEvent {
token_name,
aptogotchi_name: name,
parts,
},
);

4. Once your smart contract is deployed and the code above is executed, you can check the events in the transaction that emits them in the Explorer. Like this one here.



More Resources

Read more about Events.

Link