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

difftreelog

feat(app-promo) added `fn set_freeze_with_result`

PraetorP2023-05-23parent: #87f464d.patch.diff
in: master
The fn added to better handling of potential errors associated with `freeze` actions.

Added `MaxHolds` to `pallet_balances::Config`

2 files changed

modifiedpallets/app-promotion/src/lib.rsdiffbeforeafterboth
--- a/pallets/app-promotion/src/lib.rs
+++ b/pallets/app-promotion/src/lib.rs
@@ -84,7 +84,7 @@
 use sp_runtime::{
 	Perbill,
 	traits::{BlockNumberProvider, CheckedAdd, CheckedSub, AccountIdConversion, Zero},
-	ArithmeticError,
+	ArithmeticError, DispatchError,
 };
 
 pub const LOCK_IDENTIFIER: [u8; 8] = *b"appstake";
@@ -845,7 +845,7 @@
 			stakers
 				.into_iter()
 				.try_for_each(|s| -> Result<_, DispatchError> {
-					if let Some(lock) = Self::get_locked_balance(&s) {
+					if let Some(BalanceLock { amount, .. }) = Self::get_locked_balance(&s) {
 						if let Some(_) = Self::get_frozen_balance(&s) {
 							return Err(Error::<T>::InconsistencyState.into());
 						}
@@ -855,7 +855,7 @@
 							&s,
 						);
 
-						Self::set_freeze_unchecked(&s, lock.amount);
+						Self::set_freeze_with_result(&s, amount)?;
 						Ok(())
 					} else {
 						Ok(())
@@ -990,8 +990,8 @@
 		Self::get_frozen_balance(staker)
 			.unwrap_or_default()
 			.checked_add(&amount)
-			.map(|freeze| Self::set_freeze_unchecked(staker, freeze))
-			.ok_or(ArithmeticError::Overflow.into())
+			.map(|freeze| Self::set_freeze_with_result(staker, freeze))
+			.ok_or::<DispatchError>(ArithmeticError::Overflow.into())?
 	}
 
 	/// Sets the new state of a balance locked by the pallet.
@@ -1019,17 +1019,25 @@
 	/// - `staker`: staker account.
 	/// - `amount`: amount of frozen funds.
 	fn set_freeze_unchecked(staker: &T::AccountId, amount: BalanceOf<T>) {
+		Self::set_freeze_with_result(staker, amount);
+	}
+
+	/// Sets the new state of a balance frozen by the pallet.
+	///
+	/// - `staker`: staker account.
+	/// - `amount`: amount of frozen funds.
+	fn set_freeze_with_result(staker: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
 		if amount.is_zero() {
 			<<T as Config>::Currency as MutateFreeze<T::AccountId>>::thaw(
 				&T::FreezeIdentifier::get(),
 				&staker,
-			);
+			)
 		} else {
 			<<T as Config>::Currency as MutateFreeze<T::AccountId>>::set_freeze(
 				&T::FreezeIdentifier::get(),
 				staker,
 				amount,
-			);
+			)
 		}
 	}
 
modifiedruntime/common/config/substrate.rsdiffbeforeafterboth
132 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}
137138
138impl 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