Skip to main content

Your First Move Module

This tutorial details how to compile, test, publish and interact with Move modules on the Aptos blockchain. The steps in summary are:

  1. Install the precompiled binary for the Aptos CLI.
  2. Create an account on the Aptos blockchain and fund it.
  3. Compile and test a Move module.
  4. Publish a Move module to the Aptos blockchain.
  5. Interact with a Move module.

Step 1: Install the CLI​

Install the precompiled binary for the Aptos CLI.


Step 2: Create an account and fund it​

After installing the CLI binary, create and fund an account on the Aptos blockchain.

Start a new terminal and run the following command to initialize a new local account:

aptos init

You will see output asking to choose a network:

Choose network from [devnet, testnet, mainnet, local, custom | defaults to devnet]

Press return to accept the default devnet network or specify the network of your choosing:

No network given, using devnet...

See and respond to the prompt for your private key by accepting the default to create a new or by entering an existing key:

Enter your private key as a hex literal (0x...) [Current: None | No input: Generate new key (or keep one if present)]

Assuming you elected to create a new, you will see:

No key given, generating key...
Account a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a doesn't exist, creating it and funding it with 100000000 Octas
Account a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a funded successfully

---
Aptos CLI is now set up for account a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a as profile default! Run `aptos --help` for more information about commands
{
"Result": "Success"
}

The account address in the above output a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a is your new account and is aliased as the profile default. This account address will be different for you as it is generated randomly. From now on, either default or 0xa345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a are used interchangeably in this document. Of course, substitute your own address as needed.

Now fund this account by running this command:

aptos account fund-with-faucet --account default

You will see output resembling:

{
"Result": "Added 100000000 Octas to account a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a"
}

Step 3: Compile and test the module​

Several example Move modules are available in the aptos-core/aptos-move/move-examples directory for your use. Open a terminal and change directories into the hello_blockchain directory:

cd aptos-core/aptos-move/move-examples/hello_blockchain

Run the below command to compile the hello_blockchain module:

aptos move compile --named-addresses hello_blockchain=default

You will see output resembling:

{
"Result": [
"a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a::message"
]
}

The compile command must contain --named-addresses as above because the Move.toml file leaves this as undefined (see below).

To test the module run:

aptos move test --named-addresses hello_blockchain=default

And receive output like:

INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING Examples
Running Move unit tests
[ PASS ] 0x1a42874787568af30c785622899a27dacce066d671fa487e7fb958d6d0c85077::message::sender_can_set_message
[ PASS ] 0x1a42874787568af30c785622899a27dacce066d671fa487e7fb958d6d0c85077::message_tests::sender_can_set_message
Test result: OK. Total tests: 2; passed: 2; failed: 0
{
"Result": "Success"
}

To prepare the module for the account created in the previous step, we specify that the named address hello_blockchain is set to our account address, using the default profile alias.

[addresses]
hello_blockchain = "_"

Step 4: Publish the Move module​

After the code is compiled and tested, we can publish the module to the account created for this tutorial with the command:

aptos move publish --named-addresses hello_blockchain=default

You will see the output similar to:

package size 1631 bytes
{
"Result": {
"transaction_hash": "0x45d682997beab297a9a39237c588d31da1cd2c950c5ab498e37984e367b0fc25",
"gas_used": 13,
"gas_unit_price": 1,
"pending": null,
"sender": "a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a",
"sequence_number": 8,
"success": true,
"timestamp_us": 1661320216343795,
"version": 3977,
"vm_status": "Executed successfully"
}
}

At this point, the module is now stored on the account in the Aptos blockchain.


Step 5: Interact with the Move module​

Move modules expose access points, known as entry functions. These entry functions can be called via transactions. The Aptos CLI allows for seamless access to these entry functions. The example Move module hello_blockchain exposes a set_message entry function that takes in a string. This can be called via the CLI:

aptos move run \
--function-id 'default::message::set_message' \
--args 'string:hello, blockchain'

Upon success, the CLI will print out the following:

{
"Result": {
"transaction_hash": "0x1fe06f61c49777086497b199f3d4acbee9ea58976d37fdc06d1ea48a511a9e82",
"gas_used": 1,
"gas_unit_price": 1,
"pending": null,
"sender": "a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a",
"sequence_number": 1,
"success": true,
"timestamp_us": 1661320878825763,
"version": 5936,
"vm_status": "Executed successfully"
}
}

The set_message function modifies the hello_blockchain MessageHolder resource. A resource is a data structure that is stored in global storage. The resource can be read by querying the following REST API:


https://api.devnet.aptoslabs.com/v1/accounts/a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a/resource/0xa345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a::message::MessageHolder

After the first execution, this should contain:

{
"type": "0xa345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a::message::MessageHolder",
"data": {
"message": "hello, blockchain"
}
}

Notice that the message field contains hello, blockchain.

Each succesful call to set_message after the first call results in the MessageChange event being emitted. MessageChange is a Module Event. Module Events for a given account can be accessed via the GraphQL API.

tip

Other accounts can reuse the published module by calling the exact same function as in this example. It is left as an exercise to the reader.

Supporting documentation​