Преглед изворни кода

feat(wallet): split reserved and pending balance

thesimplekid пре 9 месеци
родитељ
комит
e7b03e7a30
1 измењених фајлова са 44 додато и 2 уклоњено
  1. 44 2
      crates/cdk/src/wallet/mod.rs

+ 44 - 2
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<HashMap<CurrencyUnit, Amount>, 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<HashMap<CurrencyUnit, Amount>, 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<Proofs, Error> {
+        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<Proofs, Error> {