@@ -14,7 +14,7 @@ use crate::{
1414use anyhow:: { Result , anyhow} ;
1515use clap:: Args ;
1616use pop_chains:: {
17- Action , CallData , CallItem , DynamicPayload , OnlineClient , Pallet , Param , Payload ,
17+ Action , CallData , CallItem , DynamicPayload , Function , OnlineClient , Pallet , Param , Payload ,
1818 SubstrateConfig , construct_extrinsic, construct_sudo_extrinsic, decode_call_data,
1919 encode_call_data, find_callable_by_name, find_pallet_by_name, raw_value_to_string,
2020 render_storage_key_values, sign_and_submit_extrinsic, supported_actions, type_to_param,
@@ -272,16 +272,7 @@ impl CallChainCommand {
272272 }
273273
274274 // Resolve dispatchable function arguments.
275- let args = if self . args . is_empty ( ) {
276- let mut args = Vec :: new ( ) ;
277- for param in & function. params {
278- let input = prompt_for_param ( cli, param, true ) ?;
279- args. push ( input) ;
280- }
281- args
282- } else {
283- self . expand_file_arguments ( ) ?
284- } ;
275+ let args = self . resolve_function_args ( function, cli) ?;
285276
286277 // If the chain has sudo prompt the user to confirm if they want to execute the
287278 // call via sudo.
@@ -493,6 +484,38 @@ impl CallChainCommand {
493484 } )
494485 . collect ( )
495486 }
487+
488+ /// Resolves dispatchable arguments by leveraging CLI-provided values when available,
489+ /// prompting for missing ones. Updates `self.args` with the resolved values.
490+ /// Returns an error if more arguments than expected are provided.
491+ fn resolve_function_args (
492+ & mut self ,
493+ function : & Function ,
494+ cli : & mut impl Cli ,
495+ ) -> Result < Vec < String > > {
496+ let expanded_args = self . expand_file_arguments ( ) ?;
497+ if expanded_args. len ( ) > function. params . len ( ) {
498+ return Err ( anyhow ! (
499+ "Expected {} arguments for `{}`, but received {}. Remove the extra values or run \
500+ without `--args` to be prompted.",
501+ function. params. len( ) ,
502+ function. name,
503+ expanded_args. len( )
504+ ) ) ;
505+ }
506+
507+ let mut resolved_args = Vec :: with_capacity ( function. params . len ( ) ) ;
508+ for ( idx, param) in function. params . iter ( ) . enumerate ( ) {
509+ if let Some ( value) = expanded_args. get ( idx) {
510+ resolved_args. push ( value. clone ( ) ) ;
511+ } else {
512+ resolved_args. push ( prompt_for_param ( cli, param, true ) ?) ;
513+ }
514+ }
515+
516+ self . args = resolved_args. clone ( ) ;
517+ Ok ( resolved_args)
518+ }
496519}
497520
498521/// Represents a configured dispatchable function call, including the pallet, function, arguments,
@@ -1129,6 +1152,45 @@ mod tests {
11291152 Ok ( ( ) )
11301153 }
11311154
1155+ #[ test]
1156+ fn resolve_function_args_preserves_cli_values ( ) -> Result < ( ) > {
1157+ let function = Function {
1158+ pallet : "System" . to_string ( ) ,
1159+ name : "remark" . to_string ( ) ,
1160+ params : vec ! [ Param { name: "remark" . to_string( ) , ..Default :: default ( ) } ] ,
1161+ is_supported : true ,
1162+ ..Default :: default ( )
1163+ } ;
1164+ let mut call_config =
1165+ CallChainCommand { args : vec ! [ "0x11" . to_string( ) ] , ..Default :: default ( ) } ;
1166+ let mut cli = MockCli :: new ( ) ;
1167+ let resolved = call_config. resolve_function_args ( & function, & mut cli) ?;
1168+ assert_eq ! ( resolved, vec![ "0x11" . to_string( ) ] ) ;
1169+ cli. verify ( )
1170+ }
1171+
1172+ #[ test]
1173+ fn resolve_function_args_prompts_for_missing_values ( ) -> Result < ( ) > {
1174+ let function = Function {
1175+ pallet : "System" . to_string ( ) ,
1176+ name : "remark" . to_string ( ) ,
1177+ params : vec ! [
1178+ Param { name: "first" . to_string( ) , ..Default :: default ( ) } ,
1179+ Param { name: "second" . to_string( ) , ..Default :: default ( ) } ,
1180+ ] ,
1181+ is_supported : true ,
1182+ ..Default :: default ( )
1183+ } ;
1184+ let mut call_config =
1185+ CallChainCommand { args : vec ! [ "0x11" . to_string( ) ] , ..Default :: default ( ) } ;
1186+ let mut cli =
1187+ MockCli :: new ( ) . expect_input ( "Enter the value for the parameter: second" , "0x22" . into ( ) ) ;
1188+ let resolved = call_config. resolve_function_args ( & function, & mut cli) ?;
1189+ assert_eq ! ( resolved, vec![ "0x11" . to_string( ) , "0x22" . to_string( ) ] ) ;
1190+ assert_eq ! ( call_config. args, resolved) ;
1191+ cli. verify ( )
1192+ }
1193+
11321194 #[ test]
11331195 fn parse_pallet_name_works ( ) -> Result < ( ) > {
11341196 assert_eq ! ( parse_pallet_name( "system" ) . unwrap( ) , "System" ) ;
0 commit comments