Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions contract-sdk/specs/token/oas20/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,24 @@ pub fn transfer<C: sdk::Context>(
return Err(Error::ZeroAmount);
}

let mut from_balance = balances.get(ctx.public_store(), from).unwrap_or_default();
let mut to_balance = balances.get(ctx.public_store(), to).unwrap_or_default();
if from == to {
let from_balance = balances.get(ctx.public_store(), from).unwrap_or_default();
if from_balance < amount {
return Err(Error::InsufficientFunds);
}
return Ok(());
}

// Remove from account balance.
let mut from_balance = balances.get(ctx.public_store(), from).unwrap_or_default();
from_balance = from_balance
.checked_sub(amount)
.ok_or(Error::InsufficientFunds)?;
to_balance += amount;

balances.insert(ctx.public_store(), from, from_balance);

// Add to account balance. Shouldn't ever overflow.
let mut to_balance = balances.get(ctx.public_store(), to).unwrap_or_default();
to_balance = to_balance.checked_add(amount).unwrap();
balances.insert(ctx.public_store(), to, to_balance);

Ok(())
Expand Down
Loading