mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-23 05:56:02 +01:00
feat: get pending balance by unit
This commit is contained in:
@@ -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)]
|
||||
|
||||
@@ -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))]
|
||||
|
||||
Reference in New Issue
Block a user