Move语言入门教程Part1:Sui开发环境搭建与项目实践

Move语言入门教程Part1:Sui开发环境搭建与项目实践

简介

Move是一种为区块链设计的编程语言,由Facebook(现Meta)开发,主要用于Diem项目,后来被Sui区块链采用。本教程将带你从零开始搭建Sui开发环境,并通过一个简单的todo_list项目来实践Move语言的基本语法和概念。

官方中文文档:https://docs-zh.sui-book.com/guides/developer/getting-started/get-address

参考:https://blog.csdn.net/gitblog_00307/article/details/148465079

环境配置

➜  ~ curl -sSfL https://raw.githubusercontent.com/Mystenlabs/suiup/main/install.sh | sh

➜  ~ export PATH="/Users/tinachan/.local/bin:$PATH"

安装sui devnet

➜  ~ suiup install sui@devnet

安装svr

suiup install mvr

安装walrus

➜  ~ suiup install walrus -y

其他指令

➜  ~ suiup list 

新建项目

~ sui move new todo_list

todo_list.move 文件添加:

/// Module: todo_list
module todo_list::todo_list;

use std::string::String;

/// List of todos. Can be managed by the owner and shared with others.
public struct TodoList has key, store {
    id: UID,
    items: vector<String>
}

/// Create a new todo list.
public fun new(ctx: &mut TxContext): TodoList {
    let list = TodoList {
        id: object::new(ctx),
        items: vector[]
    };

    (list)
}

/// Add a new todo item to the list.
public fun add(list: &mut TodoList, item: String) {
    list.items.push_back(item);
}

/// Remove a todo item from the list by index.
public fun remove(list: &mut TodoList, index: u64): String {
    list.items.remove(index)
}

/// Delete the list and the capability to manage it.
public fun delete(list: TodoList) {
    let TodoList { id, items: _ } = list;
    id.delete();
}

/// Get the number of items in the list.
public fun length(list: &TodoList): u64 {
    list.items.length()
}

构建

➜  todo_list sui move build                                                                    
INCLUDING DEPENDENCY Bridge
INCLUDING DEPENDENCY SuiSystem
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING todo_list

配置Sui CLI

sui client new-env --alias local --rpc http://127.0.0.1:9000
sui client switch --env local  #切换本地环境

启动网络

➜  ~ RUST_LOG="off,sui_node=info" sui start --with-faucet --force-regenesis

如果不使用–force-regenesis参数,下次启动时会恢复之前的网络状态

账户地址

sui client active-address

测试代币

sui client faucet # 获取测试代币
sui client gas #查看账户gas余额

连接全节点

可以通过RPC接口与本地网络交互:

curl --location --request POST 'http://127.0.0.1:9000' \
--header 'Content-Type: application/json' \
--data-raw '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "sui_getTotalTransactionBlocks",
  "params": []
}'

项目打包

➜  todo_list sui client publish --gas-budget 100000000