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

feat: check all restricted balances are above minimum when redeeming #5444

Merged
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
10 changes: 10 additions & 0 deletions state-chain/pallets/cf-funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ pub mod pallet {
/// When requesting a redemption, you must not have an amount below the minimum.
BelowMinimumFunding,

/// When requesting a redemption, all restricted balances must be above the minimum.
RestrictedBalanceBelowMinimumFunding,

/// There are not enough unrestricted funds to process the redemption.
InsufficientUnrestrictedFunds,

Expand Down Expand Up @@ -407,6 +410,13 @@ pub mod pallet {
// Use the full debit amount here - fees are paid by restricted funds by default.
total_restricted_balance = *restricted_balance;
restricted_balance.saturating_reduce(debit_amount);
// ensure that the remaining restricted balance is zero or above MinimumFunding
ensure!(
restricted_balance.is_zero() ||
*restricted_balance >= MinimumFunding::<T>::get(),
Error::<T>::RestrictedBalanceBelowMinimumFunding
);

if restricted_balance.is_zero() {
restricted_balances.remove(&address);
}
Expand Down
26 changes: 23 additions & 3 deletions state-chain/pallets/cf-funding/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,11 @@ fn redemption_expiry_removes_redemption() {
);

// Success, can request redemption again since the last one expired.
// Note that restricted balance is REDEMPTION_TAX less after refund, so adjust the
// TO_REDEEM accordingly to make sure restricted balance is above minimum
assert_ok!(Funding::redeem(
RuntimeOrigin::signed(ALICE),
TO_REDEEM.into(),
(TO_REDEEM - REDEMPTION_TAX).into(),
RESTRICTED_ADDRESS,
Default::default()
));
Expand Down Expand Up @@ -600,8 +602,8 @@ fn restore_restricted_balance_when_redemption_expires() {
do_test(RedemptionAmount::Exact(RESTRICTED_AMOUNT));
// Redeem a little less than the restricted balance.
do_test(RedemptionAmount::Exact(RESTRICTED_AMOUNT - 1));
// Redeem signficantly less than the restricted balance.
do_test(RedemptionAmount::Exact(RESTRICTED_AMOUNT - REDEMPTION_TAX * 2));
// Redeem significantly less than the restricted balance. Make sure rest is above minimum
do_test(RedemptionAmount::Exact(RESTRICTED_AMOUNT - REDEMPTION_TAX * 3));
}

#[test]
Expand Down Expand Up @@ -1160,6 +1162,24 @@ mod test_restricted_balances {
None::<Error<Test>>
),
];
test_restricted_balances![
restricted_balances_can_only_be_zero_or_above_minimum_after_redeeming,
NO_BOND,
// Succeed, with restricted balance1 = 0
(RedemptionAmount::Exact(RESTRICTED_BALANCE_1), RESTRICTED_ADDRESS_1, None::<Error<Test>>),
// Succeed, with restricted balance1 = MIN_FUNDING
(
RedemptionAmount::Exact(RESTRICTED_BALANCE_1 - MIN_FUNDING - REDEMPTION_TAX),
RESTRICTED_ADDRESS_1,
None::<Error<Test>>
),
// Fails, because restricted balance1 would have been < MIN_FUNDING
(
RedemptionAmount::Exact(RESTRICTED_BALANCE_1 - MIN_FUNDING - REDEMPTION_TAX + 1),
RESTRICTED_ADDRESS_1,
Some(Error::<Test>::RestrictedBalanceBelowMinimumFunding)
),
];
test_restricted_balances![
higher_than_restricted_amount_1_can_only_be_redeemed_to_restricted_address_2,
NO_BOND,
Expand Down
Loading