Skip to content

Commit

Permalink
program: add pause for liquidation (#880)
Browse files Browse the repository at this point in the history
* program: add pause for liquidation

* update changelog

---------

Co-authored-by: 0xbigz <[email protected]>
  • Loading branch information
crispheaney and 0xbigz authored Feb 23, 2024
1 parent 6cdca50 commit e5a7e6d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: add pause operation for liquidation ([#880](https://github.com/drift-labs/protocol-v2/pull/880))

### Fixes

- program: handle derisk lp when orders array full ([#899](https://github.com/drift-labs/protocol-v2/pull/899))
Expand Down
100 changes: 100 additions & 0 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use crate::state::events::{
};
use crate::state::margin_calculation::{MarginCalculation, MarginContext, MarketIdentifier};
use crate::state::oracle_map::OracleMap;
use crate::state::paused_operations::{PerpOperation, SpotOperation};
use crate::state::perp_market::MarketStatus;
use crate::state::perp_market_map::PerpMarketMap;
use crate::state::spot_market::SpotBalanceType;
Expand Down Expand Up @@ -100,6 +101,17 @@ pub fn liquidate_perp(
"liquidator bankrupt",
)?;

let market = perp_market_map.get_ref(&market_index)?;

validate!(
!market.is_operation_paused(PerpOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
market_index
)?;

drop(market);

// Settle user's funding payments so that collateral is up to date
settle_funding_payment(
user,
Expand Down Expand Up @@ -668,6 +680,28 @@ pub fn liquidate_spot(
"liquidator bankrupt",
)?;

let asset_spot_market = spot_market_map.get_ref(&asset_market_index)?;

validate!(
!asset_spot_market.is_operation_paused(SpotOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
asset_market_index
)?;

drop(asset_spot_market);

let liability_spot_market = spot_market_map.get_ref(&liability_market_index)?;

validate!(
!liability_spot_market.is_operation_paused(SpotOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
liability_market_index
)?;

drop(liability_spot_market);

// validate user and liquidator have spot balances
user.get_spot_position(asset_market_index).map_err(|_| {
msg!(
Expand Down Expand Up @@ -1167,6 +1201,28 @@ pub fn liquidate_borrow_for_perp_pnl(
"liquidator bankrupt",
)?;

let perp_market = perp_market_map.get_ref(&perp_market_index)?;

validate!(
!perp_market.is_operation_paused(PerpOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for perp market {}",
perp_market_index
)?;

drop(perp_market);

let liability_spot_market = spot_market_map.get_ref(&liability_market_index)?;

validate!(
!liability_spot_market.is_operation_paused(SpotOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
liability_market_index
)?;

drop(liability_spot_market);

user.get_perp_position(perp_market_index).map_err(|e| {
msg!(
"User does not have a position for perp market {}",
Expand Down Expand Up @@ -1614,6 +1670,28 @@ pub fn liquidate_perp_pnl_for_deposit(
"liquidator bankrupt",
)?;

let asset_spot_market = spot_market_map.get_ref(&asset_market_index)?;

validate!(
!asset_spot_market.is_operation_paused(SpotOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
asset_market_index
)?;

drop(asset_spot_market);

let perp_market = perp_market_map.get_ref(&perp_market_index)?;

validate!(
!perp_market.is_operation_paused(PerpOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
perp_market_index
)?;

drop(perp_market);

user.get_perp_position(perp_market_index).map_err(|e| {
msg!(
"User does not have a position for perp market {}",
Expand Down Expand Up @@ -2086,6 +2164,17 @@ pub fn resolve_perp_bankruptcy(
"liquidator bankrupt",
)?;

let market = perp_market_map.get_ref(&market_index)?;

validate!(
!market.is_operation_paused(PerpOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
market_index
)?;

drop(market);

user.get_perp_position(market_index).map_err(|e| {
msg!(
"User does not have a position for perp market {}",
Expand Down Expand Up @@ -2300,6 +2389,17 @@ pub fn resolve_spot_bankruptcy(
"liquidator bankrupt",
)?;

let market = spot_market_map.get_ref(&market_index)?;

validate!(
!market.is_operation_paused(SpotOperation::Liquidation),
ErrorCode::InvalidLiquidation,
"Liquidation operation is paused for market {}",
market_index
)?;

drop(market);

// validate user and liquidator have spot position balances
user.get_spot_position(market_index).map_err(|_| {
msg!(
Expand Down
2 changes: 2 additions & 0 deletions programs/drift/src/state/paused_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum PerpOperation {
Fill = 0b00000100,
SettlePnl = 0b00001000,
SettlePnlWithPosition = 0b00010000,
Liquidation = 0b00100000,
}

const ALL_PERP_OPERATIONS: [PerpOperation; 5] = [
Expand Down Expand Up @@ -39,6 +40,7 @@ pub enum SpotOperation {
UpdateCumulativeInterest = 0b00000001,
Fill = 0b00000010,
Withdraw = 0b00000100,
Liquidation = 0b00001000,
}

const ALL_SPOT_OPERATIONS: [SpotOperation; 3] = [
Expand Down

0 comments on commit e5a7e6d

Please sign in to comment.