From d9b44e449cddae57612a9aeb82164a3b9226feb0 Mon Sep 17 00:00:00 2001 From: nakul1010 Date: Thu, 7 Dec 2023 14:17:24 +0530 Subject: [PATCH] fix:remove duplicated methods, changed visibility of methods --- crates/redeem/src/lib.rs | 3 +++ crates/vault-registry/src/lib.rs | 29 +++++------------------------ 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/crates/redeem/src/lib.rs b/crates/redeem/src/lib.rs index 34f4568d84..cf675353c4 100644 --- a/crates/redeem/src/lib.rs +++ b/crates/redeem/src/lib.rs @@ -505,6 +505,9 @@ impl Pallet { let below_premium_redeem = ext::vault_registry::is_vault_below_premium_threshold::(&vault_id)?; let currency_id = vault_id.collateral_currency(); + // Calculate the premium collateral amount based on whether the redemption is below the premium redeem + // threshold. This should come before increasing the `to_be_redeemed` tokens and locking the amount to + // ensure accurate premium redeem calculations. let premium_collateral = if below_premium_redeem { let redeem_amount_wrapped_in_collateral = user_to_be_received_btc.convert_to(currency_id)?; let premium_redeem_rate = ext::fee::premium_redeem_reward_rate::(); diff --git a/crates/vault-registry/src/lib.rs b/crates/vault-registry/src/lib.rs index bd16f19eb0..84c7cc283f 100644 --- a/crates/vault-registry/src/lib.rs +++ b/crates/vault-registry/src/lib.rs @@ -782,9 +782,6 @@ impl Pallet { /// # Returns /// Returns a `Result` containing the calculated maximum premium as an `Amount`. pub fn get_vault_max_premium_redeem(vault_id: &DefaultVaultId) -> Result, DispatchError> { - // The goal of premium redeems is to get the vault back the a healthy collateralization ratio. As such, - // we only award a premium for the amount of tokens required to get the vault back to secure threshold. - // The goal of premium redeems is to get the vault back the a healthy collateralization ratio. As such, // we only award a premium for the amount of tokens required to get the vault back to secure threshold. // The CollateralizationRate is defined as `totalCollateral / convertToCollateral(totalTokens)` @@ -804,7 +801,8 @@ impl Pallet { let global_secure_threshold = Self::get_global_secure_threshold(&vault_id.currencies)?; let premium_redeem_rate = ext::fee::premium_redeem_reward_rate::(); - let required_collateral = Self::required_collateral(&vault_id, &to_be_backed_tokens, global_secure_threshold)?; + let required_collateral = + Self::get_required_collateral_for_wrapped(&to_be_backed_tokens, vault_id.collateral_currency())?; let current_collateral = Self::get_backing_collateral(&vault_id)?; let missing_collateral = required_collateral.saturating_sub(¤t_collateral)?; @@ -1146,7 +1144,8 @@ impl Pallet { /// /// # Errors /// * `ThresholdNotSet` - If the secure collateral threshold for the given `currency_pair` is not set. - pub fn get_global_secure_threshold( + #[cfg_attr(feature = "integration-tests", visibility::make(pub))] + fn get_global_secure_threshold( currency_pair: &VaultCurrencyPair>, ) -> Result, DispatchError> { let global_secure_threshold = @@ -1154,24 +1153,6 @@ impl Pallet { Ok(global_secure_threshold) } - /// Calculate the required collateral for a vault given the specified parameters. - /// - /// # Arguments - /// * `vault_id` - The identifier of the vault for which to calculate the required collateral. - /// * `to_be_backed_tokens` - The amount of tokens to be backed by collateral. - /// * `secure_threshold` - The secure collateral threshold to be applied in the calculation. - /// - /// # Returns - /// Returns the required collateral amount or an error if the calculation fails. - pub fn required_collateral( - vault_id: &DefaultVaultId, - to_be_backed_tokens: &Amount, - secure_threshold: UnsignedFixedPoint, - ) -> Result, DispatchError> { - let issued_tokens_in_collateral = to_be_backed_tokens.convert_to(vault_id.collateral_currency())?; // oldTokens * EXCH - issued_tokens_in_collateral.checked_rounded_mul(&secure_threshold, Rounding::NearestPrefUp) - } - /// Adds an amount tokens to the to-be-redeemed tokens balance of a vault. /// This function serves as a prevention against race conditions in the /// redeem and replace procedures. If, for example, a vault would receive @@ -1976,7 +1957,7 @@ impl Pallet { collateral.convert_to(wrapped_currency)?.checked_div(&threshold) } - pub fn vault_to_be_backed_tokens(vault_id: &DefaultVaultId) -> Result, DispatchError> { + fn vault_to_be_backed_tokens(vault_id: &DefaultVaultId) -> Result, DispatchError> { let vault = Self::get_active_rich_vault_from_id(vault_id)?; vault.to_be_backed_tokens() }