@@ -443,6 +443,9 @@ pub fn settle_expired_position(
443443 let base_asset_amount = user. perp_positions [ position_index] . base_asset_amount ;
444444 let quote_entry_amount = user. perp_positions [ position_index] . quote_entry_amount ;
445445
446+ let user_position_direction_to_close = user. perp_positions [ position_index] . get_direction_to_close ( ) ;
447+ let user_existing_position_params_for_order_action = user. perp_positions [ position_index] . get_existing_position_params_for_order_action ( user_position_direction_to_close) ;
448+
446449 let position_delta = PositionDelta {
447450 quote_asset_amount : base_asset_value,
448451 base_asset_amount : -user. perp_positions [ position_index] . base_asset_amount ,
@@ -458,15 +461,30 @@ pub fn settle_expired_position(
458461 . safe_mul ( fee_structure. fee_tiers [ 0 ] . fee_numerator as i64 ) ?
459462 . safe_div ( fee_structure. fee_tiers [ 0 ] . fee_denominator as i64 ) ?;
460463
464+ update_quote_asset_and_break_even_amount (
465+ & mut user. perp_positions [ position_index] ,
466+ perp_market,
467+ -fee. abs ( ) ,
468+ ) ?;
469+
470+ let pnl = user. perp_positions [ position_index] . quote_asset_amount ;
471+
472+ let pnl_to_settle_with_user =
473+ update_pnl_pool_and_user_balance ( perp_market, quote_spot_market, user, pnl. cast ( ) ?) ?;
474+
475+ update_quote_asset_amount (
476+ & mut user. perp_positions [ position_index] ,
477+ perp_market,
478+ -pnl_to_settle_with_user. cast ( ) ?,
479+ ) ?;
480+
461481 if position_delta. base_asset_amount != 0 {
462482 // get ids for order fills
463483 let user_order_id = get_then_update_id ! ( user, next_order_id) ;
464484 let fill_record_id = { get_then_update_id ! ( perp_market, next_fill_record_id) } ;
465485
466486 let base_asset_amount = position_delta. base_asset_amount ;
467487 let user_existing_position_direction = user. perp_positions [ position_index] . get_direction ( ) ;
468- let user_position_direction_to_close =
469- user. perp_positions [ position_index] . get_direction_to_close ( ) ;
470488
471489 let user_order = Order {
472490 slot,
@@ -487,21 +505,10 @@ pub fn settle_expired_position(
487505 order: user_order
488506 } ) ;
489507
490- let ( taker_existing_position, taker_existing_position_params_for_order_action) = {
491- let taker_position = user. perp_positions [ position_index] ;
492-
493- (
494- taker_position. base_asset_amount ,
495- taker_position. get_existing_position_params_for_order_action (
496- user_existing_position_direction,
497- ) ,
498- )
499- } ;
500-
501508 let ( taker_existing_quote_entry_amount, taker_existing_base_asset_amount) =
502509 calculate_existing_position_fields_for_order_action (
503510 base_asset_amount. unsigned_abs ( ) ,
504- taker_existing_position_params_for_order_action ,
511+ user_existing_position_params_for_order_action ,
505512 ) ?;
506513
507514 let fill_record = OrderActionRecord {
@@ -534,8 +541,8 @@ pub fn settle_expired_position(
534541 maker_order_cumulative_quote_asset_amount_filled : None ,
535542 oracle_price : perp_market. expiry_price ,
536543 bit_flags : 0 ,
537- taker_existing_quote_entry_amount : Some ( taker_existing_quote_entry_amount . unsigned_abs ( ) ) ,
538- taker_existing_base_asset_amount : Some ( taker_existing_base_asset_amount . unsigned_abs ( ) ) ,
544+ taker_existing_quote_entry_amount,
545+ taker_existing_base_asset_amount,
539546 maker_existing_quote_entry_amount : None ,
540547 maker_existing_base_asset_amount : None ,
541548 trigger_price : None ,
@@ -545,23 +552,6 @@ pub fn settle_expired_position(
545552 emit ! ( fill_record) ;
546553 }
547554
548- update_quote_asset_and_break_even_amount (
549- & mut user. perp_positions [ position_index] ,
550- perp_market,
551- -fee. abs ( ) ,
552- ) ?;
553-
554- let pnl = user. perp_positions [ position_index] . quote_asset_amount ;
555-
556- let pnl_to_settle_with_user =
557- update_pnl_pool_and_user_balance ( perp_market, quote_spot_market, user, pnl. cast ( ) ?) ?;
558-
559- update_quote_asset_amount (
560- & mut user. perp_positions [ position_index] ,
561- perp_market,
562- -pnl_to_settle_with_user. cast ( ) ?,
563- ) ?;
564-
565555 update_settled_pnl ( user, position_index, pnl_to_settle_with_user. cast ( ) ?) ?;
566556
567557 perp_market. amm . base_asset_amount_with_amm = perp_market
0 commit comments