clean up contract helpers

This commit is contained in:
wiesche
2026-09-06 20:22:19 +02:00
parent 7f0c43dfdf
commit 538040865a
5 changed files with 21 additions and 24 deletions
+13
View File
@@ -1753,6 +1753,11 @@ fn set_proof_sender(
impl ContractNewArgs {
fn get_net_change(&self) -> Result<i64, Error> {
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."
));
}
}
+1 -2
View File
@@ -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();
+2 -8
View File
@@ -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
+3 -12
View File
@@ -67,8 +67,9 @@ impl OutputSelectionArgs {
}
/// Returns the sum of our output amounts
pub fn sum_output_amounts(&self) -> Result<u64, Error> {
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(),
}
}
}
+2 -2
View File
@@ -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");