Skip to content

Commit 0d21901

Browse files
authored
refactor: consolidate balance calculations (#338)
Move the account balance updates happening within `check_transaction` into `ManagedAccount::update_balance` and reuse it in `ManagedWalletInfo::update_balance` which also already gets called in `check_transaction`.
1 parent e14bd5e commit 0d21901

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

key-wallet/src/managed_account/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,20 @@ impl ManagedAccount {
265265
}
266266

267267
/// Update the account balance
268-
pub fn update_balance(&mut self, spendable: u64, unconfirmed: u64, locked: u64) {
268+
pub fn update_balance(&mut self) {
269+
let mut spendable = 0;
270+
let mut unconfirmed = 0;
271+
let mut locked = 0;
272+
for utxo in self.utxos.values() {
273+
let value = utxo.txout.value;
274+
if utxo.is_locked {
275+
locked += value;
276+
} else if utxo.is_confirmed {
277+
spendable += value;
278+
} else {
279+
unconfirmed += value;
280+
}
281+
}
269282
self.balance = WalletBalance::new(spendable, unconfirmed, locked);
270283
self.metadata.last_used = Some(Self::current_timestamp());
271284
}

key-wallet/src/transaction_checking/wallet_checker.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,6 @@ impl WalletTransactionChecker for ManagedWalletInfo {
235235
for input in &tx.input {
236236
account.utxos.remove(&input.previous_output);
237237
}
238-
239-
// Recalculate account balance from UTXOs
240-
let mut confirmed = 0u64;
241-
let mut unconfirmed = 0u64;
242-
let mut locked = 0u64;
243-
244-
for utxo in account.utxos.values() {
245-
let value = utxo.txout.value;
246-
if utxo.is_locked {
247-
locked += value;
248-
} else if utxo.is_confirmed {
249-
confirmed += value;
250-
} else {
251-
unconfirmed += value;
252-
}
253-
}
254-
255-
let _ = account.update_balance(confirmed, unconfirmed, locked);
256238
}
257239
_ => {
258240
// Skip UTXO ingestion for identity/provider accounts

key-wallet/src/wallet/balance.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module provides a wallet balance structure containing all available balances.
44
55
use core::fmt::{Display, Formatter};
6+
use core::ops::AddAssign;
67
#[cfg(feature = "serde")]
78
use serde::{Deserialize, Serialize};
89

@@ -62,6 +63,14 @@ impl Display for WalletBalance {
6263
}
6364
}
6465

66+
impl AddAssign for WalletBalance {
67+
fn add_assign(&mut self, other: Self) {
68+
self.spendable += other.spendable;
69+
self.unconfirmed += other.unconfirmed;
70+
self.locked += other.locked;
71+
}
72+
}
73+
6574
#[cfg(test)]
6675
mod tests {
6776
use super::*;
@@ -91,4 +100,20 @@ mod tests {
91100
let display = balance.to_string();
92101
assert_eq!(display, "Spendable: 1000, Unconfirmed: 500, Locked: 200, Total: 1700");
93102
}
103+
104+
#[test]
105+
fn test_balance_add_assign() {
106+
let mut balance = WalletBalance::new(1000, 500, 200);
107+
let balance_add = WalletBalance::new(300, 100, 50);
108+
// Test adding actual balances
109+
balance += balance_add;
110+
assert_eq!(balance.spendable(), 1300);
111+
assert_eq!(balance.unconfirmed(), 600);
112+
assert_eq!(balance.locked(), 250);
113+
assert_eq!(balance.total(), 2150);
114+
// Test adding zero balances
115+
let balance_before = balance;
116+
balance += WalletBalance::default();
117+
assert_eq!(balance_before, balance);
118+
}
94119
}

key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This trait allows WalletManager to work with different wallet info implementations
44
55
use super::managed_account_operations::ManagedAccountOperations;
6+
use crate::account::ManagedAccountTrait;
67
use crate::managed_account::managed_account_collection::ManagedAccountCollection;
78
use crate::transaction_checking::WalletTransactionChecker;
89
use crate::wallet::immature_transaction::{ImmatureTransaction, ImmatureTransactionCollection};
@@ -202,24 +203,12 @@ impl WalletInfoInterface for ManagedWalletInfo {
202203
}
203204

204205
fn update_balance(&mut self) {
205-
let mut spendable = 0u64;
206-
let mut unconfirmed = 0u64;
207-
let mut locked = 0u64;
208-
209-
for account in self.accounts.all_accounts() {
210-
for utxo in account.utxos.values() {
211-
let value = utxo.txout.value;
212-
if utxo.is_locked {
213-
locked += value;
214-
} else if utxo.is_confirmed {
215-
spendable += value;
216-
} else {
217-
unconfirmed += value;
218-
}
219-
}
206+
let mut balance = WalletBalance::default();
207+
for account in self.accounts.all_accounts_mut() {
208+
account.update_balance();
209+
balance += *account.balance();
220210
}
221-
222-
self.balance = WalletBalance::new(spendable, unconfirmed, locked)
211+
self.balance = balance;
223212
}
224213

225214
fn transaction_history(&self) -> Vec<&TransactionRecord> {

0 commit comments

Comments
 (0)