Skip to content

Commit 6211546

Browse files
committed
migrate to mutate_exist
1 parent 5babe0b commit 6211546

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

rewards/src/lib.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,33 @@ impl<T: Config> Pallet<T> {
130130
// claim rewards firstly
131131
Self::claim_rewards(who, pool);
132132

133-
ShareAndWithdrawnReward::<T>::mutate(pool, who, |(share, withdrawn_rewards)| {
134-
let remove_amount = remove_amount.min(*share);
133+
ShareAndWithdrawnReward::<T>::mutate_exists(pool, who, |share_info| {
134+
if let Some((mut share, mut withdrawn_rewards)) = share_info.take() {
135+
let remove_amount = remove_amount.min(share);
135136

136-
if remove_amount.is_zero() {
137-
return;
138-
}
137+
if remove_amount.is_zero() {
138+
return;
139+
}
139140

140-
Pools::<T>::mutate(pool, |pool_info| {
141-
let proportion = FixedU128::checked_from_rational(remove_amount, *share)
142-
.expect("share is gte remove_amount and not zero which checked before; qed");
143-
let withdrawn_rewards_to_remove = proportion.saturating_mul_int(*withdrawn_rewards);
141+
Pools::<T>::mutate(pool, |pool_info| {
142+
let proportion = FixedU128::checked_from_rational(remove_amount, share)
143+
.expect("share is gte remove_amount and not zero which checked before; qed");
144+
let withdrawn_rewards_to_remove = proportion.saturating_mul_int(withdrawn_rewards);
144145

145-
pool_info.total_shares = pool_info.total_shares.saturating_sub(remove_amount);
146-
pool_info.total_rewards = pool_info.total_rewards.saturating_sub(withdrawn_rewards_to_remove);
147-
pool_info.total_withdrawn_rewards = pool_info
148-
.total_withdrawn_rewards
149-
.saturating_sub(withdrawn_rewards_to_remove);
146+
pool_info.total_shares = pool_info.total_shares.saturating_sub(remove_amount);
147+
pool_info.total_rewards = pool_info.total_rewards.saturating_sub(withdrawn_rewards_to_remove);
148+
pool_info.total_withdrawn_rewards = pool_info
149+
.total_withdrawn_rewards
150+
.saturating_sub(withdrawn_rewards_to_remove);
150151

151-
*withdrawn_rewards = withdrawn_rewards.saturating_sub(withdrawn_rewards_to_remove);
152-
});
152+
withdrawn_rewards = withdrawn_rewards.saturating_sub(withdrawn_rewards_to_remove);
153+
});
153154

154-
*share = share.saturating_sub(remove_amount);
155+
share = share.saturating_sub(remove_amount);
156+
if share != Zero::zero() {
157+
*share_info = Some((share, withdrawn_rewards));
158+
}
159+
}
155160
});
156161
}
157162

0 commit comments

Comments
 (0)