Skip to content

Commit

Permalink
fix: add integration_test_redeem_max_premium_redeemable_token test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Dec 7, 2023
1 parent 6e65b86 commit 8cde272
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,7 @@ impl<T: Config> Pallet<T> {
collateral_in_wrapped.ratio(&issued_tokens)
}

#[cfg_attr(feature = "integration-tests", visibility::make(pub))]
fn is_vault_below_threshold(
vault_id: &DefaultVaultId<T>,
threshold: UnsignedFixedPoint<T>,
Expand Down
2 changes: 1 addition & 1 deletion crates/vault-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ mod get_vaults_below_premium_collaterlization_tests {
// set back to default threshold so that vaults fall under premium redeem
set_default_thresholds();

// not returned
// not returned, since default threshold applied so vault is now not under premium threshold
let id3 = vault_id(4);
let issue_tokens2: u128 = 50;
let collateral2 = 150;
Expand Down
71 changes: 71 additions & 0 deletions parachain/runtime/runtime-tests/src/parachain/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,77 @@ mod premium_redeem_tests {
});
}

#[test]
fn integration_test_redeem_max_premium_redeemable_token() {
test_setup_for_premium_redeem(|vault_id| {
setup_vault_below_secure_threshold(vault_id.clone());

let global_secure = VaultRegistryPallet::get_global_secure_threshold(&vault_id.currencies).unwrap(); // 200%
assert!(!VaultRegistryPallet::is_vault_below_secure_threshold(&vault_id).unwrap());
assert!(VaultRegistryPallet::is_vault_below_premium_threshold(&vault_id).unwrap());
assert!(!VaultRegistryPallet::is_vault_below_threshold(&vault_id, global_secure).unwrap());

// Set up vault custom secure threshold,
let vault_custom_secure_threshold = UnsignedFixedPoint::checked_from_rational(300, 100);
assert_ok!(
RuntimeCall::VaultRegistry(VaultRegistryCall::set_custom_secure_threshold {
currency_pair: vault_id.currencies.clone(),
custom_threshold: vault_custom_secure_threshold,
})
.dispatch(origin_of(vault_id.account_id.clone()))
);

assert!(VaultRegistryPallet::is_vault_below_secure_threshold(&vault_id).unwrap());
assert!(VaultRegistryPallet::is_vault_below_premium_threshold(&vault_id).unwrap());
assert!(!VaultRegistryPallet::is_vault_below_threshold(&vault_id, global_secure).unwrap());

// calculate issued_tokens such that `max_premium` can be achieved
let premium_redeem_vaults = VaultRegistryPallet::get_premium_redeem_vaults()
.unwrap()
.get(0)
.unwrap()
.clone();
let premium_redeem_in_collateral = premium_redeem_vaults
.1
.convert_to(vault_id.collateral_currency())
.unwrap(); // 15384
let premium_reward_rate = FeePallet::premium_redeem_reward_rate(); // 0.05
let issued_tokens = premium_redeem_in_collateral
.checked_div(&premium_reward_rate)
.unwrap()
.convert_to(vault_id.wrapped_currency())
.unwrap(); // 153840

let redeem_id_1 = setup_redeem(issued_tokens, USER, &vault_id);

// still true since custom secure threshold is set as 300%
assert!(VaultRegistryPallet::is_vault_below_secure_threshold(&vault_id).unwrap());
// vault has surpassed the premium threshold, and its collateralization rate now exceeds the
// PremiumRedeemThreshold
assert!(!VaultRegistryPallet::is_vault_below_premium_threshold(&vault_id).unwrap());
assert!(!VaultRegistryPallet::is_vault_below_threshold(&vault_id, global_secure).unwrap());

let redeem_1 = RedeemPallet::get_open_redeem_request_from_id(&redeem_id_1).unwrap();
// premium didn't came as `max_premium`(15384) since inclusion fees also deducted from `issued_tokens`
assert_eq!(redeem_1.premium, 15187);

// No premium redeem vaults, but `max_premium` was not achieved in previous redeem request,
// dust value of max_premium - achieved_premium = 15384-15187 = 197 Collateral
assert_noop!(
VaultRegistryPallet::get_premium_redeem_vaults(),
VaultRegistryError::NoVaultUnderThePremiumRedeemThreshold
);

execute_redeem(redeem_id_1);

let redeem_id_2 = setup_redeem(vault_id.wrapped(800_00), USER, &vault_id);
let redeem_2 = RedeemPallet::get_open_redeem_request_from_id(&redeem_id_2).unwrap();
// no premium is given out for new redeems
assert_eq!(redeem_2.premium, 0);

execute_redeem(redeem_id_2);
});
}
#[test]
fn integration_test_premium_redeem_with_reward_for_full_request() {
test_setup_for_premium_redeem(|vault_id| {
Expand Down

0 comments on commit 8cde272

Please sign in to comment.