Skip to content

Commit bfc10dc

Browse files
committed
cli support
1 parent 2cc179a commit bfc10dc

File tree

2 files changed

+1030
-40
lines changed

2 files changed

+1030
-40
lines changed

crates/aptos/src/common/types.rs

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,12 +2699,14 @@ impl EntryFunctionArgumentsJSON {
26992699
&& struct_tag.address == move_core_types::account_address::AccountAddress::ONE;
27002700

27012701
if is_option && arg_json.value.is_array() {
2702-
// Option<T> with vector format: [] for None, [value] for Some
2702+
// Option<T> with legacy vector format: [] for None, [value] for Some
27032703
let array = arg_json.value.as_array().unwrap();
27042704
let (variant, fields) = if array.is_empty() {
2705-
("None", vec![])
2705+
("None", serde_json::Map::new())
27062706
} else if array.len() == 1 {
2707-
("Some", array.clone())
2707+
let mut map = serde_json::Map::new();
2708+
map.insert("0".to_string(), array[0].clone());
2709+
("Some", map)
27082710
} else {
27092711
return Err(CliError::CommandArgumentError(format!(
27102712
"Option<T> as vector must have 0 or 1 elements, got {}",
@@ -2713,7 +2715,7 @@ impl EntryFunctionArgumentsJSON {
27132715
};
27142716

27152717
let bcs_bytes = parser
2716-
.construct_enum_argument(&struct_tag, variant, &fields)
2718+
.construct_enum_argument(&struct_tag, variant, &fields, 0)
27172719
.await?;
27182720

27192721
args.push(ArgWithType {
@@ -2724,44 +2726,59 @@ impl EntryFunctionArgumentsJSON {
27242726
_vector_depth: 0,
27252727
arg: bcs_bytes,
27262728
});
2727-
} else if let Some(variant) = arg_json.value.get("variant").and_then(|v| v.as_str())
2728-
{
2729-
// Check if this is an enum variant (has a "variant" field)
2730-
// Parse as enum
2731-
let fields = arg_json
2732-
.value
2733-
.get("fields")
2734-
.and_then(|v| v.as_array())
2735-
.ok_or_else(|| {
2736-
CliError::CommandArgumentError(format!(
2737-
"Enum argument must have 'fields' array for variant '{}'",
2738-
variant
2739-
))
2740-
})?;
2741-
2742-
let bcs_bytes = parser
2743-
.construct_enum_argument(&struct_tag, variant, fields)
2744-
.await?;
2745-
2746-
args.push(ArgWithType {
2747-
_ty: FunctionArgType::Enum {
2748-
type_tag: struct_tag,
2749-
variant: variant.to_string(),
2750-
},
2751-
_vector_depth: 0,
2752-
arg: bcs_bytes,
2753-
});
2754-
} else {
2755-
// Parse as struct
2756-
let field_values = arg_json.value.as_object().ok_or_else(|| {
2757-
CliError::CommandArgumentError(format!(
2758-
"Struct argument must be a JSON object, got: {}",
2759-
arg_json.value
2760-
))
2761-
})?;
2729+
} else if arg_json.value.is_object() {
2730+
// Value is an object - could be struct or enum
2731+
let obj = arg_json.value.as_object().unwrap();
2732+
2733+
// Check if it's an enum variant (single key with variant name)
2734+
// or a struct (multiple keys or special struct detection)
2735+
if obj.len() == 1 {
2736+
// Single key - could be enum variant
2737+
let (potential_variant_name, variant_fields_value) =
2738+
obj.iter().next().unwrap();
2739+
2740+
// Check if the value is an object (enum fields) or something else
2741+
if let Some(fields_obj) = variant_fields_value.as_object() {
2742+
// Looks like enum format: { "VariantName": { ...fields... } }
2743+
// Try to parse as enum first
2744+
let bcs_bytes = parser
2745+
.construct_enum_argument(
2746+
&struct_tag,
2747+
potential_variant_name,
2748+
fields_obj,
2749+
0,
2750+
)
2751+
.await;
2752+
2753+
match bcs_bytes {
2754+
Ok(bytes) => {
2755+
// Successfully parsed as enum
2756+
args.push(ArgWithType {
2757+
_ty: FunctionArgType::Enum {
2758+
type_tag: struct_tag,
2759+
variant: potential_variant_name.to_string(),
2760+
},
2761+
_vector_depth: 0,
2762+
arg: bytes,
2763+
});
2764+
continue;
2765+
},
2766+
Err(e) => {
2767+
// If it failed with "is a struct, not an enum", try as struct
2768+
if e.to_string().contains("is a struct, not an enum") {
2769+
// Fall through to struct parsing
2770+
} else {
2771+
// Other error - propagate it
2772+
return Err(e);
2773+
}
2774+
},
2775+
}
2776+
}
2777+
}
27622778

2779+
// Parse as struct (either multi-key object or single-key that failed enum parsing)
27632780
let bcs_bytes = parser
2764-
.construct_struct_argument(&struct_tag, field_values)
2781+
.construct_struct_argument(&struct_tag, obj, 0)
27652782
.await?;
27662783

27672784
args.push(ArgWithType {
@@ -2771,6 +2788,11 @@ impl EntryFunctionArgumentsJSON {
27712788
_vector_depth: 0,
27722789
arg: bcs_bytes,
27732790
});
2791+
} else {
2792+
return Err(CliError::CommandArgumentError(format!(
2793+
"Invalid value for type {}. Expected object, got: {}",
2794+
arg_json.arg_type, arg_json.value
2795+
)));
27742796
}
27752797
} else {
27762798
// Parse as primitive type

0 commit comments

Comments
 (0)