From 52f9dafb3eae9f8ceab8ed7935b3e86bc06aae1e Mon Sep 17 00:00:00 2001 From: Dino Pacandi Date: Mon, 2 Oct 2023 16:33:59 +0200 Subject: [PATCH] Stake work --- pallets/dapp-staking-v3/src/lib.rs | 27 ++++++++++++++++++++---- pallets/dapp-staking-v3/src/test/mock.rs | 2 +- pallets/dapp-staking-v3/src/types.rs | 9 +++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/pallets/dapp-staking-v3/src/lib.rs b/pallets/dapp-staking-v3/src/lib.rs index ee22b05fce..f8c1e401b6 100644 --- a/pallets/dapp-staking-v3/src/lib.rs +++ b/pallets/dapp-staking-v3/src/lib.rs @@ -607,7 +607,7 @@ pub mod pallet { let protocol_state = ActiveProtocolState::::get(); let mut ledger = Ledger::::get(&account); - // Increase stake amount for the current era & period. + // Increase stake amount for the current era & period in staker's ledger ledger .add_stake_amount(amount, protocol_state.era, protocol_state.period) .map_err(|err| match err { @@ -619,6 +619,25 @@ pub mod pallet { AccountLedgerError::OldEra => Error::::InternalStakeError, })?; + // Update `StakerInfo` storage with the new stake amount on the specified contract. + let new_staking_info = if let Some(mut staking_info) = StakerInfo::::get(&account, &smart_contract) { + // TODO: do I need to check for which era this is for? Not sure, but maybe it's safer to do so. + // TODO2: revisit this later. + ensure!( + staking_info.period_number() == protocol_state.period, + Error::::InternalStakeError + ); + staking_info.stake(amount, protocol_state.period_info.period_type); + staking_info + } else { + let mut staking_info = SingularStakingInfo::new(protocol_state.period); + staking_info.stake(amount, protocol_state.period_info.period_type); + // TODO: do I need to check if the stake amount is sufficient? Above minimum threshold? + staking_info + } + + // Update contract's total stake info for this period + // TODO: maybe keep track of pending bonus rewards in the AccountLedger struct? // That way it's easy to check if stake can even be called - bonus-rewards should be zero & last staked era should be None or current one. @@ -633,9 +652,9 @@ pub mod pallet { #[pallet::call_index(10)] #[pallet::weight(Weight::zero())] pub fn unstake( - origin: OriginFor, - smart_contract: T::SmartContract, - #[pallet::compact] amount: Balance, + _origin: OriginFor, + _smart_contract: T::SmartContract, + #[pallet::compact] _amount: Balance, ) -> DispatchResult { Self::ensure_pallet_enabled()?; Ok(()) diff --git a/pallets/dapp-staking-v3/src/test/mock.rs b/pallets/dapp-staking-v3/src/test/mock.rs index a434b26fa7..dec7bea2e3 100644 --- a/pallets/dapp-staking-v3/src/test/mock.rs +++ b/pallets/dapp-staking-v3/src/test/mock.rs @@ -157,7 +157,7 @@ impl ExtBuilder { era: 1, next_era_start: BlockNumber::from(101_u32), period: 1, - period_type_and_ending_era: PeriodTypeAndEndingEra { + period_info: PeriodInfo { period_type: PeriodType::Voting, ending_era: 16, }, diff --git a/pallets/dapp-staking-v3/src/types.rs b/pallets/dapp-staking-v3/src/types.rs index e0b2e59276..cf9032d707 100644 --- a/pallets/dapp-staking-v3/src/types.rs +++ b/pallets/dapp-staking-v3/src/types.rs @@ -238,7 +238,7 @@ pub enum PeriodType { /// Wrapper type around current `PeriodType` and era number when it's expected to end. #[derive(Encode, Decode, MaxEncodedLen, Clone, Copy, Debug, PartialEq, Eq, TypeInfo)] -pub struct PeriodTypeAndEndingEra { +pub struct PeriodInfo { pub period_type: PeriodType, #[codec(compact)] pub ending_era: EraNumber, @@ -266,9 +266,10 @@ pub struct ProtocolState { pub next_era_start: BlockNumber, /// Ongoing period number. #[codec(compact)] + // TODO: move this under `PeriodInfo`? pub period: PeriodNumber, /// Ongoing period type and when is it expected to end. - pub period_type_and_ending_era: PeriodTypeAndEndingEra, + pub period_info: PeriodInfo, /// `true` if pallet is in maintenance mode (disabled), `false` otherwise. /// TODO: provide some configurable barrier to handle this on the runtime level instead? Make an item for this? pub maintenance: bool, @@ -283,7 +284,7 @@ where era: 0, next_era_start: BlockNumber::from(1_u32), period: 0, - period_type_and_ending_era: PeriodTypeAndEndingEra { + period_info: PeriodInfo { period_type: PeriodType::Voting, ending_era: 2, }, @@ -725,10 +726,12 @@ pub struct SingularStakingInfo { bep_staked_amount: Balance, /// Period number for which this entry is relevant. #[codec(compact)] + // TODO: rename to period_number? period: PeriodNumber, /// Indicates whether a staker is a loyal staker or not. loyal_staker: bool, /// Indicates whether staker claimed rewards + // TODO: isn't this redundant?! reward_claimed: bool, }