From e00f4c3e48bd1f78ab1fc5902976f42f4ef1cb0b Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Mon, 1 Jan 2024 22:03:00 +0000 Subject: [PATCH] feat: send get proofs from localstore --- crates/cashu-sdk/src/localstore/memory.rs | 4 +-- crates/cashu-sdk/src/localstore/mod.rs | 2 +- crates/cashu-sdk/src/wallet.rs | 40 +++++++++++------------ crates/cashu/src/types.rs | 6 ---- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/crates/cashu-sdk/src/localstore/memory.rs b/crates/cashu-sdk/src/localstore/memory.rs index 9e5eeae3..caf066bf 100644 --- a/crates/cashu-sdk/src/localstore/memory.rs +++ b/crates/cashu-sdk/src/localstore/memory.rs @@ -127,12 +127,12 @@ impl LocalStore for MemoryLocalStore { .map(|p| p.iter().cloned().collect())) } - async fn remove_proofs(&self, mint_url: UncheckedUrl, proofs: Proofs) -> Result<(), Error> { + async fn remove_proofs(&self, mint_url: UncheckedUrl, proofs: &Proofs) -> Result<(), Error> { let mut mint_proofs = self.proofs.lock().await; if let Some(mint_proofs) = mint_proofs.get_mut(&mint_url) { for proof in proofs { - mint_proofs.remove(&proof); + mint_proofs.remove(proof); } } diff --git a/crates/cashu-sdk/src/localstore/mod.rs b/crates/cashu-sdk/src/localstore/mod.rs index 4fcc0999..71b2825e 100644 --- a/crates/cashu-sdk/src/localstore/mod.rs +++ b/crates/cashu-sdk/src/localstore/mod.rs @@ -41,5 +41,5 @@ pub trait LocalStore { async fn add_proofs(&self, mint_url: UncheckedUrl, proof: Proofs) -> Result<(), Error>; async fn get_proofs(&self, mint_url: UncheckedUrl) -> Result, Error>; - async fn remove_proofs(&self, mint_url: UncheckedUrl, proofs: Proofs) -> Result<(), Error>; + async fn remove_proofs(&self, mint_url: UncheckedUrl, proofs: &Proofs) -> Result<(), Error>; } diff --git a/crates/cashu-sdk/src/wallet.rs b/crates/cashu-sdk/src/wallet.rs index 882f1eb8..578ae94c 100644 --- a/crates/cashu-sdk/src/wallet.rs +++ b/crates/cashu-sdk/src/wallet.rs @@ -12,7 +12,7 @@ use cashu::nuts::{ }; #[cfg(feature = "nut07")] use cashu::types::ProofsStatus; -use cashu::types::{MeltQuote, Melted, MintQuote, SendProofs}; +use cashu::types::{MeltQuote, Melted, MintQuote}; use cashu::url::UncheckedUrl; use cashu::{Amount, Bolt11Invoice}; use thiserror::Error; @@ -411,17 +411,11 @@ impl Wallet { mint_url: &UncheckedUrl, unit: &CurrencyUnit, amount: Amount, - proofs: Proofs, - ) -> Result { - let amount_available: Amount = proofs.iter().map(|p| p.amount).sum(); - - if amount_available.lt(&amount) { - println!("Not enough funds"); - return Err(Error::InsufficientFunds); - } + ) -> Result { + let proofs = self.select_proofs(mint_url.clone(), unit, amount).await?; let pre_swap = self - .create_swap(mint_url, unit, Some(amount), proofs) + .create_swap(mint_url, unit, Some(amount), proofs.clone()) .await?; let swap_response = self @@ -432,16 +426,16 @@ impl Wallet { let mut keep_proofs = Proofs::new(); let mut send_proofs = Proofs::new(); - let mut proofs = construct_proofs( + let mut post_swap_proofs = construct_proofs( swap_response.signatures, pre_swap.pre_mint_secrets.rs(), pre_swap.pre_mint_secrets.secrets(), &self.active_keys(mint_url, unit).await?.unwrap(), )?; - proofs.reverse(); + post_swap_proofs.reverse(); - for proof in proofs { + for proof in post_swap_proofs { if (proof.amount + send_proofs.iter().map(|p| p.amount).sum()).gt(&amount) { keep_proofs.push(proof); } else { @@ -449,9 +443,6 @@ impl Wallet { } } - // println!("Send Proofs: {:#?}", send_proofs); - // println!("Keep Proofs: {:#?}", keep_proofs); - let send_amount: Amount = send_proofs.iter().map(|p| p.amount).sum(); if send_amount.ne(&amount) { @@ -461,10 +452,19 @@ impl Wallet { ); } - Ok(SendProofs { - change_proofs: keep_proofs, - send_proofs, - }) + self.localstore + .remove_proofs(mint_url.clone(), &proofs) + .await?; + + self.localstore + .add_proofs(mint_url.clone(), keep_proofs) + .await?; + + // REVIEW: send proofs are not added to the store since they should be + // used. but if they are not they will be lost. There should likely be a + // pendiing proof store + + Ok(send_proofs) } /// Melt Quote diff --git a/crates/cashu/src/types.rs b/crates/cashu/src/types.rs index 4010b329..ce5d7386 100644 --- a/crates/cashu/src/types.rs +++ b/crates/cashu/src/types.rs @@ -11,12 +11,6 @@ pub struct ProofsStatus { pub spent: Proofs, } -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct SendProofs { - pub change_proofs: Proofs, - pub send_proofs: Proofs, -} - /// Melt response with proofs #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Melted {