From e7b03e7a3048070025179d98ea52f67eaa01c2e3 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 29 Jun 2024 10:45:46 +0100 Subject: [PATCH] feat(wallet): split reserved and pending balance --- crates/cdk/src/wallet/mod.rs | 46 ++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/cdk/src/wallet/mod.rs b/crates/cdk/src/wallet/mod.rs index 8929387f..6a63d06d 100644 --- a/crates/cdk/src/wallet/mod.rs +++ b/crates/cdk/src/wallet/mod.rs @@ -88,7 +88,7 @@ impl Wallet { Ok(Amount::ZERO) } - /// Total Balance of wallet + /// Total balance of pending proofs #[instrument(skip(self))] pub async fn total_pending_balance(&self) -> Result, Error> { let mut balances = HashMap::new(); @@ -98,7 +98,33 @@ impl Wallet { .get_proofs( Some(self.mint_url.clone()), Some(self.unit.clone()), - Some(vec![State::Pending, State::Reserved]), + Some(vec![State::Pending]), + None, + ) + .await? + { + for proof in proofs { + balances + .entry(proof.unit) + .and_modify(|ps| *ps += proof.proof.amount) + .or_insert(proof.proof.amount); + } + } + + Ok(balances) + } + + /// Total balance of reserved proofs + #[instrument(skip(self))] + pub async fn total_reserved_balance(&self) -> Result, Error> { + let mut balances = HashMap::new(); + + if let Some(proofs) = self + .localstore + .get_proofs( + Some(self.mint_url.clone()), + Some(self.unit.clone()), + Some(vec![State::Reserved]), None, ) .await? @@ -142,6 +168,22 @@ impl Wallet { .map(|p| p.into_iter().map(|p| p.proof).collect())) } + /// Get pending [`Proofs`] + #[instrument(skip(self))] + pub async fn get_pending_proofs(&self) -> Result { + Ok(self + .localstore + .get_proofs( + Some(self.mint_url.clone()), + Some(self.unit.clone()), + Some(vec![State::Pending]), + None, + ) + .await? + .map(|p| p.into_iter().map(|p| p.proof).collect()) + .unwrap_or_default()) + } + /// Get reserved [`Proofs`] #[instrument(skip(self))] pub async fn get_reserved_proofs(&self) -> Result {