fn do_reward_stakers(
        validator_stash: T::AccountId,
        era: EraIndex,
    ) -> DispatchResult {
        // 1. Validate input data
        let current_era = CurrentEra::get().ok_or(Error::<T>::InvalidEraToReward)?;
        ensure!(era <= current_era, Error::<T>::InvalidEraToReward);
        let history_depth = Self::history_depth();
        ensure!(era >= current_era.saturating_sub(history_depth), Error::<T>::InvalidEraToReward);

        // Note: if era has no reward to be claimed, era may be future. better not to update
        // `ledger.claimed_rewards` in this case.
        let total_era_staking_payout = <ErasStakingPayout<T>>::get(&era)
            .ok_or_else(|| Error::<T>::InvalidEraToReward)?;

        let controller = Self::bonded(&validator_stash).ok_or(Error::<T>::NotStash)?;
        let mut ledger = <Ledger<T>>::get(&controller).ok_or_else(|| Error::<T>::NotController)?;

        ledger.claimed_rewards.retain(|&x| x >= current_era.saturating_sub(history_depth));
        match ledger.claimed_rewards.binary_search(&era) {
            Ok(_) => Err(Error::<T>::AlreadyClaimed)?,
            Err(pos) => ledger.claimed_rewards.insert(pos, era),
        }
        /* Input data seems good, no errors allowed after this point */
        let exposure = <ErasStakersClipped<T>>::get(&era, &ledger.stash);
        <Ledger<T>>::insert(&controller, &ledger);

        // 2. Pay authoring reward
        let mut validator_imbalance = <PositiveImbalanceOf<T>>::zero();
        let mut total_reward: BalanceOf<T> = Zero::zero();
        if let Some(authoring_reward) = <ErasAuthoringPayout<T>>::get(&era, &validator_stash) {
            total_reward = total_reward.saturating_add(authoring_reward);
        }

        let to_num =
        |b: BalanceOf<T>| <T::CurrencyToVote as Convert<BalanceOf<T>, u64>>::convert(b);

        // 3. Retrieve total stakes and total staking reward
        let era_total_stakes = <ErasTotalStakes<T>>::get(&era);
        let staking_reward = Perbill::from_rational_approximation(to_num(exposure.total), to_num(era_total_stakes)) * total_era_staking_payout;
        total_reward = total_reward.saturating_add(staking_reward);
        let total = exposure.total.max(One::one());
        // 4. Calculate guarantee rewards for staking
        let estimated_guarantee_rewards = <ErasValidatorPrefs<T>>::get(&era, &ledger.stash).fee * total_reward;
        let mut guarantee_rewards = Zero::zero();
        // 5. Pay staking reward to guarantors
        for i in &exposure.others {
            let reward_ratio = Perbill::from_rational_approximation(i.value, total);
            // Reward guarantors
            guarantee_rewards += reward_ratio * estimated_guarantee_rewards;
            if let Some(imbalance) = Self::make_payout(
                &i.who,
                reward_ratio * estimated_guarantee_rewards
            ) {
                Self::deposit_event(RawEvent::Reward(i.who.clone(), imbalance.peek()));
            };
        }
        // 6. Pay staking reward to validator
        validator_imbalance.maybe_subsume(Self::make_payout(&ledger.stash, total_reward - guarantee_rewards));
        Self::deposit_event(RawEvent::Reward(ledger.stash, validator_imbalance.peek()));
        Ok(())
    }
/// Pay reward to stakers. Two kinds of reward.
/// One is authoring reward which is paid to validator who are elected.
/// Another one is staking reward.
Pay reward to stakers. Two kinds of reward.
One is authoring reward which is paid to validator who are elected.
Another one is staking reward.
Pay reward to stakers . Two kinds of reward . One is authoring reward which is paid to validator who are elected . Another one is staking reward .
fn do_reward_stakers(
        validator_stash: T::AccountId,
        era: EraIndex,
    ) -> DispatchResult {
        // 1. Validate input data
        let current_era = CurrentEra::get().ok_or(Error::<T>::InvalidEraToReward)?;
        ensure!(era <= current_era, Error::<T>::InvalidEraToReward);
        let history_depth = Self::history_depth();
        ensure!(era >= current_era.saturating_sub(history_depth), Error::<T>::InvalidEraToReward);
        // Note: if era has no reward to be claimed, era may be future. better not to update
        // `ledger.claimed_rewards` in this case.
        let total_era_staking_payout = <ErasStakingPayout<T>>::get(&era)
            .ok_or_else(|| Error::<T>::InvalidEraToReward)?;
        let controller = Self::bonded(&validator_stash).ok_or(Error::<T>::NotStash)?;
        let mut ledger = <Ledger<T>>::get(&controller).ok_or_else(|| Error::<T>::NotController)?;
        ledger.claimed_rewards.retain(|&x| x >= current_era.saturating_sub(history_depth));
        match ledger.claimed_rewards.binary_search(&era) {
            Ok(_) => Err(Error::<T>::AlreadyClaimed)?,
            Err(pos) => ledger.claimed_rewards.insert(pos, era),
        }
        /* Input data seems good, no errors allowed after this point */
        let exposure = <ErasStakersClipped<T>>::get(&era, &ledger.stash);
        <Ledger<T>>::insert(&controller, &ledger);
        // 2. Pay authoring reward
        let mut validator_imbalance = <PositiveImbalanceOf<T>>::zero();
        let mut total_reward: BalanceOf<T> = Zero::zero();
        if let Some(authoring_reward) = <ErasAuthoringPayout<T>>::get(&era, &validator_stash) {
            total_reward = total_reward.saturating_add(authoring_reward);
        }
        let to_num =
        |b: BalanceOf<T>| <T::CurrencyToVote as Convert<BalanceOf<T>, u64>>::convert(b);
        // 3. Retrieve total stakes and total staking reward
        let era_total_stakes = <ErasTotalStakes<T>>::get(&era);
        let staking_reward = Perbill::from_rational_approximation(to_num(exposure.total), to_num(era_total_stakes)) * total_era_staking_payout;
        total_reward = total_reward.saturating_add(staking_reward);
        let total = exposure.total.max(One::one());
        // 4. Calculate guarantee rewards for staking
        let estimated_guarantee_rewards = <ErasValidatorPrefs<T>>::get(&era, &ledger.stash).fee * total_reward;
        let mut guarantee_rewards = Zero::zero();
        // 5. Pay staking reward to guarantors
        for i in &exposure.others {
            let reward_ratio = Perbill::from_rational_approximation(i.value, total);
            // Reward guarantors
            guarantee_rewards += reward_ratio * estimated_guarantee_rewards;
            if let Some(imbalance) = Self::make_payout(
                &i.who,
                reward_ratio * estimated_guarantee_rewards
            ) {
                Self::deposit_event(RawEvent::Reward(i.who.clone(), imbalance.peek()));
            };
        }
        // 6. Pay staking reward to validator
        validator_imbalance.maybe_subsume(Self::make_payout(&ledger.stash, total_reward - guarantee_rewards));
        Self::deposit_event(RawEvent::Reward(ledger.stash, validator_imbalance.peek()));
        Ok(())
    }
