Skip to content

Commit 48fd42c

Browse files
committed
tests passing
1 parent 4c805c1 commit 48fd42c

File tree

6 files changed

+411
-357
lines changed

6 files changed

+411
-357
lines changed

pallets/capacity/src/lib.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
rustdoc::invalid_codeblock_attributes,
4949
missing_docs
5050
)]
51+
use sp_std::ops::{Add, Mul};
5152

5253
use frame_support::{
5354
dispatch::DispatchResult,
@@ -60,7 +61,6 @@ use sp_runtime::{
6061
traits::{CheckedAdd, CheckedDiv, One, Saturating, Zero},
6162
ArithmeticError, DispatchError, Perbill,
6263
};
63-
use sp_std::ops::Mul;
6464

6565
pub use common_primitives::{
6666
capacity::{Nontransferable, Replenishable, TargetValidator},
@@ -768,31 +768,34 @@ impl<T: Config> Pallet<T> {
768768

769769
fn start_new_reward_era_if_needed(current_block: T::BlockNumber) -> Weight {
770770
let current_era_info: RewardEraInfo<T::RewardEra, T::BlockNumber> = Self::get_current_era(); // 1r
771-
if current_block.saturating_sub(current_era_info.started_at) >= T::EraLength::get().into() { // 1r
771+
if current_block.saturating_sub(current_era_info.started_at) >= T::EraLength::get().into() {
772+
// 1r
772773
let new_era_info = RewardEraInfo {
773774
era_index: current_era_info.era_index.saturating_add(One::one()),
774775
started_at: current_block,
775776
};
776777

777-
let current_reward_pool = Self::get_reward_pool_for_era(current_era_info.era_index).unwrap_or_default(); // 1r
778-
let past_eras_max = T::StakingRewardsPastErasMax::get();
778+
let current_reward_pool_info =
779+
Self::get_reward_pool_for_era(current_era_info.era_index).unwrap_or_default(); // 1r
780+
let past_eras_max = T::StakingRewardsPastErasMax::get();
779781
let entries: u32 = StakingRewardPool::<T>::count();
780-
if past_eras_max.eq(&entries.into()) { // 2r
782+
if past_eras_max.eq(&entries.into()) {
783+
// 2r
781784
let current_era = Self::get_current_era().era_index;
782-
let earliest_era = current_era.saturating_sub(past_eras_max);
785+
let earliest_era = current_era.saturating_sub(past_eras_max).add(One::one());
783786
StakingRewardPool::<T>::remove(earliest_era); // 1w
784787
}
785-
CurrentEraInfo::<T>::set(new_era_info); // 1w
788+
CurrentEraInfo::<T>::set(new_era_info); // 1w
786789

787790
// let msa_handle = T::HandleProvider::get_handle_for_msa(msa_id);
788791
let total_reward_pool =
789-
T::RewardsProvider::reward_pool_size(current_reward_pool.total_staked_token.clone());
792+
T::RewardsProvider::reward_pool_size(current_reward_pool_info.total_staked_token);
790793
let new_reward_pool = RewardPoolInfo {
791-
total_staked_token: current_reward_pool.total_staked_token,
794+
total_staked_token: current_reward_pool_info.total_staked_token,
792795
total_reward_pool,
793-
unclaimed_balance: total_reward_pool.clone(),
796+
unclaimed_balance: total_reward_pool,
794797
};
795-
StakingRewardPool::<T>::insert(new_era_info.era_index.clone(), new_reward_pool); // 1w
798+
StakingRewardPool::<T>::insert(new_era_info.era_index, new_reward_pool); // 1w
796799

797800
T::WeightInfo::on_initialize()
798801
.saturating_add(T::DbWeight::get().reads(5))
@@ -839,10 +842,13 @@ impl<T: Config> Pallet<T> {
839842

840843
let mut to_msa_target = Self::get_target_for(staker, to_msa).unwrap_or_default();
841844

842-
if to_msa_target.amount.is_zero() {
845+
if to_msa_target.amount.is_zero() {
843846
to_msa_target.staking_type = staking_type.clone();
844847
} else {
845-
ensure!(to_msa_target.staking_type.ne(staking_type), Error::<T>::CannotChangeStakingType);
848+
ensure!(
849+
to_msa_target.staking_type.eq(staking_type),
850+
Error::<T>::CannotChangeStakingType
851+
);
846852
}
847853
to_msa_target
848854
.deposit(*amount, capacity_withdrawn)
@@ -956,9 +962,7 @@ impl<T: Config> StakingRewardsProvider<T> for Pallet<T> {
956962
}
957963

958964
// For now reward pool size is set to 10% of total staked token
959-
total_staked
960-
.checked_div(&BalanceOf::<T>::from(10u8))
961-
.unwrap_or_default()
965+
total_staked.checked_div(&BalanceOf::<T>::from(10u8)).unwrap_or_default()
962966
}
963967

964968
// Performs range checks plus a reward calculation based on economic model for the era range

0 commit comments

Comments
 (0)