File tree Expand file tree Collapse file tree
contract-sdk/specs/token/oas20/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -190,15 +190,24 @@ pub fn transfer<C: sdk::Context>(
190190 return Err ( Error :: ZeroAmount ) ;
191191 }
192192
193- let mut from_balance = balances. get ( ctx. public_store ( ) , from) . unwrap_or_default ( ) ;
194- let mut to_balance = balances. get ( ctx. public_store ( ) , to) . unwrap_or_default ( ) ;
193+ if from == to {
194+ let from_balance = balances. get ( ctx. public_store ( ) , from) . unwrap_or_default ( ) ;
195+ if from_balance < amount {
196+ return Err ( Error :: InsufficientFunds ) ;
197+ }
198+ return Ok ( ( ) ) ;
199+ }
195200
201+ // Remove from account balance.
202+ let mut from_balance = balances. get ( ctx. public_store ( ) , from) . unwrap_or_default ( ) ;
196203 from_balance = from_balance
197204 . checked_sub ( amount)
198205 . ok_or ( Error :: InsufficientFunds ) ?;
199- to_balance += amount;
200-
201206 balances. insert ( ctx. public_store ( ) , from, from_balance) ;
207+
208+ // Add to account balance. Shouldn't ever overflow.
209+ let mut to_balance = balances. get ( ctx. public_store ( ) , to) . unwrap_or_default ( ) ;
210+ to_balance = to_balance. checked_add ( amount) . unwrap ( ) ;
202211 balances. insert ( ctx. public_store ( ) , to, to_balance) ;
203212
204213 Ok ( ( ) )
You can’t perform that action at this time.
0 commit comments