Skip to content

Commit 07395aa

Browse files
authored
fix vector args in CLI (aptos-labs#9168)
1 parent 9e52a93 commit 07395aa

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

aptos-move/move-examples/cli-e2e-tests/common/sources/cli_e2e_tests.move

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,21 @@ module addr::cli_e2e_tests {
267267
(num64, num128, num256)
268268
}
269269

270+
#[view]
271+
public fun test_vector(
272+
input_u8: vector<u8>,
273+
input_u16: vector<u16>,
274+
input_u32: vector<u32>,
275+
input_u64: vector<u64>,
276+
input_u128: vector<u128>,
277+
input_u256: vector<u256>,
278+
input_addr: vector<address>,
279+
input_bool: vector<bool>,
280+
input_string: vector<String>,
281+
): (vector<u8>, vector<u16>, vector<u32>, vector<u64>, vector<u128>, vector<u256>, vector<address>, vector<bool>, vector<String>) {
282+
(input_u8, input_u16, input_u32, input_u64, input_u128, input_u256, input_addr, input_bool, input_string)
283+
}
284+
270285
inline fun get_hero(creator: &address, collection: &String, name: &String): (Object<Hero>, &Hero) {
271286
let token_address = token::create_token_address(
272287
creator,

crates/aptos/e2e/cases/move.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,31 @@ def test_move_view(run_helper: RunHelper, test_name=None):
233233
raise TestError(
234234
f"View function [test_big_number] did not return correct result"
235235
)
236+
237+
# Test view function with with vector arguments
238+
# Follow 2 lines are for testing vector of u16-u256
239+
response = run_helper.run_command(
240+
test_name,
241+
[
242+
"aptos",
243+
"move",
244+
"view",
245+
"--assume-yes",
246+
"--function-id",
247+
"default::cli_e2e_tests::test_vector",
248+
"--args",
249+
"string:1234", # Notice this is testing u8 vector instead of actual string
250+
f"u16:[1,2]",
251+
f"u32:[1,2]",
252+
f"u64:[1,2]",
253+
f"u128:[1,2]",
254+
f"u256:[1,2]",
255+
f'address:["0x123","0x456"]',
256+
"bool:[true,false]",
257+
'string:["abc","efg"]',
258+
],
259+
)
260+
261+
response = json.loads(response.stdout)
262+
if response["Result"] == None or len(response["Result"]) != 9:
263+
raise TestError(f"View function [test_vector] did not return correct result")

crates/aptos/src/move_tool/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,33 @@ impl ArgWithType {
15311531
_ => serde_json::to_value(bcs::from_bytes::<T>(&self.arg)?)
15321532
.map_err(|err| CliError::UnexpectedError(err.to_string())),
15331533
},
1534-
1 => serde_json::to_value(bcs::from_bytes::<Vec<T>>(&self.arg)?)
1535-
.map_err(|err| CliError::UnexpectedError(err.to_string())),
1534+
1 => match self._ty.clone() {
1535+
FunctionArgType::U64 => {
1536+
let u64_vector: Vec<u64> = bcs::from_bytes::<Vec<u64>>(&self.arg)?;
1537+
let string_vector: Vec<String> =
1538+
u64_vector.iter().map(ToString::to_string).collect();
1539+
serde_json::to_value(string_vector)
1540+
.map_err(|err| CliError::UnexpectedError(err.to_string()))
1541+
},
1542+
FunctionArgType::U128 => {
1543+
let u128_vector: Vec<u128> = bcs::from_bytes::<Vec<u128>>(&self.arg)?;
1544+
let string_vector: Vec<String> =
1545+
u128_vector.iter().map(ToString::to_string).collect();
1546+
serde_json::to_value(string_vector)
1547+
.map_err(|err| CliError::UnexpectedError(err.to_string()))
1548+
},
1549+
FunctionArgType::U256 => {
1550+
let u256_vector: Vec<U256> = bcs::from_bytes::<Vec<U256>>(&self.arg)?;
1551+
let string_vector: Vec<String> =
1552+
u256_vector.iter().map(ToString::to_string).collect();
1553+
serde_json::to_value(string_vector)
1554+
.map_err(|err| CliError::UnexpectedError(err.to_string()))
1555+
},
1556+
FunctionArgType::Raw => serde_json::to_value(&self.arg)
1557+
.map_err(|err| CliError::UnexpectedError(err.to_string())),
1558+
_ => serde_json::to_value(bcs::from_bytes::<Vec<T>>(&self.arg)?)
1559+
.map_err(|err| CliError::UnexpectedError(err.to_string())),
1560+
},
15361561

15371562
2 => serde_json::to_value(bcs::from_bytes::<Vec<Vec<T>>>(&self.arg)?)
15381563
.map_err(|err| CliError::UnexpectedError(err.to_string())),

0 commit comments

Comments
 (0)