Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions programs/drift/src/controller/pnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,20 @@ pub fn settle_expired_position(
) -> DriftResult {
validate!(!user.is_bankrupt(), ErrorCode::UserBankrupt)?;

let position_index = match get_position_index(&user.perp_positions, perp_market_index) {
Ok(index) => index,
Err(_) => {
msg!("User has no position for market {}", perp_market_index);
return Ok(());
}
};

let can_skip_margin_calc = user.perp_positions[position_index].base_asset_amount == 0
&& user.perp_positions[position_index].quote_asset_amount > 0;

// cannot settle pnl this way on a user who is in liquidation territory
if !(meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map)?)
if !meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map)?
&& !can_skip_margin_calc
{
return Err(ErrorCode::InsufficientCollateralForSettlingPNL);
}
Expand Down Expand Up @@ -390,14 +402,6 @@ pub fn settle_expired_position(
None,
)?;

let position_index = match get_position_index(&user.perp_positions, perp_market_index) {
Ok(index) => index,
Err(_) => {
msg!("User has no position for market {}", perp_market_index);
return Ok(());
}
};

let quote_spot_market = &mut spot_market_map.get_quote_spot_market_mut()?;
let perp_market = &mut perp_market_map.get_ref_mut(&perp_market_index)?;
validate!(
Expand Down
Loading