@@ -512,6 +512,75 @@ fn payout_works() {
512512 } ) ;
513513}
514514
515+ #[ test]
516+ fn transfer_failed_when_claim_rewards ( ) {
517+ ExtBuilder :: default ( ) . build ( ) . execute_with ( || {
518+ assert_ok ! ( TokensModule :: deposit( AUSD , & VAULT :: get( ) , 100 ) ) ;
519+ RewardsModule :: add_share ( & ALICE :: get ( ) , & PoolId :: Loans ( BTC ) , 100 ) ;
520+ RewardsModule :: add_share ( & BOB :: get ( ) , & PoolId :: Loans ( BTC ) , 100 ) ;
521+ assert_ok ! ( RewardsModule :: accumulate_reward( & PoolId :: Loans ( BTC ) , AUSD , 18 ) ) ;
522+
523+ assert_eq ! ( TokensModule :: free_balance( AUSD , & VAULT :: get( ) ) , 100 ) ;
524+ assert_eq ! ( TokensModule :: free_balance( AUSD , & ALICE :: get( ) ) , 0 ) ;
525+ assert_eq ! (
526+ RewardsModule :: pool_infos( PoolId :: Loans ( BTC ) ) ,
527+ PoolInfo {
528+ total_shares: 200 ,
529+ rewards: vec![ ( AUSD , ( 18 , 0 ) ) ] . into_iter( ) . collect( ) ,
530+ }
531+ ) ;
532+ assert_eq ! (
533+ RewardsModule :: shares_and_withdrawn_rewards( PoolId :: Loans ( BTC ) , ALICE :: get( ) ) ,
534+ ( 100 , Default :: default ( ) )
535+ ) ;
536+
537+ // Alice claim rewards, but the rewards are put back to pool because transfer rewards failed.
538+ assert_ok ! ( IncentivesModule :: claim_rewards(
539+ Origin :: signed( ALICE :: get( ) ) ,
540+ PoolId :: Loans ( BTC )
541+ ) ) ;
542+
543+ assert_eq ! ( TokensModule :: free_balance( AUSD , & VAULT :: get( ) ) , 100 ) ;
544+ assert_eq ! ( TokensModule :: free_balance( AUSD , & ALICE :: get( ) ) , 0 ) ;
545+ assert_eq ! (
546+ RewardsModule :: pool_infos( PoolId :: Loans ( BTC ) ) ,
547+ PoolInfo {
548+ total_shares: 200 ,
549+ rewards: vec![ ( AUSD , ( 27 , 9 ) ) ] . into_iter( ) . collect( ) ,
550+ }
551+ ) ;
552+ assert_eq ! (
553+ RewardsModule :: shares_and_withdrawn_rewards( PoolId :: Loans ( BTC ) , ALICE :: get( ) ) ,
554+ ( 100 , vec![ ( AUSD , 9 ) ] . into_iter( ) . collect( ) )
555+ ) ;
556+
557+ assert_eq ! ( TokensModule :: free_balance( AUSD , & BOB :: get( ) ) , 0 ) ;
558+ assert_eq ! (
559+ RewardsModule :: shares_and_withdrawn_rewards( PoolId :: Loans ( BTC ) , BOB :: get( ) ) ,
560+ ( 100 , Default :: default ( ) )
561+ ) ;
562+
563+ // BOB claim reward and receive the reward.
564+ assert_ok ! ( IncentivesModule :: claim_rewards(
565+ Origin :: signed( BOB :: get( ) ) ,
566+ PoolId :: Loans ( BTC )
567+ ) ) ;
568+ assert_eq ! ( TokensModule :: free_balance( AUSD , & VAULT :: get( ) ) , 87 ) ;
569+ assert_eq ! ( TokensModule :: free_balance( AUSD , & BOB :: get( ) ) , 13 ) ;
570+ assert_eq ! (
571+ RewardsModule :: pool_infos( PoolId :: Loans ( BTC ) ) ,
572+ PoolInfo {
573+ total_shares: 200 ,
574+ rewards: vec![ ( AUSD , ( 27 , 22 ) ) ] . into_iter( ) . collect( ) ,
575+ }
576+ ) ;
577+ assert_eq ! (
578+ RewardsModule :: shares_and_withdrawn_rewards( PoolId :: Loans ( BTC ) , BOB :: get( ) ) ,
579+ ( 100 , vec![ ( AUSD , 13 ) ] . into_iter( ) . collect( ) )
580+ ) ;
581+ } ) ;
582+ }
583+
515584#[ test]
516585fn claim_rewards_works ( ) {
517586 ExtBuilder :: default ( ) . build ( ) . execute_with ( || {
0 commit comments