Skip to content

Commit

Permalink
fix: test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Aug 15, 2023
1 parent 676fb32 commit b217ece
Show file tree
Hide file tree
Showing 4 changed files with 974 additions and 8 deletions.
20 changes: 18 additions & 2 deletions crates/redeem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ pub mod pallet {
AmountBelowDustAmount,
/// Invalid Cancel request.
InvalidCancelRequest,
/// Cannot replace self.
ReplaceSelfNotAllowed,
/// Vault cannot replace different currency.
InvalidWrappedCurrency,
}

/// The time difference in number of blocks between a redeem request is created and required completion time by a
Expand Down Expand Up @@ -541,6 +545,16 @@ impl<T: Config> Pallet<T> {
let old_vault = AccountOrVault::Vault(old_vault_id.clone());
let new_vault = AccountOrVault::Vault(new_vault_id.clone());

// probably this check is not strictly required, but it's better to give an
// explicit error rather than insufficient balance
ensure!(
old_vault_id.wrapped_currency() == new_vault_id.wrapped_currency(),
Error::<T>::InvalidWrappedCurrency
);

// don't allow vaults to replace themselves
ensure!(old_vault != new_vault, Error::<T>::ReplaceSelfNotAllowed);

// Calculate requestable tokens for old vault
let max_requestable_tokens: Amount<T> =
ext::vault_registry::requestable_to_be_replaced_tokens::<T>(&old_vault_id)?;
Expand Down Expand Up @@ -589,12 +603,12 @@ impl<T: Config> Pallet<T> {
ensure!(!btc_address.is_zero(), btc_relay::Error::<T>::InvalidBtcHash);

// todo: currently allowed to redeem from one currency to the other for free - decide if this is desirable
let fee_wrapped = if redeemer.get_account().clone() == vault_id.account_id {
let mut fee_wrapped = if redeemer.get_account().clone() == vault_id.account_id {
Amount::zero(vault_id.wrapped_currency())
} else {
ext::fee::get_redeem_fee::<T>(&amount_wrapped)?
};
let inclusion_fee = Self::get_current_inclusion_fee(vault_id.wrapped_currency())?;
let mut inclusion_fee = Self::get_current_inclusion_fee(vault_id.wrapped_currency())?;

let vault_to_be_burned_tokens = amount_wrapped.checked_sub(&fee_wrapped)?;

Expand Down Expand Up @@ -634,6 +648,8 @@ impl<T: Config> Pallet<T> {
};

let to_be_received_btc = if redeemer.is_vault_account() {
fee_wrapped = Amount::zero(vault_id.wrapped_currency());
inclusion_fee = Amount::zero(vault_id.wrapped_currency());
amount_wrapped.amount()
} else {
user_to_be_received_btc.amount()
Expand Down
8 changes: 2 additions & 6 deletions crates/redeem/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,7 @@ mod redeem_replace_tests {
premium: 0,
vault_id: OLD_VAULT,
btc_address,
transfer_fee: Redeem::get_current_inclusion_fee(DEFAULT_WRAPPED_CURRENCY)
.unwrap()
.amount()
transfer_fee: 0
});
})
}
Expand All @@ -828,9 +826,7 @@ mod redeem_replace_tests {
vault_id: OLD_VAULT,
amount: 10,
fee: 0,
transfer_fee: Redeem::get_current_inclusion_fee(DEFAULT_WRAPPED_CURRENCY)
.unwrap()
.amount(),
transfer_fee: 0,
});

assert_err!(
Expand Down
1 change: 1 addition & 0 deletions parachain/runtime/runtime-tests/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ mod loans;
mod multisig;
mod nomination;
mod redeem;
mod replace;
mod vault_registry;
Loading

0 comments on commit b217ece

Please sign in to comment.