Skip to content

Commit 18c554c

Browse files
committed
contract-sdk/specs/token/oas2: Fix and improve self-transfer
1 parent b559f59 commit 18c554c

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

contract-sdk/specs/token/oas20/src/helpers.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,20 @@ 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+
return Ok(());
195+
}
195196

197+
// Remove from account balance.
198+
let mut from_balance = balances.get(ctx.public_store(), from).unwrap_or_default();
196199
from_balance = from_balance
197200
.checked_sub(amount)
198201
.ok_or(Error::InsufficientFunds)?;
199-
to_balance += amount;
200-
201202
balances.insert(ctx.public_store(), from, from_balance);
203+
204+
// Add to account balance. Shouldn't ever overflow.
205+
let mut to_balance = balances.get(ctx.public_store(), to).unwrap_or_default();
206+
to_balance = to_balance.checked_add(amount).unwrap();
202207
balances.insert(ctx.public_store(), to, to_balance);
203208

204209
Ok(())

0 commit comments

Comments
 (0)