通过创建第一个 Move 项目,了解 Sui Move 项目的基本结构、编译流程和部署方式。我们将编写一个简单的 Hello World 模块,并学习如何使用 Sui CLI 进行编译、测试和链上调用。
1. 创建项目
1 2
| sui move new <PROJECT_NAME>
|
项目结构
1 2 3 4 5 6 7 8 9 10
| hello_world/ ├── Move.lock ├── Move.toml ├── build │ ├── hello_world │ └── locks ├── sources │ └── 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 27 28 29
| sui move build
sui move test
sui move test --filter
RUST_LOG="off,sui_node=info" sui start --with-faucet --force-regenesis
sui client publish
sui client call \ --package <PACKAGE_ID> \ --module <MODULE_NAME> \ --function <FUNCTION_NAME> \ --gas-budget 10000000
sui client call \ --package <PACKAGE_ID> \ --module hello_world \ --function hello_world \ --gas-budget 10000000 \ --json
|
4. 区块浏览器