Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion programs/drift/src/controller/pnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,16 @@ pub fn settle_pnl(

drop(market);

let position_index = get_position_index(&user.perp_positions, market_index)?;
let position_index = match get_position_index(&user.perp_positions, market_index) {
Ok(index) => index,
Err(e) => {
return mode.result(
e,
market_index,
&format!("User has no position in market {}", market_index),
)
}
};
let unrealized_pnl = user.perp_positions[position_index].get_unrealized_pnl(oracle_price)?;

// cannot settle negative pnl this way on a user who is in liquidation territory
Expand Down
12 changes: 4 additions & 8 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,8 +1104,7 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
&mut oracle_map,
state,
&clock,
)
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
)?;

controller::pnl::settle_pnl(
market_index,
Expand All @@ -1119,8 +1118,7 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
state,
None,
SettlePnlMode::MustSettle,
)
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
)?;
}

if state.builder_codes_enabled() || state.builder_referral_enabled() {
Expand Down Expand Up @@ -1220,8 +1218,7 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
&mut oracle_map,
state,
&clock,
)
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
)?;

controller::pnl::settle_pnl(
*market_index,
Expand All @@ -1235,8 +1232,7 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
state,
Some(meets_margin_requirement),
mode,
)
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
)?;
}

if state.builder_codes_enabled() || state.builder_referral_enabled() {
Expand Down
Loading