فهرست منبع

feat: get pending balance by unit

thesimplekid 11 ماه پیش
والد
کامیت
af00645862
2فایلهای تغییر یافته به همراه19 افزوده شده و 14 حذف شده
  1. 4 7
      bindings/cdk-js/src/wallet.rs
  2. 15 7
      crates/cdk/src/wallet.rs

+ 4 - 7
bindings/cdk-js/src/wallet.rs

@@ -54,13 +54,10 @@ impl JsWallet {
     }
 
     #[wasm_bindgen(js_name = totalPendingBalance)]
-    pub async fn total_pending_balance(&self) -> Result<JsAmount> {
-        Ok(self
-            .inner
-            .total_pending_balance()
-            .await
-            .map_err(into_err)?
-            .into())
+    pub async fn total_pending_balance(&self) -> Result<JsValue> {
+        Ok(serde_wasm_bindgen::to_value(
+            &self.inner.total_pending_balance().await.map_err(into_err)?,
+        )?)
     }
 
     #[wasm_bindgen(js_name = checkAllPendingProofs)]

+ 15 - 7
crates/cdk/src/wallet.rs

@@ -159,20 +159,28 @@ impl Wallet {
 
     /// Total Balance of wallet
     #[instrument(skip(self))]
-    pub async fn total_pending_balance(&self) -> Result<Amount, Error> {
-        let mut balance = Amount::ZERO;
+    pub async fn total_pending_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
+        let mut balances = HashMap::new();
 
         if let Some(proofs) = self
             .localstore
-            .get_proofs(None, None, Some(vec![State::Pending]), None)
+            .get_proofs(
+                None,
+                None,
+                Some(vec![State::Pending, State::Reserved]),
+                None,
+            )
             .await?
         {
-            let amount = proofs.iter().map(|p| p.proof.amount).sum();
-
-            balance += amount;
+            for proof in proofs {
+                balances
+                    .entry(proof.unit)
+                    .and_modify(|ps| *ps += proof.proof.amount)
+                    .or_insert(proof.proof.amount);
+            }
         }
 
-        Ok(balance)
+        Ok(balances)
     }
 
     #[instrument(skip(self))]