Sui —— Hello, World!

1. 创建项目

1
2
# 创建新项目
sui move new <PROJECT_NAME>

项目结构

1
2
3
4
5
6
7
8
9
10
hello_world/
├── Move.lock # Move 包管理器自动生成的锁文件,记录依赖的确切版本
├── Move.toml # Move 项目配置文件,包含包名、依赖、地址映射等
├── build # 编译输出目录(自动生成)
│ ├── hello_world # 编译后的字节码和元数据
│ └── locks
├── sources # Move 智能合约源代码目录
│ └── hello_world.move # 主模块源文件
└── tests # 单元测试代码目录
└── hello_world_tests.move # 测试文件

2. VS Code 插件拓展

  • Move —— Move 语言支持与语法检查
  • Move Formatter —— 排版 Move 代码
  • Move Syntax —— Move 语法高亮
  • Even Better TOML —— TOML 高亮与校验

3. Hello, World!

3.1 修改 hello_world.move 文件

1
2
3
4
5
6
7
module hello_world::hello_world;

use std::string::String;

public fun hello_world(): String {
b"Hello, World!".to_string()
}

3.2 修改 hello_world_tests.move 文件

1
2
3
4
5
6
7
8
9
10
#[test_only]
module hello_world::hello_world_tests;

use hello_world::hello_world;
use std::unit_test::assert_eq;

#[test]
fun test_hello_world() {
assert_eq!(hello_world::hello_world(), b"Hello, World!".to_string());
}

3.3 编译、测试和部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 编译项目
sui move build

# 运行测试
sui move test

# 运行特定测试
sui move test --filter

# 部署合约
sui client publish

# 调用合约
sui client call \
--package <PACKAGE_ID> \
--module <MODULE_NAME> \
--function <FUNCTION_NAME> \
--gas-budget 10000000

# 调用合约(JSON 格式)
sui client call \
--package <PACKAGE_ID> \
--module hello_world \
--function hello_world \
--gas-budget 10000000
--json

区块浏览器