Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions key-wallet-manager/src/wallet_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,30 @@ impl<T: WalletInfoInterface> WalletManager<T> {
}
addresses
}

/// Snapshot the current balance of every managed wallet.
pub(crate) fn snapshot_balances(&self) -> Vec<(WalletId, WalletCoreBalance)> {
self.wallet_infos.iter().map(|(id, info)| (*id, info.balance())).collect()
}

/// Emit `BalanceUpdated` events for wallets whose balance differs from the snapshot.
pub(crate) fn emit_balance_changes(&self, old_balances: &[(WalletId, WalletCoreBalance)]) {
for (wallet_id, old_balance) in old_balances {
if let Some(info) = self.wallet_infos.get(wallet_id) {
let new_balance = info.balance();
if *old_balance != new_balance {
let event = WalletEvent::BalanceUpdated {
wallet_id: *wallet_id,
spendable: new_balance.spendable(),
unconfirmed: new_balance.unconfirmed(),
immature: new_balance.immature(),
locked: new_balance.locked(),
};
let _ = self.event_sender.send(event);
}
}
}
}
}

/// Wallet manager errors
Expand Down
22 changes: 5 additions & 17 deletions key-wallet-manager/src/wallet_manager/process_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,13 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletInterface for WalletM
fn update_synced_height(&mut self, height: CoreBlockHeight) {
self.synced_height = height;

// Update each wallet and emit BalanceUpdated events if balance changed
for (wallet_id, info) in self.wallet_infos.iter_mut() {
let old_balance = info.balance();
let snapshot = self.snapshot_balances();

for (_wallet_id, info) in self.wallet_infos.iter_mut() {
info.update_synced_height(height);
let new_balance = info.balance();

// Emit event if balance changed
#[cfg(feature = "std")]
if old_balance != new_balance {
let event = WalletEvent::BalanceUpdated {
wallet_id: *wallet_id,
spendable: new_balance.spendable(),
unconfirmed: new_balance.unconfirmed(),
immature: new_balance.immature(),
locked: new_balance.locked(),
};
let _ = self.event_sender.send(event);
}
}

self.emit_balance_changes(&snapshot);
}

fn filter_committed_height(&self) -> CoreBlockHeight {
Expand Down
Loading