feat: get pending balance by unit

This commit is contained in:
thesimplekid
2024-05-23 13:41:51 +01:00
parent b108892bd4
commit af00645862
2 changed files with 19 additions and 14 deletions

View File

@@ -54,13 +54,10 @@ impl JsWallet {
}
#[wasm_bindgen(js_name = totalPendingBalance)]
pub async fn total_pending_balance(&self) -> Result<JsAmount> {
Ok(self
.inner
.total_pending_balance()
.await
.map_err(into_err)?
.into())
pub async fn total_pending_balance(&self) -> Result<JsValue> {
Ok(serde_wasm_bindgen::to_value(
&self.inner.total_pending_balance().await.map_err(into_err)?,
)?)
}
#[wasm_bindgen(js_name = checkAllPendingProofs)]

View File

@@ -159,20 +159,28 @@ impl Wallet {
/// Total Balance of wallet
#[instrument(skip(self))]
pub async fn total_pending_balance(&self) -> Result<Amount, Error> {
let mut balance = Amount::ZERO;
pub async fn total_pending_balance(&self) -> Result<HashMap<CurrencyUnit, Amount>, Error> {
let mut balances = HashMap::new();
if let Some(proofs) = self
.localstore
.get_proofs(None, None, Some(vec![State::Pending]), None)
.get_proofs(
None,
None,
Some(vec![State::Pending, State::Reserved]),
None,
)
.await?
{
let amount = proofs.iter().map(|p| p.proof.amount).sum();
balance += amount;
for proof in proofs {
balances
.entry(proof.unit)
.and_modify(|ps| *ps += proof.proof.amount)
.or_insert(proof.proof.amount);
}
}
Ok(balance)
Ok(balances)
}
#[instrument(skip(self))]