zl程序教程

您现在的位置是:首页 >  其他

当前栏目

2.MOVE从入门到实战-编译和运行脚本

入门 运行 实战 脚本 编译 move
2023-06-13 09:14:41 时间

本文作者:木头[1]

开发环境搭建

Move[2] 命令行界面(Move CLI)是一种工具,它提供了一种与 Move 交互、测试编写和运行 Move 代码以及测试开发对 Move 开发有用的新工具的简单方法。

安装

macOS 和 Linux:

cargo install --git https://github.com/move-language/move move-cli --branch main

现在,您应该能够运行 Move CLI:

我们将在此处介绍最常见的 Move CLI 命令和标志,但是您可以通过调用 找到可用的命令的完整列表。此外,通过将标志传递给每个 Move CLI 命令,可以找到可用于每个 Move CLI 命令的标志和选项的完整列表,即 move --help --help move <command> --help

新建项目

创建一个新项目的命令:move new

move new <package_name> # Create a Move package <package_name> under the current dir
move new <package_name> -p <path> # Create a Move package <package_name> under path <path>

项目结构

my-move
├─ Move.toml
└─ sources

Move.toml

官方配置清单

[package]
name = <string>                  # e.g., "MoveStdlib"
version = "<uint>.<uint>.<uint>" # e.g., "0.1.1"
license* = <string>              # e.g., "MIT", "GPL", "Apache 2.0"
authors* = [<string>]            # e.g., ["Joe Smith (joesmith@noemail.com)", "Jane Smith (janesmith@noemail.com)"]

[addresses]  # (Optional section) Declares named addresses in this package and instantiates named addresses in the package graph
# One or more lines declaring named addresses in the following format
<addr_name> = "_" | "<hex_address>" # e.g., Std = "_" or Addr = "0xC0FFEECAFE"

[dependencies] # (Optional section) Paths to dependencies and instantiations or renamings of named addresses from each dependency
# One or more lines declaring dependencies in the following format
<string> = { local = <string>, addr_subst* = { (<string> = (<string> | "<hex_address>"))+ } } # local dependencies
<string> = { git = <URL ending in .git>, subdir=<path to dir containing Move.toml inside git repo>, rev=<git commit hash>, addr_subst* = { (<string> = (<string> | "<hex_address>"))+ } } # git dependencies

[dev-addresses] # (Optional section) Same as [addresses] section, but only included in "dev" and "test" modes
# One or more lines declaring dev named addresses in the following format
<addr_name> = "_" | "<hex_address>" # e.g., Std = "_" or Addr = "0xC0FFEECAFE"

[dev-dependencies] # (Optional section) Same as [dependencies] section, but only included in "dev" and "test" modes
# One or more lines declaring dev dependencies in the following format
<string> = { local = <string>, addr_subst* = { (<string> = (<string> | <address>))+ } }

项目信息

[package]
name = "my-move"
version = "0.0.0"

Move 标准库地址

[dependencies]
MoveStdlib = { git = "https://github.com/move-language/move.git", subdir = "language/move-stdlib", rev = "main" }

模块命名(方便项目直接引用)

[addresses]
std =  "0x1"

编写第一个脚本

由于生成项目默认给的 Move 标准库是 Git 地址很慢,可以从https://github.com/diem/diem/tree/latest/language/move-stdlib[3]下载 Move 的标准库放到本地。大家也可以使用 aptos 项目整理好的库https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/framework[4],下载aptos-stdlib,move-stdlib两个包放在项目同目录

修改项目默认标准库地址

[package]
name = "my-move"
version = "0.0.0"

[addresses]
std = "0x1"

[dependencies]
AptosStdlib = { local = "../aptos-stdlib" }
MoveStdlib = { local = "../move-stdlib" }

新建脚本

sources 目录创建一个名为 debug_script.move 的文件,并在其中输入以下内容:

// sources/debug_script.move
script {
    use std::debug;
    fun debug_script(account: signer) {
        debug::print(&account)
    }
}

在沙盒环境运行脚本

move sandbox run sources/debug_script.move --signers 0xf
[debug] (&) { 0000000000000000000000000000000F }

参考资料

[1]

木头: https://learnblockchain.cn/people/3015

[2]

Move: https://learnblockchain.cn/article/3005

[3]

https://github.com/diem/diem/tree/latest/language/move-stdlib: https://github.com/diem/diem/tree/latest/language/move-stdlib

[4]

https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/framework: https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/framework