Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect check for locked/frozen amount #1275

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
.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
);
})
}
Loading