File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed
programs/drift/src/controller Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -414,8 +414,20 @@ pub fn settle_expired_position(
414414) -> DriftResult {
415415 validate ! ( !user. is_bankrupt( ) , ErrorCode :: UserBankrupt ) ?;
416416
417+ let position_index = match get_position_index ( & user. perp_positions , perp_market_index) {
418+ Ok ( index) => index,
419+ Err ( _) => {
420+ msg ! ( "User has no position for market {}" , perp_market_index) ;
421+ return Ok ( ( ) ) ;
422+ }
423+ } ;
424+
425+ let can_skip_margin_calc = user. perp_positions [ position_index] . base_asset_amount == 0
426+ && user. perp_positions [ position_index] . quote_asset_amount > 0 ;
427+
417428 // cannot settle pnl this way on a user who is in liquidation territory
418- if !( meets_maintenance_margin_requirement ( user, perp_market_map, spot_market_map, oracle_map) ?)
429+ if !meets_maintenance_margin_requirement ( user, perp_market_map, spot_market_map, oracle_map) ?
430+ && !can_skip_margin_calc
419431 {
420432 return Err ( ErrorCode :: InsufficientCollateralForSettlingPNL ) ;
421433 }
@@ -452,14 +464,6 @@ pub fn settle_expired_position(
452464 true ,
453465 ) ?;
454466
455- let position_index = match get_position_index ( & user. perp_positions , perp_market_index) {
456- Ok ( index) => index,
457- Err ( _) => {
458- msg ! ( "User has no position for market {}" , perp_market_index) ;
459- return Ok ( ( ) ) ;
460- }
461- } ;
462-
463467 let quote_spot_market = & mut spot_market_map. get_quote_spot_market_mut ( ) ?;
464468 let perp_market = & mut perp_market_map. get_ref_mut ( & perp_market_index) ?;
465469 validate ! (
You can’t perform that action at this time.
0 commit comments