diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0ae7e940..a018018d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,31 @@ jobs: cargo fmt --check ' - name: typos - run: nix develop -i -L .#nightly --command typos + run: nix develop -i -L .#nightly --command typos + + examples: + name: "Run examples" + runs-on: ubuntu-latest + strategy: + matrix: + build-args: + [ + mint-token, + p2pk, + proof-selection, + wallet + ] + steps: + - name: checkout + uses: actions/checkout@v4 + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v11 + - name: Nix Cache + uses: DeterminateSystems/magic-nix-cache-action@v6 + - name: Rust Cache + uses: Swatinem/rust-cache@v2 + - name: Run example + run: nix develop -i -L .#stable --command cargo r --example ${{ matrix.build-args }} clippy: name: "Stable build, clippy and test" 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