Aptos Module

πŸš€ Your First Aptos Module on M1 using Movement CLI

Install the Movement CLI

bash <(curl -fsSL https://raw.githubusercontent.com/movemntdev/M1/main/scripts/install.sh) --latest

Initialize your environment

Initialize your movement profile for your package development.

movement aptos move init --name hello_blockchain

If you generated your project using the movemntdev/movement-hack template, you should have a project with the following structure.

/ sources

hello_blockchain.move contains a simple module for setting the value of a message resource. Else, you can copy the following into a new file inside sources directory called hello_blockchain.move.

module hello_blockchain::message {
    use std::error;
    use std::signer;
    use std::string;
    use aptos_framework::account;
    use aptos_framework::event;

    struct MessageHolder has key {
        message: string::String,
        message_change_events: event::EventHandle<MessageChangeEvent>,
    struct MessageChangeEvent has drop, store {
        from_message: string::String,
        to_message: string::String,

    /// There is no message present
    const ENO_MESSAGE: u64 = 0;

    public fun get_message(addr: address): string::String acquires MessageHolder {
        assert!(exists<MessageHolder>(addr), error::not_found(ENO_MESSAGE));

    public entry fun set_message(account: signer, message: string::String)
    acquires MessageHolder {
        let account_addr = signer::address_of(&account);
        if (!exists<MessageHolder>(account_addr)) {
            move_to(&account, MessageHolder {
                message_change_events: account::new_event_handle<MessageChangeEvent>(&account),
        } else {
            let old_message_holder = borrow_global_mut<MessageHolder>(account_addr);
            let from_message = old_message_holder.message;
            event::emit_event(&mut old_message_holder.message_change_events, MessageChangeEvent {
                to_message: copy message,
            old_message_holder.message = message;

    #[test(account = @0x1)]
    public entry fun sender_can_set_message(account: signer) acquires MessageHolder {
        let addr = signer::address_of(&account);
        set_message(account,  string::utf8(b"Hello, Blockchain"));

          get_message(addr) == string::utf8(b"Hello, Blockchain"),

You will also need to add the following to Move.toml.

name = "{{ crate_name }}"
version = "0.0.0"

AptosFramework = {git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "main"}

# hello_blockchain = "_"
std = "0x1"

Now you need to setup your config.yaml file. This file is used to configure the movement CLI and will setup a private key for your environment. Choose testnet during setup!

movement aptos init

Run the tests with the movement CLI.

movement aptos move test --named-addresses hello_blockchain=default

If your test has passed, you can now publish your module to the testnet.

movement aptos move publish --named-addresses hello_blockchain=default

Make sure to accept the transaction in the prompt. You can now check the status of your transaction using the movement CLI.

Afterwards, you can run the module on the testnet. You are going to set the message to "hello!".

movement aptos move run --function-id default::message::set_message --args string:hello!

Make sure to check the resources of the account you used to publish the module. You should see a MessageHolder resource.

movement aptos account list --query resources --account default

You can now check the message of the account.

movement aptos move run --function-id default::message::get_message --args address:default

Congratulations! You have just written, published, and executed your first Move module on the testnet!

As your workflows become more complex, however, you may also wish to consider our JavaScript client.

Last updated