Merge pull request #574 from ok300/ok300-address-wallet-todos

Align various wallet.balance() methods
This commit is contained in:
thesimplekid
2025-02-03 09:15:48 +00:00
committed by GitHub

View File

@@ -1,9 +1,6 @@
use std::collections::HashMap;
use tracing::instrument;
use crate::nuts::nut00::ProofsMethods;
use crate::nuts::CurrencyUnit;
use crate::{Amount, Error, Wallet};
impl Wallet {
@@ -15,29 +12,13 @@ impl Wallet {
/// Total pending balance
#[instrument(skip(self))]
pub async fn total_pending_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
let proofs = self.get_pending_proofs().await?;
// TODO If only the proofs for this wallet's unit are retrieved, why build a map with key = unit?
let balances = proofs.iter().fold(HashMap::new(), |mut acc, proof| {
*acc.entry(self.unit.clone()).or_insert(Amount::ZERO) += proof.amount;
acc
});
Ok(balances)
pub async fn total_pending_balance(&self) -> Result<Amount, Error> {
Ok(self.get_pending_proofs().await?.total_amount()?)
}
/// Total reserved balance
#[instrument(skip(self))]
pub async fn total_reserved_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
let proofs = self.get_reserved_proofs().await?;
// TODO If only the proofs for this wallet's unit are retrieved, why build a map with key = unit?
let balances = proofs.iter().fold(HashMap::new(), |mut acc, proof| {
*acc.entry(self.unit.clone()).or_insert(Amount::ZERO) += proof.amount;
acc
});
Ok(balances)
pub async fn total_reserved_balance(&self) -> Result<Amount, Error> {
Ok(self.get_reserved_proofs().await?.total_amount()?)
}
}