From 19156552ffd618815e7c9ab823fac8b9d2df71c0 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 24 Jan 2025 11:17:29 +0000 Subject: [PATCH] fix: p2pk example to account for fees --- crates/cdk/Cargo.toml | 1 + crates/cdk/examples/p2pk.rs | 14 ++++++++++++-- crates/cdk/src/wallet/proofs.rs | 17 +++++++++++++++-- crates/cdk/src/wallet/send.rs | 12 +++--------- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index 35eff847..1dfad52a 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -100,6 +100,7 @@ required-features = ["wallet"] [dev-dependencies] rand = "0.8.5" bip39 = "2.0" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } criterion = "0.5.1" [[bench]] diff --git a/crates/cdk/examples/p2pk.rs b/crates/cdk/examples/p2pk.rs index 85112d06..ca3639d8 100644 --- a/crates/cdk/examples/p2pk.rs +++ b/crates/cdk/examples/p2pk.rs @@ -8,9 +8,19 @@ use cdk::wallet::types::SendKind; use cdk::wallet::{Wallet, WalletSubscription}; use cdk::Amount; use rand::Rng; +use tracing_subscriber::EnvFilter; #[tokio::main] async fn main() -> Result<(), Error> { + let default_filter = "debug"; + + let sqlx_filter = "sqlx=warn,hyper_util=warn,reqwest=warn,rustls=warn"; + + let env_filter = EnvFilter::new(format!("{},{}", default_filter, sqlx_filter)); + + // Parse input + tracing_subscriber::fmt().with_env_filter(env_filter).init(); + // Initialize the memory store for the wallet let localstore = WalletMemoryDatabase::default(); @@ -20,7 +30,7 @@ async fn main() -> Result<(), Error> { // Define the mint URL and currency unit let mint_url = "https://testnut.cashu.space"; let unit = CurrencyUnit::Sat; - let amount = Amount::from(10); + let amount = Amount::from(50); // Create a new wallet let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None)?; @@ -62,7 +72,7 @@ async fn main() -> Result<(), Error> { // Send a token with the specified amount and spending conditions let token = wallet .send( - amount, + 10.into(), None, Some(spending_conditions), &SplitTarget::default(), diff --git a/crates/cdk/src/wallet/proofs.rs b/crates/cdk/src/wallet/proofs.rs index 2ecc8354..73ece131 100644 --- a/crates/cdk/src/wallet/proofs.rs +++ b/crates/cdk/src/wallet/proofs.rs @@ -161,8 +161,11 @@ impl Wallet { proofs: Proofs, include_fees: bool, ) -> Result { - // TODO: Check all proofs are same unit - + tracing::debug!( + "Selecting proofs to send {} from {}", + amount, + proofs.total_amount()? + ); if proofs.total_amount()? < amount { return Err(Error::InsufficientFunds); } @@ -228,6 +231,11 @@ impl Wallet { amount: Amount, proofs: Proofs, ) -> Result { + tracing::debug!( + "Selecting proofs to swap {} from {}", + amount, + proofs.total_amount()? + ); let active_keyset_id = self.get_active_mint_keyset().await?.id; let (mut active_proofs, mut inactive_proofs): (Proofs, Proofs) = proofs @@ -259,6 +267,11 @@ impl Wallet { } } + tracing::debug!( + "Could not select proofs to swap: total selected: {}", + selected_proofs.total_amount()? + ); + Err(Error::InsufficientFunds) } } diff --git a/crates/cdk/src/wallet/send.rs b/crates/cdk/src/wallet/send.rs index f5146852..1d2b739a 100644 --- a/crates/cdk/src/wallet/send.rs +++ b/crates/cdk/src/wallet/send.rs @@ -52,19 +52,13 @@ impl Wallet { ) .await?; - let (available_proofs, proofs_sum) = available_proofs.into_iter().fold( - (Vec::new(), Amount::ZERO), - |(mut acc1, mut acc2), p| { - acc2 += p.amount; - acc1.push(p); - (acc1, acc2) - }, - ); + let proofs_sum = available_proofs.total_amount()?; + let available_proofs = if proofs_sum < amount { match &conditions { Some(conditions) => { + tracing::debug!("Insufficient prrofs matching conditions attempting swap"); let unspent_proofs = self.get_unspent_proofs().await?; - let proofs_to_swap = self.select_proofs_to_swap(amount, unspent_proofs).await?; let proofs_with_conditions = self