Aptos

2025年09月09日更新 4 人订阅
原价: ¥ 2.2 限时优惠
专栏简介 Aptos 开发实战:从环境搭建到第一个 Hello World Aptos 开发指南:在 JetBrains 编辑器中配置运行、编译、测试与发布部署,实现更高效开发 Aptos 区块链智能合约入门:使用 Move 实现消息存储与检索 Aptos Move 语言中的变量管理与内存所有权机制详解 Aptos Move 编程语言中的四大基础类型解析:UINT、STRING、BOOL 与 ADDRESS 深入解读 APTOS-MOVE 中的 Vector 向量核心特性与操作 深入理解APTOS-MOVE中的函数修饰符:核心概念与应用 深入解读 Aptos Move 的 Struct 特性与四大能力 Aptos Move 控制流解析:IF、WHILE与LOOP的深入解读 Aptos Move 模块的特性与实操指南:模块引用与作用域管理 Aptos Move 模块的发布与交互:完整指南 深入理解 Aptos Move 中的 Object 创建与管理 深入探索 Aptos Move:Object 配置与实操指南 使用 Aptos Move 实现随机数生成:从 AIP-41 到实战操作 Aptos Move 实践指南:构建并部署同质化代币水龙头 (FA Faucet) Aptos Move NFT 项目实操指南:从开发到部署全流程解析 Aptos Move 开发入门:从环境搭建到合约部署全流程实录 Aptos Move 入门:从零到一的合约开发与测试实战 Move 语言核心:布尔逻辑与地址类型的实战精解 深入 Aptos Move:从public到friend,函数可见性详解 Aptos Move 编程:for、while 与 loop 循环的实战详解 Aptos Move 安全编程:abort 与 assert! 错误处理实战 Aptos Move 实战:基础运算与比较逻辑的实现与测试 Aptos Move 性能优化:位运算与移位操作实战 Aptos Move 实战:as 关键字与整数类型转换技巧 Aptos Move DeFi 实战:从零构建流动性池兑换逻辑 Aptos Move 实战:用 signer 实现合约所有权与访问控制 Aptos Move 核心安全:& 与 &mut 引用机制详解 Aptos Move 实战:全面掌握 SimpleMap 的增删改查 Aptos Move 入门:掌握链上资源(Resource)的增删改查 Aptos Move 深度实践:用嵌套数据结构构建链上金银储备系统 Aptos Move 实操:如何用 Tables 构建一个链上房产管理系统

Aptos Move 模块的发布与交互:完整指南

AptosMove模块的发布与交互:完整指南随着区块链技术的飞速发展,AptosMove作为一种创新的智能合约编程语言,受到了越来越多开发者的关注。Move语言因其对安全性和资源管理的独特处理方式,逐渐成为区块链开发中的新宠。本指南将带你详细了解如何在Aptos上发布和交互Move

Aptos Move 模块的发布与交互:完整指南

随着区块链技术的飞速发展,Aptos Move 作为一种创新的智能合约编程语言,受到了越来越多开发者的关注。Move 语言因其对安全性和资源管理的独特处理方式,逐渐成为区块链开发中的新宠。本指南将带你详细了解如何在 Aptos 上发布和交互 Move 模块,涵盖从初始化账户到调用智能合约的具体流程。


本文介绍了在 Aptos 区块链上如何使用 Move 语言编写、编译、发布和交互模块的全过程。首先,我们会指导你如何配置账户并为其注入 gas 费,随后展示如何编译和测试模块。接着,文章将说明如何通过 Aptos Explorer 浏览区块链状态,如何在终端中调用模块功能,以及如何通过 SDK 进行交互操作。最后,通过具体的代码示例,我们将带你深入体验完整的发布与交互流程。


模块的发布与交互流程

发布流程

  1. 配置账户,并为其发送 gas 可以通过 aptos init 来初始化一个账户。

  2. 编译并测试模块 可以通过以下命令来编译

    aptos move compile --named-addresses hello_blockchain=default
  3. 发布模块

    aptos move publish --named-addresses hello_blockchain=default

