@@ -130,28 +130,33 @@ impl<T: Config> Pallet<T> {
130
130
// claim rewards firstly
131
131
Self :: claim_rewards ( who, pool) ;
132
132
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) ;
135
136
136
- if remove_amount. is_zero ( ) {
137
- return ;
138
- }
137
+ if remove_amount. is_zero ( ) {
138
+ return ;
139
+ }
139
140
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) ;
144
145
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) ;
150
151
151
- * withdrawn_rewards = withdrawn_rewards. saturating_sub ( withdrawn_rewards_to_remove) ;
152
- } ) ;
152
+ withdrawn_rewards = withdrawn_rewards. saturating_sub ( withdrawn_rewards_to_remove) ;
153
+ } ) ;
153
154
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
+ }
155
160
} ) ;
156
161
}
157
162
0 commit comments