ソースを参照

Improved swap to accept an external transaction

Cesar Rodas 1 ヶ月 前
コミット
00f4fc98a4

+ 1 - 0
crates/cdk-ffi/src/wallet.rs

@@ -255,6 +255,7 @@ impl Wallet {
         let result = self
             .inner
             .swap(
+                None,
                 amount.map(Into::into),
                 amount_split_target.into(),
                 cdk_proofs,

+ 1 - 0
crates/cdk-integration-tests/src/lib.rs

@@ -79,6 +79,7 @@ pub async fn attempt_to_swap_pending(wallet: &Wallet) -> Result<()> {
     let swap = wallet
         .swap(
             None,
+            None,
             SplitTarget::None,
             pending.into_iter().map(|p| p.proof).collect(),
             None,

+ 1 - 0
crates/cdk-integration-tests/tests/fake_auth.rs

@@ -373,6 +373,7 @@ async fn test_swap_with_auth() {
 
     let swapped_proofs = wallet
         .swap(
+            None,
             Some(proofs.total_amount().unwrap()),
             SplitTarget::default(),
             proofs.clone(),

+ 1 - 1
crates/cdk-integration-tests/tests/happy_path_mint_wallet.rs

@@ -324,7 +324,7 @@ async fn test_restore() {
 
     let expected_fee = wallet.get_proofs_fee(&proofs).await.unwrap();
     wallet_2
-        .swap(None, SplitTarget::default(), proofs, None, false)
+        .swap(None, None, SplitTarget::default(), proofs, None, false)
         .await
         .unwrap();
 

+ 1 - 0
crates/cdk/src/wallet/melt/melt_bolt11.rs

@@ -399,6 +399,7 @@ impl Wallet {
         if let Some((proof, exact_amount)) = exchange.take() {
             let new_proofs = self
                 .swap(
+                    Some(&mut tx),
                     Some(exact_amount),
                     SplitTarget::None,
                     vec![proof],

+ 9 - 1
crates/cdk/src/wallet/multi_mint_wallet.rs

@@ -1544,7 +1544,14 @@ impl MultiMintWallet {
                 let proofs = wallet.get_unspent_proofs().await?;
                 if !proofs.is_empty() {
                     return wallet
-                        .swap(amount, SplitTarget::default(), proofs, conditions, false)
+                        .swap(
+                            None,
+                            amount,
+                            SplitTarget::default(),
+                            proofs,
+                            conditions,
+                            false,
+                        )
                         .await;
                 }
             }
@@ -1570,6 +1577,7 @@ impl MultiMintWallet {
                 // Swap for optimized proof set
                 match wallet
                     .swap(
+                        None,
                         Some(proofs_amount),
                         SplitTarget::default(),
                         proofs,

+ 1 - 1
crates/cdk/src/wallet/proofs.rs

@@ -111,7 +111,7 @@ impl Wallet {
             .filter_map(|(p, s)| (s.state == State::Unspent).then_some(p))
             .collect();
 
-        self.swap(None, SplitTarget::default(), unspent, None, false)
+        self.swap(Some(tx), None, SplitTarget::default(), unspent, None, false)
             .await?;
 
         match tx.remove_transaction(transaction_id).await {

+ 1 - 0
crates/cdk/src/wallet/send.rs

@@ -290,6 +290,7 @@ impl PreparedSend {
             if let Some(proofs) = self
                 .wallet
                 .swap(
+                    Some(&mut tx),
                     Some(swap_amount),
                     SplitTarget::None,
                     self.proofs_to_swap,

+ 46 - 9
crates/cdk/src/wallet/swap.rs

@@ -13,18 +13,16 @@ use crate::{ensure_cdk, Amount, Error, Wallet};
 use super::Tx;
 
 impl Wallet {
-    /// Swap
-    #[instrument(skip(self, input_proofs))]
-    pub async fn swap(
+    async fn swap_inner(
         &self,
+        tx: &mut Tx<'_, '_>,
         amount: Option<Amount>,
         amount_split_target: SplitTarget,
         input_proofs: Proofs,
         spending_conditions: Option<SpendingConditions>,
         include_fees: bool,
     ) -> Result<Option<Proofs>, Error> {
-        let mut tx = self.localstore.begin_db_transaction().await?;
-        self.refresh_keysets(Some(&mut tx)).await?;
+        self.refresh_keysets(Some(tx)).await?;
 
         tracing::info!("Swapping");
         let mint_url = &self.mint_url;
@@ -32,7 +30,7 @@ impl Wallet {
 
         let pre_swap = self
             .create_swap(
-                &mut tx,
+                tx,
                 amount,
                 amount_split_target.clone(),
                 input_proofs.clone(),
@@ -45,7 +43,7 @@ impl Wallet {
 
         let active_keyset_id = pre_swap.pre_mint_secrets.keyset_id;
         let fee_and_amounts = self
-            .get_keyset_fees_and_amounts_by_id(active_keyset_id, Some(&mut tx))
+            .get_keyset_fees_and_amounts_by_id(active_keyset_id, Some(tx))
             .await?;
 
         let active_keys = self
@@ -140,11 +138,49 @@ impl Wallet {
 
         tx.update_proofs(added_proofs, deleted_ys).await?;
 
-        tx.commit().await?;
-
         Ok(send_proofs)
     }
 
+    /// Swap
+    #[instrument(skip(self, tx, input_proofs))]
+    pub async fn swap(
+        &self,
+        tx: Option<&mut Tx<'_, '_>>,
+        amount: Option<Amount>,
+        amount_split_target: SplitTarget,
+        input_proofs: Proofs,
+        spending_conditions: Option<SpendingConditions>,
+        include_fees: bool,
+    ) -> Result<Option<Proofs>, Error> {
+        if let Some(tx) = tx {
+            self.swap_inner(
+                tx,
+                amount,
+                amount_split_target,
+                input_proofs,
+                spending_conditions,
+                include_fees,
+            )
+            .await
+        } else {
+            let mut tx = self.localstore.begin_db_transaction().await?;
+            let ret = self
+                .swap_inner(
+                    &mut tx,
+                    amount,
+                    amount_split_target,
+                    input_proofs,
+                    spending_conditions,
+                    include_fees,
+                )
+                .await?;
+
+            tx.commit().await?;
+
+            Ok(ret)
+        }
+    }
+
     /// Swap from unspent proofs in db
     #[instrument(skip(self))]
     pub async fn swap_from_unspent(
@@ -192,6 +228,7 @@ impl Wallet {
 
         let to_return = self
             .swap(
+                Some(&mut tx),
                 Some(amount),
                 SplitTarget::default(),
                 proofs,