feat(wallet): split reserved and pending balance

This commit is contained in:
thesimplekid
2024-06-29 10:45:46 +01:00
parent 8e6fdc0af3
commit e7b03e7a30

View File

@@ -88,7 +88,7 @@ impl Wallet {
Ok(Amount::ZERO)
}
/// Total Balance of wallet
/// Total balance of pending proofs
#[instrument(skip(self))]
pub async fn total_pending_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
let mut balances = HashMap::new();
@@ -98,7 +98,33 @@ impl Wallet {
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(vec![State::Pending, State::Reserved]),
Some(vec![State::Pending]),
None,
)
.await?
{
for proof in proofs {
balances
.entry(proof.unit)
.and_modify(|ps| *ps += proof.proof.amount)
.or_insert(proof.proof.amount);
}
}
Ok(balances)
}
/// Total balance of reserved proofs
#[instrument(skip(self))]
pub async fn total_reserved_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
let mut balances = HashMap::new();
if let Some(proofs) = self
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(vec![State::Reserved]),
None,
)
.await?
@@ -142,6 +168,22 @@ impl Wallet {
.map(|p| p.into_iter().map(|p| p.proof).collect()))
}
/// Get pending [`Proofs`]
#[instrument(skip(self))]
pub async fn get_pending_proofs(&self) -> Result<Proofs, Error> {
Ok(self
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(vec![State::Pending]),
None,
)
.await?
.map(|p| p.into_iter().map(|p| p.proof).collect())
.unwrap_or_default())
}
/// Get reserved [`Proofs`]
#[instrument(skip(self))]
pub async fn get_reserved_proofs(&self) -> Result<Proofs, Error> {