Add get_balance to Device

This commit is contained in:
NicolasCantu 2025-04-04 12:47:14 +02:00
parent f89b79ab5e
commit d008101ea9

View File

@ -5,7 +5,7 @@ use tsify::Tsify;
use wasm_bindgen::prelude::*;
use sp_client::{
bitcoin::{absolute::Height, secp256k1::PublicKey, OutPoint, Transaction, XOnlyPublicKey},
bitcoin::{absolute::Height, secp256k1::PublicKey, Amount, OutPoint, Transaction, XOnlyPublicKey},
silentpayments::{
utils::receiving::calculate_ecdh_shared_secret,
SilentPaymentAddress
@ -52,6 +52,12 @@ impl Device {
&mut self.outputs
}
pub fn get_balance(&self) -> Amount {
self.outputs.values()
.filter(|output| output.spend_status == OutputSpendStatus::Unspent)
.fold(Amount::ZERO, |acc, x| acc + x.amount)
}
pub fn update_outputs_with_transaction(&mut self, tx: &Transaction, blockheight: u32, partial_tweak: PublicKey) -> anyhow::Result<HashMap<OutPoint, OwnedOutput>> {
// First check that we haven't already scanned this transaction
let txid = tx.txid();