Unlocking Event Emission in Aptos Move: A Comprehensive Guide and Testing Approach

Moncayo Labs
Coinmonks
Published in
4 min readMar 28, 2024

--

Mastering Event Emission: Step-by-Step Instructions and Proven Testing Strategies for Aptos Blockchain Developers

Event Emission in Aptos Blockchain Development

In the dynamic landscape of blockchain development, understanding event emission on any blockchain is paramount for building robust and transparent decentralized applications. Event emission in Aptos Move — the blockchain-friendly Rust language — provides developers with a powerful mechanism to notify external systems and users about significant state changes or actions within their smart contracts.

In Solidity, users may rely on the decentralized subgraphs to store blockchain events. On Aptos, the Aptos Indexer is used to register emitted events. Up until August 2023, event emission on Aptos was a true pain with lots of repetitive code. However, now it’s much more similar to what you will know from EVM blockchains.

In this comprehensive guide, we delve deep into the intricacies of event emission on the Aptos blockchain. From exploring the syntax and mechanics of emitting events in Aptos Move, to devising rigorous testing approaches to validate event emission.

This article serves as a valuable resource for both novice and seasoned blockchain developers. Please tell us in the comments below, whether an Aptos Indexer tutorial would be helpful too?!

How to emit an event

Defining the event

First of all, one has to define the event type somewhere, usually at the top of the module, before the functions and entry points. If you’re unsure where to put it, please refer to our previous article on the overall layout of an Aptos Move module. If you’re unsure about the Aptos struct abilities chosen for the DepositEvent struct, please refer to our previous article on Aptos Move Structs.

#[event]
/// @notice Emitted when user deposits to the protocol
/// @param Address of the user
/// @param Amount to be deposited
struct DepositEvent has drop, store {
user: address,
amount: u64,
}

Emitting the event

Now that the DepositEvent type has been defined as part of the module, we can emit this type of event within our transactions. For instance, when a user calls deposit(), we want to emit an event to track when someone made a deposit and the amounts that were deposited to our protocol. Please note that the types need to match our previous definition. Thus, the user’s &signer type must be converted to an address type for this to work.

Thus, first we fill an event struct instance with the user’s deposit details, that we would like to emit an event about. Then, we call the event::emit<DepositEvent>(event) to actually emit the event we created.

public entry fun deposit(user: &signer, amount: u64) {

// handle transfer, e.g. transfer tokens, etc.


// create an event about it.
let my_event = DepositEvent {
user: signer::address_of(user);,
amount: amount,
};

// emit the event
event::emit<DepositEvent>(my_event);
}
Photo by Ferenc Almasi on Unsplash

How to test an event was emitted

In order to actually know whether our event emission works, we will have to test it somehow. We recommend performing testing in Aptos Move, using a separate testing module. However, as part of our core module, we will have to add a test_only function to be able to return the event as part of tests.

This function should look something like this:

    #[view]
#[test_only]
public fun test_DepositEvent(user: address, amount: u64): DepositEvent {
let event = DepositEvent{
user,
amount,
};
return event
}

Now, we can call this test_DepositEvent function within our test module and compare whether the event emitted as part of calling deposit() actually matches the expected test_DepositEvent() return type. This can be done as follows:

// check event emits expected info
let expected_event = staker::test_DepositEvent(signer::address_of(user), amount);
assert!(event::was_event_emitted(&expected_event), ERROR_UNEXPECTED_EVENT);

Thank you for spending time reading this article.

If this tutorial helped you somehow, please leave a show of hands 👏 👏 👏 or follow for more Aptos resources. 🕺

--

--

Moncayo Labs
Coinmonks

Active Supporter of the Aptos Move Movement | Web3 Move Development Tutorials