Skip to content

Commit

Permalink
Ensure that sender and receiver are not the same in ft_transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
bharath-123 committed Aug 17, 2022
1 parent c708121 commit 04480ab
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
2 changes: 2 additions & 0 deletions contracts/near-x/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub const ERROR_EXPECT_RESULT_ON_CALLBACK: &str = "Callback expected result on c
pub const ERROR_MIN_DEPOSIT_TOO_HIGH: &str = "Min deposit too high";
pub const ERROR_TEMP_REWARD_FEE_SET_IN_WAIT_PERIOD: &str =
"Still in wait period for reward fee to be set";
pub const ERROR_SENDER_RECEIVER_SAME: &str = "Sender and receiver cannot be the same";
pub const ERROR_REQUIRE_AMOUNT_GT_0: &str = "Amount should be greater than 0";

/// Validator related errors
pub const ERROR_VALIDATOR_NOT_PAUSED: &str = "Validator not paused";
Expand Down
10 changes: 6 additions & 4 deletions contracts/near-x/src/fungible_token/nearx_internal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::contract::NearxPool;
use crate::errors::{ERROR_REQUIRE_AMOUNT_GT_0, ERROR_SENDER_RECEIVER_SAME};
use near_sdk::json_types::U128;
use near_sdk::{env, log, AccountId, Balance, PromiseResult};
use near_sdk::{env, log, require, AccountId, Balance, PromiseResult};

impl NearxPool {
pub fn internal_nearx_transfer(
Expand All @@ -9,7 +10,8 @@ impl NearxPool {
receiver_id: &AccountId,
amount: u128,
) {
assert!(amount > 0, "The amount should be a positive number");
require!(sender_id != receiver_id, ERROR_SENDER_RECEIVER_SAME);
require!(amount > 0, ERROR_REQUIRE_AMOUNT_GT_0);
let mut sender_acc = self.internal_get_account(sender_id);
let mut receiver_acc = self.internal_get_account(receiver_id);
assert!(
Expand All @@ -20,9 +22,9 @@ impl NearxPool {
);

sender_acc.stake_shares -= amount;
receiver_acc.stake_shares += amount;

self.internal_update_account(sender_id, &sender_acc);

receiver_acc.stake_shares += amount;
self.internal_update_account(receiver_id, &receiver_acc);
}

Expand Down
77 changes: 77 additions & 0 deletions contracts/near-x/tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod helpers;

use crate::helpers::abs_diff_eq;
use helpers::ntoy;
use near_contract_standards::fungible_token::core::FungibleTokenCore;
use near_sdk::json_types::{U128, U64};
use near_sdk::test_utils::testing_env_with_promise_results;
use near_sdk::{
Expand Down Expand Up @@ -2891,3 +2892,79 @@ fn test_pause_validator() {

assert_eq!(contract.total_validator_weight, 50);
}

#[test]
#[should_panic]
fn test_ft_transfer_same_user() {
let (mut context, mut contract) =
contract_setup(owner_account(), operator_account(), treasury_account());

let user1_account_id = AccountId::from_str("user1").unwrap();

let user1_account = Account {
stake_shares: ntoy(10),
unstaked_amount: 0,
withdrawable_epoch_height: 0,
};

update_account(&mut contract, user1_account_id.clone(), &user1_account);

context.predecessor_account_id = user1_account_id.clone();
context.attached_deposit = 1;
testing_env!(context.clone());

contract.ft_transfer(user1_account_id.clone(), U128(ntoy(5)), None);

let user1_account = get_account(&contract, user1_account_id.clone());
assert_eq!(
user1_account,
Account {
stake_shares: ntoy(10),
unstaked_amount: 0,
withdrawable_epoch_height: 0
}
);
}

#[test]
fn test_ft_transfer() {
let (mut context, mut contract) =
contract_setup(owner_account(), operator_account(), treasury_account());

let user1_account_id = AccountId::from_str("user1").unwrap();
let user2_account_id = AccountId::from_str("user2").unwrap();

let user1_account = Account {
stake_shares: ntoy(10),
unstaked_amount: 0,
withdrawable_epoch_height: 0,
};

update_account(&mut contract, user1_account_id.clone(), &user1_account);

context.predecessor_account_id = user1_account_id.clone();
context.attached_deposit = 1;
testing_env!(context.clone());

contract.ft_transfer(user2_account_id.clone(), U128(ntoy(5)), None);

let user1_account = get_account(&contract, user1_account_id.clone());
assert_eq!(
user1_account,
Account {
stake_shares: ntoy(5),
unstaked_amount: 0,
withdrawable_epoch_height: 0
}
);

let user2_account = get_account(&contract, user2_account_id.clone());
assert_eq!(
user2_account,
Account {
stake_shares: ntoy(5),
unstaked_amount: 0,
withdrawable_epoch_height: 0
}
);
}
Binary file modified res/near_x.wasm
Binary file not shown.

0 comments on commit 04480ab

Please sign in to comment.