difftreelog
feat(app-promo) added `fn set_freeze_with_result`
in: master
The fn added to better handling of potential errors associated with `freeze` actions. Added `MaxHolds` to `pallet_balances::Config`
2 files changed
pallets/app-promotion/src/lib.rsdiffbeforeafterboth84use sp_runtime::{84use sp_runtime::{85 Perbill,85 Perbill,86 traits::{BlockNumberProvider, CheckedAdd, CheckedSub, AccountIdConversion, Zero},86 traits::{BlockNumberProvider, CheckedAdd, CheckedSub, AccountIdConversion, Zero},87 ArithmeticError,87 ArithmeticError, DispatchError,88};88};898990pub const LOCK_IDENTIFIER: [u8; 8] = *b"appstake";90pub const LOCK_IDENTIFIER: [u8; 8] = *b"appstake";845 stakers845 stakers846 .into_iter()846 .into_iter()847 .try_for_each(|s| -> Result<_, DispatchError> {847 .try_for_each(|s| -> Result<_, DispatchError> {848 if let Some(lock) = Self::get_locked_balance(&s) {848 if let Some(BalanceLock { amount, .. }) = Self::get_locked_balance(&s) {849 if let Some(_) = Self::get_frozen_balance(&s) {849 if let Some(_) = Self::get_frozen_balance(&s) {850 return Err(Error::<T>::InconsistencyState.into());850 return Err(Error::<T>::InconsistencyState.into());851 }851 }855 &s,855 &s,856 );856 );857857858 Self::set_freeze_unchecked(&s, lock.amount);858 Self::set_freeze_with_result(&s, amount)?;859 Ok(())859 Ok(())860 } else {860 } else {861 Ok(())861 Ok(())990 Self::get_frozen_balance(staker)990 Self::get_frozen_balance(staker)991 .unwrap_or_default()991 .unwrap_or_default()992 .checked_add(&amount)992 .checked_add(&amount)993 .map(|freeze| Self::set_freeze_unchecked(staker, freeze))993 .map(|freeze| Self::set_freeze_with_result(staker, freeze))994 .ok_or(ArithmeticError::Overflow.into())994 .ok_or::<DispatchError>(ArithmeticError::Overflow.into())?995 }995 }996996997 /// Sets the new state of a balance locked by the pallet.997 /// Sets the new state of a balance locked by the pallet.1018 ///1018 ///1019 /// - `staker`: staker account.1019 /// - `staker`: staker account.1020 /// - `amount`: amount of frozen funds.1020 /// - `amount`: amount of frozen funds.1021 fn set_freeze_unchecked(staker: &T::AccountId, amount: BalanceOf<T>) {1021 fn set_freeze_unchecked(staker: &T::AccountId, amount: BalanceOf<T>) {1022 Self::set_freeze_with_result(staker, amount);1023 }10241025 /// Sets the new state of a balance frozen by the pallet.1026 ///1027 /// - `staker`: staker account.1028 /// - `amount`: amount of frozen funds.1029 fn set_freeze_with_result(staker: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult {1022 if amount.is_zero() {1030 if amount.is_zero() {1023 <<T as Config>::Currency as MutateFreeze<T::AccountId>>::thaw(1031 <<T as Config>::Currency as MutateFreeze<T::AccountId>>::thaw(1024 &T::FreezeIdentifier::get(),1032 &T::FreezeIdentifier::get(),1025 &staker,1033 &staker,1026 );1034 )1027 } else {1035 } else {1028 <<T as Config>::Currency as MutateFreeze<T::AccountId>>::set_freeze(1036 <<T as Config>::Currency as MutateFreeze<T::AccountId>>::set_freeze(1029 &T::FreezeIdentifier::get(),1037 &T::FreezeIdentifier::get(),1030 staker,1038 staker,1031 amount,1039 amount,1032 );1040 )1033 }1041 }1034 }1042 }10351043runtime/common/config/substrate.rsdiffbeforeafterboth132 pub const ExistentialDeposit: u128 = EXISTENTIAL_DEPOSIT;132 pub const ExistentialDeposit: u128 = EXISTENTIAL_DEPOSIT;133 pub const MaxLocks: u32 = 50;133 pub const MaxLocks: u32 = 50;134 pub const MaxReserves: u32 = 50;134 pub const MaxReserves: u32 = 50;135 pub const MaxHolds: u32 = 10;135 pub const MaxFreezes: u32 = 20;136 pub const MaxFreezes: u32 = 10;136}137}137138138impl pallet_balances::Config for Runtime {139impl pallet_balances::Config for Runtime {150 type WeightInfo = pallet_balances::weights::SubstrateWeight<Self>;151 type WeightInfo = pallet_balances::weights::SubstrateWeight<Self>;151 type HoldIdentifier = [u8; 16];152 type HoldIdentifier = [u8; 16];152 type FreezeIdentifier = [u8; 16];153 type FreezeIdentifier = [u8; 16];153 type MaxHolds = ();154 type MaxHolds = MaxHolds;154 type MaxFreezes = MaxFreezes;155 type MaxFreezes = MaxFreezes;155}156}156157