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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: dynamic liquidation fee for liq_perp_with_fill ([#1185](https://github.com/drift-labs/protocol-v2/pull/1185))
- program: calculate_accumulated_interest return early based on ts ([#1192](https://github.com/drift-labs/protocol-v2/pull/1192))
- program: add logging to pyth pull updates ([#1189](https://github.com/drift-labs/protocol-v2/pull/1189))

Expand Down
56 changes: 50 additions & 6 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 @@ -118,15 +118,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 @@ -708,15 +706,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 @@ -966,13 +962,23 @@ pub fn liquidate_perp_with_fill(
)?;

let existing_direction = user.perp_positions[position_index].get_direction();
let max_liquidation_fee = perp_market_map
.get_ref(&market_index)?
.get_max_liquidation_fee()?;

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 @@ -3012,3 +3018,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