跳转到内容

编译 Move 脚本

可以使用 Aptos CLI 中已有的 Aptos Move 编译器编译 Move 脚本。有关如何安装 Aptos CLI 并将其用于 Move 合约,请前往使用 Move 合约页面。

安装 Aptos CLI 后,可在脚本包目录中运行以下命令编译脚本:

Terminal window
aptos move compile

随后,build/ 下会出现与 Move 中函数同名的已编译字节码文件。

例如,包 transfer_half 中的以下脚本会编译为 build/transfer_half/bytecode_scripts/transfer_half.mv

script {
use std::signer;
use aptos_framework::coin;
use aptos_framework::aptos_account;
fun transfer_half<Coin>(caller: &signer, receiver_address: address) {
// Retrieve the balance of the caller
let caller_address: address = signer::address_of(caller);
let balance: u64 = coin::balance<Coin>(caller_address);
// Send half to the receiver
let half = balance / 2;
aptos_account::transfer_coins<Coin>(caller, receiver_address, half);
}
}

此外,对于恰好包含一个脚本的包,可使用以下便捷命令:

Terminal window
aptos move compile-script

输出内容如下,其中会返回脚本的准确位置和一个哈希,便于使用:

Terminal window
Compiling, may take a little while to download git dependencies...
UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-framework.git
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING transfer_half
{
"Result": {
"script_location": "/opt/git/developer-docs/apps/docusaurus/static/move-examples/scripts/transfer_half/script.mv",
"script_hash": "9b57ffa952da2a35438e2cf7e941ef2120bb6c2e4674d4fcefb51d5e8431a148"
}
}