Преглед на файлове

feat: mint_balances and get_mints

thesimplekid преди 1 година
родител
ревизия
8faf8491f0

+ 4 - 0
crates/cashu-sdk/src/localstore/memory.rs

@@ -34,6 +34,10 @@ impl LocalStore for MemoryLocalStore {
         Ok(self.mints.lock().await.get(&mint_url).cloned().flatten())
     }
 
+    async fn get_mints(&self) -> Result<HashMap<UncheckedUrl, Option<MintInfo>>, Error> {
+        Ok(self.mints.lock().await.clone())
+    }
+
     async fn add_mint_keysets(
         &self,
         mint_url: UncheckedUrl,

+ 3 - 0
crates/cashu-sdk/src/localstore/mod.rs

@@ -3,6 +3,8 @@ mod memory;
 #[cfg(not(target_arch = "wasm32"))]
 pub mod redb_store;
 
+use std::collections::HashMap;
+
 use async_trait::async_trait;
 use cashu::nuts::{Id, KeySetInfo, Keys, MintInfo, Proofs};
 use cashu::types::{MeltQuote, MintQuote};
@@ -35,6 +37,7 @@ pub trait LocalStore {
         mint_info: Option<MintInfo>,
     ) -> Result<(), Error>;
     async fn get_mint(&self, mint_url: UncheckedUrl) -> Result<Option<MintInfo>, Error>;
+    async fn get_mints(&self) -> Result<HashMap<UncheckedUrl, Option<MintInfo>>, Error>;
 
     async fn add_mint_keysets(
         &self,

+ 20 - 0
crates/cashu-sdk/src/localstore/redb_store.rs

@@ -1,3 +1,4 @@
+use std::collections::HashMap;
 use std::sync::Arc;
 
 use async_trait::async_trait;
@@ -80,6 +81,25 @@ impl LocalStore for RedbLocalStore {
         Ok(None)
     }
 
+    async fn get_mints(&self) -> Result<HashMap<UncheckedUrl, Option<MintInfo>>, Error> {
+        let db = self.db.lock().await;
+        let read_txn = db.begin_read()?;
+        let table = read_txn.open_table(MINTS_TABLE)?;
+
+        let mints = table
+            .iter()?
+            .flatten()
+            .map(|(mint, mint_info)| {
+                (
+                    serde_json::from_str(mint.value()).unwrap(),
+                    serde_json::from_str(mint_info.value()).unwrap(),
+                )
+            })
+            .collect();
+
+        Ok(mints)
+    }
+
     async fn add_mint_keysets(
         &self,
         mint_url: UncheckedUrl,

+ 22 - 0
crates/cashu-sdk/src/wallet.rs

@@ -95,6 +95,28 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
         self.backup_info.clone().map(|b| b.counter)
     }
 
+    pub async fn mint_balances(&self) -> Result<HashMap<UncheckedUrl, Amount>, Error> {
+        let mints = self.localstore.get_mints().await?;
+
+        let mut balances = HashMap::new();
+
+        for (mint, _) in mints {
+            if let Some(proofs) = self.localstore.get_proofs(mint.clone()).await? {
+                let amount = proofs.iter().map(|p| p.amount).sum();
+
+                balances.insert(mint, amount);
+            } else {
+                balances.insert(mint, Amount::ZERO);
+            }
+        }
+
+        Ok(balances)
+    }
+
+    pub async fn get_proofs(&self, mint_url: UncheckedUrl) -> Result<Option<Proofs>, Error> {
+        Ok(self.localstore.get_proofs(mint_url).await?)
+    }
+
     /// Check if a proof is spent
     #[cfg(feature = "nut07")]
     pub async fn check_proofs_spent(