Skip to content

Commit bfef3f4

Browse files
authored
program: allow settling positive pnl expired pos during liquidation (#1959)
* program: allow settling positive pnl expired pos during liquidation * fix
1 parent ee0bf58 commit bfef3f4

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
@@ -353,8 +353,20 @@ pub fn settle_expired_position(
353353
) -> DriftResult {
354354
validate!(!user.is_bankrupt(), ErrorCode::UserBankrupt)?;
355355

356+
let position_index = match get_position_index(&user.perp_positions, perp_market_index) {
357+
Ok(index) => index,
358+
Err(_) => {
359+
msg!("User has no position for market {}", perp_market_index);
360+
return Ok(());
361+
}
362+
};
363+
364+
let can_skip_margin_calc = user.perp_positions[position_index].base_asset_amount == 0
365+
&& user.perp_positions[position_index].quote_asset_amount > 0;
366+
356367
// cannot settle pnl this way on a user who is in liquidation territory
357-
if !(meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map)?)
368+
if !meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map)?
369+
&& !can_skip_margin_calc
358370
{
359371
return Err(ErrorCode::InsufficientCollateralForSettlingPNL);
360372
}
@@ -390,14 +402,6 @@ pub fn settle_expired_position(
390402
None,
391403
)?;
392404

393-
let position_index = match get_position_index(&user.perp_positions, perp_market_index) {
394-
Ok(index) => index,
395-
Err(_) => {
396-
msg!("User has no position for market {}", perp_market_index);
397-
return Ok(());
398-
}
399-
};
400-
401405
let quote_spot_market = &mut spot_market_map.get_quote_spot_market_mut()?;
402406
let perp_market = &mut perp_market_map.get_ref_mut(&perp_market_index)?;
403407
validate!(

0 commit comments

Comments
 (0)