本文讲Aptos环境搭建以及运行一个Aptos版本的Hello World!
上手任何一门语言时,搭建好环境然后跑一个Hello World程序是一件很有成就感的事。那么这篇教程,就来讲讲Aptos环境搭建以及运行一个Aptos版本的Hello World!
Apto环境搭建、Aptos-cli、Hello World程序
首先,在开始这篇文章之前,你需要在WSL2上先搭建好Move开发环境。如果对Move开发环境搭建不了解。你可以参考我之前的文章进行搭建。
搭建Aptos开发环境,你需要安装Aptos开发脚手架,Aptos-cli。Aptos-cli的定义如下。
The aptos tool is a command line interface (CLI) for debugging, development, and node operations. This document describes how to install the aptos CLI tool. See Use Aptos CLI for how to use the CLI.
官方文档: https://aptos.dev/cli-tools/aptos-cli-tool/install-aptos-cli
安装Aptos-cli有两种方式,第一种使用源码自行编译,这种安装方式耗时很长,大约需要4~5h,这种方式我个人不推荐。我推荐使用官方已经编译好的安装包进行安装,省时省力。
~/bin/aptos
in Linux. #解压后把aptos复制到这个目录下chmod +x ~/bin/aptos
. #给文件赋权限~/bin/aptos help
to read help instructions.~/bin
to your path in your appropriate .bashrc
or .zshrc
for future use.安装完成之后,打开wsl控制台,输入aptos
查看是否安装完成。
区块链工程项目的创建步骤,通常来讲有以下几个动作。
2、创建目录,编写合约文件 你可以参考我的目录结构,然后创建相应的文件夹和文件。
HelloAptos.move
module HelloAptos::Message{
use std::string;
use std::error;
use std::debug;
use aptos_std::event;
use std::signer;
struct Message has key{
msg:string::String,
message_change_events: event::EventHandle<MessageEvent>,
}
struct MessageEvent 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
);
}
}
Move.toml
[package]
name = "hello-aptos"
version = "0.0.0"
[dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework/", rev = "devnet" }
[addresses]
HelloAptos = "fbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b"
其中fbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b 需要替换成你的账号信息。
4、编译合约
ouhuang@LAPTOP-HM465BTT:~/learn_aptos/contract$ sudo aptos move compile --package-dir /home/ouhuang/learn_aptos/contract/
[sudo] password for ouhuang:
{
"Result": [
"FBD4140A63E8FE7D6E9E25AFF78E11876B1B6C21BC8D34B51CBFFF40CA03164B::Message"
]
}
ouhuang@LAPTOP-HM465BTT:~/learn_aptos/contract$ sudo aptos move compile --package-dir .
{
"Result": [
"FBD4140A63E8FE7D6E9E25AFF78E11876B1B6C21BC8D34B51CBFFF40CA03164B::Message"
]
}
5、部署合约
ouhuang@LAPTOP-HM465BTT:~/learn_aptos/contract$ sudo aptos move publish --package-dir .
{
"Result": {
"changes": [
{
"address": "fbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b",
"data": {
"authentication_key": "0xfbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b",
"coin_register_events": {
"counter": "1",
"guid": {
"id": {
"addr": "0xfbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b",
"creation_num": "0"
}
}
},
"sequence_number": "1"
},
"event": "write_resource",
"resource": "0x1::account::Account"
},
{
"address": "fbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b",
"data": {
"coin": {
"value": "9998"
},
"deposit_events": {
"counter": "1",
"guid": {
"id": {
"addr": "0xfbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b",
"creation_num": "1"
}
}
},
"withdraw_events": {
"counter": "0",
"guid": {
"id": {
"addr": "0xfbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b",
"creation_num": "2"
}
}
}
},
"event": "write_resource",
"resource": "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>"
},
{
"address": "fbd4140a63e8fe7d6e9e25aff78e11876b1b6c21bc8d34b51cbfff40ca03164b",
"event": "write_module"
}
],
"gas_used": 2,
"success": true,
"version": 452397,
"vm_status": "Executed successfully"
}
}
至此,Hello World程序运行成功。我将在下一篇文章里,拆解HelloAptos.move中的合约代码。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!