From 538040865a87c9bb5e5f6658e4603af9a06bb818 Mon Sep 17 00:00:00 2001 From: wiesche Date: Sun, 6 Sep 2026 20:22:19 +0200 Subject: [PATCH] clean up contract helpers --- controller/src/command.rs | 13 +++++++++++++ libwallet/src/contract/actions/view.rs | 3 +-- libwallet/src/contract/slate.rs | 10 ++-------- libwallet/src/contract/types.rs | 15 +++------------ src/cmd/wallet_args.rs | 4 ++-- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/controller/src/command.rs b/controller/src/command.rs index 75008b5..605c912 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -1753,6 +1753,11 @@ fn set_proof_sender( impl ContractNewArgs { fn get_net_change(&self) -> Result { + if self.receive.is_some() && self.send.is_some() { + return Err(Error::ArgumentError( + "Can't pass both --receive and --send parameters.".into(), + )); + } let to_i64 = |v: u64| { i64::try_from(v).map_err(|_| Error::ArgumentError(format!("Amount {} is too large", v))) }; @@ -2234,5 +2239,13 @@ mod contract_tests { api_args.setup_args.selection_args.use_inputs.as_deref(), Some("commitment") ); + + let mut invalid = args; + invalid.send = Some(1); + assert!(matches!( + invalid.to_api_args(), + Err(Error::ArgumentError(message)) + if message == "Can't pass both --receive and --send parameters." + )); } } diff --git a/libwallet/src/contract/actions/view.rs b/libwallet/src/contract/actions/view.rs index 8c1d51f..7e99d8c 100644 --- a/libwallet/src/contract/actions/view.rs +++ b/libwallet/src/contract/actions/view.rs @@ -114,8 +114,7 @@ where // Count signatures present (a participant is "complete" once it has a partial sig). let num_sigs = slate .participant_data - .clone() - .into_iter() + .iter() .filter(|v| v.is_complete()) .count(); diff --git a/libwallet/src/contract/slate.rs b/libwallet/src/contract/slate.rs index 1b5417f..ffab2d2 100644 --- a/libwallet/src/contract/slate.rs +++ b/libwallet/src/contract/slate.rs @@ -501,15 +501,9 @@ where /// We can finalize if all partial sigs are present pub fn can_finalize(slate: &Slate) -> bool { - let res = slate - .participant_data - .clone() - .into_iter() - .filter(|v| !v.is_complete()) - .count(); - // We can finalize if the number of partial sigs is the same as the number of participants - res == 0 && slate.participant_data.len() == slate.num_participants as usize + slate.participant_data.iter().all(|v| v.is_complete()) + && slate.participant_data.len() == slate.num_participants as usize } /// Finalize slate diff --git a/libwallet/src/contract/types.rs b/libwallet/src/contract/types.rs index 4f7c582..9401ec5 100644 --- a/libwallet/src/contract/types.rs +++ b/libwallet/src/contract/types.rs @@ -67,8 +67,9 @@ impl OutputSelectionArgs { } /// Returns the sum of our output amounts pub fn sum_output_amounts(&self) -> Result { - self.output_amounts()? + self.make_outputs .iter() + .flatten() .try_fold(0u64, |acc, v| acc.checked_add(*v)) .ok_or_else(|| Error::GenericError("output amounts sum overflow".to_string())) } @@ -173,17 +174,7 @@ impl Default for ContractNewArgsAPI { fn default() -> ContractNewArgsAPI { ContractNewArgsAPI { ttl_blocks: None, - setup_args: ContractSetupArgsAPI { - src_acct_name: None, - net_change: None, - num_participants: 2, - fee_rate: None, - add_outputs: false, - selection_args: OutputSelectionArgs { - ..Default::default() - }, - proof_args: ProofArgs::default(), - }, + setup_args: ContractSetupArgsAPI::default(), } } } diff --git a/src/cmd/wallet_args.rs b/src/cmd/wallet_args.rs index 24861fd..8355658 100644 --- a/src/cmd/wallet_args.rs +++ b/src/cmd/wallet_args.rs @@ -1057,7 +1057,7 @@ pub fn parse_contract_new_args( }; if receive.is_some() && send.is_some() { return Err(ParseError::ArgumentError(String::from( - "You can only specify receive or send, not both.", + "Can't pass both --receive and --send parameters.", ))); }; if receive.is_none() && send.is_none() { @@ -1166,7 +1166,7 @@ pub fn parse_contract_setup_args( }; if receive.is_some() && send.is_some() { return Err(ParseError::ArgumentError(String::from( - "You can only specify receive or send, not both.", + "Can't pass both --receive and --send parameters.", ))); }; let as_json = args.is_present("as-json");