Skip to content

Commit dc19c0d

Browse files
fix
1 parent ef94446 commit dc19c0d

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

key-wallet/src/managed_account/managed_platform_account.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,33 @@ impl ManagedPlatformAccount {
109109
self.metadata.last_used = Some(Self::current_timestamp());
110110
}
111111

112-
/// Update the credit balance for a specific address (add/subtract)
112+
/// Add credits to a specific address balance
113113
///
114114
/// Returns the new credit balance for the address.
115-
pub fn update_address_credit_balance(
115+
pub fn add_address_credit_balance(
116116
&mut self,
117117
address: PlatformP2PKHAddress,
118-
delta: i64,
118+
amount: u64,
119119
) -> u64 {
120120
let current = self.address_balances.get(&address).copied().unwrap_or(0);
121-
let new_balance = if delta >= 0 {
122-
current.saturating_add(delta as u64)
123-
} else {
124-
current.saturating_sub((-delta) as u64)
125-
};
121+
let new_balance = current.saturating_add(amount);
122+
self.address_balances.insert(address, new_balance);
123+
self.recalculate_credit_balance();
124+
self.metadata.last_used = Some(Self::current_timestamp());
125+
new_balance
126+
}
127+
128+
/// Remove credits from a specific address balance
129+
///
130+
/// Uses saturating subtraction - balance will not go below zero.
131+
/// Returns the new credit balance for the address.
132+
pub fn remove_address_credit_balance(
133+
&mut self,
134+
address: PlatformP2PKHAddress,
135+
amount: u64,
136+
) -> u64 {
137+
let current = self.address_balances.get(&address).copied().unwrap_or(0);
138+
let new_balance = current.saturating_sub(amount);
126139
self.address_balances.insert(address, new_balance);
127140
self.recalculate_credit_balance();
128141
self.metadata.last_used = Some(Self::current_timestamp());
@@ -380,13 +393,13 @@ mod tests {
380393
assert_eq!(account.address_credit_balance(&addr), 500);
381394
assert_eq!(account.total_credit_balance(), 500); // Recalculated from address balances
382395

383-
// Update address credit balance
384-
let new_balance = account.update_address_credit_balance(addr, 200);
396+
// Add to address credit balance
397+
let new_balance = account.add_address_credit_balance(addr, 200);
385398
assert_eq!(new_balance, 700);
386399
assert_eq!(account.total_credit_balance(), 700);
387400

388-
// Negative update
389-
let new_balance = account.update_address_credit_balance(addr, -100);
401+
// Remove from address credit balance
402+
let new_balance = account.remove_address_credit_balance(addr, 100);
390403
assert_eq!(new_balance, 600);
391404
assert_eq!(account.total_credit_balance(), 600);
392405
}

key-wallet/src/transaction_checking/platform_checker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl WalletPlatformChecker for ManagedWalletInfo {
126126
// Find the account that contains this address
127127
for account in self.accounts.platform_payment_accounts.values_mut() {
128128
if account.contains_platform_address(address) {
129-
let new_balance = account.update_address_credit_balance(*address, amount as i64);
129+
let new_balance = account.add_address_credit_balance(*address, amount);
130130
return Some(new_balance);
131131
}
132132
}
@@ -140,7 +140,7 @@ impl WalletPlatformChecker for ManagedWalletInfo {
140140
amount: u64,
141141
) -> Option<u64> {
142142
if let Some(account) = self.accounts.platform_payment_accounts.get_mut(account_key) {
143-
let new_balance = account.update_address_credit_balance(address, amount as i64);
143+
let new_balance = account.add_address_credit_balance(address, amount);
144144
Some(new_balance)
145145
} else {
146146
None
@@ -155,7 +155,7 @@ impl WalletPlatformChecker for ManagedWalletInfo {
155155
// Find the account that contains this address
156156
for account in self.accounts.platform_payment_accounts.values_mut() {
157157
if account.contains_platform_address(address) {
158-
let new_balance = account.update_address_credit_balance(*address, -(amount as i64));
158+
let new_balance = account.remove_address_credit_balance(*address, amount);
159159
return Some(new_balance);
160160
}
161161
}

0 commit comments

Comments
 (0)