交互流程

  1. 区块链浏览器 https://explorer.aptoslabs.com/

  2. 终端

    aptos move run --function-id 'default::message::set_message' --args 'string:hello, blockchain'
  3. SDK

    详细参考 Aptos SDK 文档: aptos.dev

    https://aptos.dev/en/build/sdks

实操示例

1. 创建项目并初始化

使用以下命令创建新项目并初始化账户:

mkdir lesson8
cd lesson8
aptos move init --name lesson8
aptos init

实操创建项目并初始化

hello_aptos on  main [?] via 🅒 base
➜
mcd lesson8

hello_aptos/lesson8 on  main [?] via 🅒 base
➜
aptos move init --name lesson8
{
  "Result": "Success"
}

hello_aptos/lesson8 on  main [?] via 🅒 base
➜
aptos init
Configuring for profile default
Choose network from [devnet, testnet, mainnet, local, custom | defaults to devnet]

No network given, using devnet...
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 0xfc990a9602f8874456fbf765f433360fad098a0c5a910aaf8758c68611b898b8 doesn't exist, creating it and funding it with 100000000 Octas
Account 0xfc990a9602f8874456fbf765f433360fad098a0c5a910aaf8758c68611b898b8 funded successfully

---
Aptos CLI is now set up for account 0xfc990a9602f8874456fbf765f433360fad098a0c5a910aaf8758c68611b898b8 as profile default!
 See the account here: https://explorer.aptoslabs.com/account/0xfc990a9602f8874456fbf765f433360fad098a0c5a910aaf8758c68611b898b8?network=devnet
 Run `aptos --help` for more information about commands
{
  "Result": "Success"
}

hello_aptos/lesson8 on  main [?] via 🅒 base took 7.7s
➜
open -a RustRover .

2. 查看项目结构

tree . -L 6 -I 'build'

输出结构大致如下:

.
├── Move.toml
├── scripts
├── sources
│   ├── hello_blockchain.move
│   └── hello_blockchain_test.move
└── tests

实操

hello_aptos/lesson8 on  main [?] via 🅒 base took 5.7s 
➜ tree . -L 6 -I 'build'                                                                        

.
├── Move.toml
├── scripts
├── sources
│   ├── hello_blockchain.move
│   └── hello_blockchain_test.move
└── tests

4 directories, 3 files

3. 模块编写:hello_blockchain.move

该模块实现了一个简单的消息存储和获取功能。使用 MessageHolder 资源来保存消息,并通过 set_message 和 get_message 方法来设置和获取消息。

module hello_blockchain::message {
    use std::error;
    use std::signer;
    use std::string;
    use aptos_framework::event;
    #[test_only]
    use std::debug;

    //:!:>resource
    struct 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 present
    const 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
        );
    }
}

hello_blockchain_test.move

#[test_only]
module hello_blockchain::message_tests {
    use std::signer;
    use std::unit_test;
    use std::vector;
    use std::string;

    use hello_blockchain::message;

    fun get_account(): signer {
        vector::pop_back(&mut unit_test::create_signers_for_testing(1))
    }

    #[test]
    public entry fun sender_can_set_message() {
        let account = get_account();
        let addr = signer::address_of(&account);
        aptos_framework::account::create_account_for_test(addr);
        message::set_message(account,  string::utf8(b"Hello, Blockchain"));

        assert!(
            message::get_message(addr) == string::utf8(b"Hello, Blockchain"),
            0
        );
    }
}

Move.toml

[package]
name = "lesson8"
version = "1.0.0"
authors = []

[addresses]
hello_blockchain = "_"

[dev-addresses]

[dependencies.AptosFramework]
git = "https://github.com/aptos-labs/aptos-core.git"
rev = "mainnet"
subdir = "aptos-move/framework/aptos-framework"

[dev-dependencies]

https://github.com/aptos-labs/aptos-core/blob/main/...

剩余50%的内容订阅专栏后可查看

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论