Move.

帖子

分享您的知识。

Jackson.
Nov 09, 2023
专家问答

从 Move 模块调用函数:方法和最佳实践

你好,我正在尝试从 Move 模块中调用一个函数 (make_sword),地址0x4c6cccd6bc62eac9a9d023b6fa671d8f8c7eaf8ae7e67fae89d26883360dd89d在 MOVE 中. 是否可以调用来自不同的 Move 模块的函数,如果可以,我可以使用哪些方法或方法?有没有类似于 Solidity 中接口的概念来实现这一点?

  • Move
  • Move Module
0
2
分享
评论
.

答案

2
Steven.
Nov 9 2023, 12:36

是的,可以从 Move 模块中调用函数. 你可以使用几种方法来实现这一点:

  1. moveCall功能: 你可以使用 unsafe_moveCall 函数来调用其他 Move 模块中的函数. 此方法允许您使用 curl 直接调用特定函数. 您可以在此处的文档中找到更多详细信息. 但是,使用这种方法时请谨慎行事,因为它被标记为 “不安全”.

  2. Sui CLI: 另一种选择是使用 Sui CLI,在那里你可以使用 sui 客户端调用 [args + params] 命令从其他模块调用函数. 这为与 Move 模块交互提供了一种更结构化和用户友好的方式.

在尝试这些方法时,请确保您正在测试网络或开发网络上工作,因为即使在测试期间拨打电话也需要汽油. 在拨打这些电话时,必须注意煤气的使用并遵守最佳实践. 如果您还有其他问题或需要其他方法的帮助,请随时向他们提问 此处

1
评论
.
Jeremy.
Nov 9 2023, 12:42

是的,可以在 Move 中调用来自不同模块的函数. 为此,您需要有权访问要从中调用该函数的模块的源代码. 这是因为 Move 需要 Move 模块的源代码来编译并将其链接到您的程序中.

如果模块的源代码位于 git 存储库中,则可以在 move.toml 文件中指定 git 存储库和源代码路径,如下所示:

[dependencies.other]
git = 'https://github.com/banool/move-examples.git'
rev = 'main'
subdir = 'call_other_module/other'
If the source code is local, you can specify the path to the source code in your Move.toml file:

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

如果你没有源代码,你可以尝试使用aptos move下载命令下载它. --account 参数应该是你要下载的模块的地址:

aptos move 下载 account 6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7--package other

一旦您可以访问模块的源代码,就可以像这样调用该函数:0x4c6cccd6bc62eac9a9d023b6fa671d8f8c7eaf8ae7e67fae89d26883360dd89d::make_sword();

在此示例中,0x4c6cccd6bc62eac9a9d023b6fa671d8f8c7eaf8ae7e67fae89d26883360dd89d是模块的地址,make_sword也是您要调用的函数.

关于你关于 Move 中接口的问题,没有与 Solidity 接口直接对等的接口. 但是,Move的公开和公开(朋友)可见度修改器确实有类似的概念. 公共函数可以由任何模块或脚本中定义的任何函数调用. 公共(朋友)函数只能由模块中定义的函数调用,这些函数是在定义该函数的模块的好友列表中明确指定的 move-language.github.io.

以下是如何使用这些可见性修饰符的示例:

address 0x42 {
module m {
   friend 0x42::n; // friend declaration
   public(friend) fun foo(): u64 { 0 }
   fun calls_foo(): u64 { foo() } // valid
}
module n {
   fun calls_m_foo(): u64 {
       0x42::m::foo() // valid
   }
}
module other {
   fun calls_m_foo(): u64 {
       0x42::m::foo() // ERROR!
//     ^^^^^^^^^^^^ 'foo' can only be called from a 'friend' of module '0x42::m'
   }
}
}
script {
   fun calls_m_foo(): u64 {
       0x42::m::foo() // ERROR!
//     ^^^^^^^^^^^^ 'foo' can only be called from a 'friend' of module '0x42::m'
   }
}

在此示例中,只能从 n 模块调用 foo 函数,因为它在 m 模块中声明为公共(朋友)函数,并且 n 被列为 m 的朋友.

0
评论
.

你知道答案吗?

请登录并分享。

Move is an executable bytecode language used to implement custom transactions and smart contracts.

148帖子231答案
Sui.X.Peera.

赚取你的 1000 Sui 份额

获取声誉积分,并因帮助 Sui 社区成长而获得奖励。

奖励活动七月