Browse Source

runtime: Fix staking handler.

Shamil Gadelshin 3 years ago
parent
commit
e35816b87d

+ 5 - 5
runtime-modules/staking-handler/src/lib.rs

@@ -7,6 +7,7 @@
 #![cfg_attr(not(feature = "std"), no_std)]
 
 use frame_support::dispatch::{DispatchError, DispatchResult};
+use frame_support::ensure;
 use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, WithdrawReasons};
 use sp_arithmetic::traits::Zero;
 use sp_std::marker::PhantomData;
@@ -152,11 +153,10 @@ impl<
             return Ok(());
         }
 
-        let free_balance = <pallet_balances::Module<T>>::free_balance(account_id);
-
-        if new_stake > free_balance {
-            return Err(DispatchError::Other("Not enough balance for a new stake."));
-        }
+        ensure!(
+            Self::is_enough_balance_for_stake(account_id, new_stake),
+            DispatchError::Other("Not enough balance for a new stake.")
+        );
 
         Self::lock(account_id, new_stake);
 

+ 4 - 4
runtime-modules/working-group/src/checks.rs

@@ -45,11 +45,11 @@ pub(crate) fn ensure_stake_for_opening_type<T: Trait<I>, I: Instance>(
         ensure_origin_is_active_leader::<T, I>(origin)?;
         let lead = crate::Module::<T, I>::worker_by_id(ensure_lead_is_set::<T, I>()?);
 
+        let new_stake = T::LeaderOpeningStake::get()
+            + T::StakingHandler::current_stake(&lead.staking_account_id);
+
         ensure!(
-            T::StakingHandler::is_enough_balance_for_stake(
-                &lead.staking_account_id,
-                T::LeaderOpeningStake::get()
-            ),
+            T::StakingHandler::is_enough_balance_for_stake(&lead.staking_account_id, new_stake),
             Error::<T, I>::InsufficientBalanceToCoverStake
         );
     }

+ 4 - 4
runtime-modules/working-group/src/tests/mod.rs

@@ -119,13 +119,13 @@ fn add_opening_fails_with_insufficient_balance() {
             .hire_lead();
 
         let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
-            stake_amount: <Test as Trait>::MinimumApplicationStake::get() + 1,
+            stake_amount: <Test as Trait>::MinimumApplicationStake::get(),
             leaving_unstaking_period: <Test as Trait>::MinUnstakingPeriodLimit::get() + 1,
         });
 
-        add_opening_fixture.call_and_assert(Err(DispatchError::Other(
-            "Not enough balance for a new stake.",
-        )));
+        add_opening_fixture.call_and_assert(Err(
+            Error::<Test, DefaultInstance>::InsufficientBalanceToCoverStake.into(),
+        ));
     });
 }