# Aptos入门-何如发布合约 **Published by:** [CPBOX](https://paragraph.com/@cpbox/) **Published on:** 2025-05-22 **URL:** https://paragraph.com/@cpbox/aptos ## Content 前言在进行 Aptos 开发前,第一步是搭建本地开发环境,并验证工具链是否配置正确。最直接的方式,是通过编写并运行一个简单的 Move 程序,实现链上版本的 “Hello World”。本教程将以最小化的步骤,引导你完成 Aptos 开发环境的初始化与基础模块的部署,确保你具备开始构建更复杂应用的基础能力。关键词Apto环境搭建、Aptos-cli、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 HelloWorld cd HelloWorld ➜ HelloWorld aptos init Configuring for profile default Choose network from [devnet, testnet, mainnet, local, custom | defaults to devnet] testnet 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 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 和代码module HelloAptos::Message{ use std::string; use std::error; use std::debug; use aptos_std::event; use std::signer;structMessage has key{ msg:string::String, message_change_events: event::EventHandle<MessageEvent>, }structMessageEvent has drop, store { from_message: string::String, to_message: string::String, }const ENO_MESSAGE: u64 = 0;public entry fun say_message(account:&signer, message_bytes: vector<u8>) acquires Message { let message = string::utf8(message_bytes); let account_addr = signer::address_of(account);if (!exists<Message>(account_addr)) { debug::print(&account_addr);move_to(account, Message { msg:message, message_change_events: event::new_event_handle<MessageEvent>(account), }); } else {// debug::print(message); let old_message = borrow_global_mut<Message>(account_addr); let from_message = *&old_message.msg; event::emit_event(&mut old_message.message_change_events, MessageEvent { from_message, to_message: copy message, }); old_message.msg = message; } }public fun get_message(addr: address): string::String acquires Message { assert!(exists<Message>(addr), error::not_found(ENO_MESSAGE)); *&borrow_global<Message>(addr).msg } #[test(account = @0x1)]public entry fun sender_can_set_message(account: signer) acquires Message { let addr = signer::address_of(&account);say_message(&account, b"Hello, Aptos"); assert!(get_message(addr) == string::utf8(b"Hello, Aptos"), ENO_MESSAGE ); }} 这个已经过时了,APTOS 在2023年-2025年有很大的变动,这个sdk已经不再支持。当前可行的一个发送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 compile Compiling, may take a little while to download git dependencies... UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.git INCLUDING DEPENDENCY AptosFramework INCLUDING DEPENDENCY AptosStdlib INCLUDING DEPENDENCY MoveStdlib BUILDING hello-world { "Result": [ "b90343a98fc27691da66b9758887793932e89f3ed6d2fd22af5442e1b99271b4::hello_world" ] } 这就是成功了发布➜ HelloWorld aptos move publish Compiling, may take a little while to download git dependencies... UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.git INCLUDING DEPENDENCY AptosFramework INCLUDING DEPENDENCY AptosStdlib INCLUDING DEPENDENCY MoveStdlib BUILDING hello-world package 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.git INCLUDING DEPENDENCY AptosFramework INCLUDING DEPENDENCY AptosStdlib INCLUDING DEPENDENCY MoveStdlib BUILDING hello-world package size 1496 bytes Do you want to submit a transaction for a range of [200800 - 301200] Octas at a gas unit price of 100 Octas? [yes/no] > yes Transaction 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" } } 大功告成。结语如果你对APTOS 有兴趣,欢迎你可以来cpbox社区讨论,加入群组>> 同时CPBOX还有很多其他产品,如果你想了解cpbox产品的其他用途和功能 可以点击 https://docs.cpbox.io/ 如果在产品功能使用中遇到了问题或者你有一些好的建议或者想要帮助开发的需求 可以通过主页 https://www.cpbox.io/cn/ 最下方的联系方式来找到我们 ## Publication Information - [CPBOX](https://paragraph.com/@cpbox/): Publication homepage - [All Posts](https://paragraph.com/@cpbox/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@cpbox): Subscribe to updates