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

木头 发布于 2022-08-09 阅读 9531

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

开发环境搭建

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

安装

macOS 和 Linux:

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

现在,您应该能够运行Move CLI: image.png 我们将在此处介绍最常见的 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下载Move的标准库放到本地。 大家也可以使用aptos项目整理好的库https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/framework,下载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 }

相关文章

17 条评论

$ move sandbox run sources/debug_script.move --signers 0xf     

error[E03002]: unbound module
  ┌─ ./sources/debug_script.move:2:9
  │
2 │     use std::debug;
  │         ^^^^^^^^^^ Invalid 'use'. Unbound module: '(std=0x1)::debug'

error[E03002]: unbound module
  ┌─ ./sources/debug_script.move:4:9
  │
4 │         debug::print(&account)
  │         ^^^^^ Unbound module alias 'debug'

运行起来报这个错误是什么原因呢

2022-08-10 08:17
木头 回复 duam

没有引入标准库

2022-08-10 10:05

我就是直接按照上面的步骤来的

  1. move new my-move
  2. cd my-move
  3. 创建sources/debug_script.move
  4. move sandbox run sources/debug_script.move --signers 0xf 标准库使用的是默认的,这样有问题吗
2022-08-10 10:54
木头 回复 duam

我看了下默认生成的库不存在了,自行下载到本地重新引用下就可以了

2022-08-10 12:51
blockstudy 回复 duam

如果用aptos,可以把dependencies改一下,如: AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "main" }

2022-08-17 11:33

我把move下载到本地, 执行出错是为什么, W:\myspace\workspace_move_2022\sample>move sandbox run sources/debug_script.move --signers 0xf Error: Unable to resolve packages for package 'sample'

Caused by: 0: While resolving dependency 'MoveStdlib' in package 'sample' 1: While processing dependency 'MoveStdlib' 2: Unable to find package manifest for 'MoveStdlib' at "Move.toml\.\../move-stdlib"

我的文件夹结构是: move-stdlib move-stdlib\docs move-stdlib\modules move-stdlib\nursery move-stdlib\src move-stdlib\templates move-stdlib\tests move-stdlib\Cargo.toml move-stdlib\error_description.errmap sources sources\debug_script.move Move.toml

Move.toml内容: [package] name = "sample" version = "0.0.0"

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

[addresses] std = "0x1"

2022-08-25 08:31

把aptos, move-stdlib 和 aptos-stdlib都下载了, 现在可以运行了. 不能用 https://github.com/diem/diem/tree/latest/language/move-stdlib

2022-08-25 08:55
木头 回复 tonyh

move-stdlib放在哪个目录?

2022-08-25 14:27
tonyh 回复 木头

已经解决了, 把 move-stdlib 和 aptos-stdlib 放在项目根目录, 然后在 Move.toml 加上

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

就可以了! 我的目录结构是这样的:

aptos-stdlib
build
move-stdlib
sources
storage
Move.toml

但是 move-stdlib 需要用 https://github.com/aptos-labs/aptos-core/tree/main/aptos-move/framework 这个里面提取出来的, 不能用 https://github.com/diem/diem/tree/latest/language/move-stdlib, 因为这个缺少 Move.toml

2022-08-25 21:39
tonyh 回复 木头

纠正一下, Move.toml里面的路径是一个点,不是两个点.

MoveStdlib = { local = "./move-stdlib" }
AptosStdlib = { local = "./aptos-stdlib" }
2022-08-25 21:40
木头 回复 tonyh

建议你不要放在项目根目录,放在项目同目录

2022-08-26 08:32
tonyh 回复 木头

好的, 这样可以多个项目共用是吧

2022-08-26 09:39

很棒的系列教程, 可以提交到 https://awesome-move.com 直接在 GitHub 提交 PR https://github.com/awesome-move/awesome-move

2022-08-29 18:33

有Move 开发微信群吗?

2022-09-30 13:43

我的实验中不去专门下载lib,也很快。我的 Move.toml [package] name = "hello-aptos" version = "0.0.0"

[dependencies] AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework/", rev = "main" } MoveStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/move-framework/", rev = "main" }

[addresses] std = "0x1"

2022-10-02 22:58