Skip to content

Commit

Permalink
fix:remove duplicated methods, changed visibility of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Dec 7, 2023
1 parent 8cde272 commit d9b44e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
3 changes: 3 additions & 0 deletions crates/redeem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ impl<T: Config> Pallet<T> {
let below_premium_redeem = ext::vault_registry::is_vault_below_premium_threshold::<T>(&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::<T>();
Expand Down
29 changes: 5 additions & 24 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,6 @@ impl<T: Config> Pallet<T> {
/// # Returns
/// Returns a `Result` containing the calculated maximum premium as an `Amount<T>`.
pub fn get_vault_max_premium_redeem(vault_id: &DefaultVaultId<T>) -> Result<Amount<T>, 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)`
Expand All @@ -804,7 +801,8 @@ impl<T: Config> Pallet<T> {
let global_secure_threshold = Self::get_global_secure_threshold(&vault_id.currencies)?;
let premium_redeem_rate = ext::fee::premium_redeem_reward_rate::<T>();

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(&current_collateral)?;
Expand Down Expand Up @@ -1146,32 +1144,15 @@ impl<T: Config> Pallet<T> {
///
/// # 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<CurrencyId<T>>,
) -> Result<UnsignedFixedPoint<T>, DispatchError> {
let global_secure_threshold =
Self::secure_collateral_threshold(&currency_pair).ok_or(Error::<T>::ThresholdNotSet)?;
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<T>,
to_be_backed_tokens: &Amount<T>,
secure_threshold: UnsignedFixedPoint<T>,
) -> Result<Amount<T>, 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
Expand Down Expand Up @@ -1976,7 +1957,7 @@ impl<T: Config> Pallet<T> {
collateral.convert_to(wrapped_currency)?.checked_div(&threshold)
}

pub fn vault_to_be_backed_tokens(vault_id: &DefaultVaultId<T>) -> Result<Amount<T>, DispatchError> {
fn vault_to_be_backed_tokens(vault_id: &DefaultVaultId<T>) -> Result<Amount<T>, DispatchError> {
let vault = Self::get_active_rich_vault_from_id(vault_id)?;
vault.to_be_backed_tokens()
}
Expand Down

0 comments on commit d9b44e4

Please sign in to comment.