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:
Install the precombiled binary for the Aptos CLI.
Create an account on the Aptos blockchain and fund it.
Compile and test a Move module.
Publish a Move module to the Aptos blockchain.
Interact with a Move module.
Step 1: Install the CLI
Install the precombiled binary for the Aptos CLI.
Step 2: Create an account and fund it
After installing the CLI binary, next step is to create and fund an account on the Aptos blockchain.
Begin by starting a new terminal and run the below command to initialize a new local account:
aptos initThe output will be similar to below.
Enter your rest endpoint [Current: None | No input: https://fullnode.devnet.aptoslabs.com/v1]No rest url given, using https://fullnode.devnet.aptoslabs.com/v1...Enter your faucet endpoint [Current: None | No input: https://faucet.devnet.aptoslabs.com | 'skip' to not use a faucet]No faucet url given, using https://faucet.devnet.aptoslabs.com...Enter your private key as a hex literal (0x...) [Current: None | No input: Generate new key (or keep one if present)]No key given, generating key...Account a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a doesn't exist, creating it and funding it with 10000 coinsAptos is now set up for account a345dbfb0c94416589721360f207dcc92ecfe4f06d8ddc1c286f569d59721e5a! 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 interchangeable.
Now fund this account by running this command:
You will see an output similar to the below:
Step 3: Compile and test the module
Several example Move modules are available in the aptos-core/aptos-move/move-examples directory. Open a terminal and change directories into the hello_blockchain directory:
Run the below command to compile the hello_blockchain module:
To test the module run:
The CLI entry must contain --named-addresses because the Move.toml file leaves this as undefined (see below). 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.
Step 4: Publish the Move module
After the code was compiled and tested, we can publish the module to the account created for this tutorial. Run this below command:
You will see the output similar to the below:
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, also referred as entry functions. These access points can be called via transactions. The CLI allows for seamless access to these access points. The example Move module hello_blockchain exposes a set_message entry function that takes in a string. This can be called via the CLI:
Upon success, the CLI will print out the following:
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:
which, after the first execution contains the following:
Notice that the message field contains hello, blockchain.
Each successful call to set_message after the first call results in an update to message_change_events. The message_change_events for a given account can be accessed via the REST API:
where, after a call to set the message to hello, blockchain, again, the event stream would contain the following:
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.
Last updated