]> Untitled Git - bdk/commitdiff
Add "add_utxos" method on TxBuilder
authorLLFourn <lloyd.fourn@gmail.com>
Fri, 22 Jan 2021 03:59:01 +0000 (14:59 +1100)
committerLLFourn <lloyd.fourn@gmail.com>
Fri, 22 Jan 2021 04:08:31 +0000 (15:08 +1100)
To replace the previously existing ".utxos"

src/wallet/tx_builder.rs

index b8bc715d3c2b0fac51fccc94197b30f6c2ff408b..4fca77e3f34643b9586e5866b392ad600d500016 100644 (file)
@@ -269,19 +269,36 @@ impl<'a, B, D: BatchDatabase, Cs: CoinSelectionAlgorithm<D>, Ctx: TxBuilderConte
         self
     }
 
-    /// Add a utxo to the internal list of utxos that **must** be spent
+    /// Add the list of outpoints to the internal list of UTXOs that **must** be spent.
+    ///
+    /// If an error occurs while adding any of the UTXOs then none of them are added and the error is returned.
     ///
     /// These have priority over the "unspendable" utxos, meaning that if a utxo is present both in
     /// the "utxos" and the "unspendable" list, it will be spent.
-    pub fn add_utxo(&mut self, outpoint: OutPoint) -> Result<&mut Self, Error> {
+    pub fn add_utxos(&mut self, outpoints: &[OutPoint]) -> Result<&mut Self, Error> {
         let deriv_ctx = crate::wallet::descriptor_to_pk_ctx(self.wallet.secp_ctx());
-        let utxo = self.wallet.get_utxo(outpoint)?.ok_or(Error::UnknownUTXO)?;
-        let descriptor = self.wallet.get_descriptor_for_keychain(utxo.keychain);
-        let satisfaction_weight = descriptor.max_satisfaction_weight(deriv_ctx).unwrap();
-        self.params.utxos.push((utxo, satisfaction_weight));
+        let utxos = outpoints
+            .iter()
+            .map(|outpoint| self.wallet.get_utxo(*outpoint)?.ok_or(Error::UnknownUTXO))
+            .collect::<Result<Vec<_>, _>>()?;
+
+        for utxo in utxos {
+            let descriptor = self.wallet.get_descriptor_for_keychain(utxo.keychain);
+            let satisfaction_weight = descriptor.max_satisfaction_weight(deriv_ctx).unwrap();
+            self.params.utxos.push((utxo, satisfaction_weight));
+        }
+
         Ok(self)
     }
 
+    /// Add a utxo to the internal list of utxos that **must** be spent
+    ///
+    /// These have priority over the "unspendable" utxos, meaning that if a utxo is present both in
+    /// the "utxos" and the "unspendable" list, it will be spent.
+    pub fn add_utxo(&mut self, outpoint: OutPoint) -> Result<&mut Self, Error> {
+        self.add_utxos(&[outpoint])
+    }
+
     /// Only spend utxos added by [`add_utxo`].
     ///
     /// The wallet will **not** add additional utxos to the transaction even if they are needed to