diff --git a/src/builder.rs b/src/builder.rs index 2b9729c0c..fa7c2f93a 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -464,6 +464,18 @@ impl Builder { i64::try_from(value_balance).and_then(|i| V::try_from(i).map_err(|_| value::OverflowError)) } + /// Returns the number of actions to add to this bundle in order to contain at least MIN_ACTION actions. + fn num_missing_actions(&self) -> usize { + let num_actions = [self.spends.len(), self.recipients.len()] + .iter() + .max() + .cloned() + .unwrap(); + (num_actions < MIN_ACTIONS) + .then(|| MIN_ACTIONS - num_actions) + .unwrap_or(0) + } + /// Builds a bundle containing the given spent notes and recipients. /// /// The returned bundle will have no proof or signatures; these can be applied with @@ -480,11 +492,11 @@ impl Builder { { let num_spends = spends.len(); let num_recipients = recipients.len(); - let num_actions = [num_spends, num_recipients, MIN_ACTIONS] - .iter() - .max() - .cloned() - .unwrap(); + let mut num_actions = [num_spends, num_recipients].iter().max().cloned().unwrap(); + // We might have to add dummy/split actions only for the first asset to reach MIN_ACTIONS. + pre_actions + .is_empty() + .then(|| num_actions += self.num_missing_actions()); let first_spend = spends.first().cloned(); diff --git a/tests/zsa.rs b/tests/zsa.rs index cbda7fc19..17c0a4b60 100644 --- a/tests/zsa.rs +++ b/tests/zsa.rs @@ -408,7 +408,7 @@ fn zsa_issue_and_transfer() { ], vec![], anchor, - 4, + 2, &keys, ) .unwrap(); @@ -436,7 +436,7 @@ fn zsa_issue_and_transfer() { ], vec![], native_anchor, - 5, + 4, &keys, ) .unwrap(); @@ -468,7 +468,7 @@ fn zsa_issue_and_transfer() { ], vec![], anchor_t7, - 4, + 2, &keys, ) .unwrap(); @@ -489,7 +489,7 @@ fn zsa_issue_and_transfer() { ], vec![], anchor_t7, - 4, + 2, &keys, ) .unwrap();