From fd5def03fc2712d77fa0f1bb43f4d9f80a1646e4 Mon Sep 17 00:00:00 2001 From: PraetorP Date: Mon, 13 Feb 2023 13:40:09 +0000 Subject: [PATCH] feat(app-promo): added impl for `unstake_partial` --- --- a/pallets/app-promotion/src/lib.rs +++ b/pallets/app-promotion/src/lib.rs @@ -156,7 +156,7 @@ pub struct Pallet(_); #[pallet::event] - #[pallet::generate_deposit(fn deposit_event)] + #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { /// Staking recalculation was performed /// @@ -546,48 +546,8 @@ #[pallet::weight(::WeightInfo::unstake())] pub fn unstake_partial(staker: OriginFor, amount: BalanceOf) -> DispatchResult { let staker_id = ensure_signed(staker)?; - let config = >::get(); - - // calculate block number where the sum would be free - let block = >::block_number() + config.pending_interval; - - let mut pendings = >::get(block); - // checks that we can do unreserve stakes in the block - ensure!(!pendings.is_full(), Error::::PendingForBlockOverflow); - - Self::partial_unstake(&staker_id, amount, pendings, block)?; - - let mut total_stakes = 0u64; - - let total_staked: BalanceOf = Staked::::drain_prefix((&staker_id,)) - .map(|(_, (amount, _))| { - total_stakes += 1; - amount - }) - .sum(); - - if total_staked.is_zero() { - return Ok(()); // TO-DO - } - - // pendings - // .try_push((staker_id.clone(), total_staked)) - // .map_err(|_| Error::::PendingForBlockOverflow)?; - - // >::insert(block, pendings); - - // TotalStaked::::set( - // TotalStaked::::get() - // .checked_sub(&total_staked) - // .ok_or(ArithmeticError::Underflow)?, - // ); - - // StakesPerAccount::::remove(&staker_id); - - Self::deposit_event(Event::Unstake(staker_id, total_staked)); - - Ok(()) + Self::partial_unstake(&staker_id, amount) } /// Sets the pallet to be the sponsor for the collection. @@ -861,18 +821,21 @@ T::PalletId::get().into_account_truncating() } - fn partial_unstake( - staker_id: &T::AccountId, - unstaked_balance: BalanceOf, - mut pendings: BoundedVec< - (T::AccountId, BalanceOf), - sp_core::ConstU32, - >, - pending_block: T::BlockNumber, - ) -> DispatchResult { + fn partial_unstake(staker_id: &T::AccountId, unstaked_balance: BalanceOf) -> DispatchResult { + if unstaked_balance == Default::default() { return Ok(()); } + + let config = >::get(); + + // calculate block number where the sum would be free + let unpending_block = >::block_number() + config.pending_interval; + + let mut pendings = >::get(unpending_block); + + // checks that we can do unreserve stakes in the block + ensure!(!pendings.is_full(), Error::::PendingForBlockOverflow); let mut stakes = Staked::::iter_prefix((staker_id,)).collect::>(); @@ -901,7 +864,7 @@ if acc_amount == >::default() { return None; } - if acc_amount <= balance_per_block { + if acc_amount < balance_per_block { let res = (block, (balance_per_block - acc_amount, recalc_block)); acc_amount = >::default(); return Some(res); @@ -919,7 +882,7 @@ StakesPerAccount::::try_mutate(staker_id, |stakes| -> DispatchResult { *stakes = stakes - .checked_div(will_deleted_stakes_count) + .checked_sub(will_deleted_stakes_count) .ok_or(ArithmeticError::Underflow)?; Ok(()) })?; @@ -936,7 +899,9 @@ } }); - >::insert(pending_block, pendings); + >::insert(unpending_block, pendings); + + Self::deposit_event(Event::Unstake(staker_id.clone(), total_staked)); Ok(()) } -- gitstuff