Skip to content

Commit

Permalink
feat: mint_balances and get_mints
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Jan 2, 2024
1 parent be8ef41 commit 8faf849
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/cashu-sdk/src/localstore/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions crates/cashu-sdk/src/localstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions crates/cashu-sdk/src/localstore/redb_store.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions crates/cashu-sdk/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8faf849

Please sign in to comment.