Skip to content

Commit

Permalink
chore: refactor test reward
Browse files Browse the repository at this point in the history
Signed-off-by: dung5ire <dung@5ire.org>
  • Loading branch information
dung5ire committed Aug 27, 2024
1 parent a8ad267 commit f834018
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions frame/reward/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ use crate::Error;
use crate::{Rewards,ValidatorRewardAccounts,NominatorRewardAccounts,EraReward};
use frame_support::{assert_ok, assert_noop};
use frame_support::traits::Currency;

pub const VALIDATOR: u64 = 11;
pub const NOMINATOR: u64 = 22;
pub const USER: u64 = 1;
pub const USER_2: u64 = 2;


#[test]
fn get_rewards_should_work() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
ValidatorRewardAccounts::<Test>::insert(11, 1000);
ValidatorRewardAccounts::<Test>::insert(VALIDATOR, 1000);
assert_eq!(active_era(), 0);
assert_eq!(RewardBalance::free_balance(11), 1000);
assert_ok!(Reward::get_rewards(who(11), 11));
assert_eq!(RewardBalance::free_balance(VALIDATOR), 1000);
assert_ok!(Reward::get_rewards(who(VALIDATOR), VALIDATOR));
});
}

Expand All @@ -19,8 +26,8 @@ fn anyone_can_call_rewards_should_work() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
assert_eq!(active_era(), 0);
ValidatorRewardAccounts::<Test>::insert(11, 1000);
assert_ok!(Reward::get_rewards(who(1), 11));
ValidatorRewardAccounts::<Test>::insert(VALIDATOR, 1000);
assert_ok!(Reward::get_rewards(who(USER), VALIDATOR));
});
}

Expand All @@ -29,9 +36,8 @@ fn get_rewards_by_not_validator_should_not_work() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
assert_eq!(active_era(), 0);
let normal_user:u64 = 2;

assert_noop!(Reward::get_rewards(who(1), normal_user), Error::<Test>::NoReward);
assert_noop!(Reward::get_rewards(who(USER), USER_2), Error::<Test>::NoReward);
});
}

Expand All @@ -40,9 +46,9 @@ fn get_multiple_rewards_in_the_same_era_should_not_work() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
assert_eq!(active_era(), 0);
ValidatorRewardAccounts::<Test>::insert(11, 1000);
assert_ok!(Reward::get_rewards(who(1), 11));
assert_noop!(Reward::get_rewards(who(1), 11), Error::<Test>::WaitTheEraToComplete);
ValidatorRewardAccounts::<Test>::insert(VALIDATOR, 1000);
assert_ok!(Reward::get_rewards(who(USER), VALIDATOR));
assert_noop!(Reward::get_rewards(who(USER), VALIDATOR), Error::<Test>::WaitTheEraToComplete);
});
}

Expand All @@ -51,31 +57,28 @@ fn nominator_receiving_reward() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
assert_eq!(active_era(), 0);

let validator: u64 = 11;
let nominator: u64 = 21;
let nominator_reward :u128 = 500;
let validator_reward: u128 = 1000;

let earlier_nominator_balance =RewardBalance::free_balance(21);
let earlier_validator_balance =RewardBalance::free_balance(11);
let earlier_nominator_balance =RewardBalance::free_balance(NOMINATOR);
let earlier_validator_balance =RewardBalance::free_balance(VALIDATOR);

ValidatorRewardAccounts::<Test>::insert(validator, validator_reward);
NominatorRewardAccounts::<Test>::insert(validator, nominator, nominator_reward);
EraReward::<Test>::insert(validator,vec![nominator]);
ValidatorRewardAccounts::<Test>::insert(VALIDATOR, validator_reward);
NominatorRewardAccounts::<Test>::insert(VALIDATOR, NOMINATOR, nominator_reward);
EraReward::<Test>::insert(VALIDATOR,vec![NOMINATOR]);

let _ = Balances::deposit_creating(&Reward::account_id(), 15000000);
let reward_account_balance_before =RewardBalance::free_balance(Reward::account_id());
assert_eq!(reward_account_balance_before , 15000000);
assert_ok!(Reward::get_rewards(who(1), validator));
let _= Reward::claim_rewards(validator);
assert_ok!(Reward::get_rewards(who(USER), VALIDATOR));
let _= Reward::claim_rewards(VALIDATOR);

// Validator balance check
let validator_balance =RewardBalance::free_balance(21);
let validator_balance =RewardBalance::free_balance(VALIDATOR);
assert_eq!(validator_balance, earlier_validator_balance + validator_reward);

// Nominator balance check
let nominator_balance =RewardBalance::free_balance(21);
let nominator_balance =RewardBalance::free_balance(NOMINATOR);
assert_eq!(nominator_balance,earlier_nominator_balance + nominator_reward);

// Reward balance check
Expand Down

0 comments on commit f834018

Please sign in to comment.