Aptos: Publish a module to save & retrieve a message on-chain
TLDR
- Aptos uses the Move language and “published” modules in place of traditional smart contracts.
- This tutorial shows you how to set up an Aptos node, initialize a Move project, and publish a simple module that stores a string on-chain.
- You’ll run through the basics of module creation, compilation, testing, and then sending transactions to set and retrieve data.
- Use the Aptos CLI for everything from local testing to publishing, and wrap up by querying the on-chain data with Aptos’ REST API.
Main article
Aptos uses its own terminology for widely-known Web3 entities. Smart contracts are called Modules and are written in the Move language. Modules are also not deployed but published on the Aptos chain.
The objective of this tutorial is to familiarize you with the Aptos network, the Move language and modules written in it. In the end of this tutorial, you will be able to publish, test, and interact with Move modules in Aptos.
Specifically, in this tutorial, you will:
- Initialize an Aptos project using the Aptos CLI.
- Publish a module on the Aptos testnet.
- Interact with the module to save a message.
- Use the Aptos REST API to retrieve the message.
Prerequisites
- Chainstack account to deploy an Aptos node.
- Martian Aptos wallet to receive testnet Aptos token (APT).
- Aptos CLI to compile, publish, and interact with the Move module.
Overview
To get from zero to publishing your string via the module to Aptos testnet, do the following:
With Chainstack, create a .
With Chainstack, join Aptos testnet.
With Chainstack, access your Aptos node credentials.
Set up your Martian wallet to work through the Chainstack Aptos node.
Fund your account through the Aptos testnet faucet.
Install the Aptos CLI.
Create a Move project.
Create and configure your Aptos project.
Create a module in the Move language.
Compile and test the Move module.
Publish the Move module.
Save and retrieve a message on the Aptos chain.
Step-by-step
Create a public chain project
See Create a project.
Join the Aptos testnet
Get node access and credentials
See View node access and credentials.
Set up Martian wallet
See Aptos tooling: Martian wallet.
Fund your account
Your account needs to pay fees in testnet APT to publish the module and interact with it. Fund your account with the Aptos testnet faucet.
Install the Aptos CLI
You need the Aptos CLI to interact with your Move module. Set up the Aptos CLI.
Create a Move project
-
In your project directory, create a Move project:
where
save-message
— name of the package.This creates a
sources
directory and aMove.toml
file. -
Open your
Move.toml
file and edit it to add[addresses]
and[dev-addresses]
, where:dev = "_"
— your default Aptos account.dev = "0xC0FFEE"
— an alternative Aptos account for tests.
Example:
Note that packages have one-time names. If you want to re-publish the package, you must change its name.
Create and configure an Aptos project
-
In your project directory, run
aptos init > custom
. This will start a configuration process, during which you need to set up your Chainstack endpoint and Martian wallet private key. Adding the private key will retrieve your Aptos public address automatically. -
Add your Aptos node endpoint deployed with Chainstack.
-
At the faucet URL request, type
skip
since you have already funded your account on the previous step. -
Paste your Martian wallet private key to finish configuring your project. The key is used to send transactions and retrieve your public address. Example of a successful result:
As a result, you get a
.aptos
directory with aconfig.yaml
file inside it. Inconfig.yaml
, you will find your project setup.
Create a Move module
In your Move project directory, navigate to the sources
directory. Create your Move module file message.move
which allows you to call the set_message
function and save a string on-chain:
Compile and test the Move module
-
To compile your Move module, run:
-
After the module compiled, run a build-in test which checks if the
set_message
andget_message
functions work:
Publish the Move module
-
Publish your compiled and tested Move module by running:
-
Type
yes
to confirm publishing the transaction on the Aptos chain.The module will publish and the terminal will return the module information. You can use the transaction hash to retrieve transaction details. To do so, run:
where YOUR_CHAINSTACK_ENDPOINT is your Aptos node endpoint you used earlier.
Save and retrieve a message on the Aptos chain
-
To save a message on the Aptos chain, run:
where:
run
— a Move command to call functionsfunction-id
— a function to callargs
— arguments of the function
-
Type
yes
to confirm publishing the transaction on the Aptos chain. -
Retrieve the published message via the REST API by running:
where YOUR_CHAINSTACK_ENDPOINT is your Aptos node endpoint you used earlier.
Successful response example:
Conclusion
This tutorial guided you through the basics of creating, publishing, and testing a simple module that saves a string on the Aptos chain.