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

bigz/add-tiers-for-liq-perp-pnl #368

Merged
merged 15 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
78 changes: 61 additions & 17 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ use crate::math::liquidation::{
validate_transfer_satisfies_limit_price, LiquidationMultiplierType,
};
use crate::math::margin::{
calculate_margin_requirement_and_total_collateral, meets_initial_margin_requirement,
MarginRequirementType,
calculate_margin_requirement_and_total_collateral,
calculate_margin_requirement_and_total_collateral_and_liability_info,
calculate_user_safest_position_tiers, meets_initial_margin_requirement, MarginRequirementType,
};
use crate::math::oracle::DriftAction;
use crate::math::orders::{
Expand Down Expand Up @@ -1551,7 +1552,8 @@ pub fn liquidate_perp_pnl_for_deposit(
) -> DriftResult {
// liquidator takes over remaining negative perpetual pnl in exchange for a user deposit
// can only be done once the perpetual position's size is 0
// blocked when the user deposit oracle is deemed invalid
// blocked when 1) user deposit oracle is deemed invalid
// or 2) user has outstanding liability with higher tier

validate!(
!user.is_bankrupt(),
Expand Down Expand Up @@ -1609,7 +1611,14 @@ pub fn liquidate_perp_pnl_for_deposit(
now,
)?;

let (asset_amount, asset_price, asset_decimals, asset_weight, asset_liquidation_multiplier) = {
let (
asset_amount,
asset_price,
_asset_tier,
asset_decimals,
asset_weight,
asset_liquidation_multiplier,
) = {
let mut asset_market = spot_market_map.get_ref_mut(&asset_market_index)?;
let (asset_price_data, validity_guard_rails) =
oracle_map.get_price_data_and_guard_rails(&asset_market.oracle)?;
Expand Down Expand Up @@ -1643,6 +1652,7 @@ pub fn liquidate_perp_pnl_for_deposit(
(
token_amount,
token_price,
asset_market.asset_tier,
asset_market.decimals,
asset_market.maintenance_asset_weight,
calculate_liquidation_multiplier(
Expand All @@ -1655,6 +1665,7 @@ pub fn liquidate_perp_pnl_for_deposit(
let (
unsettled_pnl,
quote_price,
contract_tier,
quote_decimals,
pnl_liability_weight,
pnl_liquidation_multiplier,
Expand Down Expand Up @@ -1691,6 +1702,7 @@ pub fn liquidate_perp_pnl_for_deposit(
(
unsettled_pnl.unsigned_abs(),
quote_price,
market.contract_tier,
6_u32,
SPOT_WEIGHT_PRECISION,
calculate_liquidation_multiplier(
Expand All @@ -1700,15 +1712,22 @@ pub fn liquidate_perp_pnl_for_deposit(
)
};

let (margin_requirement, total_collateral, margin_requirement_plus_buffer, _) =
calculate_margin_requirement_and_total_collateral(
user,
perp_market_map,
MarginRequirementType::Maintenance,
spot_market_map,
oracle_map,
Some(liquidation_margin_buffer_ratio as u128),
)?;
let (
margin_requirement,
total_collateral,
margin_requirement_plus_buffer,
_all_oracles_valid,
_,
_,
) = calculate_margin_requirement_and_total_collateral_and_liability_info(
user,
perp_market_map,
MarginRequirementType::Maintenance,
spot_market_map,
oracle_map,
Some(liquidation_margin_buffer_ratio as u128),
false,
)?;

if !user.is_being_liquidated() && total_collateral >= margin_requirement.cast()? {
return Err(ErrorCode::SufficientCollateral);
Expand Down Expand Up @@ -1737,6 +1756,10 @@ pub fn liquidate_perp_pnl_for_deposit(
None,
)?;

let (safest_tier_spot_liability, safest_tier_perp_liability) =
calculate_user_safest_position_tiers(user, perp_market_map, spot_market_map)?;
let is_contract_tier_violation =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel like it might just be cleaner to check the tier violation here and return early all at once here, instead of spreading the logic, but will defer to you

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that'd be a lot of copy paste code tho

!(contract_tier.is_as_safe_as(&safest_tier_perp_liability, &safest_tier_spot_liability));
// check if user exited liquidation territory
let (intermediate_total_collateral, intermediate_margin_requirement_with_buffer) =
if !canceled_order_ids.is_empty() {
Expand All @@ -1762,9 +1785,10 @@ pub fn liquidate_perp_pnl_for_deposit(
.cast::<u64>()?;
user.increment_margin_freed(margin_freed)?;

if intermediate_total_collateral
>= intermediate_margin_requirement_plus_buffer.cast()?
{
let exiting_liq_territory = intermediate_total_collateral
>= intermediate_margin_requirement_plus_buffer.cast()?;

if exiting_liq_territory || is_contract_tier_violation {
let market = perp_market_map.get_ref(&perp_market_index)?;
let market_oracle_price = oracle_map.get_price_data(&market.amm.oracle)?.price;

Expand All @@ -1790,7 +1814,17 @@ pub fn liquidate_perp_pnl_for_deposit(
..LiquidationRecord::default()
});

user.exit_liquidation();
if exiting_liq_territory {
user.exit_liquidation();
} else if is_contract_tier_violation {
msg!(
"return early after cancel orders: liquidating contract tier={:?} pnl is riskier than outstanding {:?} & {:?}",
contract_tier,
safest_tier_perp_liability,
safest_tier_spot_liability
);
}

return Ok(());
}

Expand All @@ -1802,6 +1836,16 @@ pub fn liquidate_perp_pnl_for_deposit(
(total_collateral, margin_requirement_plus_buffer)
};

if is_contract_tier_violation {
msg!(
"liquidating contract tier={:?} pnl is riskier than outstanding {:?} & {:?}",
contract_tier,
safest_tier_perp_liability,
safest_tier_spot_liability
);
return Err(ErrorCode::TierViolationLiquidatingPerpPnl);
}

let margin_shortage = calculate_margin_shortage(
intermediate_margin_requirement_with_buffer,
intermediate_total_collateral,
Expand Down
Loading