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

Prevent self transfer #176

Merged
merged 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.
19 changes: 19 additions & 0 deletions examples/fungible-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ impl FungibleToken {
if amount == 0 {
env::panic(b"Can't transfer 0 tokens");
}
assert_ne!(
owner_id, new_owner_id,
"The new owner should be different from the current owner"
);
// Retrieving the account from the state.
let mut account = self.get_account(&owner_id);

Expand Down Expand Up @@ -321,6 +325,21 @@ mod tests {
assert_eq!(contract.get_balance(bob()).0, transfer_amount);
}

#[test]
#[should_panic(expected = "The new owner should be different from the current owner")]
fn test_transfer_fail_self() {
let mut context = get_context(carol());
testing_env!(context.clone());
let total_supply = 1_000_000_000_000_000u128;
let mut contract = FungibleToken::new(carol(), total_supply.into());
context.storage_usage = env::storage_usage();

context.attached_deposit = 1000 * STORAGE_PRICE_PER_BYTE;
testing_env!(context.clone());
let transfer_amount = total_supply / 3;
contract.transfer(carol(), transfer_amount.into());
}

#[test]
#[should_panic(expected = "Can not set allowance for yourself")]
fn test_self_allowance_fail() {
Expand Down