Aptos CLI Usage Guide and REPL Design Suggestions | Learning Move 0x04

NonceGeekDAO
6 min readAug 7, 2023

--

On the one hand, this article is Aptos’s CLI tool operation guide. on the other hand, it will extend to talk about some of the author’s views on the design of CLI/REPL tools.

0x01 Aptos CLI Tool Operation Guide

Source of information for this article:

https://aptos.dev/tools/aptos-cli/use-cli/use-aptos-cli/#command-line-help

1.1 In general the features CLI will include

No matter which chain it is, the CLI tool abstractly includes the following functions:

  • start a local test chain
  • connect to a chain node
  • Get chain information
  • Account Management
  • Receive test coins (Faucet)
  • transfer
  • deploy contract
  • Contract (check the status of the module)
  • Contract interaction
  • Resource viewing (Only for Move-based Chain)
  • Chain Governance (Optional)

1.2 Install Aptos CLI

See:

https://aptos.dev/cli-tools/aptos-cli-tool/install-aptos-cli

Generally speaking, it is recommended to use the precompiled tool (precompiled binary) directly, which will save errors that may be encountered during compilation.

1.3 Initialize Aptos Account

A local folder named .aptoswill be created with the configuration config.yaml which can be used to store configuration during CLI runs. This is local to your runtime, so you’ll need to keep running the CLI from that folder, or reinitialize in another folder.

If everything is initialized with the default values, the aptos CLI will connect to the testnet:

