如何调用不同 Move 模块/智能合约中的函数?

2024-02-25

我知道链上有一个 Move 模块(智能合约),其功能如下:

public entry fun do_nothing() {}

我知道它部署在6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7::other::do_nothing。您可以在资源管理器中看到该模块here https://explorer.aptoslabs.com/account/0x6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7/modules.

我有一个自己的 Move 模块,如下所示。

Move.toml:

[package]
name = 'mine'
version = '1.0.0'

[dependencies.AptosFramework]
git = 'https://github.com/aptos-labs/aptos-core.git'
rev = 'main'
subdir = 'aptos-move/framework/aptos-framework'

[addresses]
my_addr = "81e2e2499407693c81fe65c86405ca70df529438339d9da7a6fc2520142b591e"
other_addr = "6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7"

sources/mine.move:

module my_addr::mine {
    use other_addr::other::do_nothing;

    public entry fun do_stuff() {
        do_nothing();
    }
}

正如你所看到的,我告诉编译器other模块是通过设置other_addr = "6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7"。但是,当我尝试编译 Move 模块时,它失败了,并显示“未绑定模块”,这意味着它不知道“其他”模块是什么。

$ aptos move compile --named-addresses my_addr="`yq .profiles.default.account < .aptos/config.yaml`"
Compiling, may take a little while to download git dependencies...
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING mine
error[E03002]: unbound module
  ┌─ /Users/dport/github/move-examples/call_other_module/mine/sources/mine.move:2:9
  │
2 │     use other_addr::other::do_nothing;
  │         ^^^^^^^^^^^^^^^^^ Invalid 'use'. Unbound module: '(other_addr=0x6286DFD5E2778EC069D5906CD774EFDBA93AB2BEC71550FA69363482FBD814E7)::other'

error[E03005]: unbound unscoped name
  ┌─ /Users/dport/github/move-examples/call_other_module/mine/sources/mine.move:5:9
  │
5 │         do_nothing();
  │         ^^^^^^^^^^ Unbound function 'do_nothing' in current scope

{
  "Error": "Move compilation failed: Compilation error"
}

为什么编译失败?为什么编译器不能根据它在以下位置找到的 Move 模块的 ABI 为我找出答案:other_addr上链?


问题

为了发布调用另一个 Move 模块中的函数的 Move 模块,您需要其源代码。所有 Move 模块都是如此,而不仅仅是您自己的模块。你会注意到Move.toml已经存在依赖关系AptosFramework。这允许您调用所有框架函数,例如与硬币、代币、签名者、时间戳等相关的内容。

因此,要实现此功能,您需要访问源代码。

来源:Git 依赖项

如果您可以访问另一个 git 存储库中的源代码,您可以告诉编译器在哪里可以找到other模块,将其添加到 Move.toml 中:

[dependencies.other]
git = 'https://github.com/banool/move-examples.git'
rev = 'main'
subdir = 'call_other_module/other'

这是告诉编译器,“源代码other可以在call_other_module/other/该 git 存储库的目录”。

来源:本地

如果您本地有源代码,则可以这样做:

[dependencies.other]
local = "../other"

论点在哪里local是源代码的路径。

来源:我没有?

如果没有源码,可以尝试下载。默认情况下,当有人发布 Move 模块时,他们会在其中包含源代码。

首先尝试下载代码:

cd /tmp
aptos move download --account 6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7 --package other

如果源代码确实部署在链上,您应该看到以下内容:

Saved package with 1 module(s) to `/tmp/other`
{
  "Result": "Download succeeded"
}

Inside /tmp/other你会找到完整的来源,包括Move.toml and sources/.

从这里开始,您只需按照以下步骤操作即可Source: Local above.

Note: 的值--package应该匹配name领域在Move.toml已部署的代码。有关如何根据链上数据确定这一点的更多信息。

来源: 下载失败?

如果你跑了aptos move download并看到了这个:

module without code: other
Saved package with 1 module(s) to `/private/tmp/other_code/other`
{
  "Result": "Download succeeded"
}

你会发现sources/other.move是空的。

这意味着作者发布了带有此 CLI 参数集的代码:

--included-artifacts none

这意味着他们故意选择不将来源包含在链上。

不幸的是,现在你运气不好。编译的硬性要求是,如果要调用另一个 Move 模块中的函数,则必须拥有该模块的源代码。管道中的工作应该能够反编译 Move 字节码,但尚未准备好。


我希望这有帮助,编码愉快!

该答案中使用的代码可以在这里找到:https://github.com/banool/move-examples/tree/main/call_other_module https://github.com/banool/move-examples/tree/main/call_other_module.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何调用不同 Move 模块/智能合约中的函数? 的相关文章

  • 通过 Android Intent 发送链接到 Whatsapp

    我正在尝试从我的 Android 应用程序向 Whatsapp 或短信等聊天应用程序发送带有链接的短信 这些应用程序不接受 text html 类型作为 Intent 类型 当我使用 text plain 类型时 我的消息仅包含主题而没有消
  • Express.js 和 connect-mongo 会话持续时间

    我使用express session和connect mongo来存储用户会话 我将 cookie maxAge 设置为从现在起的 2 周 但我想要的是 如果用户在这 2 周内处于活动状态 则会话会延长到另外 2 周 这样当他不活动 2 周
  • 如何反序列化 JSON,其中值的类型在另一个字段中指定?

    我不确定应该如何尝试反序列化一些如下所示的 JSON columns name stringColumn type string name DateColumn type date name NumberColumn type number
  • 什么时候在 Ruby 中使用 Singleton 类是明智的? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 滥用模式匹配

    我来自 C 发现自己爱上了 F 模式匹配语法 因为它比 C 更简单switch并且更有用 我喜欢尽可能多地使用它 以像本例这样的奇怪方式使用它是否会带来性能或任何其他缺点 match 0 with when a b a gt b 在这个特定
  • git-am 未正确读取配置

    看起来git am continue没有正确读取我的配置 git am continue Applying fatal empty ident name for lt gt notallowed 我尝试遵循中的建议git post rece
  • 查找总和等于 2020 的 n 个值

    我有一个向量Vec具有这些值 1721 979 366 299 675 1456 我正在努力寻找一种方法来获得以下组合n 我最初想做的是n 2 值的总和等于 2020 年 在示例中 很容易将其视为 1721 和 299 2020 年之和 但

随机推荐