@@ -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 }
0 commit comments