Skip to content

migrate to mutate_exist #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 1, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,33 @@ impl<T: Config> Pallet<T> {
// claim rewards firstly
Self::claim_rewards(who, pool);

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

if remove_amount.is_zero() {
return;
}
if remove_amount.is_zero() {
return;
}

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

pool_info.total_shares = pool_info.total_shares.saturating_sub(remove_amount);
pool_info.total_rewards = pool_info.total_rewards.saturating_sub(withdrawn_rewards_to_remove);
pool_info.total_withdrawn_rewards = pool_info
.total_withdrawn_rewards
.saturating_sub(withdrawn_rewards_to_remove);
pool_info.total_shares = pool_info.total_shares.saturating_sub(remove_amount);
pool_info.total_rewards = pool_info.total_rewards.saturating_sub(withdrawn_rewards_to_remove);
pool_info.total_withdrawn_rewards = pool_info
.total_withdrawn_rewards
.saturating_sub(withdrawn_rewards_to_remove);

*withdrawn_rewards = withdrawn_rewards.saturating_sub(withdrawn_rewards_to_remove);
});
withdrawn_rewards = withdrawn_rewards.saturating_sub(withdrawn_rewards_to_remove);
});

*share = share.saturating_sub(remove_amount);
share = share.saturating_sub(remove_amount);
if share != Zero::zero() {
*share_info = Some((share, withdrawn_rewards));
}
}
});
}

Expand Down