Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEP141: safe maths fixes #830

Merged
merged 7 commits into from
Jun 18, 2022
Merged
Changes from 2 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
28 changes: 23 additions & 5 deletions near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ impl FungibleTokenCore for FungibleToken {
let sender_id = env::predecessor_account_id();
let amount: Balance = amount.into();
self.internal_transfer(&sender_id, &receiver_id, amount, memo);
let receiver_gas = env::prepaid_gas()
.0
.checked_sub(GAS_FOR_FT_TRANSFER_CALL.0)
.unwrap_or_else(|| env::panic_str("Prepaid gas overflow"));
// Initiating receiver's call and the callback
ext_ft_receiver::ext(receiver_id.clone())
.with_static_gas(env::prepaid_gas() - GAS_FOR_FT_TRANSFER_CALL)
.with_static_gas(receiver_gas.into())
.ft_on_transfer(sender_id.clone(), amount.into(), msg)
.then(
ext_ft_resolver::ext(env::current_account_id())
Expand Down Expand Up @@ -185,21 +189,35 @@ impl FungibleToken {
let receiver_balance = self.accounts.get(&receiver_id).unwrap_or(0);
if receiver_balance > 0 {
let refund_amount = std::cmp::min(receiver_balance, unused_amount);
self.accounts.insert(&receiver_id, &(receiver_balance - refund_amount));
if let Some(new_receiver_balance) = receiver_balance.checked_sub(refund_amount) {
self.accounts.insert(&receiver_id, &new_receiver_balance);
} else {
env::panic_str("The receiver account doesn't have enough balance");
}

if let Some(sender_balance) = self.accounts.get(sender_id) {
self.accounts.insert(sender_id, &(sender_balance + refund_amount));
if let Some(new_sender_balance) = sender_balance.checked_add(refund_amount) {
self.accounts.insert(sender_id, &new_sender_balance);
} else {
env::panic_str("Sender balance overflow");
}

FtTransfer {
old_owner_id: &receiver_id,
new_owner_id: sender_id,
amount: &U128(refund_amount),
memo: Some("refund"),
}
.emit();
return (amount - refund_amount, 0);
let used_amount = amount
.checked_sub(refund_amount)
.unwrap_or_else(|| env::panic_str("Total supply overflow"));
return (used_amount, 0);
} else {
// Sender's account was deleted, so we need to burn tokens.
self.total_supply -= refund_amount;
self.total_supply
.checked_sub(refund_amount)
.unwrap_or_else(|| env::panic_str("Total supply overflow"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't subtracting from self.total_supply, so this is a bug

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed in 261efd0.
Please note this actually also means that this part is not enough covered with the unit tests.

log!("The account of the sender was deleted");
FtBurn {
owner_id: &receiver_id,
Expand Down