Skip to content

Commit fb8c70a

Browse files
committed
change tests
1 parent 2effb9c commit fb8c70a

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

programs/drift/src/state/order_params.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl OrderParams {
722722

723723
let baseline_start_price_offset = if baseline_start_price_offset_slow
724724
.abs_diff(baseline_start_price_offset_fast)
725-
< perp_market.amm.last_mark_price_twap_5min / 200
725+
<= perp_market.amm.last_mark_price_twap_5min / 200
726726
{
727727
let frac_of_long_spread_in_price: i64 = perp_market
728728
.amm
@@ -748,6 +748,7 @@ impl OrderParams {
748748
}
749749
} else {
750750
// more than 50bps different of fast/slow twap, use fast only
751+
crate::dlog!("hi", baseline_start_price_offset_slow, baseline_start_price_offset_fast);
751752
baseline_start_price_offset_fast
752753
};
753754

@@ -897,15 +898,15 @@ fn get_auction_duration(
897898
) -> DriftResult<u8> {
898899
let percent_diff = price_diff.safe_mul(PERCENTAGE_PRECISION_U64)?.div(price);
899900

900-
let slots_per_bp = if contract_tier.is_as_safe_as_contract(&ContractTier::B) {
901+
let slots_per_pct = if contract_tier.is_as_safe_as_contract(&ContractTier::B) {
901902
100
902903
} else {
903904
60
904905
};
905906

906907
Ok(percent_diff
907-
.safe_mul(slots_per_bp)?
908-
.safe_div_ceil(PERCENTAGE_PRECISION_U64 / 100)? // 1% = 60 slots
908+
.safe_mul(slots_per_pct)?
909+
.safe_div_ceil(PERCENTAGE_PRECISION_U64 / 100)? // 1% = 40 slots
909910
.clamp(1, 180) as u8) // 180 slots max
910911
}
911912

programs/drift/src/state/order_params/tests.rs

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,11 @@ mod update_perp_auction_params {
409409
..AMM::default()
410410
};
411411
amm.last_bid_price_twap = (oracle_price * 15 / 10 - 192988) as u64;
412-
amm.last_mark_price_twap_5min = (oracle_price * 16 / 10) as u64;
412+
amm.last_mark_price_twap_5min = (oracle_price * 155 / 100) as u64;
413413
amm.last_ask_price_twap = (oracle_price * 16 / 10 + 192988) as u64;
414414
amm.historical_oracle_data.last_oracle_price_twap = oracle_price;
415415
amm.historical_oracle_data.last_oracle_price_twap_5min = oracle_price;
416+
amm.last_mark_price_twap_5min = (amm.last_ask_price_twap + amm.last_bid_price_twap) / 2;
416417

417418
amm.historical_oracle_data.last_oracle_price = oracle_price;
418419
let perp_market = PerpMarket {
@@ -436,7 +437,7 @@ mod update_perp_auction_params {
436437
.update_perp_auction_params(&perp_market, oracle_price, false)
437438
.unwrap();
438439

439-
assert_eq!(order_params_after.auction_start_price, Some(72_524_319));
440+
assert_eq!(order_params_after.auction_start_price, Some(79_750_000));
440441
assert_eq!(order_params_after.auction_end_price, Some(90_092_988));
441442
assert_eq!(order_params_after.auction_duration, Some(180));
442443
}
@@ -456,13 +457,13 @@ mod update_perp_auction_params {
456457
..AMM::default()
457458
};
458459
amm.last_bid_price_twap = (oracle_price * 99 / 100) as u64;
459-
amm.last_mark_price_twap_5min = oracle_price as u64;
460460
amm.last_ask_price_twap = (oracle_price * 101 / 100) as u64;
461461
amm.historical_oracle_data.last_oracle_price_twap = oracle_price;
462462
amm.historical_oracle_data.last_oracle_price_twap_5min = oracle_price;
463+
amm.last_mark_price_twap_5min = (amm.last_ask_price_twap + amm.last_bid_price_twap) / 2;
463464

464465
amm.historical_oracle_data.last_oracle_price = oracle_price;
465-
let perp_market = PerpMarket {
466+
let mut perp_market = PerpMarket {
466467
amm,
467468
..PerpMarket::default()
468469
};
@@ -563,10 +564,10 @@ mod update_perp_auction_params {
563564
.update_perp_auction_params(&perp_market, oracle_price, false)
564565
.unwrap();
565566
assert_ne!(order_params_before, order_params_after);
566-
assert_eq!(order_params_after.auction_duration, Some(175));
567+
assert_eq!(order_params_after.auction_duration, Some(120));
567568
assert_eq!(
568569
order_params_after.auction_start_price,
569-
Some(100 * PRICE_PRECISION_I64 - 901000)
570+
Some(100 * PRICE_PRECISION_I64)
570571
);
571572
assert_eq!(
572573
order_params_after.auction_end_price,
@@ -599,20 +600,25 @@ mod update_perp_auction_params {
599600
direction: PositionDirection::Short,
600601
..OrderParams::default()
601602
};
603+
604+
// tighten bid/ask to mark twap 5min to activate buffer
605+
perp_market.amm.last_bid_price_twap = amm.last_mark_price_twap_5min - 100000;
606+
perp_market.amm.last_ask_price_twap = amm.last_mark_price_twap_5min + 100000;
602607
let mut order_params_after = order_params_before;
603608
order_params_after
604609
.update_perp_auction_params(&perp_market, oracle_price, false)
605610
.unwrap();
606611
assert_ne!(order_params_before, order_params_after);
607-
assert_eq!(order_params_after.auction_duration, Some(174));
608612
assert_eq!(
609613
order_params_after.auction_start_price,
610-
Some(100 * PRICE_PRECISION_I64 + 899000) // %1 / 10 = 10 bps aggression
614+
Some(100 * PRICE_PRECISION_I64 + 100100) // a bit more passive than mid
611615
);
612616
assert_eq!(
613617
order_params_after.auction_end_price,
614618
Some(98 * PRICE_PRECISION_I64)
615619
);
620+
assert_eq!(order_params_after.auction_duration, Some(127));
621+
616622
}
617623

618624
#[test]
@@ -786,10 +792,12 @@ mod update_perp_auction_params {
786792
};
787793
amm.historical_oracle_data.last_oracle_price = oracle_price;
788794
amm.historical_oracle_data.last_oracle_price_twap = oracle_price - 97238;
795+
amm.historical_oracle_data.last_oracle_price_twap_5min = oracle_price - 97238;
789796
amm.last_ask_price_twap =
790797
(amm.historical_oracle_data.last_oracle_price_twap as u64) + 217999;
791798
amm.last_bid_price_twap =
792799
(amm.historical_oracle_data.last_oracle_price_twap as u64) + 17238;
800+
amm.last_mark_price_twap_5min = (amm.last_ask_price_twap + amm.last_bid_price_twap) / 2;
793801

794802
let mut perp_market = PerpMarket {
795803
amm,
@@ -812,7 +820,7 @@ mod update_perp_auction_params {
812820
.update_perp_auction_params(&perp_market, oracle_price, false)
813821
.unwrap();
814822
assert_ne!(order_params_before, order_params_after);
815-
assert_eq!(order_params_after.auction_start_price.unwrap(), 98901080);
823+
assert_eq!(order_params_after.auction_start_price.unwrap(), 99018698);
816824
let amm_bid_price = amm.bid_price(amm.reserve_price().unwrap()).unwrap();
817825
assert_eq!(amm_bid_price, 98010000);
818826
assert!(order_params_after.auction_start_price.unwrap() as u64 > amm_bid_price);
@@ -832,7 +840,7 @@ mod update_perp_auction_params {
832840
.update_perp_auction_params(&perp_market, oracle_price, false)
833841
.unwrap();
834842
assert_ne!(order_params_before, order_params_after);
835-
assert_eq!(order_params_after.auction_start_price.unwrap(), 99118879);
843+
assert_eq!(order_params_after.auction_start_price.unwrap(), 99216738);
836844

837845
// skip for prelaunch oracle
838846
perp_market.amm.oracle_source = OracleSource::Prelaunch;
@@ -893,12 +901,13 @@ mod update_perp_auction_params {
893901
..AMM::default()
894902
};
895903
amm.historical_oracle_data.last_oracle_price = oracle_price;
904+
amm.historical_oracle_data.last_oracle_price_twap_5min = oracle_price - 97238;
896905
amm.historical_oracle_data.last_oracle_price_twap = oracle_price - 97238;
897906
amm.last_ask_price_twap =
898907
(amm.historical_oracle_data.last_oracle_price_twap as u64) + 217999;
899908
amm.last_bid_price_twap =
900909
(amm.historical_oracle_data.last_oracle_price_twap as u64) + 17238;
901-
910+
amm.last_mark_price_twap_5min = (amm.last_ask_price_twap + amm.last_bid_price_twap) / 2;
902911
let perp_market = PerpMarket {
903912
amm,
904913
contract_tier: ContractTier::B,
@@ -920,7 +929,8 @@ mod update_perp_auction_params {
920929
.update_perp_auction_params(&perp_market, oracle_price, false)
921930
.unwrap();
922931
assert_ne!(order_params_before, order_params_after);
923-
assert_eq!(order_params_after.auction_start_price.unwrap(), -98920);
932+
assert_eq!(order_params_after.auction_start_price.unwrap(), 18698);
933+
assert_eq!(order_params_after.auction_end_price.unwrap(), 2196053);
924934

925935
let order_params_before = OrderParams {
926936
order_type: OrderType::Oracle,
@@ -985,10 +995,12 @@ mod update_perp_auction_params {
985995
};
986996
amm.historical_oracle_data.last_oracle_price = oracle_price;
987997
amm.historical_oracle_data.last_oracle_price_twap = oracle_price - 97238;
998+
amm.historical_oracle_data.last_oracle_price_twap_5min = oracle_price - 97238;
988999
amm.last_ask_price_twap =
9891000
(amm.historical_oracle_data.last_oracle_price_twap as u64) + 217999;
9901001
amm.last_bid_price_twap =
9911002
(amm.historical_oracle_data.last_oracle_price_twap as u64) + 17238;
1003+
amm.last_mark_price_twap_5min = (amm.last_ask_price_twap + amm.last_bid_price_twap) / 2;
9921004

9931005
let perp_market = PerpMarket {
9941006
amm,
@@ -1011,7 +1023,7 @@ mod update_perp_auction_params {
10111023
.update_perp_auction_params(&perp_market, oracle_price, false)
10121024
.unwrap();
10131025
assert_ne!(order_params_before, order_params_after);
1014-
assert_eq!(order_params_after.auction_start_price.unwrap(), 98653580);
1026+
assert_eq!(order_params_after.auction_start_price.unwrap(), 98771198);
10151027

10161028
let order_params_before = OrderParams {
10171029
order_type: OrderType::Market,
@@ -1030,7 +1042,7 @@ mod update_perp_auction_params {
10301042
assert_ne!(order_params_before, order_params_after);
10311043
assert_eq!(
10321044
order_params_after.auction_start_price.unwrap(),
1033-
(99 * PRICE_PRECISION_I64 - oracle_price / 400) - 98920 // approx equal with some noise
1045+
(99 * PRICE_PRECISION_I64 - oracle_price / 400 + 18698) // approx equal with some noise
10341046
);
10351047

10361048
let order_params_before = OrderParams {
@@ -1049,7 +1061,7 @@ mod update_perp_auction_params {
10491061
.unwrap();
10501062
assert_eq!(
10511063
order_params_after.auction_start_price.unwrap(),
1052-
99118879 + oracle_price / 400
1064+
99118879 + oracle_price / 400 + 97859
10531065
);
10541066

10551067
let order_params_before = OrderParams {
@@ -1069,7 +1081,7 @@ mod update_perp_auction_params {
10691081
assert_ne!(order_params_before, order_params_after);
10701082
assert_eq!(
10711083
order_params_after.auction_start_price.unwrap(),
1072-
(99 * PRICE_PRECISION_U64 + 100000) as i64 + oracle_price / 400 + 18879 // use limit price and oracle buffer with some noise
1084+
(99 * PRICE_PRECISION_U64 + 100000) as i64 + oracle_price / 400 + 116738 // use limit price and oracle buffer with some noise
10731085
);
10741086

10751087
let order_params_before = OrderParams {
@@ -1088,11 +1100,11 @@ mod update_perp_auction_params {
10881100
.unwrap();
10891101
assert_eq!(
10901102
order_params_after.auction_start_price.unwrap(),
1091-
99118879 + oracle_price / 400
1103+
99118879 + oracle_price / 400 + 97859
10921104
);
10931105
assert_eq!(order_params_after.auction_end_price.unwrap(), 98028211);
10941106

1095-
assert_eq!(order_params_after.auction_duration, Some(82));
1107+
assert_eq!(order_params_after.auction_duration, Some(88));
10961108

10971109
let order_params_before = OrderParams {
10981110
order_type: OrderType::Market,
@@ -1110,11 +1122,11 @@ mod update_perp_auction_params {
11101122
.unwrap();
11111123
assert_eq!(
11121124
order_params_after.auction_start_price.unwrap(),
1113-
98901080 - oracle_price / 400
1125+
98901080 - oracle_price / 400 + 117618
11141126
);
11151127
assert_eq!(order_params_after.auction_end_price.unwrap(), 100207026);
11161128

1117-
assert_eq!(order_params_after.auction_duration, Some(95));
1129+
assert_eq!(order_params_after.auction_duration, Some(88));
11181130
}
11191131

11201132
#[test]
@@ -1325,8 +1337,11 @@ mod get_close_perp_params {
13251337
let amm = AMM {
13261338
last_ask_price_twap: 101 * PRICE_PRECISION_U64,
13271339
last_bid_price_twap: 99 * PRICE_PRECISION_U64,
1340+
last_mark_price_twap_5min: 99 * PRICE_PRECISION_U64,
13281341
historical_oracle_data: HistoricalOracleData {
13291342
last_oracle_price_twap: 100 * PRICE_PRECISION_I64,
1343+
last_oracle_price_twap_5min: 100 * PRICE_PRECISION_I64,
1344+
13301345
..HistoricalOracleData::default()
13311346
},
13321347
mark_std: PRICE_PRECISION_U64,
@@ -1385,7 +1400,7 @@ mod get_close_perp_params {
13851400
let auction_start_price = params.auction_start_price.unwrap();
13861401
let auction_end_price = params.auction_end_price.unwrap();
13871402
let oracle_price_offset = params.oracle_price_offset.unwrap();
1388-
assert_eq!(auction_start_price, PRICE_PRECISION_I64);
1403+
assert_eq!(auction_start_price, 2 * PRICE_PRECISION_I64);
13891404
assert_eq!(auction_end_price, 4 * PRICE_PRECISION_I64);
13901405
assert_eq!(oracle_price_offset, 4 * PRICE_PRECISION_I64 as i32);
13911406

@@ -1420,7 +1435,7 @@ mod get_close_perp_params {
14201435
let auction_start_price = params.auction_start_price.unwrap();
14211436
let auction_end_price = params.auction_end_price.unwrap();
14221437
let oracle_price_offset = params.oracle_price_offset.unwrap();
1423-
assert_eq!(auction_start_price, -3 * PRICE_PRECISION_I64);
1438+
assert_eq!(auction_start_price, -2 * PRICE_PRECISION_I64);
14241439
assert_eq!(auction_end_price, 0);
14251440
assert_eq!(oracle_price_offset, 0);
14261441

@@ -1436,8 +1451,11 @@ mod get_close_perp_params {
14361451
let amm = AMM {
14371452
last_ask_price_twap: 101 * PRICE_PRECISION_U64,
14381453
last_bid_price_twap: 99 * PRICE_PRECISION_U64,
1454+
last_mark_price_twap_5min: 100 * PRICE_PRECISION_U64,
1455+
14391456
historical_oracle_data: HistoricalOracleData {
14401457
last_oracle_price_twap: 100 * PRICE_PRECISION_I64,
1458+
last_oracle_price_twap_5min: 100 * PRICE_PRECISION_I64,
14411459
..HistoricalOracleData::default()
14421460
},
14431461
mark_std: PRICE_PRECISION_U64,
@@ -1462,7 +1480,7 @@ mod get_close_perp_params {
14621480
let auction_start_price = params.auction_start_price.unwrap();
14631481
let auction_end_price = params.auction_end_price.unwrap();
14641482
let oracle_price_offset = params.oracle_price_offset.unwrap();
1465-
assert_eq!(auction_start_price, 1000000);
1483+
assert_eq!(auction_start_price, 0);
14661484
assert_eq!(auction_end_price, -2 * PRICE_PRECISION_I64);
14671485
assert_eq!(oracle_price_offset, -2 * PRICE_PRECISION_I64 as i32);
14681486

@@ -1498,7 +1516,7 @@ mod get_close_perp_params {
14981516
let auction_start_price = params.auction_start_price.unwrap();
14991517
let auction_end_price = params.auction_end_price.unwrap();
15001518
let oracle_price_offset = params.oracle_price_offset.unwrap();
1501-
assert_eq!(auction_start_price, 3 * PRICE_PRECISION_I64);
1519+
assert_eq!(auction_start_price, 2 * PRICE_PRECISION_I64);
15021520
assert_eq!(auction_end_price, 0);
15031521
assert_eq!(oracle_price_offset, 0);
15041522

@@ -1536,7 +1554,7 @@ mod get_close_perp_params {
15361554
let auction_start_price = params.auction_start_price.unwrap();
15371555
let auction_end_price = params.auction_end_price.unwrap();
15381556
let oracle_price_offset = params.oracle_price_offset.unwrap();
1539-
assert_eq!(auction_start_price, -PRICE_PRECISION_I64);
1557+
assert_eq!(auction_start_price, -2 * PRICE_PRECISION_I64);
15401558
assert_eq!(auction_end_price, -4 * PRICE_PRECISION_I64);
15411559
assert_eq!(oracle_price_offset, -4 * PRICE_PRECISION_I64 as i32);
15421560

@@ -1613,7 +1631,7 @@ mod get_close_perp_params {
16131631
let auction_start_price = params.auction_start_price.unwrap();
16141632
let auction_end_price = params.auction_end_price.unwrap();
16151633
let oracle_price_offset = params.oracle_price_offset.unwrap();
1616-
assert_eq!(auction_start_price, 641);
1634+
assert_eq!(auction_start_price, 284);
16171635
assert_eq!(auction_end_price, -1021);
16181636
assert_eq!(oracle_price_offset, -1021);
16191637

0 commit comments

Comments
 (0)