This project demonstrates a basic Aptos Move smart contract deployment with nickname storage functionality.
- Install Aptos CLI
- Have an Aptos account with sufficient APT tokens for gas fees
aptos initThis command sets up your Aptos configuration and creates a new account if needed.
aptos move initThis creates the basic Move project structure with:
Move.toml- Project configuration filesources/- Directory for Move source filesscripts/- Directory for Move scripts
aptos account balance --account <account_address>Replace <account_address> with your actual account address to check APT balance.
Visit the Aptos Faucet to get test tokens:
- Faucet URL: https://aptos.dev/en/network/faucet
- Select your network (testnet/devnet)
- Enter your account address
- Request tokens
The main contract is located in sources/first_deploy.move:
module hello_blockchain::first_deploy {
use std::string;
use std::signer;
struct Nickname has key {
name: string::String,
}
public fun init(account: &signer) {
let nickname = string::utf8(b"replace this");
move_to(account, Nickname { name: nickname });
}
public fun get_nickname(account: &signer): string::String acquires Nickname {
let nickname = borrow_global<Nickname>(signer::address_of(account));
nickname.name
}
}Module Declaration
module hello_blockchain::first_deploy- Defines the module name and namespace
Struct Definition
Nickname- A resource struct that stores a string namehas key- Ability that allows the struct to be stored in global storage
Functions
-
init(account: &signer)- Initializes a nickname for the account
- Creates a
Nicknameresource with the value "Tan" - Moves the resource to the account's global storage
-
get_nickname(account: &signer): string::String- Retrieves the nickname from the account's storage
- Uses
borrow_globalto access the storedNicknameresource - Returns the name as a string
aptos move compileThis compiles your Move code and checks for syntax errors.
aptos move publish --profile defaultThis deploys your compiled contract to the Aptos blockchain.
your-project/
├── Move.toml # Project configuration
├── sources/
│ └── first_deploy.move # Main contract file
└── README.md # This file
Your Move.toml should look like this:
[package]
name = "hello_blockchain"
version = "1.0.0"
[addresses]
hello_blockchain = "_"
[dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "mainnet" }-
Setup Environment
aptos init aptos move init
-
Fund Account
- Visit faucet and get test tokens
- Verify balance:
aptos account balance --account <your_address>
-
Deploy Contract
aptos move compile aptos move publish --profile default
-
Interact with Contract
- The
initfunction will be called automatically during deployment - Use
get_nicknameto retrieve the stored nickname
- The
- Resources: The
Nicknamestruct is a resource that lives in global storage - Global Storage: Using
move_toandborrow_globalto interact with account storage - Signer: Authentication mechanism ensuring only the account owner can perform operations
- String Handling: Using the standard library's string utilities
- Compilation Errors: Check your Move syntax and ensure all dependencies are properly declared
- Insufficient Gas: Make sure your account has enough APT tokens for transaction fees
- Profile Issues: Verify your
--profilematches the one configured inaptos init
- Explore more complex Move patterns
- Add more functions to interact with the
Nicknameresource - Learn about Move's other abilities (copy, drop, store)
- Experiment with more advanced data structures