Skip to content

Commit 6a226af

Browse files
moosecat2crispheaney
authored andcommitted
program: make mm oracle sequence id based (drift-labs#1834)
* make mm oracle sequence id based * sdkbug fix * add volatiltiy check for sequence ids * add zero checks for sequnce ids * safe div * CHANGELOG * cargo fmt -- --------- Co-authored-by: Chris Heaney <[email protected]>
1 parent 5f87b51 commit 6a226af

File tree

20 files changed

+192
-4
lines changed

20 files changed

+192
-4
lines changed

CHANGELOG.md

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

1010
### Features
1111

12+
- program: add sequence id to exchange/mm oracle ([#1834](https://github.com/drift-labs/protocol-v2/pull/1834))
1213
- program: perp position max margin ratio ([#1847](https://github.com/drift-labs/protocol-v2/pull/1847))
1314
- program: rm lp ([#1755](https://github.com/drift-labs/protocol-v2/pull/1755))
1415

programs/drift/src/controller/amm/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ fn formualic_k_tests() {
133133
confidence: 0,
134134
delay: 2,
135135
has_sufficient_number_of_data_points: true,
136+
sequence_id: None,
136137
};
137138

138139
// zero funding cost
@@ -192,6 +193,7 @@ fn iterative_bounds_formualic_k_tests() {
192193
confidence: 0,
193194
delay: 2,
194195
has_sufficient_number_of_data_points: true,
196+
sequence_id: None,
195197
};
196198

197199
// negative funding cost
@@ -236,6 +238,7 @@ fn iterative_no_bounds_formualic_k_tests() {
236238
confidence: 0,
237239
delay: 2,
238240
has_sufficient_number_of_data_points: true,
241+
sequence_id: None,
239242
};
240243

241244
// negative funding cost

programs/drift/src/controller/position/tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ fn amm_pred_market_example() {
508508
confidence: 47843,
509509
delay: 1,
510510
has_sufficient_number_of_data_points: true,
511+
sequence_id: None,
511512
};
512513
let mm_oracle_price_data = perp_market
513514
.get_mm_oracle_price_data(
@@ -646,6 +647,7 @@ fn amm_ref_price_decay_tail_test() {
646647
confidence: PRICE_PRECISION_U64 / 100000,
647648
delay: 1,
648649
has_sufficient_number_of_data_points: true,
650+
sequence_id: None,
649651
};
650652
let mm_oracle_price_data = perp_market
651653
.get_mm_oracle_price_data(
@@ -822,6 +824,7 @@ fn amm_ref_price_offset_decay_logic() {
822824
confidence: PRICE_PRECISION_U64 / 1000,
823825
delay: 1,
824826
has_sufficient_number_of_data_points: true,
827+
sequence_id: None,
825828
};
826829
let mm_oracle_price_data = perp_market
827830
.get_mm_oracle_price_data(
@@ -995,6 +998,7 @@ fn amm_negative_ref_price_offset_decay_logic() {
995998
confidence: PRICE_PRECISION_U64 / 1000,
996999
delay: 1,
9971000
has_sufficient_number_of_data_points: true,
1001+
sequence_id: None,
9981002
};
9991003
let mm_oracle_price_data = perp_market
10001004
.get_mm_oracle_price_data(
@@ -1184,6 +1188,7 @@ fn amm_perp_ref_offset() {
11841188
confidence: PRICE_PRECISION_U64 / 1000,
11851189
delay: 1,
11861190
has_sufficient_number_of_data_points: true,
1191+
sequence_id: None,
11871192
};
11881193
let mm_oracle_price_data = perp_market
11891194
.get_mm_oracle_price_data(
@@ -2446,6 +2451,7 @@ fn recenter_amm_2() {
24462451
let mm_oracle_price_data = MMOraclePriceData::new(
24472452
oracle_price_data.price,
24482453
oracle_price_data.delay + 1,
2454+
1,
24492455
OracleValidity::default(),
24502456
*oracle_price_data,
24512457
)
@@ -2587,6 +2593,7 @@ fn test_move_amm() {
25872593
let mm_oracle_price_data = MMOraclePriceData::new(
25882594
oracle_price_data.price,
25892595
oracle_price_data.delay + 1,
2596+
1,
25902597
OracleValidity::default(),
25912598
*oracle_price_data,
25922599
)

programs/drift/src/controller/repeg/tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub fn update_amm_test() {
7676
confidence: 0,
7777
delay: 2,
7878
has_sufficient_number_of_data_points: true,
79+
sequence_id: None,
7980
};
8081

8182
let reserve_price_before = market.amm.reserve_price().unwrap();
@@ -227,6 +228,7 @@ pub fn update_amm_test_bad_oracle() {
227228
confidence: 0,
228229
delay: 12,
229230
has_sufficient_number_of_data_points: true,
231+
sequence_id: None,
230232
};
231233
let mm_oracle_price_data = market
232234
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)
@@ -285,6 +287,7 @@ pub fn update_amm_larg_conf_test() {
285287
confidence: 0,
286288
delay: 9,
287289
has_sufficient_number_of_data_points: true,
290+
sequence_id: None,
288291
};
289292
let mm_oracle_price_data = market
290293
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)
@@ -309,6 +312,7 @@ pub fn update_amm_larg_conf_test() {
309312
confidence: 100 * PRICE_PRECISION_U64,
310313
delay: 1,
311314
has_sufficient_number_of_data_points: true,
315+
sequence_id: None,
312316
};
313317
let mm_oracle_price_data = market
314318
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)
@@ -335,6 +339,7 @@ pub fn update_amm_larg_conf_test() {
335339
confidence: 100 * PRICE_PRECISION_U64,
336340
delay: 1,
337341
has_sufficient_number_of_data_points: true,
342+
sequence_id: None,
338343
};
339344

340345
let fee_budget = calculate_fee_pool(&market).unwrap();
@@ -377,6 +382,7 @@ pub fn update_amm_larg_conf_test() {
377382
confidence: 121 * PRICE_PRECISION_U64,
378383
delay: 1,
379384
has_sufficient_number_of_data_points: true,
385+
sequence_id: None,
380386
};
381387
let mm_oracle_price_data = market
382388
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)
@@ -432,6 +438,7 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() {
432438
confidence: 0,
433439
delay: 9,
434440
has_sufficient_number_of_data_points: true,
441+
sequence_id: None,
435442
};
436443
let mm_oracle_price_data = market
437444
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)
@@ -486,6 +493,7 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() {
486493
confidence: 100 * PRICE_PRECISION_U64,
487494
delay: 1,
488495
has_sufficient_number_of_data_points: true,
496+
sequence_id: None,
489497
};
490498
let mm_oracle_price_data = market
491499
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)
@@ -512,6 +520,7 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() {
512520
confidence: 100 * PRICE_PRECISION_U64,
513521
delay: 1,
514522
has_sufficient_number_of_data_points: true,
523+
sequence_id: None,
515524
};
516525
let mm_oracle_price_data = market
517526
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)
@@ -588,6 +597,7 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() {
588597
confidence: 121 * PRICE_PRECISION_U64,
589598
delay: 1,
590599
has_sufficient_number_of_data_points: true,
600+
sequence_id: None,
591601
};
592602
let mm_oracle_price_data = market
593603
.get_mm_oracle_price_data(oracle_price_data, slot, &state.oracle_guard_rails.validity)

programs/drift/src/controller/spot_balance/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,7 @@ fn check_usdc_spot_market_twap() {
17001700
confidence: 1,
17011701
delay: 0,
17021702
has_sufficient_number_of_data_points: true,
1703+
sequence_id: None,
17031704
};
17041705

17051706
update_spot_market_twap_stats(&mut spot_market, Some(&oracle_price_data), now).unwrap();

programs/drift/src/math/amm/tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn calculate_amm_available_guards() {
2222
confidence: PRICE_PRECISION_U64 / 100,
2323
delay: 1,
2424
has_sufficient_number_of_data_points: true,
25+
sequence_id: None,
2526
};
2627

2728
assert_eq!(market.amm.net_revenue_since_last_funding, 0);
@@ -98,6 +99,7 @@ fn calculate_net_user_pnl_test() {
9899
confidence: PRICE_PRECISION_U64 / 100,
99100
delay: 1,
100101
has_sufficient_number_of_data_points: true,
102+
sequence_id: None,
101103
};
102104

103105
let net_user_pnl = calculate_net_user_pnl(&amm, oracle_price_data.price).unwrap();
@@ -127,6 +129,7 @@ fn calculate_expiry_price_long_imbalance_with_loss_test() {
127129
confidence: 0,
128130
delay: 2,
129131
has_sufficient_number_of_data_points: true,
132+
sequence_id: None,
130133
};
131134

132135
let market_position = PerpPosition {
@@ -209,6 +212,7 @@ fn calculate_expiry_price_long_imbalance_test() {
209212
confidence: 0,
210213
delay: 2,
211214
has_sufficient_number_of_data_points: true,
215+
sequence_id: None,
212216
};
213217

214218
let market_position = PerpPosition {
@@ -307,6 +311,7 @@ fn calculate_expiry_price_test() {
307311
confidence: PRICE_PRECISION_U64 / 100,
308312
delay: 1,
309313
has_sufficient_number_of_data_points: true,
314+
sequence_id: None,
310315
};
311316

312317
let mut expiry_price = calculate_expiry_price(&amm, oracle_price_data.price, 0).unwrap();
@@ -324,6 +329,7 @@ fn calculate_expiry_price_test() {
324329
confidence: 0,
325330
delay: 2,
326331
has_sufficient_number_of_data_points: true,
332+
sequence_id: None,
327333
};
328334

329335
let market_position = PerpPosition {
@@ -483,10 +489,12 @@ fn calc_mark_std_tests() {
483489
confidence: 0,
484490
delay: 2,
485491
has_sufficient_number_of_data_points: true,
492+
sequence_id: None,
486493
};
487494
let mm_oracle_price_data = MMOraclePriceData::new(
488495
oracle_price_data.price,
489496
3,
497+
1,
490498
OracleValidity::default(),
491499
oracle_price_data,
492500
)
@@ -605,10 +613,12 @@ fn update_mark_twap_tests() {
605613
confidence: PRICE_PRECISION_U64 / 100,
606614
delay: 1,
607615
has_sufficient_number_of_data_points: true,
616+
sequence_id: None,
608617
};
609618
let mm_oracle_price_data = MMOraclePriceData::new(
610619
oracle_price_data.price,
611620
3,
621+
1,
612622
OracleValidity::default(),
613623
oracle_price_data,
614624
)
@@ -704,10 +714,12 @@ fn update_mark_twap_tests() {
704714
confidence: PRICE_PRECISION_U64 / 80,
705715
delay: 14,
706716
has_sufficient_number_of_data_points: true,
717+
sequence_id: None,
707718
};
708719
let mm_oracle_price_data = MMOraclePriceData::new(
709720
oracle_price_data.price,
710721
15,
722+
1,
711723
OracleValidity::default(),
712724
oracle_price_data,
713725
)
@@ -773,10 +785,12 @@ fn calc_oracle_twap_tests() {
773785
confidence: PRICE_PRECISION_U64 / 100,
774786
delay: 1,
775787
has_sufficient_number_of_data_points: true,
788+
sequence_id: None,
776789
};
777790
let mm_oracle_price_data = MMOraclePriceData::new(
778791
oracle_price_data.price,
779792
2,
793+
1,
780794
OracleValidity::default(),
781795
oracle_price_data,
782796
)
@@ -798,10 +812,12 @@ fn calc_oracle_twap_tests() {
798812
confidence: 0,
799813
delay: 2,
800814
has_sufficient_number_of_data_points: true,
815+
sequence_id: None,
801816
};
802817
let mm_oracle_price_data = MMOraclePriceData::new(
803818
oracle_price_data.price,
804819
3,
820+
1,
805821
OracleValidity::default(),
806822
oracle_price_data,
807823
)
@@ -833,10 +849,12 @@ fn calc_oracle_twap_tests() {
833849
confidence: 0,
834850
delay: 2,
835851
has_sufficient_number_of_data_points: true,
852+
sequence_id: None,
836853
};
837854
let mm_oracle_price_data = MMOraclePriceData::new(
838855
oracle_price_data.price,
839856
3,
857+
1,
840858
OracleValidity::default(),
841859
oracle_price_data,
842860
)
@@ -898,10 +916,12 @@ fn calc_oracle_twap_clamp_update_tests() {
898916
confidence: PRICE_PRECISION_U64 / 10,
899917
delay: 1,
900918
has_sufficient_number_of_data_points: true,
919+
sequence_id: None,
901920
};
902921
let mm_oracle_price_data = MMOraclePriceData::new(
903922
oracle_price_data.price,
904923
2,
924+
1,
905925
OracleValidity::default(),
906926
oracle_price_data,
907927
)
@@ -985,10 +1005,12 @@ fn test_last_oracle_conf_update() {
9851005
confidence: PRICE_PRECISION_U64 / 10,
9861006
delay: 1,
9871007
has_sufficient_number_of_data_points: true,
1008+
sequence_id: None,
9881009
};
9891010
let mm_oracle_price_data = MMOraclePriceData::new(
9901011
oracle_price_data.price,
9911012
2,
1013+
1,
9921014
OracleValidity::default(),
9931015
oracle_price_data,
9941016
)
@@ -1004,11 +1026,13 @@ fn test_last_oracle_conf_update() {
10041026
confidence: 1,
10051027
delay: 5,
10061028
has_sufficient_number_of_data_points: true,
1029+
sequence_id: None,
10071030
};
10081031

10091032
let mm_oracle_price_data = MMOraclePriceData::new(
10101033
oracle_price_data.price,
10111034
2,
1035+
1,
10121036
OracleValidity::default(),
10131037
oracle_price_data,
10141038
)

programs/drift/src/math/funding/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ fn unsettled_funding_pnl() {
415415
let mm_oracle_price_data = MMOraclePriceData::new(
416416
oracle_price_data.price,
417417
oracle_price_data.delay + 1,
418+
0,
418419
OracleValidity::default(),
419420
*oracle_price_data,
420421
)

programs/drift/src/math/margin/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ mod test {
265265
confidence: 0,
266266
delay: 2,
267267
has_sufficient_number_of_data_points: true,
268+
sequence_id: None,
268269
};
269270

270271
let market_position = PerpPosition {
@@ -4197,6 +4198,7 @@ mod calculate_perp_position_value_and_pnl_prediction_market {
41974198
confidence: 0,
41984199
delay: 2,
41994200
has_sufficient_number_of_data_points: true,
4201+
sequence_id: None,
42004202
};
42014203

42024204
let market_position = PerpPosition {
@@ -4260,6 +4262,7 @@ mod calculate_perp_position_value_and_pnl_prediction_market {
42604262
confidence: 0,
42614263
delay: 2,
42624264
has_sufficient_number_of_data_points: true,
4265+
sequence_id: None,
42634266
};
42644267

42654268
let market_position = PerpPosition {

programs/drift/src/math/oracle/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn calculate_oracle_valid() {
3434
confidence: PRICE_PRECISION_U64 / 100,
3535
delay: 1,
3636
has_sufficient_number_of_data_points: true,
37+
sequence_id: None,
3738
};
3839
let mut market: PerpMarket = PerpMarket {
3940
amm,
@@ -84,6 +85,7 @@ fn calculate_oracle_valid() {
8485
confidence: PRICE_PRECISION_U64 / 100,
8586
delay: 11,
8687
has_sufficient_number_of_data_points: true,
88+
sequence_id: None,
8789
};
8890
oracle_status = get_oracle_status(
8991
&market,

programs/drift/src/math/repeg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub fn calculate_repeg_validity(
7676
confidence: oracle_conf,
7777
delay: _,
7878
has_sufficient_number_of_data_points: _,
79+
sequence_id: _,
7980
} = *oracle_price_data;
8081

8182
let oracle_price_u128 = oracle_price.cast::<u64>()?;

0 commit comments

Comments
 (0)