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
Show file tree
Hide file tree
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
Binary file modified examples/fungible-token/res/fungible_token.wasm
Binary file not shown.
35 changes: 28 additions & 7 deletions near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use near_sdk::{
const GAS_FOR_RESOLVE_TRANSFER: Gas = Gas(5_000_000_000_000);
const GAS_FOR_FT_TRANSFER_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_TRANSFER.0);

const ERR_TOTAL_SUPPLY_OVERFLOW: &str = "Total supply overflow";

/// Implementation of a FungibleToken standard.
/// Allows to include NEP-141 compatible token to any contract.
/// There are next traits that any contract may implement:
Expand Down Expand Up @@ -69,7 +71,7 @@ impl FungibleToken {
self.total_supply = self
.total_supply
.checked_add(amount)
.unwrap_or_else(|| env::panic_str("Total supply overflow"));
.unwrap_or_else(|| env::panic_str(ERR_TOTAL_SUPPLY_OVERFLOW));
} else {
env::panic_str("Balance overflow");
}
Expand All @@ -82,7 +84,7 @@ impl FungibleToken {
self.total_supply = self
.total_supply
.checked_sub(amount)
.unwrap_or_else(|| env::panic_str("Total supply overflow"));
.unwrap_or_else(|| env::panic_str(ERR_TOTAL_SUPPLY_OVERFLOW));
} else {
env::panic_str("The account doesn't have enough balance");
}
Expand Down Expand Up @@ -135,9 +137,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 +191,36 @@ 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(ERR_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 = self
.total_supply
.checked_sub(refund_amount)
.unwrap_or_else(|| env::panic_str(ERR_TOTAL_SUPPLY_OVERFLOW));
log!("The account of the sender was deleted");
FtBurn {
owner_id: &receiver_id,
Expand Down