如何调用其他Move模块/智能合约函数?编译失败求助
问题描述
已知链上存在一个Move模块,其函数定义为:
public entry fun do_nothing() {}
该模块的部署地址为6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7::other::do_nothing。
我编写了自己的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(); } }
执行编译命令:
$ 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" }
疑问
编译失败的原因是什么?为何编译器无法基于链上other_addr处的Move模块ABI自动识别该模块?
原因分析与解决方法
核心原因
Move编译器不会自动从链上拉取依赖模块的代码或ABI,它仅能识别本地项目内的源代码,或是通过Move.toml配置的Git/本地依赖中的模块。你仅在[addresses]中配置了地址映射,但未提供对应模块的源代码或正确的依赖引用,因此编译器找不到other_addr::other模块的定义,触发"unbound module"错误。
解决步骤
要引用链上已部署的模块,需将该模块的源代码添加到本地项目,或通过依赖管理引入:
- 获取目标模块源代码:从链上导出与部署版本完全一致的Move源代码,将其放入本地项目的
sources目录,或是单独创建一个依赖包。 - 配置依赖(可选):若目标模块属于公开Git仓库,可在
Move.toml的[dependencies]中添加该仓库,并在[addresses]中映射对应地址,确保编译时能定位到模块。 - 验证地址映射:确保
other_addr的地址与链上模块部署地址完全匹配,大小写一致。
示例:若将other模块的源代码保存为sources/other.move,内容如下:
module other_addr::other { public entry fun do_nothing() {} }
重新编译后,编译器即可识别other_addr::other模块,顺利完成编译。
内容的提问来源于stack exchange,提问作者Daniel Porteous
相关产品推荐
相关产品推荐

