Skip to content

ArhonJay/smc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Aptos Move Project - First Deploy

This project demonstrates a basic Aptos Move smart contract deployment with nickname storage functionality.

Prerequisites

  • Install Aptos CLI
  • Have an Aptos account with sufficient APT tokens for gas fees

Project Setup

1. Initialize Aptos Configuration

aptos init

This command sets up your Aptos configuration and creates a new account if needed.

2. Initialize Move Project

aptos move init

This creates the basic Move project structure with:

  • Move.toml - Project configuration file
  • sources/ - Directory for Move source files
  • scripts/ - Directory for Move scripts

Account Management

Check Account Balance

aptos account balance --account <account_address>

Replace <account_address> with your actual account address to check APT balance.

Fund Your Account

Visit the Aptos Faucet to get test tokens:

Smart Contract Code

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
    }
}

Contract Explanation

Module Declaration

  • module hello_blockchain::first_deploy - Defines the module name and namespace

Struct Definition

  • Nickname - A resource struct that stores a string name
  • has key - Ability that allows the struct to be stored in global storage

Functions

  1. init(account: &signer)

    • Initializes a nickname for the account
    • Creates a Nickname resource with the value "Tan"
    • Moves the resource to the account's global storage
  2. get_nickname(account: &signer): string::String

    • Retrieves the nickname from the account's storage
    • Uses borrow_global to access the stored Nickname resource
    • Returns the name as a string

Deployment Commands

1. Compile the Contract

aptos move compile

This compiles your Move code and checks for syntax errors.

2. Publish the Contract

aptos move publish --profile default

This deploys your compiled contract to the Aptos blockchain.

Project Structure

your-project/
├── Move.toml                 # Project configuration
├── sources/
│   └── first_deploy.move    # Main contract file
└── README.md               # This file

Move.toml Configuration

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" }

Usage Flow

  1. Setup Environment

    aptos init
    aptos move init
  2. Fund Account

    • Visit faucet and get test tokens
    • Verify balance: aptos account balance --account <your_address>
  3. Deploy Contract

    aptos move compile
    aptos move publish --profile default
  4. Interact with Contract

    • The init function will be called automatically during deployment
    • Use get_nickname to retrieve the stored nickname

Key Move Concepts Demonstrated

  • Resources: The Nickname struct is a resource that lives in global storage
  • Global Storage: Using move_to and borrow_global to interact with account storage
  • Signer: Authentication mechanism ensuring only the account owner can perform operations
  • String Handling: Using the standard library's string utilities

Troubleshooting

  • 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 --profile matches the one configured in aptos init

Next Steps

  • Explore more complex Move patterns
  • Add more functions to interact with the Nickname resource
  • Learn about Move's other abilities (copy, drop, store)
  • Experiment with more advanced data structures

Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages