Skip to content

Commit 2d2776a

Browse files
committedJun 21, 2024
Fix incorrect check for locked/frozen amount
1 parent fe0d9e6 commit 2d2776a

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed
 

‎pallets/dapp-staking-v3/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ pub mod pallet {
838838

839839
// Calculate & check amount available for locking
840840
let available_balance =
841-
T::Currency::total_balance(&account).saturating_sub(ledger.active_locked_amount());
841+
T::Currency::total_balance(&account).saturating_sub(ledger.total_locked_amount());
842842
let amount_to_lock = available_balance.min(amount);
843843
ensure!(!amount_to_lock.is_zero(), Error::<T>::ZeroAmount);
844844

‎pallets/dapp-staking-v3/src/test/testing_utils.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ impl MemorySnapshot {
8686
/// Returns locked balance in dApp staking for the specified account.
8787
/// In case no balance is locked, returns zero.
8888
pub fn locked_balance(&self, account: &AccountId) -> Balance {
89-
self.ledger
90-
.get(&account)
91-
.map_or(Balance::zero(), |ledger| ledger.active_locked_amount())
89+
self.ledger.get(&account).map_or(Balance::zero(), |ledger| {
90+
ledger.locked + ledger.unlocking.iter().fold(0, |acc, x| acc + x.amount)
91+
})
9292
}
9393
}
9494

@@ -240,10 +240,15 @@ pub(crate) fn assert_lock(account: AccountId, amount: Balance) {
240240
"Total locked balance should be increased by the amount locked."
241241
);
242242

243+
let post_frozen_balance = Balances::balance_frozen(&FreezeReason::DAppStaking.into(), &account);
243244
assert_eq!(
244245
init_frozen_balance + expected_lock_amount,
245-
Balances::balance_frozen(&FreezeReason::DAppStaking.into(), &account)
246+
post_frozen_balance
246247
);
248+
assert!(
249+
Balances::total_balance(&account) >= post_frozen_balance,
250+
"Total balance should never be less than frozen balance."
251+
)
247252
}
248253

249254
/// Start the unlocking process for locked funds and assert success.

‎pallets/dapp-staking-v3/src/test/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -3286,3 +3286,19 @@ fn unstake_correctly_reduces_future_contract_stake() {
32863286
assert_unstake(staker_1, &smart_contract, amount_2 + 3);
32873287
})
32883288
}
3289+
3290+
#[test]
3291+
fn lock_correctly_considers_unlocking_amount() {
3292+
ExtBuilder::build().execute_with(|| {
3293+
// Lock the entire amount & immediately start the unlocking process
3294+
let (staker, unlock_amount) = (1, 13);
3295+
let total_balance = Balances::total_balance(&staker);
3296+
assert_lock(staker, total_balance);
3297+
assert_unlock(staker, unlock_amount);
3298+
3299+
assert_noop!(
3300+
DappStaking::lock(RuntimeOrigin::signed(staker), 1),
3301+
Error::<Test>::ZeroAmount
3302+
);
3303+
})
3304+
}

0 commit comments

Comments
 (0)