在进行 Aptos 开发前,第一步是搭建本地开发环境,并验证工具链是否配置正确。最直接的方式,是通过编写并运行一个简单的 Move 程序,实现链上版本的 “Hello World”。本教程将以最小化的步骤,引导你完成 Aptos 开发环境的初始化与基础模块的部署,确保你具备开始构建更复杂应用的基础能力。
首先,在开始这篇文章之前你需要搭建好 aptos-cli, 可以参考 https://aptos.dev/en/build/cli
我们使用的 mac 系统,直接使用 brew 安装,
brew update
brew install aptos
当然如果你可以下载下来 git clone https://github.com/aptos-labs/aptos-core.git 然后自己编译安装,参考教程>>
Hello World 工程
创建目录,编写合约文件 你可以参考我的目录结构,然后创建相应的文件夹和文件。
mkdir HelloWorldcd HelloWorld➜ HelloWorld aptos initConfiguring for profile defaultChoose network from [devnet, testnet, mainnet, local, custom | defaults to devnet]testnetEnter 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 0xb90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4 has been already found on chain---Aptos CLI is now set up for account 0xb90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4 as profile default!---See the account here: https://explorer.aptoslabs.com/account/0xb90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4?network=testnet{ "Result": "Success"}创建代码目录 sources 和代码
APTOS 在 2023 年 -2025 年有很大的变动,当前可行的一个发送 message 的写法如下
module HelloWorld::hello_world { use std::string; use std::error; use std::option; use std::signer; /// define a resource struct Message has key { content: string::String, } /// initialize the resource (only allow one call per account) public entry fun initialize(account: &signer, initial_message: string::String) { assert!( !exists<Message>(signer::address_of(account)), error::already_exists(0) ); move_to(account, Message { content: initial_message }); } /// update the message public entry fun update(account: &signer, new_message: string::String) acquires Message { let addr = signer::address_of(account); let msg = borrow_global_mut<Message>(addr); msg.content = new_message; } /// read the message (can be used for view functions) public fun get_message(addr: address): option::Option<string::String> acquires Message{ if (exists<Message>(addr)) { let msg = borrow_global<Message>(addr); option::some(msg.content) } else { option::none() } }}创建你的 Move.toml 文件
[package]name = "hello-world"version = "0.0.1"[dependencies]AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "devnet" }[addresses]HelloWorld = "b90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4"其中 b90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4 是我在第一步 aptos init 的时候创建的地址公钥,你换成自己的就行
编译, 注意这一步需要保证 github 的畅通,它会自己去寻找依赖
➜ HelloWorld aptos move compileCompiling, may take a little while to download git dependencies...UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.gitINCLUDING DEPENDENCY AptosFrameworkINCLUDING DEPENDENCY AptosStdlibINCLUDING DEPENDENCY MoveStdlibBUILDING hello-world{ "Result": [ "b90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4::hello_world" ]}这就是成功了
发布
➜ HelloWorld aptos move publishCompiling, may take a little while to download git dependencies...UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.gitINCLUDING DEPENDENCY AptosFrameworkINCLUDING DEPENDENCY AptosStdlibINCLUDING DEPENDENCY MoveStdlibBUILDING hello-worldpackage size 1496 bytes{ "Error": "Simulation failed with status: MAX_GAS_UNITS_BELOW_MIN_TRANSACTION_GAS_UNITS"}这个错是因为没钱,来点测试的钱吧
aptos account fund-with-faucet{ "Error": "Invalid arguments: To get testnet APT you must visit https://aptos.dev/network/faucet. If you are confident you want to use a faucet programmatically, set --faucet-url or add a faucet URL to .aptos/config.yaml for the current profile"}devnet 是可以直接获取到的,testnet 访问 https://aptos.dev/network/faucet 登录后
➜ HelloWorld aptos account balance{ "Result": [ { "asset_type": "coin", "coin_type": "0x1::aptos_coin::AptosCoin", "balance": 100000000 } ]}查询下余额
➜ HelloWorld aptos account balance{ "Result": [ { "asset_type": "coin", "coin_type": "0x1::aptos_coin::AptosCoin", "balance": 100000000 } ]}不再为 0,再发布就 ok 了
➜ HelloWorld aptos move publish Compiling, may take a little while to download git dependencies...UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.gitINCLUDING DEPENDENCY AptosFrameworkINCLUDING DEPENDENCY AptosStdlibINCLUDING DEPENDENCY MoveStdlibBUILDING hello-worldpackage size 1496 bytesDo you want to submit a transaction for a range of [200800 - 301200] Octas at a gas unit price of 100 Octas? [yes/no] >yesTransaction submitted: https://explorer.aptoslabs.com/txn/0x9f59e24d2e621413dd9eca74fafebe02992663347a82fe6952b85c657317ef23?network=testnet{ "Result": { "transaction_hash": "0x9f59e24d2e621413dd9eca74fafebe02992663347a82fe6952b85c657317ef23", "gas_used": 2008, "gas_unit_price": 100, "sender": "b90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4", "sequence_number": 0, "success": true, "timestamp_us": 1747688688251830, "version": 6730369949, "vm_status": "Executed successfully" }}大功告成。
当然如果你觉得自己部署合约太麻烦,也可以使用 CPBOX 一键发币。CPBOX 支持多个主网发币
如果你对 APTOS 有兴趣,欢迎你可以来 cpbox 社区讨论
【免责声明】市场有风险,投资需谨慎。本文不构成投资建议,用户应考虑本文中的任何意见、观点或结论是否符合其特定状况。据此投资,责任自负。
