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

program: dynamic liquidation fee for liq_perp_with_fill #1185

Merged
merged 13 commits into from
Aug 21, 2024
65 changes: 58 additions & 7 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::math::liquidation::{
calculate_liability_transfer_implied_by_asset_amount,
calculate_liability_transfer_to_cover_margin_shortage, calculate_liquidation_multiplier,
calculate_max_pct_to_liquidate, calculate_perp_if_fee, calculate_spot_if_fee,
get_liquidation_order_params, validate_transfer_satisfies_limit_price,
get_liquidation_fee, get_liquidation_order_params, validate_transfer_satisfies_limit_price,
LiquidationMultiplierType,
};
use crate::math::margin::{
Expand Down Expand Up @@ -68,12 +68,14 @@ use crate::state::state::State;
use crate::state::traits::Size;
use crate::state::user::{MarketType, Order, OrderStatus, OrderType, User, UserStats};
use crate::state::user_map::{UserMap, UserStatsMap};
use crate::validate;
use crate::{get_then_update_id, load_mut};
use crate::{validate, LIQUIDATION_FEE_PRECISION, MARGIN_PRECISION};

#[cfg(test)]
mod tests;

const MAX_LIQUIDATION_MULTIPLIER: u32 = 3;

pub fn liquidate_perp(
market_index: u16,
liquidator_max_base_asset_amount: u64,
Expand Down Expand Up @@ -118,15 +120,13 @@ pub fn liquidate_perp(

drop(market);

// Settle user's funding payments so that collateral is up to date
settle_funding_payment(
user,
user_key,
perp_market_map.get_ref_mut(&market_index)?.deref_mut(),
now,
)?;

// Settle user's funding payments so that collateral is up to date
settle_funding_payment(
liquidator,
liquidator_key,
Expand Down Expand Up @@ -711,15 +711,13 @@ pub fn liquidate_perp_with_fill(

drop(market);

// Settle user's funding payments so that collateral is up to date
settle_funding_payment(
&mut user,
user_key,
perp_market_map.get_ref_mut(&market_index)?.deref_mut(),
now,
)?;

// Settle user's funding payments so that collateral is up to date
settle_funding_payment(
&mut liquidator,
liquidator_key,
Expand Down Expand Up @@ -969,13 +967,28 @@ pub fn liquidate_perp_with_fill(
)?;

let existing_direction = user.perp_positions[position_index].get_direction();
let max_liquidation_fee =
Copy link
Member

Choose a reason for hiding this comment

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

  1. should use safe_math
  2. should prob just call get_ref once and then drop it

could potentially also just make this a method on the perp market account?

(perp_market_map.get_ref(&market_index)?.liquidator_fee * MAX_LIQUIDATION_MULTIPLIER).min(
perp_market_map
.get_ref(&market_index)?
.margin_ratio_maintenance
* LIQUIDATION_FEE_PRECISION
/ MARGIN_PRECISION,
);

let liquidator_fee_adjusted = get_liquidation_fee(
liquidator_fee,
max_liquidation_fee,
user.last_active_slot,
slot,
)?;

let order_params = get_liquidation_order_params(
market_index,
existing_direction,
base_asset_amount,
oracle_price,
liquidator_fee,
liquidator_fee_adjusted,
)?;

let order_id = user.next_order_id;
Expand Down Expand Up @@ -3015,3 +3028,41 @@ pub fn calculate_margin_freed(

Ok((margin_freed, margin_calculation_after))
}

pub fn set_user_status_to_being_liquidated(
user: &mut User,
perp_market_map: &PerpMarketMap,
spot_market_map: &SpotMarketMap,
oracle_map: &mut OracleMap,
slot: u64,
state: &State,
) -> DriftResult {
validate!(
!user.is_bankrupt(),
ErrorCode::UserBankrupt,
"user bankrupt",
)?;

validate!(
!user.is_being_liquidated(),
ErrorCode::UserIsBeingLiquidated,
"user is already being liquidated",
)?;

let liquidation_margin_buffer_ratio = state.liquidation_margin_buffer_ratio;
let margin_calculation = calculate_margin_requirement_and_total_collateral_and_liability_info(
user,
perp_market_map,
spot_market_map,
oracle_map,
MarginContext::liquidation(liquidation_margin_buffer_ratio),
)?;

if !user.is_being_liquidated() && margin_calculation.meets_margin_requirement() {
msg!("margin calculation: {:?}", margin_calculation);
return Err(ErrorCode::SufficientCollateral);
} else {
user.enter_liquidation(slot)?;
}
Ok(())
}
Loading
Loading