Skip to content

Commit 08ff4cb

Browse files
crispheaneymoosecat2
authored andcommitted
program: allow settling positive pnl expired pos during liquidation (#1959)
* program: allow settling positive pnl expired pos during liquidation * fix
1 parent 878e3ba commit 08ff4cb

File tree

1 file changed

+13
-9
lines changed
  • programs/drift/src/controller

1 file changed

+13
-9
lines changed

programs/drift/src/controller/pnl.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,20 @@ pub fn settle_expired_position(
414414
) -> DriftResult {
415415
validate!(!user.is_bankrupt(), ErrorCode::UserBankrupt)?;
416416

417+
let position_index = match get_position_index(&user.perp_positions, perp_market_index) {
418+
Ok(index) => index,
419+
Err(_) => {
420+
msg!("User has no position for market {}", perp_market_index);
421+
return Ok(());
422+
}
423+
};
424+
425+
let can_skip_margin_calc = user.perp_positions[position_index].base_asset_amount == 0
426+
&& user.perp_positions[position_index].quote_asset_amount > 0;
427+
417428
// cannot settle pnl this way on a user who is in liquidation territory
418-
if !(meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map)?)
429+
if !meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map)?
430+
&& !can_skip_margin_calc
419431
{
420432
return Err(ErrorCode::InsufficientCollateralForSettlingPNL);
421433
}
@@ -452,14 +464,6 @@ pub fn settle_expired_position(
452464
true,
453465
)?;
454466

455-
let position_index = match get_position_index(&user.perp_positions, perp_market_index) {
456-
Ok(index) => index,
457-
Err(_) => {
458-
msg!("User has no position for market {}", perp_market_index);
459-
return Ok(());
460-
}
461-
};
462-
463467
let quote_spot_market = &mut spot_market_map.get_quote_spot_market_mut()?;
464468
let perp_market = &mut perp_market_map.get_ref_mut(&perp_market_index)?;
465469
validate!(

0 commit comments

Comments
 (0)