Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: wallet into multiple mods #372

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions crates/cdk/src/wallet/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::collections::HashMap;

use tracing::instrument;

use crate::{
nuts::{CurrencyUnit, State},
Amount, Error, Wallet,
};

impl Wallet {
/// Total unspent balance of wallet
#[instrument(skip(self))]
pub async fn total_balance(&self) -> Result<Amount, Error> {
let proofs = self
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit),
Some(vec![State::Unspent]),
None,
)
.await?;
let balance = Amount::try_sum(proofs.iter().map(|p| p.proof.amount))?;

Ok(balance)
}

/// Total pending balance
#[instrument(skip(self))]
pub async fn total_pending_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
let proofs = self
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit),
Some(vec![State::Pending]),
None,
)
.await?;

let balances = proofs.iter().fold(HashMap::new(), |mut acc, proof| {
*acc.entry(proof.unit).or_insert(Amount::ZERO) += proof.proof.amount;
acc
});

Ok(balances)
}

/// Total reserved balance
#[instrument(skip(self))]
pub async fn total_reserved_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
let proofs = self
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit),
Some(vec![State::Reserved]),
None,
)
.await?;

let balances = proofs.iter().fold(HashMap::new(), |mut acc, proof| {
*acc.entry(proof.unit).or_insert(Amount::ZERO) += proof.proof.amount;
acc
});

Ok(balances)
}
}
95 changes: 95 additions & 0 deletions crates/cdk/src/wallet/keysets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use tracing::instrument;

use crate::nuts::Id;
use crate::nuts::KeySetInfo;
use crate::nuts::Keys;
use crate::Error;
use crate::Wallet;

impl Wallet {
/// Get keys for mint keyset
///
/// Selected keys from localstore if they are already known
/// If they are not known queries mint for keyset id and stores the [`Keys`]
#[instrument(skip(self))]
pub async fn get_keyset_keys(&self, keyset_id: Id) -> Result<Keys, Error> {
let keys = if let Some(keys) = self.localstore.get_keys(&keyset_id).await? {
keys
} else {
let keys = self
.client
.get_mint_keyset(self.mint_url.clone().try_into()?, keyset_id)
.await?;

self.localstore.add_keys(keys.keys.clone()).await?;

keys.keys
};

Ok(keys)
}

/// Get keysets for mint
///
/// Queries mint for all keysets
#[instrument(skip(self))]
pub async fn get_mint_keysets(&self) -> Result<Vec<KeySetInfo>, Error> {
let keysets = self
.client
.get_mint_keysets(self.mint_url.clone().try_into()?)
.await?;

self.localstore
.add_mint_keysets(self.mint_url.clone(), keysets.keysets.clone())
.await?;

Ok(keysets.keysets)
}

/// Get active keyset for mint
///
/// Queries mint for current keysets then gets [`Keys`] for any unknown
/// keysets
#[instrument(skip(self))]
pub async fn get_active_mint_keyset(&self) -> Result<KeySetInfo, Error> {
let keysets = self
.client
.get_mint_keysets(self.mint_url.clone().try_into()?)
.await?;
let keysets = keysets.keysets;

self.localstore
.add_mint_keysets(self.mint_url.clone(), keysets.clone())
.await?;

let active_keysets = keysets
.clone()
.into_iter()
.filter(|k| k.active && k.unit == self.unit)
.collect::<Vec<KeySetInfo>>();

match self
.localstore
.get_mint_keysets(self.mint_url.clone())
.await?
{
Some(known_keysets) => {
let unknown_keysets: Vec<&KeySetInfo> = keysets
.iter()
.filter(|k| known_keysets.contains(k))
.collect();

for keyset in unknown_keysets {
self.get_keyset_keys(keyset.id).await?;
}
}
None => {
for keyset in keysets {
self.get_keyset_keys(keyset.id).await?;
}
}
}

active_keysets.first().ok_or(Error::NoActiveKeyset).cloned()
}
}
Loading
Loading