Skip to content

Commit

Permalink
Fix incorrect check for locked/frozen amount (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard authored Jun 21, 2024
1 parent b76b0a9 commit f63d30e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ pub mod pallet {

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

Expand Down
13 changes: 9 additions & 4 deletions pallets/dapp-staking-v3/src/test/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ impl MemorySnapshot {
/// Returns locked balance in dApp staking for the specified account.
/// In case no balance is locked, returns zero.
pub fn locked_balance(&self, account: &AccountId) -> Balance {
self.ledger
.get(&account)
.map_or(Balance::zero(), |ledger| ledger.active_locked_amount())
self.ledger.get(&account).map_or(Balance::zero(), |ledger| {
ledger.locked + ledger.unlocking.iter().fold(0, |acc, x| acc + x.amount)
})
}
}

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

let post_frozen_balance = Balances::balance_frozen(&FreezeReason::DAppStaking.into(), &account);
assert_eq!(
init_frozen_balance + expected_lock_amount,
Balances::balance_frozen(&FreezeReason::DAppStaking.into(), &account)
post_frozen_balance
);
assert!(
Balances::total_balance(&account) >= post_frozen_balance,
"Total balance should never be less than frozen balance."
)
}

/// Start the unlocking process for locked funds and assert success.
Expand Down
16 changes: 16 additions & 0 deletions pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3286,3 +3286,19 @@ fn unstake_correctly_reduces_future_contract_stake() {
assert_unstake(staker_1, &smart_contract, amount_2 + 3);
})
}

#[test]
fn lock_correctly_considers_unlocking_amount() {
ExtBuilder::build().execute_with(|| {
// Lock the entire amount & immediately start the unlocking process
let (staker, unlock_amount) = (1, 13);
let total_balance = Balances::total_balance(&staker);
assert_lock(staker, total_balance);
assert_unlock(staker, unlock_amount);

assert_noop!(
DappStaking::lock(RuntimeOrigin::signed(staker), 1),
Error::<Test>::ZeroAmount
);
})
}

0 comments on commit f63d30e

Please sign in to comment.