Skip to content

Commit

Permalink
fix: add integration test for get_premium_vaults method.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Dec 21, 2023
1 parent 8df7f47 commit 1a3c3d6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
6 changes: 4 additions & 2 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,6 @@ impl<T: Config> Pallet<T> {
Ok(Self::get_vault_from_id(&vault_id)?.is_liquidated())
}

#[cfg(feature = "integration-tests")]
// note: unlike `is_vault_below_secure_threshold` and `is_vault_below_liquidation_threshold`,
// this function uses to_be_backed tokens
pub fn will_be_below_premium_threshold(vault_id: &DefaultVaultId<T>) -> Result<bool, DispatchError> {
Expand Down Expand Up @@ -1707,7 +1706,10 @@ impl<T: Config> Pallet<T> {

let request_redeem_tokens_for_max_premium = vault_to_burn_tokens.checked_div(&amount_wrapped).ok()?;

if Self::ensure_not_banned(&vault_id).is_ok() && !request_redeem_tokens_for_max_premium.is_zero() {
if Self::ensure_not_banned(&vault_id).is_ok() && !request_redeem_tokens_for_max_premium.is_zero()
// need the check as `inclusion_fee` will be a non zero amount, hence `request_redeem_tokens_for_max_premium` will also be a non zero amount
&& Self::will_be_below_premium_threshold(&vault_id).unwrap_or(false)
{
Some((vault_id, request_redeem_tokens_for_max_premium))
} else {
None
Expand Down
7 changes: 3 additions & 4 deletions crates/vault-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,14 +1052,13 @@ mod get_vaults_below_premium_collaterlization_tests {
})
}

// ToDo: move test case to parachain test
#[test]
fn get_vaults_below_premium_collateralization_filters_banned_and_sufficiently_collateralized_vaults() {
run_test(|| {
// not returned, because is is not under premium threshold (which is set to 100% for this test)
let id1 = vault_id(3);
let issue_tokens1: u128 = 50;
let collateral1 = 61;
let collateral1 = 50;
add_vault(id1.clone(), issue_tokens1, collateral1);

// returned
Expand All @@ -1081,8 +1080,8 @@ mod get_vaults_below_premium_collaterlization_tests {
VaultRegistry::_set_secure_collateral_threshold(DEFAULT_CURRENCY_PAIR, secure);

assert_eq!(
VaultRegistry::get_premium_redeem_vaults(0_u32),
Ok(vec!((id2, wrapped(20))))
VaultRegistry::get_premium_redeem_vaults(10_u32),
Ok(vec!((id2, wrapped(issue_tokens2))))
);
})
}
Expand Down
33 changes: 19 additions & 14 deletions parachain/runtime/runtime-tests/src/parachain/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ fn test_with<R>(execute: impl Fn(VaultId) -> R) {
})
};

// test_with(Token(DOT), Token(KBTC), None);
test_with(Token(DOT), Token(KBTC), None);
test_with(Token(DOT), Token(IBTC), None);
// test_with(Token(DOT), Token(IBTC), Some(Token(KSM)));
// test_with(Token(KSM), Token(IBTC), None);
// test_with(ForeignAsset(1), Token(IBTC), None);
// test_with(LendToken(1), Token(IBTC), None);
test_with(Token(DOT), Token(IBTC), Some(Token(KSM)));
test_with(Token(KSM), Token(IBTC), None);
test_with(ForeignAsset(1), Token(IBTC), None);
test_with(LendToken(1), Token(IBTC), None);
}

fn test_setup_for_premium_redeem<R>(execute: impl Fn(VaultId) -> R) {
Expand All @@ -49,12 +49,12 @@ fn test_setup_for_premium_redeem<R>(execute: impl Fn(VaultId) -> R) {
})
};

// test_with(Token(DOT), Token(KBTC), None);
test_with(Token(DOT), Token(KBTC), None);
test_with(Token(DOT), Token(IBTC), None);
// test_with(Token(DOT), Token(IBTC), Some(Token(KSM)));
// test_with(Token(KSM), Token(IBTC), None);
// test_with(ForeignAsset(1), Token(IBTC), None);
// test_with(LendToken(1), Token(IBTC), None);
test_with(Token(DOT), Token(IBTC), Some(Token(KSM)));
test_with(Token(KSM), Token(IBTC), None);
test_with(ForeignAsset(1), Token(IBTC), None);
test_with(LendToken(1), Token(IBTC), None);
}

fn common_setup<R>(
Expand Down Expand Up @@ -201,11 +201,16 @@ mod premium_redeem_tests {
}

#[test]
fn integration_test_try_get_premium_vaults_which_are_sufficiently_collateralized() {
fn integration_test_try_get_premium_vaults_which_is_sufficiently_collateralized_then_under_collateralized() {
test_setup_for_premium_redeem(|vault_id| {
let premium_redeem_vaults = RedeemPallet::get_premium_redeem_vaults().unwrap();
// Fixme: No premium vaults should be return, length of vector should be zero
assert_eq!(premium_redeem_vaults.len(), 2);
assert_noop!(
RedeemPallet::get_premium_redeem_vaults(),
VaultRegistryError::NoVaultUnderThePremiumRedeemThreshold
);

// put vault under premium redeem threshold
setup_vault_below_premium_threshold(vault_id.clone());
assert_eq!(RedeemPallet::get_premium_redeem_vaults().unwrap().len(), 1);
});
}

Expand Down

0 comments on commit 1a3c3d6

Please sign in to comment.