Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Inconsistent self-redeem logic #1107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/redeem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ mod self_redeem {
}

/// returns (fees, consumed_issued_tokens)
fn calculate_token_amounts<T: Config>(
pub fn calculate_token_amounts<T: Config>(
vault_id: &DefaultVaultId<T>,
requested_redeem_amount: &Amount<T>,
) -> Result<(Amount<T>, Amount<T>), DispatchError> {
Expand Down Expand Up @@ -479,9 +479,11 @@ impl<T: Config> Pallet<T> {
// Make sure this can't happen again.
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 == vault_id.account_id {
Amount::zero(vault_id.wrapped_currency())
// allow to redeem for zero fee if and only if redeemable amount is equal to requested redeem amount
let (fees, _consumed_issued_tokens) =
self_redeem::calculate_token_amounts::<T>(&vault_id, &amount_wrapped)?;
fees
} else {
ext::fee::get_redeem_fee::<T>(&amount_wrapped)?
};
Expand Down
101 changes: 95 additions & 6 deletions crates/redeem/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,17 @@ fn test_request_redeem_fails_with_default_btc_address() {
}

#[test]
fn test_request_redeem_succeeds_with_self_redeem() {
fn test_request_redeem_succeeds_with_self_redeem_feeless_call() {
run_test(|| {
convert_to.mock_safe(|_, x| MockResult::Return(Ok(x)));
let issued_tokens = 10;
<vault_registry::Pallet<Test>>::insert_vault(
&VAULT,
vault_registry::Vault {
id: VAULT,
to_be_replaced_tokens: 0,
to_be_issued_tokens: 0,
issued_tokens: 10,
issued_tokens,
to_be_redeemed_tokens: 0,
active_replace_collateral: 0,
replace_collateral: 0,
Expand All @@ -267,13 +268,13 @@ fn test_request_redeem_succeeds_with_self_redeem() {
);

let redeemer = VAULT.account_id;
let amount = 90;
let amount = issued_tokens;
let btc_address = BtcAddress::random();
let redeem_fee = 5;

ext::vault_registry::try_increase_to_be_redeemed_tokens::<Test>.mock_safe(move |vault_id, amount_btc| {
assert_eq!(vault_id, &VAULT);
assert_eq!(amount_btc, &wrapped(amount));

MockResult::Return(Ok(()))
});

Expand All @@ -286,11 +287,13 @@ fn test_request_redeem_succeeds_with_self_redeem() {

ext::security::get_secure_id::<Test>.mock_safe(move |_| MockResult::Return(H256::zero()));
ext::vault_registry::is_vault_below_premium_threshold::<Test>.mock_safe(move |_| MockResult::Return(Ok(false)));
ext::fee::get_redeem_fee::<Test>.mock_safe(move |_| MockResult::Return(Ok(wrapped(redeem_fee))));

let btc_fee = Redeem::get_current_inclusion_fee(DEFAULT_WRAPPED_CURRENCY).unwrap();

assert_ok!(Redeem::request_redeem(
RuntimeOrigin::signed(redeemer),
amount,
issued_tokens,
btc_address,
VAULT
));
Expand All @@ -299,14 +302,15 @@ fn test_request_redeem_succeeds_with_self_redeem() {
redeem_id: H256::zero(),
redeemer,
amount: amount - btc_fee.amount(),
fee: 0,
fee: 0, //redeem fee zero since all redeemable tokens redeem by vault
premium: 0,
vault_id: VAULT,
btc_address,
transfer_fee: Redeem::get_current_inclusion_fee(DEFAULT_WRAPPED_CURRENCY)
.unwrap()
.amount()
});

assert_ok!(
Redeem::get_open_redeem_request_from_id(&H256::zero()),
RedeemRequest {
Expand All @@ -328,6 +332,91 @@ fn test_request_redeem_succeeds_with_self_redeem() {
})
}

#[test]
fn test_request_redeem_succeeds_with_self_redeem_but_pays_fee() {
run_test(|| {
convert_to.mock_safe(|_, x| MockResult::Return(Ok(x)));
<vault_registry::Pallet<Test>>::insert_vault(
&VAULT,
vault_registry::Vault {
id: VAULT,
to_be_replaced_tokens: 0,
to_be_issued_tokens: 0,
issued_tokens: 10,
to_be_redeemed_tokens: 0,
active_replace_collateral: 0,
replace_collateral: 0,
banned_until: None,
secure_collateral_threshold: None,
status: VaultStatus::Active(true),
liquidated_collateral: 0,
},
);

let redeemer = VAULT.account_id;
let amount = 90;
let btc_address = BtcAddress::random();
let redeem_fee = 5;

ext::vault_registry::try_increase_to_be_redeemed_tokens::<Test>.mock_safe(move |vault_id, amount_btc| {
assert_eq!(vault_id, &VAULT);
assert_eq!(amount_btc, &wrapped(amount - redeem_fee));
MockResult::Return(Ok(()))
});

Amount::<Test>::lock_on.mock_safe(move |amount_wrapped, account| {
assert_eq!(account, &redeemer);
assert_eq!(amount_wrapped, &wrapped(amount));

MockResult::Return(Ok(()))
});

ext::security::get_secure_id::<Test>.mock_safe(move |_| MockResult::Return(H256::zero()));
ext::vault_registry::is_vault_below_premium_threshold::<Test>.mock_safe(move |_| MockResult::Return(Ok(false)));
ext::fee::get_redeem_fee::<Test>.mock_safe(move |_| MockResult::Return(Ok(wrapped(redeem_fee))));

let btc_fee = Redeem::get_current_inclusion_fee(DEFAULT_WRAPPED_CURRENCY).unwrap();

assert_ok!(Redeem::request_redeem(
RuntimeOrigin::signed(redeemer),
amount,
btc_address,
VAULT
));

assert_emitted!(Event::RequestRedeem {
redeem_id: H256::zero(),
redeemer,
amount: amount - btc_fee.amount() - redeem_fee,
fee: redeem_fee,
premium: 0,
vault_id: VAULT,
btc_address,
transfer_fee: Redeem::get_current_inclusion_fee(DEFAULT_WRAPPED_CURRENCY)
.unwrap()
.amount()
});
assert_ok!(
Redeem::get_open_redeem_request_from_id(&H256::zero()),
RedeemRequest {
period: Redeem::redeem_period(),
vault: VAULT,
opentime: 1,
fee: redeem_fee,
amount_btc: amount - btc_fee.amount() - redeem_fee,
premium: 0,
redeemer,
btc_address,
btc_height: 0,
status: RedeemRequestStatus::Pending,
transfer_fee_btc: Redeem::get_current_inclusion_fee(DEFAULT_WRAPPED_CURRENCY)
.unwrap()
.amount(),
}
);
})
}

#[test]
fn test_liquidation_redeem_succeeds() {
run_test(|| {
Expand Down