fn do_reward_stakers ( validator_stash : T :: AccountId , era : EraIndex , ) -> DispatchResult { // 1. Validate input data let current_era = CurrentEra :: get ( ) . ok_or ( Error :: < T > :: InvalidEraToReward ) ? ; ensure ! ( era current_era Error T InvalidEraToReward ) ; let history_depth = Self :: history_depth ( ) ; ensure ! ( era current_era saturating_sub ( history_depth ) Error T InvalidEraToReward ) ; // Note: if era has no reward to be claimed, era may be future. better not to update // `ledger.claimed_rewards` in this case. let total_era_staking_payout = < ErasStakingPayout < T > > :: get ( & era ) . ok_or_else ( | | Error :: < T > :: InvalidEraToReward ) ? ; let controller = Self :: bonded ( & validator_stash ) . ok_or ( Error :: < T > :: NotStash ) ? ; let mut ledger = < Ledger < T > > :: get ( & controller ) . ok_or_else ( | | Error :: < T > :: NotController ) ? ; ledger . claimed_rewards . retain ( | & x | x >= current_era . saturating_sub ( history_depth ) ) ; match ledger . claimed_rewards . binary_search ( & era ) { Ok ( _ ) => Err ( Error :: < T > :: AlreadyClaimed ) ? , Err ( pos ) => ledger . claimed_rewards . insert ( pos , era ) , } /* Input data seems good, no errors allowed after this point */ let exposure = < ErasStakersClipped < T > > :: get ( & era , & ledger . stash ) ; < Ledger < T > > :: insert ( & controller , & ledger ) ; // 2. Pay authoring reward let mut validator_imbalance = < PositiveImbalanceOf < T > > :: zero ( ) ; let mut total_reward : BalanceOf < T > = Zero :: zero ( ) ; if let Some ( authoring_reward ) = < ErasAuthoringPayout < T > > :: get ( & era , & validator_stash ) { total_reward = total_reward . saturating_add ( authoring_reward ) ; } let to_num = | b : BalanceOf < T > | < T :: CurrencyToVote as Convert < BalanceOf < T > , u64 > > :: convert ( b ) ; // 3. Retrieve total stakes and total staking reward let era_total_stakes = < ErasTotalStakes < T > > :: get ( & era ) ; let staking_reward = Perbill :: from_rational_approximation ( to_num ( exposure . total ) , to_num ( era_total_stakes ) ) * total_era_staking_payout ; total_reward = total_reward . saturating_add ( staking_reward ) ; let total = exposure . total . max ( One :: one ( ) ) ; // 4. Calculate guarantee rewards for staking let estimated_guarantee_rewards = < ErasValidatorPrefs < T > > :: get ( & era , & ledger . stash ) . fee * total_reward ; let mut guarantee_rewards = Zero :: zero ( ) ; // 5. Pay staking reward to guarantors for i in & exposure . others { let reward_ratio = Perbill :: from_rational_approximation ( i . value , total ) ; // Reward guarantors guarantee_rewards += reward_ratio * estimated_guarantee_rewards ; if let Some ( imbalance ) = Self :: make_payout ( & i . who , reward_ratio * estimated_guarantee_rewards ) { Self :: deposit_event ( RawEvent :: Reward ( i . who . clone ( ) , imbalance . peek ( ) ) ) ; } ; } // 6. Pay staking reward to validator validator_imbalance . maybe_subsume ( Self :: make_payout ( & ledger . stash , total_reward - guarantee_rewards ) ) ; Self :: deposit_event ( RawEvent :: Reward ( ledger . stash , validator_imbalance . peek ( ) ) ) ; Ok ( ( ) ) }
Pay reward to stakers.
Pay reward to stakers .
validator_stash
T::AccountId
era
EraIndex