1- use crate :: storage_types:: { DataKey , BALANCE_LIFETIME_THRESHOLD , BALANCE_BUMP_AMOUNT } ;
1+ use crate :: storage_types:: { DataKey , BALANCE_BUMP_AMOUNT , BALANCE_LIFETIME_THRESHOLD } ;
22use soroban_sdk:: { Address , Env } ;
33
44/// Returns the balance for an address, or 0 if not set
55pub fn read_balance ( e : & Env , addr : Address ) -> i128 {
66 let key = DataKey :: Balance ( addr) ;
77 let storage = e. storage ( ) . persistent ( ) ;
8-
8+
99 if let Some ( balance) = storage. get :: < DataKey , i128 > ( & key) {
1010 storage. extend_ttl ( & key, BALANCE_LIFETIME_THRESHOLD , BALANCE_BUMP_AMOUNT ) ;
1111 balance
@@ -19,24 +19,27 @@ pub fn receive_balance(e: &Env, addr: Address, amount: i128) {
1919 if crate :: freeze:: is_frozen ( e, & addr) {
2020 panic ! ( "account frozen" ) ;
2121 }
22-
22+
2323 let key = DataKey :: Balance ( addr. clone ( ) ) ;
2424 let current_balance = read_balance ( e, addr) ; // TTL is extended here
2525 let new_balance = current_balance + amount;
26-
26+
2727 e. storage ( ) . persistent ( ) . set ( & key, & new_balance) ;
2828}
2929/// Subtracts amount from address balance — panics if insufficient
3030pub fn spend_balance ( e : & Env , addr : Address , amount : i128 ) {
3131 let key = DataKey :: Balance ( addr. clone ( ) ) ;
3232 let current_balance = read_balance ( e, addr) ;
33-
33+
3434 if current_balance < amount {
35- panic ! ( "insufficient balance: attempted to spend {} but only {} available" , amount, current_balance) ;
35+ panic ! (
36+ "insufficient balance: attempted to spend {} but only {} available" ,
37+ amount, current_balance
38+ ) ;
3639 }
37-
40+
3841 let new_balance = current_balance - amount;
39-
42+
4043 let storage = e. storage ( ) . persistent ( ) ;
4144 storage. set ( & key, & new_balance) ;
4245 storage. extend_ttl ( & key, BALANCE_LIFETIME_THRESHOLD , BALANCE_BUMP_AMOUNT ) ;
@@ -46,18 +49,25 @@ pub fn spend_balance(e: &Env, addr: Address, amount: i128) {
4649// (Make sure to import DataKey if not already imported)
4750
4851pub fn read_total_supply ( e : & Env ) -> i128 {
49- e. storage ( ) . instance ( ) . get ( & DataKey :: TotalSupply ) . unwrap_or ( 0 )
52+ e. storage ( )
53+ . instance ( )
54+ . get ( & DataKey :: TotalSupply )
55+ . unwrap_or ( 0 )
5056}
5157
5258pub fn increase_supply ( e : & Env , amount : i128 ) {
5359 let supply = read_total_supply ( e) ;
54- e. storage ( ) . instance ( ) . set ( & DataKey :: TotalSupply , & ( supply + amount) ) ;
60+ e. storage ( )
61+ . instance ( )
62+ . set ( & DataKey :: TotalSupply , & ( supply + amount) ) ;
5563}
5664
5765pub fn decrease_supply ( e : & Env , amount : i128 ) {
5866 let supply = read_total_supply ( e) ;
5967 if supply < amount {
6068 panic ! ( "supply cannot be negative" ) ;
6169 }
62- e. storage ( ) . instance ( ) . set ( & DataKey :: TotalSupply , & ( supply - amount) ) ;
63- }
70+ e. storage ( )
71+ . instance ( )
72+ . set ( & DataKey :: TotalSupply , & ( supply - amount) ) ;
73+ }
0 commit comments