从模板开始创建 Move 包
按照以下步骤快速开始.
-
初始化
运行以下命令使用
hello-blockchain
模板初始化包:Terminal window aptos move init --name hello_blockchain --template hello-blockchain -
开始构建
模板在
sources
下创建了一个hello_blockchain.move
文件来帮助你开始.hello_blockchain.move module hello_blockchain::message {use std::error;use std::signer;use std::string;use aptos_framework::event;#[test_only]use std::debug;//:!:>resourcestruct MessageHolder has key {message: string::String,}//<:!:resource#[event]struct MessageChange has drop, store {account: address,from_message: string::String,to_message: string::String,}/// There is no message presentconst ENO_MESSAGE: u64 = 0;#[view]public fun get_message(addr: address): string::String acquires MessageHolder {assert!(exists<MessageHolder>(addr), error::not_found(ENO_MESSAGE));borrow_global<MessageHolder>(addr).message}public entry fun set_message(account: signer, message: string::String)acquires MessageHolder {let account_addr = signer::address_of(&account);if (!exists<MessageHolder>(account_addr)) {move_to(&account, MessageHolder {message,})} else {let old_message_holder = borrow_global_mut<MessageHolder>(account_addr);let from_message = old_message_holder.message;event::emit(MessageChange {account: account_addr,from_message,to_message: copy message,});old_message_holder.message = message;}}#[test(account = @0x1)]public entry fun sender_can_set_message(account: signer) acquires MessageHolder {let msg: string::String = string::utf8(b"Running test for sender_can_set_message...");debug::print(&msg);let addr = signer::address_of(&account);aptos_framework::account::create_account_for_test(addr);set_message(account, string::utf8(b"Hello, Blockchain"));assert!(get_message(addr) == string::utf8(b"Hello, Blockchain"),ENO_MESSAGE);}} -
查看所有模板
运行以下命令查看所有模板(并获取初始化包的一般帮助):
Terminal window aptos move init --help