Skip to content

Commit

Permalink
Fix a bug where if the user withdraws all debit and collateral, the s…
Browse files Browse the repository at this point in the history
…ystem would not allow it. (#2109)

Now minimum collateral amount is only maintained if user does not withdraw all of its collaterals

Co-authored-by: Roy Yang <[email protected]>
  • Loading branch information
2 people authored and xlc committed May 13, 2022
1 parent 45c4120 commit ac5eb40
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
7 changes: 4 additions & 3 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ pub mod module {
InvalidCollateralType,
/// Remain debit value in CDP below the dust amount
RemainDebitValueTooSmall,
/// Remain collateral value in CDP below the dust amount
/// Remain collateral value in CDP below the dust amount.
/// Withdraw all collateral or leave more than the minimum.
CollateralAmountBelowMinimum,
/// Feed price is invalid
InvalidFeedPrice,
Expand Down Expand Up @@ -1294,8 +1295,8 @@ impl<T: Config> RiskManager<T::AccountId, CurrencyId, Balance, Balance> for Pall
debit_value >= T::MinimumDebitValue::get(),
Error::<T>::RemainDebitValueTooSmall,
);
} else {
// check the collateral amount is above minimum when debit is 0
} else if !collateral_balance.is_zero() {
// If there are any collateral remaining, then it must be above the minimum
ensure!(
collateral_balance >= T::MinimumCollateralAmount::get(&currency_id),
Error::<T>::CollateralAmountBelowMinimum,
Expand Down
4 changes: 4 additions & 0 deletions modules/cdp-engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,7 @@ fn minimal_collateral_works() {
);
assert_ok!(CDPEngineModule::check_position_valid(BTC, 9, 20, true));
assert_ok!(CDPEngineModule::check_position_valid(BTC, 10, 0, true));
assert_ok!(CDPEngineModule::check_position_valid(BTC, 0, 0, true));

// Adjust position fails if collateral is too small
assert_noop!(
Expand All @@ -1787,5 +1788,8 @@ fn minimal_collateral_works() {
CDPEngineModule::adjust_position(&ALICE, BTC, -1, 0),
Error::<Runtime>::CollateralAmountBelowMinimum,
);

// Allow the user to withdraw all assets
assert_ok!(CDPEngineModule::adjust_position(&ALICE, BTC, 0, 0));
});
}
16 changes: 16 additions & 0 deletions runtime/integration-tests/src/honzon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,5 +662,21 @@ fn cdp_engine_minimum_collateral_amount_works() {
(relaychain_minimum_collateral_amount - 1) as i128,
(MinimumDebitValue::get() * 10) as i128,
));

// Native collateral can be withdrawal in its entirety if debit is 0
assert_ok!(CdpEngine::adjust_position(
&AccountId::from(ALICE),
NATIVE_CURRENCY,
1i128 - (native_minimum_collateral_amount as i128),
-((MinimumDebitValue::get() * 10) as i128),
));

// Other tokens collateral can be withdrawal in its entirety if debit is 0
assert_ok!(CdpEngine::adjust_position(
&AccountId::from(ALICE),
RELAY_CHAIN_CURRENCY,
1i128 - (relaychain_minimum_collateral_amount as i128),
-((MinimumDebitValue::get() * 10) as i128),
));
});
}

0 comments on commit ac5eb40

Please sign in to comment.