diff --git a/programs/drift/src/controller/pnl.rs b/programs/drift/src/controller/pnl.rs index c46d46763d..c08912beb5 100644 --- a/programs/drift/src/controller/pnl.rs +++ b/programs/drift/src/controller/pnl.rs @@ -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 diff --git a/programs/drift/src/instructions/keeper.rs b/programs/drift/src/instructions/keeper.rs index 3fb3db113a..cc8c87b321 100644 --- a/programs/drift/src/instructions/keeper.rs +++ b/programs/drift/src/instructions/keeper.rs @@ -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, @@ -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() { @@ -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, @@ -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() {