Skip to content

Commit e211be2

Browse files
committed
add get_existing_position_params_for_order_action
1 parent cf4e0fd commit e211be2

File tree

1 file changed

+38
-20
lines changed
  • programs/drift/src/controller

1 file changed

+38
-20
lines changed

programs/drift/src/controller/pnl.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::math::safe_math::SafeMath;
2121
use crate::math::spot_balance::get_token_amount;
2222

2323
use crate::get_then_update_id;
24+
use crate::math::orders::calculate_existing_position_fields_for_order_action;
2425
use crate::msg;
2526
use crate::state::events::{
2627
LiquidateBorrowForPerpPnlRecord, LiquidatePerpPnlForDepositRecord, LiquidatePerpRecord,
@@ -457,23 +458,6 @@ pub fn settle_expired_position(
457458
.safe_mul(fee_structure.fee_tiers[0].fee_numerator as i64)?
458459
.safe_div(fee_structure.fee_tiers[0].fee_denominator as i64)?;
459460

460-
update_quote_asset_and_break_even_amount(
461-
&mut user.perp_positions[position_index],
462-
perp_market,
463-
-fee.abs(),
464-
)?;
465-
466-
let pnl = user.perp_positions[position_index].quote_asset_amount;
467-
468-
let pnl_to_settle_with_user =
469-
update_pnl_pool_and_user_balance(perp_market, quote_spot_market, user, pnl.cast()?)?;
470-
471-
update_quote_asset_amount(
472-
&mut user.perp_positions[position_index],
473-
perp_market,
474-
-pnl_to_settle_with_user.cast()?,
475-
)?;
476-
477461
if position_delta.base_asset_amount != 0 {
478462
// get ids for order fills
479463
let user_order_id = get_then_update_id!(user, next_order_id);
@@ -503,6 +487,23 @@ pub fn settle_expired_position(
503487
order: user_order
504488
});
505489

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+
501+
let (taker_existing_quote_entry_amount, taker_existing_base_asset_amount) =
502+
calculate_existing_position_fields_for_order_action(
503+
base_asset_amount.unsigned_abs(),
504+
taker_existing_position_params_for_order_action,
505+
)?;
506+
506507
let fill_record = OrderActionRecord {
507508
ts: now,
508509
action: OrderAction::Fill,
@@ -513,7 +514,7 @@ pub fn settle_expired_position(
513514
filler_reward: None,
514515
fill_record_id: Some(fill_record_id),
515516
base_asset_amount_filled: Some(base_asset_amount.unsigned_abs()),
516-
quote_asset_amount_filled: Some(pnl.unsigned_abs()),
517+
quote_asset_amount_filled: Some(base_asset_value.unsigned_abs()),
517518
taker_fee: Some(fee.unsigned_abs()),
518519
maker_fee: None,
519520
referrer_reward: None,
@@ -533,8 +534,8 @@ pub fn settle_expired_position(
533534
maker_order_cumulative_quote_asset_amount_filled: None,
534535
oracle_price: perp_market.expiry_price,
535536
bit_flags: 0,
536-
taker_existing_quote_entry_amount: Some(quote_entry_amount.unsigned_abs()),
537-
taker_existing_base_asset_amount: Some(base_asset_amount.unsigned_abs()),
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()),
538539
maker_existing_quote_entry_amount: None,
539540
maker_existing_base_asset_amount: None,
540541
trigger_price: None,
@@ -544,6 +545,23 @@ pub fn settle_expired_position(
544545
emit!(fill_record);
545546
}
546547

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+
547565
update_settled_pnl(user, position_index, pnl_to_settle_with_user.cast()?)?;
548566

549567
perp_market.amm.base_asset_amount_with_amm = perp_market

0 commit comments

Comments
 (0)