Skip to content

Commit 4eba379

Browse files
committed
move check inside settle_pnl, ensure balance checks are done
1 parent b09c15c commit 4eba379

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

programs/drift/src/controller/pnl.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ pub fn settle_pnl(
7979

8080
drop(market);
8181

82-
let position_index = get_position_index(&user.perp_positions, market_index)?;
82+
let position_index = match get_position_index(&user.perp_positions, market_index) {
83+
Ok(index) => index,
84+
Err(e) => {
85+
return mode.result(
86+
e,
87+
market_index,
88+
&format!("User has no position in market {}", market_index),
89+
)
90+
}
91+
};
8392
let unrealized_pnl = user.perp_positions[position_index].get_unrealized_pnl(oracle_price)?;
8493

8594
// cannot settle negative pnl this way on a user who is in liquidation territory

programs/drift/src/instructions/keeper.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
11071107
)
11081108
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
11091109

1110-
let res = controller::pnl::settle_pnl(
1110+
controller::pnl::settle_pnl(
11111111
market_index,
11121112
user,
11131113
ctx.accounts.authority.key,
@@ -1119,14 +1119,7 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
11191119
state,
11201120
None,
11211121
SettlePnlMode::MustSettle,
1122-
);
1123-
if let Err(e) = res {
1124-
if e != ErrorCode::UserHasNoPositionInMarket {
1125-
return Err(e.into());
1126-
} else {
1127-
msg!("User has no position in market {}", market_index);
1128-
}
1129-
}
1122+
)?;
11301123
}
11311124

11321125
if state.builder_codes_enabled() || state.builder_referral_enabled() {
@@ -1229,7 +1222,7 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
12291222
)
12301223
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
12311224

1232-
let res = controller::pnl::settle_pnl(
1225+
controller::pnl::settle_pnl(
12331226
*market_index,
12341227
user,
12351228
ctx.accounts.authority.key,
@@ -1241,14 +1234,7 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
12411234
state,
12421235
Some(meets_margin_requirement),
12431236
mode,
1244-
);
1245-
if let Err(e) = res {
1246-
if e != ErrorCode::UserHasNoPositionInMarket {
1247-
return Err(e.into());
1248-
} else {
1249-
msg!("User has no position in market {}", market_index);
1250-
}
1251-
}
1237+
)?;
12521238
}
12531239

12541240
if state.builder_codes_enabled() || state.builder_referral_enabled() {

0 commit comments

Comments
 (0)