JSON 参数教程
包信息
本节引用 CliArgs
示例包,其包含以下清单:
move.toml
[package]
name = "CliArgs"
version = "0.1.0"
upgrade_policy = "compatible"
[addresses]
test_account = "_"
[dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-framework.git", rev = "mainnet", subdir = "aptos-framework" }
在这里,包以命名地址 test_account
部署。
ℹ️
将你的工作目录设置为 aptos-move/move-examples/cli_args
以便跟随:
Terminal
cd <aptos-core-parent-directory>/aptos-core/aptos-move/move-examples/cli_args
部署包
首先为将要部署包的 Ace 生成一个虚荣地址:
Terminal
aptos key generate \
--vanity-prefix 0xace \
--output-file ace.key
输出
Terminal
{
"Result": {
"Account Address:": "0xacef1b9b7d4ab208b99fed60746d18dcd74865edb7eb3c3f1428233988e4ba46",
"PublicKey Path": "ace.key.pub",
"PrivateKey Path": "ace.key"
}
}
ℹ️
确切的账户地址在每次运行时应有所不同,但虚荣前缀不应改变。
将 Ace 的地址存储在 shell 变量中,以便稍后可以内联调用:
Terminal
# 你的确切地址会有所不同
ace_addr=0xacef1b9b7d4ab208b99fed60746d18dcd74865edb7eb3c3f1428233988e4ba46
通过水龙头为 Ace 的账户提供资金(仅在开发网上有效):
Terminal
aptos account fund-with-faucet --account $ace_addr
输出
Terminal
{
"Result": "Added 100000000 Octas to account acef1b9b7d4ab208b99fed60746d18dcd74865edb7eb3c3f1428233988e4ba46"
}
现在在 Ace 的账户下发布包:
Terminal
aptos move publish \
--named-addresses test_account=$ace_addr \
--private-key-file ace.key \
--assume-yes
输出
Terminal
{
"Result": {
"transaction_hash": "0x1d7b074dd95724c5459a1c30fe4cb3875e7b0478cc90c87c8e3f21381625bec1",
"gas_used": 1294,
"gas_unit_price": 100,
"sender": "acef1b9b7d4ab208b99fed60746d18dcd74865edb7eb3c3f1428233988e4ba46",
"sequence_number": 0,
"success": true,
"timestamp_us": 1685077849297587,
"version": 528422121,
"vm_status": "Executed successfully"
}
}
入口函数
包中的唯一模块 cli_args.move
定义了一个简单的 Holder
资源,具有各种数据类型的字段:
Holder in cli_args.move
module test_account::cli_args {
use std::signer;
use aptos_std::type_info::{Self, TypeInfo};
use std::string::String;
struct Holder has key, drop {
u8_solo: u8,
bytes: vector<u8>,
utf8_string: String,
bool_vec: vector<bool>,
address_vec_vec: vector<vector<address>>,
type_info_1: TypeInfo,
type_info_2: TypeInfo,
}
一个公共入口函数可以用于设置字段,具有多层嵌套向量:
Setter function in cli_args.move
/// 在 `account` 下设置 `Holder` 中的值。
public entry fun set_vals<T1, T2>(
account: signer,
u8_solo: u8,
bytes: vector<u8>,
utf8_string: String,
bool_vec: vector<bool>,
address_vec_vec: vector<vector<address>>,
) acquires Holder {
let account_addr = signer::address_of(&account);
if (exists<Holder>(account_addr)) {
move_from<Holder>(account_addr);
};
move_to(&account, Holder {
u8_solo,
bytes,
utf8_string,
bool_vec,
address_vec_vec,
type_info_1: type_info::type_of<T1>(),
type_info_2: type_info::type_of<T2>(),
});
}
在包发布后,可以使用 aptos move run
调用 set_vals()
:
ℹ️
要从命令行传递向量(包括嵌套向量)作为参数,请使用转义的 JSON 语法!
Running function with nested vector arguments from CLI
aptos move run \
--function-id $ace_addr::cli_args::set_vals \
--type-args \
0x1::account::Account \
0x1::chain_id::ChainId \
--args \
u8:123 \
"hex:0x1234" \
"string:hello, world\! ♥" \
"bool:[false, true, false, false]" \
'address:[["0xace", "0xbee"], ["0xcad"], []]' \
--private-key-file ace.key \
...(about 100 lines omitted)...
## 脚本函数
包还包含一个脚本 `set_vals.move`,它是设置函数的包装器:
```move filename="script"
script {
use test_account::cli_args;
use std::vector;
use std::string::String;
/// 获取一个 `bool` 向量,其中每个元素指示
/// 对应的 `u8_vec` 中的元素是否大于 `u8_solo`。
/// 然后将 `address_solo` 打包到一个 `vector<vector<address>>` 中,并
/// 将结果参数集传递给公共入口函数。
fun set_vals<T1, T2>(
account: signer,
u8_solo: u8,
bytes: vector<u8>,
utf8_string: String,
u8_vec: vector<u8>,
address_solo: address,
) {
let bool_vec = vector::map_ref(&u8_vec, |e_ref| *e_ref > u8_solo);
let addr_vec_vec = vector[vector[address_solo]];
cli_args::set_vals<T1, T2>(account, u8_solo, bytes, utf8_string, bool_vec, addr_vec_vec);
}
}
首先编译包(这将编译脚本):
Compilation
aptos move compile --named-addresses test_account=$ace_addr
输出
Terminal
{
"Result": [
"acef1b9b7d4ab208b99fed60746d18dcd74865edb7eb3c3f1428233988e4ba46::cli_args"
]
}
接下来,运行 aptos move run-script
:
Arguments via CLI
aptos move run-script \
--compiled-script-path build/CliArgs/bytecode_scripts/set_vals.mv \
--type-args \
0x1::account::Account \
0x1::chain_id::ChainId \
--args \
u8:123 \
"hex:0x1234" \
"string:hello, world\! ♥" \
"u8:[122, 123, 124, 125]" \
address:"0xace" \
--private-key-file ace.key \
--assume-yes
输出
Terminal
{
"Result": {
"transaction_hash": "0x1d644eba8187843cc43919469112339bc2c435a49a733ac813b7bc6c79770152",
"gas_used": 3,
"gas_unit_price": 100,
"sender": "acef1b9b7d4ab208b99fed60746d18dcd74865edb7eb3c3f1428233988e4ba46",
"sequence_number": 4,
"success": true,
"timestamp_us": 1685078516832128,
"version": 528427132,
"vm_status": "Executed successfully"
}
}
Arguments via JSON file
aptos move run-script \
--compiled-script-path build/CliArgs/bytecode_scripts/set_vals.mv \
--json-file script_function_arguments.json \
--private-key-file ace.key \
--assume-yes
输出
Terminal
{
"Result": {
"transaction_hash": "0x840e2d6a5ab80d5a570effb3665f775f1755e0fd8d76e52bfa7241aaade883d7",
"gas_used": 3,
"gas_unit_price": 100,
"sender": "acef1b9b7d4ab208b99fed60746d18dcd74865edb7eb3c3f1428233988e4ba46",
"sequence_number": 5,
"success": true,
"timestamp_us": 1685078616832128,
"version": 528428132,
"vm_status": "Executed successfully"
}
}
script_function_arguments.json
{
"type_args": [
"0x1::account::Account",
"0x1::chain_id::ChainId"
],
"args": [
{
"type": "u8",
"value": 123
},
{
"type": "hex",
"value": "0x1234"
},
{
"type": "string",
"value": "hello, world! ♥"
},
{
"type": "u8",
"value": [
122,
123,
124,
125
]
},
{
"type": "address",
"value": "0xace"
}
]
}
这两种脚本函数调用都会产生以下 reveal()
视图函数输出:
View function call
aptos move view \
--function-id $ace_addr::cli_args::reveal \
--type-args \
0x1::account::Account \
0x1::chain_id::ChainId \
--args address:$ace_addr
View function output
{
"Result": [
{
"address_vec_vec": [["0xace"]],
"bool_vec": [false, false, true, true],
"bytes": "0x1234",
"type_info_1_match": true,
"type_info_2_match": true,
"u8_solo": 123,
"utf8_string": "hello, world! ♥"
}
]
}
ℹ️
截至本文撰写时,aptos
CLI 仅支持脚本函数参数为类型为 u8
的向量,并且仅支持深度为 1 的向量。因此 vector<address>
和 vector<vector<u8>>
是无效的脚本函数参数类型。