Skip to content

Commit 8e1594c

Browse files
0xbigzcrispheaneywphan
authored
program: use-5min-for-target-expiry-price (#1967)
* program: use-5min-for-target-expiry-price * program: support fill record for base amount at expiry * update for orderrecord * add get_existing_position_params_for_order_action * tweak * clean up * update CHANGELOG.md --------- Co-authored-by: Chris Heaney <[email protected]> Co-authored-by: wphan <[email protected]>
1 parent 8388765 commit 8e1594c

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232

3333
- program: add titan to whitelisted swap programs ([#1952](https://github.com/drift-labs/protocol-v2/pull/1952))
3434
- program: allow hot wallet to increase max spread and pause funding ([#1957](https://github.com/drift-labs/protocol-v2/pull/1957))
35+
- program: use-5min-for-target-expiry-price ([#1967](https://github.com/drift-labs/protocol-v2/pull/1967))
3536

3637
### Fixes
3738

programs/drift/src/controller/pnl.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use crate::math::position::calculate_base_asset_value_with_expiry_price;
2020
use crate::math::safe_math::SafeMath;
2121
use crate::math::spot_balance::get_token_amount;
2222

23+
use crate::get_then_update_id;
24+
use crate::math::orders::calculate_existing_position_fields_for_order_action;
2325
use crate::msg;
26+
use crate::state::events::{OrderAction, OrderActionRecord, OrderRecord};
2427
use crate::state::events::{OrderActionExplanation, SettlePnlExplanation, SettlePnlRecord};
2528
use crate::state::oracle_map::OracleMap;
2629
use crate::state::paused_operations::PerpOperation;
@@ -30,7 +33,7 @@ use crate::state::settle_pnl_mode::SettlePnlMode;
3033
use crate::state::spot_market::{SpotBalance, SpotBalanceType};
3134
use crate::state::spot_market_map::SpotMarketMap;
3235
use crate::state::state::State;
33-
use crate::state::user::{MarketType, User};
36+
use crate::state::user::{MarketType, Order, OrderStatus, OrderType, User};
3437
use crate::validate;
3538
use anchor_lang::prelude::Pubkey;
3639
use anchor_lang::prelude::*;
@@ -438,6 +441,11 @@ pub fn settle_expired_position(
438441
let base_asset_amount = user.perp_positions[position_index].base_asset_amount;
439442
let quote_entry_amount = user.perp_positions[position_index].quote_entry_amount;
440443

444+
let user_position_direction_to_close =
445+
user.perp_positions[position_index].get_direction_to_close();
446+
let user_existing_position_params_for_order_action = user.perp_positions[position_index]
447+
.get_existing_position_params_for_order_action(user_position_direction_to_close);
448+
441449
let position_delta = PositionDelta {
442450
quote_asset_amount: base_asset_value,
443451
base_asset_amount: -user.perp_positions[position_index].base_asset_amount,
@@ -470,6 +478,80 @@ pub fn settle_expired_position(
470478
-pnl_to_settle_with_user.cast()?,
471479
)?;
472480

481+
if position_delta.base_asset_amount != 0 {
482+
// get ids for order fills
483+
let user_order_id = get_then_update_id!(user, next_order_id);
484+
let fill_record_id = get_then_update_id!(perp_market, next_fill_record_id);
485+
486+
let base_asset_amount = position_delta.base_asset_amount;
487+
let user_existing_position_direction = user.perp_positions[position_index].get_direction();
488+
489+
let user_order = Order {
490+
slot,
491+
base_asset_amount: base_asset_amount.unsigned_abs(),
492+
order_id: user_order_id,
493+
market_index: perp_market.market_index,
494+
status: OrderStatus::Open,
495+
order_type: OrderType::Market,
496+
market_type: MarketType::Perp,
497+
direction: user_position_direction_to_close,
498+
existing_position_direction: user_existing_position_direction,
499+
..Order::default()
500+
};
501+
502+
emit!(OrderRecord {
503+
ts: now,
504+
user: *user_key,
505+
order: user_order
506+
});
507+
508+
let (taker_existing_quote_entry_amount, taker_existing_base_asset_amount) =
509+
calculate_existing_position_fields_for_order_action(
510+
base_asset_amount.unsigned_abs(),
511+
user_existing_position_params_for_order_action,
512+
)?;
513+
514+
let fill_record = OrderActionRecord {
515+
ts: now,
516+
action: OrderAction::Fill,
517+
action_explanation: OrderActionExplanation::MarketExpired,
518+
market_index: perp_market.market_index,
519+
market_type: MarketType::Perp,
520+
filler: None,
521+
filler_reward: None,
522+
fill_record_id: Some(fill_record_id),
523+
base_asset_amount_filled: Some(base_asset_amount.unsigned_abs()),
524+
quote_asset_amount_filled: Some(base_asset_value.unsigned_abs()),
525+
taker_fee: Some(fee.unsigned_abs()),
526+
maker_fee: None,
527+
referrer_reward: None,
528+
quote_asset_amount_surplus: None,
529+
spot_fulfillment_method_fee: None,
530+
taker: Some(*user_key),
531+
taker_order_id: Some(user_order_id),
532+
taker_order_direction: Some(user_position_direction_to_close),
533+
taker_order_base_asset_amount: Some(base_asset_amount.unsigned_abs()),
534+
taker_order_cumulative_base_asset_amount_filled: Some(base_asset_amount.unsigned_abs()),
535+
taker_order_cumulative_quote_asset_amount_filled: Some(base_asset_value.unsigned_abs()),
536+
maker: None,
537+
maker_order_id: None,
538+
maker_order_direction: None,
539+
maker_order_base_asset_amount: None,
540+
maker_order_cumulative_base_asset_amount_filled: None,
541+
maker_order_cumulative_quote_asset_amount_filled: None,
542+
oracle_price: perp_market.expiry_price,
543+
bit_flags: 0,
544+
taker_existing_quote_entry_amount,
545+
taker_existing_base_asset_amount,
546+
maker_existing_quote_entry_amount: None,
547+
maker_existing_base_asset_amount: None,
548+
trigger_price: None,
549+
builder_idx: None,
550+
builder_fee: None,
551+
};
552+
emit!(fill_record);
553+
}
554+
473555
update_settled_pnl(user, position_index, pnl_to_settle_with_user.cast()?)?;
474556

475557
perp_market.amm.base_asset_amount_with_amm = perp_market

programs/drift/src/controller/pnl/delisting.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub mod delisting_test {
209209
amm_jit_intensity: 100,
210210
historical_oracle_data: HistoricalOracleData {
211211
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
212+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
212213
..HistoricalOracleData::default()
213214
},
214215
quote_asset_amount: -(QUOTE_PRECISION_I128 * 50), //longs have $100 cost basis
@@ -318,6 +319,7 @@ pub mod delisting_test {
318319
amm_jit_intensity: 100,
319320
historical_oracle_data: HistoricalOracleData {
320321
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
322+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
321323
..HistoricalOracleData::default()
322324
},
323325
quote_asset_amount: -(QUOTE_PRECISION_I128 * 10), //longs have $20 cost basis
@@ -430,6 +432,7 @@ pub mod delisting_test {
430432
amm_jit_intensity: 100,
431433
historical_oracle_data: HistoricalOracleData {
432434
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
435+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
433436
..HistoricalOracleData::default()
434437
},
435438
total_fee_minus_distributions: -(100000 * QUOTE_PRECISION_I128), // down $100k
@@ -543,6 +546,7 @@ pub mod delisting_test {
543546
amm_jit_intensity: 100,
544547
historical_oracle_data: HistoricalOracleData {
545548
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
549+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
546550
..HistoricalOracleData::default()
547551
},
548552
total_fee_minus_distributions: -(100000 * QUOTE_PRECISION_I128), // down $100k
@@ -652,6 +656,7 @@ pub mod delisting_test {
652656
amm_jit_intensity: 100,
653657
historical_oracle_data: HistoricalOracleData {
654658
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
659+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
655660
..HistoricalOracleData::default()
656661
},
657662
quote_asset_amount: -(QUOTE_PRECISION_I128 * 10), //longs have $20 cost basis
@@ -870,6 +875,8 @@ pub mod delisting_test {
870875
amm_jit_intensity: 100,
871876
historical_oracle_data: HistoricalOracleData {
872877
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
878+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
879+
873880
..HistoricalOracleData::default()
874881
},
875882
quote_asset_amount: (QUOTE_PRECISION_I128 * 10), //longs have -$20 cost basis
@@ -1091,6 +1098,8 @@ pub mod delisting_test {
10911098
amm_jit_intensity: 100,
10921099
historical_oracle_data: HistoricalOracleData {
10931100
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
1101+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
1102+
10941103
..HistoricalOracleData::default()
10951104
},
10961105
quote_asset_amount: (QUOTE_PRECISION_I128 * 20 * 2000), //longs have -$20 cost basis
@@ -1294,6 +1303,8 @@ pub mod delisting_test {
12941303
amm_jit_intensity: 100,
12951304
historical_oracle_data: HistoricalOracleData {
12961305
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
1306+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
1307+
12971308
..HistoricalOracleData::default()
12981309
},
12991310
quote_asset_amount: -(QUOTE_PRECISION_I128 * 20 * 1000 - QUOTE_PRECISION_I128),
@@ -1717,6 +1728,8 @@ pub mod delisting_test {
17171728
amm_jit_intensity: 100,
17181729
historical_oracle_data: HistoricalOracleData {
17191730
last_oracle_price_twap: (99 * PRICE_PRECISION) as i64,
1731+
last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64,
1732+
17201733
..HistoricalOracleData::default()
17211734
},
17221735
quote_asset_amount: (QUOTE_PRECISION_I128 * 200)

programs/drift/src/controller/repeg.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ pub fn settle_expired_market(
431431
let target_expiry_price = if market.amm.oracle_source == OracleSource::Prelaunch {
432432
market.amm.historical_oracle_data.last_oracle_price
433433
} else {
434-
market.amm.historical_oracle_data.last_oracle_price_twap
434+
market
435+
.amm
436+
.historical_oracle_data
437+
.last_oracle_price_twap_5min
435438
};
436439

437440
crate::dlog!(target_expiry_price);

0 commit comments

Comments
 (0)