]> Untitled Git - bdk/commitdiff
Make maintain_single_recipient return a Result
authorLLFourn <lloyd.fourn@gmail.com>
Fri, 29 Jan 2021 01:10:22 +0000 (12:10 +1100)
committerLLFourn <lloyd.fourn@gmail.com>
Fri, 29 Jan 2021 01:33:07 +0000 (12:33 +1100)
preferable to panicking.

src/wallet/mod.rs
src/wallet/tx_builder.rs

index aacfe480d117fd0027b39f5f6c147f1d56c542ba..bece035e816961a8ec33845576909a70f2b53eae 100644 (file)
@@ -2543,7 +2543,8 @@ mod test {
         let mut builder = wallet.build_fee_bump(txid).unwrap();
         builder
             .fee_rate(FeeRate::from_sat_per_vb(2.5))
-            .maintain_single_recipient();
+            .maintain_single_recipient()
+            .unwrap();
         let (psbt, details) = builder.finish().unwrap();
 
         assert_eq!(details.sent, original_details.sent);
@@ -2584,7 +2585,10 @@ mod test {
             .unwrap();
 
         let mut builder = wallet.build_fee_bump(txid).unwrap();
-        builder.maintain_single_recipient().fee_absolute(300);
+        builder
+            .maintain_single_recipient()
+            .unwrap()
+            .fee_absolute(300);
         let (psbt, details) = builder.finish().unwrap();
 
         assert_eq!(details.sent, original_details.sent);
@@ -2643,6 +2647,7 @@ mod test {
         builder
             .drain_wallet()
             .maintain_single_recipient()
+            .unwrap()
             .fee_rate(FeeRate::from_sat_per_vb(5.0));
         let (_, details) = builder.finish().unwrap();
         assert_eq!(details.sent, 75_000);
index 4fca77e3f34643b9586e5866b392ad600d500016..a4848096ba7b8f34e9e3b8412121f4d5405d2029 100644 (file)
@@ -514,12 +514,13 @@ impl<'a, B, D: BatchDatabase> TxBuilder<'a, B, D, DefaultCoinSelectionAlgorithm,
     /// Fails if the transaction has more than one outputs.
     ///
     /// [`add_utxo`]: Self::add_utxo
-    pub fn maintain_single_recipient(&mut self) -> &mut Self {
+    pub fn maintain_single_recipient(&mut self) -> Result<&mut Self, Error> {
         let mut recipients = self.params.recipients.drain(..).collect::<Vec<_>>();
-        assert_eq!(recipients.len(), 1, "maintain_single_recipient must not be called while bumping a transactions with more than one output");
+        if recipients.len() != 1 {
+            return Err(Error::SingleRecipientMultipleOutputs);
+        }
         self.params.single_recipient = Some(recipients.pop().unwrap().0);
-        // Since we are fee bumping and maintaining a single recipient we never want to add any more non-manual inputs.
-        self
+        Ok(self)
     }
 }