Browse Source

fix: send proofs with conditions

thesimplekid 7 months ago
parent
commit
72bef1b852
2 changed files with 41 additions and 5 deletions
  1. 6 2
      crates/cdk/examples/p2pk.rs
  2. 35 3
      crates/cdk/src/wallet/mod.rs

+ 6 - 2
crates/cdk/examples/p2pk.rs

@@ -47,12 +47,16 @@ async fn main() -> Result<(), Error> {
 
     let spending_conditions = SpendingConditions::new_p2pk(secret.public_key(), None);
 
+    let bal = wallet.total_balance().await.unwrap();
+
+    println!("{}", bal);
+
     let token = wallet
         .send(
             amount,
             None,
             Some(spending_conditions),
-            &SplitTarget::None,
+            &SplitTarget::default(),
             &SendKind::default(),
             false,
         )
@@ -67,7 +71,7 @@ async fn main() -> Result<(), Error> {
         .await
         .unwrap();
 
-    println!("Redeamed locked token worth: {}", u64::from(amount));
+    println!("Redeemed locked token worth: {}", u64::from(amount));
 
     Ok(())
 }

+ 35 - 3
crates/cdk/src/wallet/mod.rs

@@ -1029,10 +1029,42 @@ impl Wallet {
                 (acc1, acc2)
             },
         );
+        let available_proofs = if proofs_sum < amount {
+            match &conditions {
+                Some(conditions) => {
+                    let available_proofs = self
+                        .localstore
+                        .get_proofs(
+                            Some(mint_url.clone()),
+                            Some(*unit),
+                            Some(vec![State::Unspent]),
+                            None,
+                        )
+                        .await?;
 
-        if proofs_sum < amount {
-            return Err(Error::InsufficientFunds);
-        }
+                    let available_proofs = available_proofs.into_iter().map(|p| p.proof).collect();
+
+                    let proofs_to_swap =
+                        self.select_proofs_to_swap(amount, available_proofs).await?;
+
+                    let proofs_with_conditions = self
+                        .swap(
+                            Some(amount),
+                            SplitTarget::default(),
+                            proofs_to_swap,
+                            Some(conditions.clone()),
+                            include_fees,
+                        )
+                        .await?;
+                    proofs_with_conditions.ok_or(Error::InsufficientFunds)?
+                }
+                None => {
+                    return Err(Error::InsufficientFunds);
+                }
+            }
+        } else {
+            available_proofs
+        };
 
         let selected = self
             .select_proofs_to_send(amount, available_proofs, include_fees)