Post
Share your knowledge.
Calling Functions from Move Module: Methods and Best Practices
Hello, I am trying to call a function (make_sword) from Move module with the address 0x4c6cccd6bc62eac9a9d023b6fa671d8f8c7eaf8ae7e67fae89d26883360dd89d
in MOVE. Is it possible to call functions from different Move modules, and if so, what methods or approaches can I use? Are there any concepts similar to interfaces in Solidity for achieving this?
- Move
- Move Module
Answers
2Yes, it is possible to call functions from Move module (s). There are a few methods you can use to achieve this:
-
moveCall
Function: You can use the unsafe_moveCall function to call functions from other Move module (s). This method allows you to make direct calls to specific functions using curl. You can find more details in the documentation here. However, please be cautious while using this approach as it's labeled as "unsafe." -
Sui CLI: Another option is to use the Sui CLI, where you can call functions from other modules using the sui client call [args + params] command. This provides a more structured and user-friendly way to interact with Move module (s).
When experimenting with these methods, ensure that you are working on the testnet or devnet, as making calls will require gas, even during testing. It's essential to be mindful of gas usage and adhere to best practices while making these calls. If you have further questions or need assistance with a different approach, feel free to ask them here;
Yes, it is possible to call functions from different modules in Move. To do this, you need to have access to the source code of the module you want to call the function from. This is because Move requires the source code of a Move module to compile and link it into your program.
If the source code of the module is in a git repository, you can specify the git repository and the path to the source code in your Move.toml file like so:
[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"
If you don't have the source code, you can try to download it using the aptos move download command. The --account argument should be the address of the module you want to download:
aptos move download --account 6286dfd5e2778ec069d5906cd774efdba93ab2bec71550fa69363482fbd814e7
--package other
Once you have access to the source code of the module, you can call the function like this: 0x4c6cccd6bc62eac9a9d023b6fa671d8f8c7eaf8ae7e67fae89d26883360dd89d::make_sword()
;
In this example, 0x4c6cccd6bc62eac9a9d023b6fa671d8f8c7eaf8ae7e67fae89d26883360dd89d
is the address of the module and make_sword
is the function you want to call.
Regarding your question about interfaces in Move, there is no direct equivalent to Solidity's interfaces. However, Move does have a similar concept with its public and public(friend) visibility modifiers. A public function can be called by any function defined in any module or script. A public(friend) function can only be called by functions defined in modules that are explicitly specified in the friend list of the module that defines the function move-language.github.io.
Here's an example of how to use these visibility modifiers:
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'
}
}
In this example, the foo function can only be called from the n module because it is declared as a public(friend) function in the m module and n is listed as a friend of m.
Do you know the answer?
Please log in and share it.
Move is an executable bytecode language used to implement custom transactions and smart contracts.