@@ -102,7 +102,9 @@ pub mod pallet {
102102 use frame_system:: pallet_prelude:: * ;
103103
104104 #[ pallet:: config]
105- pub trait Config : frame_system:: Config + pallet_evm:: account:: Config {
105+ pub trait Config :
106+ frame_system:: Config + pallet_evm:: account:: Config + pallet_configuration:: Config
107+ {
106108 /// Type to interact with the native token
107109 type Currency : ExtendedLockableCurrency < Self :: AccountId >
108110 + ReservableCurrency < Self :: AccountId > ;
@@ -253,11 +255,11 @@ pub mod pallet {
253255 ValueQuery ,
254256 > ;
255257
256- /// Stores a key for record for which the next revenue recalculation would be performed.
258+ /// Stores a key for record for which the revenue recalculation was performed.
257259 /// If `None`, then recalculation has not yet been performed or calculations have been completed for all stakers.
258260 #[ pallet:: storage]
259261 #[ pallet:: getter( fn get_next_calculated_record) ]
260- pub type NextCalculatedRecord < T : Config > =
262+ pub type PreviousCalculatedRecord < T : Config > =
261263 StorageValue < Value = ( T :: AccountId , T :: BlockNumber ) , QueryKind = OptionQuery > ;
262264
263265 #[ pallet:: hooks]
@@ -328,6 +330,7 @@ pub mod pallet {
328330 amount >= <BalanceOf <T >>:: from( 100u128 ) * T :: Nominal :: get( ) ,
329331 ArithmeticError :: Underflow
330332 ) ;
333+ let config = <PalletConfiguration < T > >:: get ( ) ;
331334
332335 let balance =
333336 <<T as Config >:: Currency as Currency < T :: AccountId > >:: free_balance ( & staker_id) ;
@@ -349,17 +352,17 @@ pub mod pallet {
349352 // Calculation of the number of recalculation periods,
350353 // after how much the first interest calculation should be performed for the stake
351354 let recalculate_after_interval: T :: BlockNumber =
352- if block_number % T :: RecalculationInterval :: get ( ) == 0u32 . into ( ) {
355+ if block_number % config . recalculation_interval == 0u32 . into ( ) {
353356 1u32 . into ( )
354357 } else {
355358 2u32 . into ( )
356359 } ;
357360
358361 // Сalculation of the number of the relay block
359362 // in which it is necessary to accrue remuneration for the stake.
360- let recalc_block = ( block_number / T :: RecalculationInterval :: get ( )
363+ let recalc_block = ( block_number / config . recalculation_interval
361364 + recalculate_after_interval)
362- * T :: RecalculationInterval :: get ( ) ;
365+ * config . recalculation_interval ;
363366
364367 <Staked < T > >:: insert ( ( & staker_id, block_number) , {
365368 let mut balance_and_recalc_block = <Staked < T > >:: get ( ( & staker_id, block_number) ) ;
@@ -391,9 +394,10 @@ pub mod pallet {
391394 #[ pallet:: weight( T :: WeightInfo :: unstake( ) ) ]
392395 pub fn unstake ( staker : OriginFor < T > ) -> DispatchResultWithPostInfo {
393396 let staker_id = ensure_signed ( staker) ?;
397+ let config = <PalletConfiguration < T > >:: get ( ) ;
394398
395399 // calculate block number where the sum would be free
396- let block = <frame_system:: Pallet < T > >:: block_number ( ) + T :: PendingInterval :: get ( ) ;
400+ let block = <frame_system:: Pallet < T > >:: block_number ( ) + config . pending_interval ;
397401
398402 let mut pendings = <PendingUnstake < T > >:: get ( block) ;
399403
@@ -555,31 +559,40 @@ pub mod pallet {
555559 /// # Arguments
556560 ///
557561 /// * `stakers_number`: the number of stakers for which recalculation will be performed
558- #[ pallet:: weight( T :: WeightInfo :: payout_stakers( stakers_number. unwrap_or( 20 ) as u32 ) ) ]
562+ #[ pallet:: weight( T :: WeightInfo :: payout_stakers( stakers_number. unwrap_or( DEFAULT_NUMBER_PAYOUTS ) as u32 ) ) ]
559563 pub fn payout_stakers ( admin : OriginFor < T > , stakers_number : Option < u8 > ) -> DispatchResult {
560564 let admin_id = ensure_signed ( admin) ?;
561565
562566 ensure ! (
563567 admin_id == Admin :: <T >:: get( ) . ok_or( Error :: <T >:: AdminNotSet ) ?,
564568 Error :: <T >:: NoPermission
565569 ) ;
570+ let config = <PalletConfiguration < T > >:: get ( ) ;
571+
572+ let mut stakers_number = stakers_number. unwrap_or ( DEFAULT_NUMBER_PAYOUTS ) ;
573+
574+ ensure ! (
575+ stakers_number <= config. max_stakers_per_calculation && stakers_number != 0 ,
576+ Error :: <T >:: NoPermission
577+ ) ;
566578
567579 // calculate the number of the current recalculation block,
568580 // this is necessary in order to understand which stakers we should calculate interest
569- let current_recalc_block =
570- Self :: get_current_recalc_block ( T :: RelayBlockNumberProvider :: current_block_number ( ) ) ;
581+ let current_recalc_block = Self :: get_current_recalc_block (
582+ T :: RelayBlockNumberProvider :: current_block_number ( ) ,
583+ & config,
584+ ) ;
571585
572586 // calculate the number of the next recalculation block,
573587 // this value is set for the stakers to whom the recalculation will be performed
574- let next_recalc_block = current_recalc_block + T :: RecalculationInterval :: get ( ) ;
588+ let next_recalc_block = current_recalc_block + config . recalculation_interval ;
575589
576590 let mut storage_iterator = Self :: get_next_calculated_key ( )
577591 . map_or ( Staked :: < T > :: iter ( ) , |key| Staked :: < T > :: iter_from ( key) ) ;
578592
579- NextCalculatedRecord :: < T > :: set ( None ) ;
593+ PreviousCalculatedRecord :: < T > :: set ( None ) ;
580594
581595 {
582- let mut stakers_number = stakers_number. unwrap_or ( 20 ) ;
583596 let last_id = RefCell :: new ( None ) ;
584597 let income_acc = RefCell :: new ( BalanceOf :: < T > :: default ( ) ) ;
585598 let amount_acc = RefCell :: new ( BalanceOf :: < T > :: default ( ) ) ;
@@ -622,10 +635,6 @@ pub mod pallet {
622635 ( amount, next_recalc_block_for_stake) ,
623636 ) ) = storage_iterator. next ( )
624637 {
625- if stakers_number == 0 {
626- NextCalculatedRecord :: < T > :: set ( Some ( ( current_id, staked_block) ) ) ;
627- break ;
628- }
629638 if last_id. borrow ( ) . as_ref ( ) != Some ( & current_id) {
630639 flush_stake ( ) ?;
631640 * last_id. borrow_mut ( ) = Some ( current_id. clone ( ) ) ;
@@ -639,11 +648,18 @@ pub mod pallet {
639648 next_recalc_block,
640649 amount,
641650 ( ( current_recalc_block - next_recalc_block_for_stake)
642- / T :: RecalculationInterval :: get ( ) )
643- . into ( ) + 1 ,
651+ / config . recalculation_interval )
652+ . into ( ) + 1 ,
644653 & mut * income_acc. borrow_mut ( ) ,
645654 ) ;
646655 }
656+
657+ if stakers_number == 0 {
658+ if storage_iterator. next ( ) . is_some ( ) {
659+ PreviousCalculatedRecord :: < T > :: set ( Some ( ( current_id, staked_block) ) ) ;
660+ }
661+ break ;
662+ }
647663 }
648664 flush_stake ( ) ?;
649665 }
@@ -802,15 +818,19 @@ impl<T: Config> Pallet<T> {
802818 where
803819 I : EncodeLike < BalanceOf < T > > + Balance ,
804820 {
821+ let config = <PalletConfiguration < T > >:: get ( ) ;
805822 let mut income = base;
806823
807- ( 0 ..iters) . for_each ( |_| income += T :: IntervalIncome :: get ( ) * income) ;
824+ ( 0 ..iters) . for_each ( |_| income += config . interval_income * income) ;
808825
809826 income - base
810827 }
811828
812- fn get_current_recalc_block ( current_relay_block : T :: BlockNumber ) -> T :: BlockNumber {
813- ( current_relay_block / T :: RecalculationInterval :: get ( ) ) * T :: RecalculationInterval :: get ( )
829+ fn get_current_recalc_block (
830+ current_relay_block : T :: BlockNumber ,
831+ config : & PalletConfiguration < T > ,
832+ ) -> T :: BlockNumber {
833+ ( current_relay_block / config. recalculation_interval ) * config. recalculation_interval
814834 }
815835
816836 fn get_next_calculated_key ( ) -> Option < Vec < u8 > > {
0 commit comments