Skip to content

Commit

Permalink
Fix for incorrect freeze amount
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Dec 20, 2023
1 parent 808686f commit 3ffb53c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 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 @@ -1528,7 +1528,7 @@ pub mod pallet {
T::Currency::set_freeze(
&FreezeReason::DAppStaking.into(),
account,
ledger.active_locked_amount(),
ledger.total_locked_amount(),
)?;
Ledger::<T>::insert(account, ledger);
}
Expand Down
5 changes: 3 additions & 2 deletions pallets/dapp-staking-v3/src/test/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,9 @@ pub(crate) fn assert_unlock(account: AccountId, amount: Balance) {
);

assert_eq!(
init_frozen_balance - expected_unlock_amount,
Balances::balance_frozen(&FreezeReason::DAppStaking.into(), &account)
init_frozen_balance ,
Balances::balance_frozen(&FreezeReason::DAppStaking.into(), &account),
"Frozen balance must remain the same since the funds are still locked/frozen, only undergoing the unlocking process."
);
}

Expand Down
61 changes: 60 additions & 1 deletion pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
use frame_support::{
assert_noop, assert_ok, assert_storage_noop,
error::BadOrigin,
traits::{Currency, Get, OnFinalize, OnInitialize},
traits::{fungible::Unbalanced as FunUnbalanced, Currency, Get, OnFinalize, OnInitialize},
};
use sp_runtime::traits::Zero;

Expand Down Expand Up @@ -2520,3 +2520,62 @@ fn stake_after_period_ends_with_max_staked_contracts() {
}
})
}

#[test]
fn post_unlock_balance_cannot_be_transfered() {
ExtBuilder::build().execute_with(|| {
let staker = 2;

// Lock some of the free balance
let init_free_balance = Balances::free_balance(&staker);
let lock_amount = init_free_balance / 3;
assert_lock(staker, lock_amount);

// Make sure second account is empty
let other_account = 42;
assert_ok!(Balances::write_balance(&other_account, 0));

// 1. Ensure we can only transfer what is not locked/frozen.
assert_ok!(Balances::transfer_all(
RuntimeOrigin::signed(staker),
other_account,
true
));
assert_eq!(
Balances::free_balance(&other_account),
init_free_balance - lock_amount,
"Only what is locked can be transferred."
);

// 2. Start the 'unlocking process' for the locked amount, but ensure it still cannot be transferred.
assert_unlock(staker, lock_amount);

assert_ok!(Balances::write_balance(&other_account, 0));
assert_ok!(Balances::transfer_all(
RuntimeOrigin::signed(staker),
other_account,
true
));
assert!(
Balances::free_balance(&other_account).is_zero(),
"Nothing could have been transferred since it's still locked/frozen."
);

// 3. Claim the unlocked chunk, and ensure it can be transferred afterwards.
run_to_block(Ledger::<Test>::get(&staker).unlocking[0].unlock_block);
assert_claim_unlocked(staker);

assert_ok!(Balances::write_balance(&other_account, 0));
assert_ok!(Balances::transfer_all(
RuntimeOrigin::signed(staker),
other_account,
false
));
assert_eq!(
Balances::free_balance(&other_account),
lock_amount,
"Everything should have been transferred."
);
assert!(Balances::free_balance(&staker).is_zero());
})
}

0 comments on commit 3ffb53c

Please sign in to comment.