Skip to content

Commit c1a21f6

Browse files
committed
test and add .is_zero() instead of ==
1 parent 6211546 commit c1a21f6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

rewards/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<T: Config> Pallet<T> {
153153
});
154154

155155
share = share.saturating_sub(remove_amount);
156-
if share != Zero::zero() {
156+
if !share.is_zero() {
157157
*share_info = Some((share, withdrawn_rewards));
158158
}
159159
}

rewards/src/tests.rs

+32
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,35 @@ fn accumulate_reward_should_work() {
341341
);
342342
});
343343
}
344+
345+
#[test]
346+
fn share_to_zero_removes_storage() {
347+
ExtBuilder::default().build().execute_with(|| {
348+
assert_eq!(ShareAndWithdrawnReward::<Runtime>::contains_key(DOT_POOL, ALICE), false);
349+
RewardsModule::add_share(&ALICE, &DOT_POOL, 100);
350+
RewardsModule::add_share(&BOB, &DOT_POOL, 100);
351+
Pools::<Runtime>::mutate(DOT_POOL, |pool_info| {
352+
pool_info.total_rewards += 10000;
353+
});
354+
355+
assert_eq!(
356+
RewardsModule::pools(DOT_POOL),
357+
PoolInfo {
358+
total_shares: 200,
359+
total_rewards: 10000,
360+
total_withdrawn_rewards: 0,
361+
}
362+
);
363+
364+
// chescks if key is removed
365+
assert_eq!(ShareAndWithdrawnReward::<Runtime>::contains_key(DOT_POOL, ALICE), true);
366+
RewardsModule::remove_share(&ALICE, &DOT_POOL, 100);
367+
assert_eq!(ShareAndWithdrawnReward::<Runtime>::contains_key(DOT_POOL, ALICE), false);
368+
369+
RewardsModule::remove_share(&BOB, &DOT_POOL, 50);
370+
assert_eq!(ShareAndWithdrawnReward::<Runtime>::contains_key(DOT_POOL, BOB), true);
371+
372+
RewardsModule::remove_share(&BOB, &DOT_POOL, 100);
373+
assert_eq!(ShareAndWithdrawnReward::<Runtime>::contains_key(DOT_POOL, BOB), false);
374+
});
375+
}

0 commit comments

Comments
 (0)