Skip to content

Commit 2b4d4db

Browse files
authored
claim_rewards should not create empty records (#835)
1 parent c23d3d2 commit 2b4d4db

2 files changed

Lines changed: 44 additions & 16 deletions

File tree

rewards/src/lib.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,22 +274,24 @@ impl<T: Config> Pallet<T> {
274274
return;
275275
}
276276

277-
PoolInfos::<T>::mutate(pool, |pool_info| {
278-
let total_shares = U256::from(pool_info.total_shares.to_owned().saturated_into::<u128>());
279-
pool_info.rewards.iter_mut().for_each(
280-
|(reward_currency, (total_reward, total_withdrawn_reward))| {
281-
Self::claim_one(
282-
withdrawn_rewards,
283-
*reward_currency,
284-
share.to_owned(),
285-
total_reward.to_owned(),
286-
total_shares,
287-
total_withdrawn_reward,
288-
who,
289-
pool,
290-
);
291-
},
292-
);
277+
PoolInfos::<T>::mutate_exists(pool, |maybe_pool_info| {
278+
if let Some(pool_info) = maybe_pool_info {
279+
let total_shares = U256::from(pool_info.total_shares.to_owned().saturated_into::<u128>());
280+
pool_info.rewards.iter_mut().for_each(
281+
|(reward_currency, (total_reward, total_withdrawn_reward))| {
282+
Self::claim_one(
283+
withdrawn_rewards,
284+
*reward_currency,
285+
share.to_owned(),
286+
total_reward.to_owned(),
287+
total_shares,
288+
total_withdrawn_reward,
289+
who,
290+
pool,
291+
);
292+
},
293+
);
294+
}
293295
});
294296
}
295297
});

rewards/src/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,32 @@ fn claim_rewards_should_not_create_empty_records() {
121121
SharesAndWithdrawnRewards::<Runtime>::contains_key(&DOT_POOL, &ALICE),
122122
false
123123
);
124+
125+
PoolInfos::<Runtime>::mutate(DOT_POOL, |pool_info| {
126+
pool_info.rewards.insert(NATIVE_COIN, (10_000, 0));
127+
});
128+
RewardsModule::add_share(&ALICE, &DOT_POOL, 100);
129+
assert_eq!(
130+
RewardsModule::pool_infos(DOT_POOL),
131+
PoolInfo {
132+
total_shares: 100,
133+
rewards: vec![(NATIVE_COIN, (10_000, 0))].into_iter().collect()
134+
}
135+
);
136+
assert_eq!(
137+
RewardsModule::shares_and_withdrawn_rewards(DOT_POOL, ALICE),
138+
(100, vec![(NATIVE_COIN, 0)].into_iter().collect())
139+
);
140+
141+
PoolInfos::<Runtime>::remove(DOT_POOL);
142+
assert_eq!(PoolInfos::<Runtime>::contains_key(DOT_POOL), false);
143+
144+
RewardsModule::claim_rewards(&ALICE, &DOT_POOL);
145+
assert_eq!(PoolInfos::<Runtime>::contains_key(&DOT_POOL), false);
146+
assert_eq!(
147+
RewardsModule::shares_and_withdrawn_rewards(DOT_POOL, ALICE),
148+
(100, vec![(NATIVE_COIN, 0)].into_iter().collect())
149+
);
124150
})
125151
}
126152

0 commit comments

Comments
 (0)