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 开发实战:从环境搭建到第一个 Hello World

Aptos开发实战:从环境搭建到第一个HelloWorld引言Aptos区块链以其高性能和独特的设计为开发者提供了强大的工具。本篇学习笔记将带你一步步完成Aptos开发环境的搭建,并通过编写第一个HelloWorld项目,帮助你快速掌握Aptos开发的基础流程。从安装Ap

Aptos 开发实战:从环境搭建到第一个 Hello World

引言

Aptos 区块链以其高性能和独特的设计为开发者提供了强大的工具。本篇学习笔记将带你一步步完成 Aptos 开发环境的搭建,并通过编写第一个 Hello World 项目,帮助你快速掌握 Aptos 开发的基础流程。从安装 Aptos CLI 到项目初始化与测试,本文为你提供了详细的实操指南,让你快速上手 Aptos 开发。

Aptos is a Layer-1 community-driven network, governed by the people who build on it from around the world. It’s decentralized. It’s fast. It’s blockchain at its best.

Aptos 区块链上的每个账户都由一个 32 字节的账户地址标识。 与其他区块链中账户和地址隐式不同,Aptos 上的账户是显式的,需要先创建才能执行交易。 可以通过将 Aptos 代币 (APT) 转移到 Aptos 来显式或隐式创建账户。

Aptos 上有三种类型的账户:

标准账户——这是一个典型的账户,对应一个地址和一对相应的公钥/私钥。 资源账户- 一个没有对应私钥的自主账户,供开发者存储资源或上链发布模块。 对象- 存储在代表单个实体的单个地址内的一组复杂资源。

帐户地址为 32 字节。它们通常显示为 64 个十六进制字符,每个十六进制字符为一个半字节。有时地址以 0x 为前缀。 Aptos区块链默认采用Ed25519签名交易。

Aptos 区块链存储三种类型的数据:

交易:交易表示区块链上的账户正在执行的预期操作(例如转移资产)。 状态:(区块链账本)状态代表交易执行输出的累积,即存储在所有资源内的价值。 事件:交易执行时发布的辅助数据。 只有交易才能改变账本状态。

Aptos 开发环境配置

image-20240907224538438.png

Install Aptos CLI

Install the Aptos CLI on Mac

brew update
brew install aptos

查看版本检查安装是否成功

aptos help

intensive-colearning-aptos on  main via 🅒 base took 4.3s 
➜ aptos --version              
aptos 4.1.0

更新 Aptos

brew update
brew upgrade aptos

更多请参考:

image.png

image.png

初始化账户

~/Code/Aptos via 🅒 base
➜
mcd hello_aptos

Code/Aptos/hello_aptos via 🅒 base
➜
aptos init
Configuring for profile default
Choose network from [devnet, testnet, mainnet, local, custom | defaults to devnet]
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 0xee6c038b66df7ed8aa91eb700938003ce29647f402c090ededd89b87a3c70e35 doesn't exist, creating it and funding it with 100000000 Octas
Account 0xee6c038b66df7ed8aa91eb700938003ce29647f402c090ededd89b87a3c70e35 funded successfully

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

image-20240907224928113.png

查看账户列表


Code/Aptos/hello_aptos via 🅒 base took 1m 32.7s
➜
aptos account list
{
  "Result": [
    {
      "0x1::account::Account": {
        "authentication_key": "0xee6c038b66df7ed8aa91eb700938003ce29647f402c090ededd89b87a3c70e35",
        "coin_register_events": {
          "counter": "0",
          "guid": {
            "id": {
              "addr": "0xee6c038b66df7ed8aa91eb700938003ce29647f402c090ededd89b87a3c70e35",
              "creation_num": "0"
            }
          }
        },
        "guid_creation_num": "2",
        "key_rotation_events": {
          "counter": "0",
          "guid": {
            "id": {
              "addr": "0xee6c038b66df7ed8aa91eb700938003ce29647f402c090ededd89b87a3c70e35",
              "creation_num": "1"
            }
          }
        },
        "rotation_capability_offer": {
          "for": {
            "vec": []
          }
        },
        "sequence_number": "0",
        "signer_capability_offer": {
          "for": {
            "vec": []
          }
        }
      }
    }
  ]
}

获取水龙头因为测试网会定期重置

Code/Aptos/hello_aptos via 🅒 base took 2.5s
➜
aptos account fund-with-faucet --account 0xee6c038b66df7ed8aa91eb700938003ce29647f402c090ededd89b87a3c70e35
{
  "Result": "Added 100000000 Octas to account 0xee6c038b66df7ed8aa91eb700938003ce29647f402c090ededd89b87a3c70e35"
}

实操 Hello World

创建项目

Code/Aptos/hello_aptos via 🅒 base
➜
mcd lesson1

初始化move项目

Aptos/hello_aptos/lesson1 via 🅒 base
➜
aptos move init --name lesson1
{
  "Result": "Success"
}

Aptos/hello_aptos/lesson1 via 🅒 base took 2.1s
➜
ls
Move.toml scripts   sources   tests

初始化账户

aptos init

查看项目结构

Aptos/hello_aptos/lesson1 via 🅒 base 
➜ tree . -L 6 -I 'build'                  

.
├── Move.toml
├── scripts
├── sources
│   └── main.move
└── tests

4 directories, 2 files

image-20240908001331633.png

Move.toml 代码

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

[addresses]
Lesson1 = "dfc044d199141627a77203495452bc5165c070edbcbf357f211096fbe974ddf7"
[dev-addresses]

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

[dev-dependencies]

main.move 代码

module Lesson1::HelloWorld {
    #[test_only]
    use std::debug::print;
    #[test_only]
    use std::string::utf8;

    #[test]
    fun test_hello_world() {
        print(&utf8(b"hello world"));
    }
}

测试 Test

Aptos/hello_aptos/lesson1 via 🅒 base took 25.0s 
➜ aptos move test
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING lesson1
Running Move unit tests
[debug] "hello world"
[ PASS    ] 0xdfc044d199141627a77203495452bc5165c070edbcbf357f211096fbe974ddf7::HelloWorld::test_hello_world
Test result: OK. Total tests: 1; passed: 1; failed: 0
{
  "Result": "Success"
}

image-20240908111316657.png

参考

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

0 条评论

请先 登录 后评论