Skip to content

Commit

Permalink
Merge pull request #974 from gregdhill/fix/withdraw-minimum
Browse files Browse the repository at this point in the history
fix: don't allow vaults to withdraw below minimum
  • Loading branch information
sander2 authored Mar 21, 2023
2 parents caf0362 + b899c26 commit 9cf3b68
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,12 @@ impl<T: Config> Pallet<T> {
Err(x) => return Err(x),
};

ensure!(
new_collateral.is_zero()
|| new_collateral.ge(&Self::get_minimum_collateral_vault(vault_id.currencies.collateral))?,
Error::<T>::InsufficientVaultCollateralAmount
);

let is_below_threshold =
Pallet::<T>::is_collateral_below_vault_secure_threshold(&new_collateral, &vault.backed_tokens()?, &vault)?;
Ok(!is_below_threshold)
Expand Down
25 changes: 25 additions & 0 deletions crates/vault-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,31 @@ fn register_vault_fails_when_already_registered() {
});
}

#[test]
fn should_check_withdraw_collateral() {
run_test(|| {
create_sample_vault();
VaultRegistry::get_minimum_collateral_vault
.mock_safe(move |currency_id| MockResult::Return(Amount::new(DEFAULT_COLLATERAL / 2, currency_id)));

// should allow withdraw all
assert_ok!(
VaultRegistry::is_allowed_to_withdraw_collateral(&DEFAULT_ID, &amount(DEFAULT_COLLATERAL)),
true,
);
// should allow withdraw above minimum
assert_ok!(
VaultRegistry::is_allowed_to_withdraw_collateral(&DEFAULT_ID, &amount(DEFAULT_COLLATERAL / 4)),
true,
);
// should not allow withdraw above zero, below minimum
assert_err!(
VaultRegistry::is_allowed_to_withdraw_collateral(&DEFAULT_ID, &amount(DEFAULT_COLLATERAL / 4 * 3)),
TestError::InsufficientVaultCollateralAmount,
);
});
}

#[test]
fn try_increase_to_be_issued_tokens_succeeds() {
run_test(|| {
Expand Down

0 comments on commit 9cf3b68

Please sign in to comment.