@@ -272,34 +272,92 @@ impl<T: Config> Pallet<T> {
272
272
let total_shares = U256 :: from ( pool_info. total_shares . to_owned ( ) . saturated_into :: < u128 > ( ) ) ;
273
273
pool_info. rewards . iter_mut ( ) . for_each (
274
274
|( reward_currency, ( total_reward, total_withdrawn_reward) ) | {
275
- let withdrawn_reward = withdrawn_rewards. get ( reward_currency) . copied ( ) . unwrap_or_default ( ) ;
276
-
277
- let total_reward_proportion: T :: Balance =
278
- U256 :: from ( share. to_owned ( ) . saturated_into :: < u128 > ( ) )
279
- . saturating_mul ( U256 :: from ( total_reward. to_owned ( ) . saturated_into :: < u128 > ( ) ) )
280
- . checked_div ( total_shares)
281
- . unwrap_or_default ( )
282
- . as_u128 ( )
283
- . unique_saturated_into ( ) ;
284
-
285
- let reward_to_withdraw = total_reward_proportion
286
- . saturating_sub ( withdrawn_reward)
287
- . min ( total_reward. saturating_sub ( * total_withdrawn_reward) ) ;
288
-
289
- if reward_to_withdraw. is_zero ( ) {
290
- return ;
291
- }
292
-
293
- * total_withdrawn_reward = total_withdrawn_reward. saturating_add ( reward_to_withdraw) ;
294
- withdrawn_rewards
295
- . insert ( * reward_currency, withdrawn_reward. saturating_add ( reward_to_withdraw) ) ;
296
-
297
- // pay reward to `who`
298
- T :: Handler :: payout ( who, pool, * reward_currency, reward_to_withdraw) ;
275
+ Self :: claim_one (
276
+ withdrawn_rewards,
277
+ * reward_currency,
278
+ share,
279
+ total_reward,
280
+ total_shares,
281
+ total_withdrawn_reward,
282
+ who,
283
+ pool,
284
+ ) ;
299
285
} ,
300
286
) ;
301
287
} ) ;
302
288
}
303
289
} ) ;
304
290
}
291
+
292
+ pub fn claim_reward ( who : & T :: AccountId , pool : & T :: PoolId , reward_currency : T :: CurrencyId ) {
293
+ SharesAndWithdrawnRewards :: < T > :: mutate_exists ( pool, who, |maybe_share_withdrawn| {
294
+ if let Some ( ( share, withdrawn_rewards) ) = maybe_share_withdrawn {
295
+ if share. is_zero ( ) {
296
+ return ;
297
+ }
298
+
299
+ PoolInfos :: < T > :: mutate ( pool, |pool_info| {
300
+ let total_shares = U256 :: from ( pool_info. total_shares . to_owned ( ) . saturated_into :: < u128 > ( ) ) ;
301
+ if let Some ( ( total_reward, total_withdrawn_reward) ) = pool_info. rewards . get_mut ( & reward_currency) {
302
+ Self :: claim_one (
303
+ withdrawn_rewards,
304
+ reward_currency,
305
+ share,
306
+ total_reward,
307
+ total_shares,
308
+ total_withdrawn_reward,
309
+ who,
310
+ pool,
311
+ ) ;
312
+ }
313
+ } ) ;
314
+ }
315
+ } ) ;
316
+ }
317
+
318
+ #[ allow( clippy:: too_many_arguments) ] // just we need to have all these to do the stuff
319
+ fn claim_one (
320
+ withdrawn_rewards : & mut BTreeMap < T :: CurrencyId , T :: Balance > ,
321
+ reward_currency : T :: CurrencyId ,
322
+ share : & mut T :: Share ,
323
+ total_reward : & mut T :: Balance ,
324
+ total_shares : U256 ,
325
+ total_withdrawn_reward : & mut T :: Balance ,
326
+ who : & T :: AccountId ,
327
+ pool : & T :: PoolId ,
328
+ ) {
329
+ let withdrawn_reward = withdrawn_rewards. get ( & reward_currency) . copied ( ) . unwrap_or_default ( ) ;
330
+ let reward_to_withdraw = Self :: reward_to_withdraw (
331
+ share,
332
+ total_reward,
333
+ total_shares,
334
+ withdrawn_reward,
335
+ total_withdrawn_reward,
336
+ ) ;
337
+ if !reward_to_withdraw. is_zero ( ) {
338
+ * total_withdrawn_reward = total_withdrawn_reward. saturating_add ( reward_to_withdraw) ;
339
+ withdrawn_rewards. insert ( reward_currency, withdrawn_reward. saturating_add ( reward_to_withdraw) ) ;
340
+
341
+ // pay reward to `who`
342
+ T :: Handler :: payout ( who, pool, reward_currency, reward_to_withdraw) ;
343
+ }
344
+ }
345
+
346
+ fn reward_to_withdraw (
347
+ share : & mut T :: Share ,
348
+ total_reward : & mut T :: Balance ,
349
+ total_shares : U256 ,
350
+ withdrawn_reward : T :: Balance ,
351
+ total_withdrawn_reward : & mut T :: Balance ,
352
+ ) -> T :: Balance {
353
+ let total_reward_proportion: T :: Balance = U256 :: from ( share. to_owned ( ) . saturated_into :: < u128 > ( ) )
354
+ . saturating_mul ( U256 :: from ( total_reward. to_owned ( ) . saturated_into :: < u128 > ( ) ) )
355
+ . checked_div ( total_shares)
356
+ . unwrap_or_default ( )
357
+ . as_u128 ( )
358
+ . unique_saturated_into ( ) ;
359
+ total_reward_proportion
360
+ . saturating_sub ( withdrawn_reward)
361
+ . min ( total_reward. saturating_sub ( * total_withdrawn_reward) )
362
+ }
305
363
}
0 commit comments