difftreelog
feat(app-promo) added impl for `unstake_partial`
in: master
1 file changed
pallets/app-promotion/src/lib.rsdiffbeforeafterboth156 pub struct Pallet<T>(_);156 pub struct Pallet<T>(_);157157158 #[pallet::event]158 #[pallet::event]159 #[pallet::generate_deposit(fn deposit_event)]159 #[pallet::generate_deposit(pub(super) fn deposit_event)]160 pub enum Event<T: Config> {160 pub enum Event<T: Config> {161 /// Staking recalculation was performed161 /// Staking recalculation was performed162 ///162 ///546 #[pallet::weight(<T as Config>::WeightInfo::unstake())]546 #[pallet::weight(<T as Config>::WeightInfo::unstake())]547 pub fn unstake_partial(staker: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {547 pub fn unstake_partial(staker: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {548 let staker_id = ensure_signed(staker)?;548 let staker_id = ensure_signed(staker)?;549 let config = <PalletConfiguration<T>>::get();550551 // calculate block number where the sum would be free552 let block = <frame_system::Pallet<T>>::block_number() + config.pending_interval;553554 let mut pendings = <PendingUnstake<T>>::get(block);555556 // checks that we can do unreserve stakes in the block557 ensure!(!pendings.is_full(), Error::<T>::PendingForBlockOverflow);558549559 Self::partial_unstake(&staker_id, amount, pendings, block)?;550 Self::partial_unstake(&staker_id, amount)560561 let mut total_stakes = 0u64;562563 let total_staked: BalanceOf<T> = Staked::<T>::drain_prefix((&staker_id,))564 .map(|(_, (amount, _))| {565 total_stakes += 1;566 amount567 })568 .sum();569570 if total_staked.is_zero() {571 return Ok(()); // TO-DO572 }573574 // pendings575 // .try_push((staker_id.clone(), total_staked))576 // .map_err(|_| Error::<T>::PendingForBlockOverflow)?;577578 // <PendingUnstake<T>>::insert(block, pendings);579580 // TotalStaked::<T>::set(581 // TotalStaked::<T>::get()582 // .checked_sub(&total_staked)583 // .ok_or(ArithmeticError::Underflow)?,584 // );585586 // StakesPerAccount::<T>::remove(&staker_id);587588 Self::deposit_event(Event::Unstake(staker_id, total_staked));589590 Ok(())591 }551 }592552593 /// Sets the pallet to be the sponsor for the collection.553 /// Sets the pallet to be the sponsor for the collection.863823864 fn partial_unstake(824 fn partial_unstake(staker_id: &T::AccountId, unstaked_balance: BalanceOf<T>) -> DispatchResult {865 staker_id: &T::AccountId,825 866 unstaked_balance: BalanceOf<T>,867 mut pendings: BoundedVec<868 (T::AccountId, BalanceOf<T>),869 sp_core::ConstU32<PENDING_LIMIT_PER_BLOCK>,870 >,871 pending_block: T::BlockNumber,872 ) -> DispatchResult {873 if unstaked_balance == Default::default() {826 if unstaked_balance == Default::default() {874 return Ok(());827 return Ok(());875 }828 }829 830 let config = <PalletConfiguration<T>>::get();831832 // calculate block number where the sum would be free833 let unpending_block = <frame_system::Pallet<T>>::block_number() + config.pending_interval;834835 let mut pendings = <PendingUnstake<T>>::get(unpending_block);836837 // checks that we can do unreserve stakes in the block838 ensure!(!pendings.is_full(), Error::<T>::PendingForBlockOverflow);876839877 let mut stakes = Staked::<T>::iter_prefix((staker_id,)).collect::<Vec<_>>();840 let mut stakes = Staked::<T>::iter_prefix((staker_id,)).collect::<Vec<_>>();878841901 if acc_amount == <BalanceOf<T>>::default() {864 if acc_amount == <BalanceOf<T>>::default() {902 return None;865 return None;903 }866 }904 if acc_amount <= balance_per_block {867 if acc_amount < balance_per_block {905 let res = (block, (balance_per_block - acc_amount, recalc_block));868 let res = (block, (balance_per_block - acc_amount, recalc_block));906 acc_amount = <BalanceOf<T>>::default();869 acc_amount = <BalanceOf<T>>::default();907 return Some(res);870 return Some(res);919882920 StakesPerAccount::<T>::try_mutate(staker_id, |stakes| -> DispatchResult {883 StakesPerAccount::<T>::try_mutate(staker_id, |stakes| -> DispatchResult {921 *stakes = stakes884 *stakes = stakes922 .checked_div(will_deleted_stakes_count)885 .checked_sub(will_deleted_stakes_count)923 .ok_or(ArithmeticError::Underflow)?;886 .ok_or(ArithmeticError::Underflow)?;924 Ok(())887 Ok(())925 })?;888 })?;936 }899 }937 });900 });938901939 <PendingUnstake<T>>::insert(pending_block, pendings);902 <PendingUnstake<T>>::insert(unpending_block, pendings);903904 Self::deposit_event(Event::Unstake(staker_id.clone(), total_staked));940905941 Ok(())906 Ok(())942 }907 }