git.delta.rocks / unique-network / refs/commits / fd5def03fc27

difftreelog

feat(app-promo) added impl for `unstake_partial`

PraetorP2023-02-13parent: #ea43c5e.patch.diff
in: master

1 file changed

modifiedpallets/app-promotion/src/lib.rsdiffbeforeafterboth
156 pub struct Pallet<T>(_);156 pub struct Pallet<T>(_);
157157
158 #[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 performed
162 ///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();
550
551 // calculate block number where the sum would be free
552 let block = <frame_system::Pallet<T>>::block_number() + config.pending_interval;
553
554 let mut pendings = <PendingUnstake<T>>::get(block);
555
556 // checks that we can do unreserve stakes in the block
557 ensure!(!pendings.is_full(), Error::<T>::PendingForBlockOverflow);
558549
559 Self::partial_unstake(&staker_id, amount, pendings, block)?;550 Self::partial_unstake(&staker_id, amount)
560
561 let mut total_stakes = 0u64;
562
563 let total_staked: BalanceOf<T> = Staked::<T>::drain_prefix((&staker_id,))
564 .map(|(_, (amount, _))| {
565 total_stakes += 1;
566 amount
567 })
568 .sum();
569
570 if total_staked.is_zero() {
571 return Ok(()); // TO-DO
572 }
573
574 // pendings
575 // .try_push((staker_id.clone(), total_staked))
576 // .map_err(|_| Error::<T>::PendingForBlockOverflow)?;
577
578 // <PendingUnstake<T>>::insert(block, pendings);
579
580 // TotalStaked::<T>::set(
581 // TotalStaked::<T>::get()
582 // .checked_sub(&total_staked)
583 // .ok_or(ArithmeticError::Underflow)?,
584 // );
585
586 // StakesPerAccount::<T>::remove(&staker_id);
587
588 Self::deposit_event(Event::Unstake(staker_id, total_staked));
589
590 Ok(())
591 }551 }
592552
593 /// Sets the pallet to be the sponsor for the collection.553 /// Sets the pallet to be the sponsor for the collection.
863823
864 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();
831
832 // calculate block number where the sum would be free
833 let unpending_block = <frame_system::Pallet<T>>::block_number() + config.pending_interval;
834
835 let mut pendings = <PendingUnstake<T>>::get(unpending_block);
836
837 // checks that we can do unreserve stakes in the block
838 ensure!(!pendings.is_full(), Error::<T>::PendingForBlockOverflow);
876839
877 let mut stakes = Staked::<T>::iter_prefix((staker_id,)).collect::<Vec<_>>();840 let mut stakes = Staked::<T>::iter_prefix((staker_id,)).collect::<Vec<_>>();
878841
901 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);
919882
920 StakesPerAccount::<T>::try_mutate(staker_id, |stakes| -> DispatchResult {883 StakesPerAccount::<T>::try_mutate(staker_id, |stakes| -> DispatchResult {
921 *stakes = stakes884 *stakes = stakes
922 .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 });
938901
939 <PendingUnstake<T>>::insert(pending_block, pendings);902 <PendingUnstake<T>>::insert(unpending_block, pendings);
903
904 Self::deposit_event(Event::Unstake(staker_id.clone(), total_staked));
940905
941 Ok(())906 Ok(())
942 }907 }