Skip to content

Commit

Permalink
Remaining changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Aug 29, 2024
1 parent 8b98765 commit 4c1c3a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 55 deletions.
60 changes: 9 additions & 51 deletions pallets/dapp-staking/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
//! * `DAppTierRewards` - composite of `DAppTier` objects, describing the entire reward distribution for a particular era.
//!

use frame_support::{pallet_prelude::*, BoundedBTreeMap, BoundedVec};
use frame_support::{pallet_prelude::*, BoundedBTreeMap, BoundedVec, DefaultNoBound};
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
use sp_arithmetic::fixed_point::FixedU128;
Expand Down Expand Up @@ -351,6 +351,7 @@ pub struct UnlockingChunk {
MaxEncodedLen,
RuntimeDebugNoBound,
PartialEqNoBound,
DefaultNoBound,
EqNoBound,
CloneNoBound,
TypeInfo,
Expand All @@ -375,21 +376,6 @@ pub struct AccountLedger<UnlockingLen: Get<u32>> {
pub(crate) contract_stake_count: u32,
}

impl<UnlockingLen> Default for AccountLedger<UnlockingLen>
where
UnlockingLen: Get<u32>,
{
fn default() -> Self {
Self {
locked: Balance::zero(),
unlocking: BoundedVec::default(),
staked: StakeAmount::default(),
staked_future: None,
contract_stake_count: Zero::zero(),
}
}
}

impl<UnlockingLen> AccountLedger<UnlockingLen>
where
UnlockingLen: Get<u32>,
Expand Down Expand Up @@ -1011,7 +997,7 @@ impl SingularStakingInfo {
///
/// `period` - period number for which this entry is relevant.
/// `subperiod` - subperiod during which this entry is created.
pub fn new(period: PeriodNumber, subperiod: Subperiod) -> Self {
pub(crate) fn new(period: PeriodNumber, subperiod: Subperiod) -> Self {
Self {
previous_staked: Default::default(),
staked: StakeAmount {
Expand Down Expand Up @@ -1393,6 +1379,7 @@ pub enum EraRewardSpanError {
MaxEncodedLen,
RuntimeDebugNoBound,
PartialEqNoBound,
DefaultNoBound,
EqNoBound,
CloneNoBound,
TypeInfo,
Expand All @@ -1414,7 +1401,7 @@ where
SL: Get<u32>,
{
/// Create new instance of the `EraRewardSpan`
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Self {
span: Default::default(),
first_era: 0,
Expand Down Expand Up @@ -1527,6 +1514,7 @@ impl TierThreshold {
MaxEncodedLen,
RuntimeDebugNoBound,
PartialEqNoBound,
DefaultNoBound,
EqNoBound,
CloneNoBound,
TypeInfo,
Expand Down Expand Up @@ -1585,23 +1573,14 @@ impl<NT: Get<u32>> TierParameters<NT> {
}
}

impl<NT: Get<u32>> Default for TierParameters<NT> {
fn default() -> Self {
Self {
reward_portion: BoundedVec::default(),
slot_distribution: BoundedVec::default(),
tier_thresholds: BoundedVec::default(),
}
}
}

/// Configuration of dApp tiers.
#[derive(
Encode,
Decode,
MaxEncodedLen,
RuntimeDebugNoBound,
PartialEqNoBound,
DefaultNoBound,
EqNoBound,
CloneNoBound,
TypeInfo,
Expand All @@ -1623,17 +1602,6 @@ pub struct TiersConfiguration<NT: Get<u32>, T: TierSlotsFunc, P: Get<FixedU128>>
pub(crate) _phantom: PhantomData<(T, P)>,
}

impl<NT: Get<u32>, T: TierSlotsFunc, P: Get<FixedU128>> Default for TiersConfiguration<NT, T, P> {
fn default() -> Self {
Self {
slots_per_tier: BoundedVec::default(),
reward_portion: BoundedVec::default(),
tier_thresholds: BoundedVec::default(),
_phantom: Default::default(),
}
}
}

// Some TODOs:
// Some parts regarding tiers should be refactored.
// * There's no need to keep thresholds in two separate storage items since the calculation can always be done compared to the
Expand Down Expand Up @@ -1760,6 +1728,7 @@ impl<NT: Get<u32>, T: TierSlotsFunc, P: Get<FixedU128>> TiersConfiguration<NT, T
MaxEncodedLen,
RuntimeDebugNoBound,
PartialEqNoBound,
DefaultNoBound,
EqNoBound,
CloneNoBound,
TypeInfo,
Expand All @@ -1777,21 +1746,10 @@ pub struct DAppTierRewards<MD: Get<u32>, NT: Get<u32>> {
pub(crate) rank_rewards: BoundedVec<Balance, NT>,
}

impl<MD: Get<u32>, NT: Get<u32>> Default for DAppTierRewards<MD, NT> {
fn default() -> Self {
Self {
dapps: BoundedBTreeMap::default(),
rewards: BoundedVec::default(),
period: 0,
rank_rewards: BoundedVec::default(),
}
}
}

impl<MD: Get<u32>, NT: Get<u32>> DAppTierRewards<MD, NT> {
/// Attempt to construct `DAppTierRewards` struct.
/// If the provided arguments exceed the allowed capacity, return an error.
pub fn new(
pub(crate) fn new(
dapps: BTreeMap<DAppId, RankedTier>,
rewards: Vec<Balance>,
period: PeriodNumber,
Expand Down
6 changes: 2 additions & 4 deletions precompiles/dapp-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ where

// Get the appropriate era reward span
let era_span_index = DAppStaking::<R>::era_reward_span_index(era);
let reward_span =
EraRewards::<R>::get(&era_span_index).unwrap_or(EraRewardSpanFor::<R>::new());
let reward_span = EraRewards::<R>::get(&era_span_index).unwrap_or_default();

// Sum up staker & dApp reward pools for the era
let reward = reward_span.get(era).map_or(Zero::zero(), |r| {
Expand Down Expand Up @@ -198,8 +197,7 @@ where
handle.record_db_read::<R>(20 + EraRewardSpanFor::<R>::max_encoded_len())?;

let era_span_index = DAppStaking::<R>::era_reward_span_index(era);
let reward_span =
EraRewards::<R>::get(&era_span_index).unwrap_or(EraRewardSpanFor::<R>::new());
let reward_span = EraRewards::<R>::get(&era_span_index).unwrap_or_default();

let staked = reward_span.get(era).map_or(Zero::zero(), |r| r.staked());

Expand Down

0 comments on commit 4c1c3a8

Please sign in to comment.