--- a/client/rpc/src/lib.rs +++ b/client/rpc/src/lib.rs @@ -424,7 +424,7 @@ client: Arc, _marker: std::marker::PhantomData

, } - + impl $name { pub fn new(client: Arc) -> Self { Self { --- a/pallets/app-promotion/src/lib.rs +++ b/pallets/app-promotion/src/lib.rs @@ -41,6 +41,7 @@ vec, iter::Sum, borrow::ToOwned, + cell::RefCell, }; use sp_core::H160; use codec::EncodeLike; @@ -474,58 +475,121 @@ let next_recalc_block = current_recalc_block + T::RecalculationInterval::get(); let mut storage_iterator = Self::get_next_calculated_key() - .map_or(Staked::::iter(), |key| { - Staked::::iter_from(key) - }); + .map_or(Staked::::iter(), |key| Staked::::iter_from(key)); NextCalculatedRecord::::set(None); + // { + // let mut stakers_number = stakers_number.unwrap_or(20); + // let mut last_id = admin_id; + // let mut income_acc = BalanceOf::::default(); + // let mut amount_acc = BalanceOf::::default(); + + // while let Some(( + // (current_id, staked_block), + // (amount, next_recalc_block_for_stake), + // )) = storage_iterator.next() + // { + // if last_id != current_id { + // if income_acc != BalanceOf::::default() { + // >::transfer( + // &T::TreasuryAccountId::get(), + // &last_id, + // income_acc, + // ExistenceRequirement::KeepAlive, + // ) + // .and_then(|_| Self::add_lock_balance(&last_id, income_acc))?; + + // Self::deposit_event(Event::StakingRecalculation( + // last_id, amount, income_acc, + // )); + // } + + // if stakers_number == 0 { + // NextCalculatedRecord::::set(Some((current_id, staked_block))); + // break; + // } + // stakers_number -= 1; + // income_acc = BalanceOf::::default(); + // last_id = current_id; + // }; + // if current_recalc_block >= next_recalc_block_for_stake { + // Self::recalculate_and_insert_stake( + // &last_id, + // staked_block, + // next_recalc_block, + // amount, + // ((current_recalc_block - next_recalc_block_for_stake) + // / T::RecalculationInterval::get()) + // .into() + 1, + // &mut income_acc, + // ); + // } + // } + // } + { let mut stakers_number = stakers_number.unwrap_or(20); - let mut last_id = admin_id; - let mut income_acc = BalanceOf::::default(); - - while let Some(((current_id, staked_block), (amount, next_recalc_block_for_stake))) = - storage_iterator.next() - { - if last_id != current_id { - if income_acc != BalanceOf::::default() { + let last_id = RefCell::new(None); + let income_acc = RefCell::new(BalanceOf::::default()); + let amount_acc = RefCell::new(BalanceOf::::default()); + + let flush_stake = || -> DispatchResult { + if let Some(last_id) = &*last_id.borrow() { + if !income_acc.borrow().is_zero() { >::transfer( &T::TreasuryAccountId::get(), - &last_id, - income_acc, + last_id, + *income_acc.borrow(), ExistenceRequirement::KeepAlive, ) - .and_then(|_| Self::add_lock_balance(&last_id, income_acc))?; + .and_then(|_| Self::add_lock_balance(last_id, *income_acc.borrow()))?; Self::deposit_event(Event::StakingRecalculation( - last_id, amount, income_acc, + last_id.clone(), + *amount_acc.borrow(), + *income_acc.borrow(), )); } - if stakers_number == 0 { - NextCalculatedRecord::::set(Some((current_id, staked_block))); - break; - } - stakers_number -= 1; - income_acc = BalanceOf::::default(); - last_id = current_id; + *income_acc.borrow_mut() = BalanceOf::::default(); + *amount_acc.borrow_mut() = BalanceOf::::default(); + } + Ok(()) + }; + + while let Some(( + (current_id, staked_block), + (amount, next_recalc_block_for_stake), + )) = storage_iterator.next() + { + if stakers_number == 0 { + NextCalculatedRecord::::set(Some((current_id, staked_block))); + break; + } + stakers_number -= 1; + if last_id.borrow().as_ref() != Some(¤t_id) { + flush_stake()?; }; + *last_id.borrow_mut() = Some(current_id.clone()); if current_recalc_block >= next_recalc_block_for_stake { + *amount_acc.borrow_mut() += amount; Self::recalculate_and_insert_stake( - &last_id, + ¤t_id, staked_block, next_recalc_block, amount, ((current_recalc_block - next_recalc_block_for_stake) / T::RecalculationInterval::get()) .into() + 1, - &mut income_acc, + &mut *income_acc.borrow_mut(), ); } } + flush_stake()?; } + Ok(()) } } --- a/runtime/common/config/pallets/app_promotion.rs +++ b/runtime/common/config/pallets/app_promotion.rs @@ -30,7 +30,7 @@ parameter_types! { pub const AppPromotionId: PalletId = PalletId(*b"appstake"); pub const RecalculationInterval: BlockNumber = 20; - pub const PendingInterval: BlockNumber = 20; + pub const PendingInterval: BlockNumber = 10; pub const Nominal: Balance = UNIQUE; pub const Day: BlockNumber = DAYS; pub IntervalIncome: Perbill = Perbill::from_rational(RecalculationInterval::get(), RELAY_DAYS) * Perbill::from_rational(5u32, 10_000);