Skip to content

Commit

Permalink
Stake work
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Oct 2, 2023
1 parent 2882b23 commit 52f9daf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
27 changes: 23 additions & 4 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ pub mod pallet {
let protocol_state = ActiveProtocolState::<T>::get();
let mut ledger = Ledger::<T>::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 {
Expand All @@ -619,6 +619,25 @@ pub mod pallet {
AccountLedgerError::OldEra => Error::<T>::InternalStakeError,
})?;

// Update `StakerInfo` storage with the new stake amount on the specified contract.
let new_staking_info = if let Some(mut staking_info) = StakerInfo::<T>::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::<T>::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.

Expand All @@ -633,9 +652,9 @@ pub mod pallet {
#[pallet::call_index(10)]
#[pallet::weight(Weight::zero())]
pub fn unstake(
origin: OriginFor<T>,
smart_contract: T::SmartContract,
#[pallet::compact] amount: Balance,
_origin: OriginFor<T>,
_smart_contract: T::SmartContract,
#[pallet::compact] _amount: Balance,
) -> DispatchResult {
Self::ensure_pallet_enabled()?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
9 changes: 6 additions & 3 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -266,9 +266,10 @@ pub struct ProtocolState<BlockNumber: AtLeast32BitUnsigned + MaxEncodedLen> {
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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
}

Expand Down

0 comments on commit 52f9daf

Please sign in to comment.