$ ./aptos init
Configuring for profile default
Enter your rest endpoint [Current: None | No input: https://fullnode.devnet.aptoslabs.com]
No rest url given, using https://fullnode.devnet.aptoslabs.com...  
Enter your faucet endpoint [Current: None | No input: https://faucet.devnet.aptoslabs.com]
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 50A49D913AA6381C01579E3FC00784B49AFA3A771F06389EBC65F8FF3A4E9A7D doesn't exist, creating it and funding it with 10000 coins
Aptos is now set up for account 50A49D913AA6381C01579E3FC00784B49AFA3A771F06389EBC65F8FF3A4E9A7D! Run `aptos help` for more information about commands
{
"Result": "Success"
}

1.4 Start a local test network and configure CLI

Start the local test chain:

$ ./aptos node run-local-testnet --with-faucet
Building genesis with 1 validators. Directory of output: "/Users/liaohua/aptos/.aptos/testnet"
Completed generating configuration:
Log file: "/Users/liaohua/aptos/.aptos/testnet/validator.log"
Test dir: "/Users/liaohua/aptos/.aptos/testnet"
Aptos root key path: "/Users/liaohua/aptos/.aptos/testnet/mint.key"
Waypoint: 0:81bffa64e06416fe9978f1e91d9f58e222836d303a3984dbd470d2c821a743b2
ChainId: testing
REST API endpoint: http://0.0.0.0:8080
Metrics endpoint: http://0.0.0.0:9101/metrics
FullNode network: /ip4/0.0.0.0/tcp/6181
Aptos is running, press ctrl-c to exitFaucet is running. Faucet endpoint: 0.0.0.0:8081

Create a new account configuration for the CLI. Note that the window of the above command should not be closed.

$ ./aptos init --profile local --rest-url http://localhost:8080 --faucet-url http://localhost:8081
Configuring for profile local
Using command line argument for rest URL http://localhost:8080/
Using command line argument for faucet URL http://localhost:8081/
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 4a327db3bce440f47d65b293a9688a7fd59e69a3cc1ddf0b2889a3e4f6d4de62 doesn't exist, creating it and funding it with 10000 coins
Aptos is now set up for account 4a327db3bce440f47d65b293a9688a7fd59e69a3cc1ddf0b2889a3e4f6d4de62! Run `aptos help` for more information about commands
{
"Result": "Success"
}

Reset of the local test chain:

$ ./aptos node run-local-testnet --with-faucet --force-restart

1.5 Account Management

# Take the test coin
# The faucet would like to get automatically when a acct created on devnet or testnet.
$ ./aptos account fund-with-faucet --profile $PROFILE --account $PROFILE
{
"Result": "Added 10000 coins to account 4a327db3bce440f47d65b293a9688a7fd59e69a3cc1ddf0b2889a3e4f6d4de62"
}
# create resource account
$ ./aptos account create-resource-account --profile $PROFILE --seed 1

The ownership of Resource Account is controlled by a resource, which can be stored in the account. Through Resource Account, we can achieve a function similar to the contract account in Solidity.

1.6 Resource view

$ ./aptos account list --query resources --account default --profile $PROFILE # or just "account list"
{
"Result": [
{
"0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>": {
"coin": {
"value": "10000"
},
...
]}

1.7 Module view

Different items under an account can be viewed through different types of queries. Currently, “Resource” and “Module” are supported, but more query types are coming soon. For example, to get a module:

$ ./aptos account list --query modules --profile $PROFILE

1.8 Transfer operation

Let’s create a new user account first, still linking to the local testnet:

./aptos init --profile bob --rest-url http://localhost:8080 --faucet-url http://localhost:8081

Then you can transfer money between different Profiles:

./aptos account transfer --account bob --amount 100 --profile $PROFILE

Or transfer the coins to any account:

aptos account transfer --account 0x2df41622c0c1baabaa73b2c24360d205e23e803959ebbcb0e5b80462165893ed --amount 100 --profile testnet9

1.9 Contract Compilation

Take hello_blockchain as an example.

https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/move-examples/hello_blockchain

./aptos move compile --package-dir [path-to-example]/hello_blockchain --named-addresses hello_blockchain=$PROFILE --profile $PROFILE

The compiled files can be viewed in the hello blockchain folder.

1.10 Contract Deployment

Note:

You may need to delete the build folder under this folder, otherwise an error will be reported.

./aptos move publish --package-dir [path-to-example]/move-examples/hello_blockchain --named-addresses hello_blockchain=local --profile $PROFILE
package size 1601 bytes
{
"Result": {
"transaction_hash": "0xe9468512b4aa83be6f0ab1fc49bfc329b2f99fb9db76ec015d90cacdd0649b57",
"gas_used": 182,
"gas_unit_price": 1,
"sender": "4a327db3bce440f47d65b293a9688a7fd59e69a3cc1ddf0b2889a3e4f6d4de62",
"sequence_number": 4,
"success": true,
"timestamp_us": 1662198442260668,
"version": 62620,
"vm_status": "Executed successfully"
}
}

1.11 Contract Call

./aptos move run --function-id 4a327db3bce440f47d65b293a9688a7fd59e69a3cc1ddf0b2889a3e4f6d4de62::message::set_message --args string:Hello! --profile $PROFILE
{
"Result": {
"transaction_hash": "0x9cf6782132a17f4c04047bc4823e26b79811ed94bf524f49c62ca47c25a43028",
"gas_used": 39,
"gas_unit_price": 1,
"sender": "4a327db3bce440f47d65b293a9688a7fd59e69a3cc1ddf0b2889a3e4f6d4de62",
"sequence_number": 5,
"success": true,
"timestamp_us": 1662198697122879,
"version": 66122,
"vm_status": "Executed successfully"
}
}

1.13 Generate Keys

aptos key generate --key-type ed25519 --output-file output.key

0x02 REPL Design Suggestion

💡What is a REPL?

REPL is what we often call the console, sometimes called the CLI. The REPL is used a lot in our development work:

  • Bash
  • Python REPL: includes the original python repl and ipython
  • iex: REPL for Elixir
  • Blockchain REPL: can be said to be the standard configuration of the blockchain, such as FISCO BCOS console and Aptos CLI.

A pleasing and idealistic REPL should have the following qualities (personal subjective opinion):

2.1 Rich colors

Differentiating different statements by different colors is a good implementation.

2.2 Support historical query

Divided into “weak support” and “strong support”. If it is weakly supported, it is easy to view historical commands through ↑↓. If it is strongly supported, you can limit the scope of the query by entering a part. For example, after entering print, press ↑↓ to query all the historical commands of print* that have been input.

2.3 Support command completion

The ideal situation is to be able to automatically prompt through the tab key. However, if command completion is not yet supported, --help is also available.

2.4 Support command help

The convention is to add --help after the command to view the command help. On this one, support for Markdown syntax is is a bonus item for REPL!

--

--

NonceGeekDAO
NonceGeekDAO

Written by NonceGeekDAO

Technical Articles of NonceGeekDAO.

No responses yet