-
Notifications
You must be signed in to change notification settings - Fork 116
/
perp_market.rs
1287 lines (1168 loc) · 47.5 KB
/
perp_market.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use anchor_lang::prelude::*;
use std::cmp::max;
use crate::controller::position::{PositionDelta, PositionDirection};
use crate::error::{DriftResult, ErrorCode};
use crate::math::amm;
use crate::math::casting::Cast;
#[cfg(test)]
use crate::math::constants::{
AMM_RESERVE_PRECISION, MAX_CONCENTRATION_COEFFICIENT, PRICE_PRECISION_I64,
};
use crate::math::constants::{
AMM_RESERVE_PRECISION_I128, BID_ASK_SPREAD_PRECISION, BID_ASK_SPREAD_PRECISION_U128,
LP_FEE_SLICE_DENOMINATOR, LP_FEE_SLICE_NUMERATOR, MARGIN_PRECISION_U128, PERCENTAGE_PRECISION,
SPOT_WEIGHT_PRECISION, TWENTY_FOUR_HOUR,
};
use crate::math::helpers::get_proportion_i128;
use crate::math::margin::{
calculate_size_discount_asset_weight, calculate_size_premium_liability_weight,
MarginRequirementType,
};
use crate::math::safe_math::SafeMath;
use crate::math::stats;
use crate::state::events::OrderActionExplanation;
use crate::state::oracle::{HistoricalOracleData, OracleSource};
use crate::state::spot_market::{AssetTier, SpotBalance, SpotBalanceType};
use crate::state::traits::{MarketIndexOffset, Size};
use crate::{AMM_TO_QUOTE_PRECISION_RATIO, PRICE_PRECISION};
use borsh::{BorshDeserialize, BorshSerialize};
use crate::state::paused_operations::PerpOperation;
use drift_macros::assert_no_slop;
use static_assertions::const_assert_eq;
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, PartialEq, Debug, Eq)]
pub enum MarketStatus {
/// warm up period for initialization, fills are paused
Initialized,
/// all operations allowed
Active,
/// Deprecated in favor of PausedOperations
FundingPaused,
/// Deprecated in favor of PausedOperations
AmmPaused,
/// Deprecated in favor of PausedOperations
FillPaused,
/// Deprecated in favor of PausedOperations
WithdrawPaused,
/// fills only able to reduce liability
ReduceOnly,
/// market has determined settlement price and positions are expired must be settled
Settlement,
/// market has no remaining participants
Delisted,
}
impl Default for MarketStatus {
fn default() -> Self {
MarketStatus::Initialized
}
}
impl MarketStatus {
pub fn validate_not_deprecated(&self) -> DriftResult {
if matches!(
self,
MarketStatus::FundingPaused
| MarketStatus::AmmPaused
| MarketStatus::FillPaused
| MarketStatus::WithdrawPaused
) {
msg!("MarketStatus is deprecated");
Err(ErrorCode::DefaultError)
} else {
Ok(())
}
}
}
#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, PartialEq, Debug, Eq)]
pub enum ContractType {
Perpetual,
Future,
}
impl Default for ContractType {
fn default() -> Self {
ContractType::Perpetual
}
}
#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, PartialEq, Debug, Eq, PartialOrd, Ord)]
pub enum ContractTier {
/// max insurance capped at A level
A,
/// max insurance capped at B level
B,
/// max insurance capped at C level
C,
/// no insurance
Speculative,
/// no insurance, only single position allowed
Isolated,
}
impl ContractTier {
pub fn default() -> Self {
ContractTier::Speculative
}
pub fn is_as_safe_as(&self, best_contract: &ContractTier, best_asset: &AssetTier) -> bool {
self.is_as_safe_as_contract(best_contract) && self.is_as_safe_as_asset(best_asset)
}
pub fn is_as_safe_as_contract(&self, other: &ContractTier) -> bool {
// Contract Tier A safest
self <= other
}
pub fn is_as_safe_as_asset(&self, other: &AssetTier) -> bool {
// allow Contract Tier A,B,C to rank above Assets below Collateral status
if other == &AssetTier::Unlisted {
true
} else {
other >= &AssetTier::Cross && self <= &ContractTier::C
}
}
}
#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, PartialEq, Debug, Eq, PartialOrd, Ord)]
pub enum AMMLiquiditySplit {
ProtocolOwned,
LPOwned,
Shared,
}
impl AMMLiquiditySplit {
pub fn get_order_action_explanation(&self) -> OrderActionExplanation {
match &self {
AMMLiquiditySplit::ProtocolOwned => OrderActionExplanation::OrderFilledWithAMMJit,
AMMLiquiditySplit::LPOwned => OrderActionExplanation::OrderFilledWithLPJit,
AMMLiquiditySplit::Shared => OrderActionExplanation::OrderFilledWithAMMJitLPSplit,
}
}
}
#[account(zero_copy(unsafe))]
#[derive(Eq, PartialEq, Debug)]
#[repr(C)]
pub struct PerpMarket {
/// The perp market's address. It is a pda of the market index
pub pubkey: Pubkey,
/// The automated market maker
pub amm: AMM,
/// The market's pnl pool. When users settle negative pnl, the balance increases.
/// When users settle positive pnl, the balance decreases. Can not go negative.
pub pnl_pool: PoolBalance,
/// Encoded display name for the perp market e.g. SOL-PERP
pub name: [u8; 32],
/// The perp market's claim on the insurance fund
pub insurance_claim: InsuranceClaim,
/// The max pnl imbalance before positive pnl asset weight is discounted
/// pnl imbalance is the difference between long and short pnl. When it's greater than 0,
/// the amm has negative pnl and the initial asset weight for positive pnl is discounted
/// precision = QUOTE_PRECISION
pub unrealized_pnl_max_imbalance: u64,
/// The ts when the market will be expired. Only set if market is in reduce only mode
pub expiry_ts: i64,
/// The price at which positions will be settled. Only set if market is expired
/// precision = PRICE_PRECISION
pub expiry_price: i64,
/// Every trade has a fill record id. This is the next id to be used
pub next_fill_record_id: u64,
/// Every funding rate update has a record id. This is the next id to be used
pub next_funding_rate_record_id: u64,
/// Every amm k updated has a record id. This is the next id to be used
pub next_curve_record_id: u64,
/// The initial margin fraction factor. Used to increase margin ratio for large positions
/// precision: MARGIN_PRECISION
pub imf_factor: u32,
/// The imf factor for unrealized pnl. Used to discount asset weight for large positive pnl
/// precision: MARGIN_PRECISION
pub unrealized_pnl_imf_factor: u32,
/// The fee the liquidator is paid for taking over perp position
/// precision: LIQUIDATOR_FEE_PRECISION
pub liquidator_fee: u32,
/// The fee the insurance fund receives from liquidation
/// precision: LIQUIDATOR_FEE_PRECISION
pub if_liquidation_fee: u32,
/// The margin ratio which determines how much collateral is required to open a position
/// e.g. margin ratio of .1 means a user must have $100 of total collateral to open a $1000 position
/// precision: MARGIN_PRECISION
pub margin_ratio_initial: u32,
/// The margin ratio which determines when a user will be liquidated
/// e.g. margin ratio of .05 means a user must have $50 of total collateral to maintain a $1000 position
/// else they will be liquidated
/// precision: MARGIN_PRECISION
pub margin_ratio_maintenance: u32,
/// The initial asset weight for positive pnl. Negative pnl always has an asset weight of 1
/// precision: SPOT_WEIGHT_PRECISION
pub unrealized_pnl_initial_asset_weight: u32,
/// The maintenance asset weight for positive pnl. Negative pnl always has an asset weight of 1
/// precision: SPOT_WEIGHT_PRECISION
pub unrealized_pnl_maintenance_asset_weight: u32,
/// number of users in a position (base)
pub number_of_users_with_base: u32,
/// number of users in a position (pnl) or pnl (quote)
pub number_of_users: u32,
pub market_index: u16,
/// Whether a market is active, reduce only, expired, etc
/// Affects whether users can open/close positions
pub status: MarketStatus,
/// Currently only Perpetual markets are supported
pub contract_type: ContractType,
/// The contract tier determines how much insurance a market can receive, with more speculative markets receiving less insurance
/// It also influences the order perp markets can be liquidated, with less speculative markets being liquidated first
pub contract_tier: ContractTier,
pub paused_operations: u8,
/// The spot market that pnl is settled in
pub quote_spot_market_index: u16,
/// Between -100 and 100, represents what % to increase/decrease the fee by
/// E.g. if this is -50 and the fee is 5bps, the new fee will be 2.5bps
/// if this is 50 and the fee is 5bps, the new fee will be 7.5bps
pub fee_adjustment: i16,
pub padding: [u8; 46],
}
impl Default for PerpMarket {
fn default() -> Self {
PerpMarket {
pubkey: Pubkey::default(),
amm: AMM::default(),
pnl_pool: PoolBalance::default(),
name: [0; 32],
insurance_claim: InsuranceClaim::default(),
unrealized_pnl_max_imbalance: 0,
expiry_ts: 0,
expiry_price: 0,
next_fill_record_id: 0,
next_funding_rate_record_id: 0,
next_curve_record_id: 0,
imf_factor: 0,
unrealized_pnl_imf_factor: 0,
liquidator_fee: 0,
if_liquidation_fee: 0,
margin_ratio_initial: 0,
margin_ratio_maintenance: 0,
unrealized_pnl_initial_asset_weight: 0,
unrealized_pnl_maintenance_asset_weight: 0,
number_of_users_with_base: 0,
number_of_users: 0,
market_index: 0,
status: MarketStatus::default(),
contract_type: ContractType::default(),
contract_tier: ContractTier::default(),
paused_operations: 0,
quote_spot_market_index: 0,
fee_adjustment: 0,
padding: [0; 46],
}
}
}
impl Size for PerpMarket {
const SIZE: usize = 1216;
}
impl MarketIndexOffset for PerpMarket {
const MARKET_INDEX_OFFSET: usize = 1160;
}
impl PerpMarket {
pub fn is_in_settlement(&self, now: i64) -> bool {
let in_settlement = matches!(
self.status,
MarketStatus::Settlement | MarketStatus::Delisted
);
let expired = self.expiry_ts != 0 && now >= self.expiry_ts;
in_settlement || expired
}
pub fn is_reduce_only(&self) -> DriftResult<bool> {
Ok(self.status == MarketStatus::ReduceOnly)
}
pub fn is_operation_paused(&self, operation: PerpOperation) -> bool {
PerpOperation::is_operation_paused(self.paused_operations, operation)
}
pub fn get_sanitize_clamp_denominator(self) -> DriftResult<Option<i64>> {
Ok(match self.contract_tier {
ContractTier::A => Some(10_i64), // 10%
ContractTier::B => Some(5_i64), // 20%
ContractTier::C => Some(2_i64), // 50%
ContractTier::Speculative => None, // DEFAULT_MAX_TWAP_UPDATE_PRICE_BAND_DENOMINATOR
ContractTier::Isolated => None, // DEFAULT_MAX_TWAP_UPDATE_PRICE_BAND_DENOMINATOR
})
}
pub fn get_auction_end_min_max_divisors(self) -> DriftResult<(u64, u64)> {
Ok(match self.contract_tier {
ContractTier::A => (1000, 50), // 10 bps, 2%
ContractTier::B => (1000, 20), // 10 bps, 5%
ContractTier::C => (500, 20), // 50 bps, 5%
ContractTier::Speculative => (100, 10), // 1%, 10%
ContractTier::Isolated => (50, 5), // 2%, 20%
})
}
pub fn get_max_price_divergence_for_funding_rate(
self,
oracle_price_twap: i64,
) -> DriftResult<i64> {
// clamp to to 3% price divergence for safer markets and higher for lower contract tiers
if self.contract_tier.is_as_safe_as_contract(&ContractTier::B) {
oracle_price_twap.safe_div(33) // 3%
} else if self.contract_tier.is_as_safe_as_contract(&ContractTier::C) {
oracle_price_twap.safe_div(20) // 5%
} else {
oracle_price_twap.safe_div(10) // 10%
}
}
pub fn get_margin_ratio(
&self,
size: u128,
margin_type: MarginRequirementType,
) -> DriftResult<u32> {
if self.status == MarketStatus::Settlement {
return Ok(0); // no liability weight on size
}
let default_margin_ratio = match margin_type {
MarginRequirementType::Initial => self.margin_ratio_initial,
MarginRequirementType::Fill => {
self.margin_ratio_initial
.safe_add(self.margin_ratio_maintenance)?
/ 2
}
MarginRequirementType::Maintenance => self.margin_ratio_maintenance,
};
let size_adj_margin_ratio = calculate_size_premium_liability_weight(
size,
self.imf_factor,
default_margin_ratio,
MARGIN_PRECISION_U128,
)?;
let margin_ratio = default_margin_ratio.max(size_adj_margin_ratio);
Ok(margin_ratio)
}
pub fn get_unrealized_asset_weight(
&self,
unrealized_pnl: i128,
margin_type: MarginRequirementType,
) -> DriftResult<u32> {
let mut margin_asset_weight = match margin_type {
MarginRequirementType::Initial | MarginRequirementType::Fill => {
self.unrealized_pnl_initial_asset_weight
}
MarginRequirementType::Maintenance => self.unrealized_pnl_maintenance_asset_weight,
};
if margin_asset_weight > 0
&& matches!(
margin_type,
MarginRequirementType::Fill | MarginRequirementType::Initial
)
&& self.unrealized_pnl_max_imbalance > 0
{
let net_unsettled_pnl = amm::calculate_net_user_pnl(
&self.amm,
self.amm.historical_oracle_data.last_oracle_price,
)?;
if net_unsettled_pnl > self.unrealized_pnl_max_imbalance.cast::<i128>()? {
margin_asset_weight = margin_asset_weight
.cast::<u128>()?
.safe_mul(self.unrealized_pnl_max_imbalance.cast()?)?
.safe_div(net_unsettled_pnl.unsigned_abs())?
.cast()?;
}
}
// the asset weight for a position's unrealized pnl + unsettled pnl in the margin system
// > 0 (positive balance)
// < 0 (negative balance) always has asset weight = 1
let unrealized_asset_weight = if unrealized_pnl > 0 {
// todo: only discount the initial margin s.t. no one gets liquidated over upnl?
// a larger imf factor -> lower asset weight
match margin_type {
MarginRequirementType::Initial | MarginRequirementType::Fill => {
if margin_asset_weight > 0 {
calculate_size_discount_asset_weight(
unrealized_pnl
.unsigned_abs()
.safe_mul(AMM_TO_QUOTE_PRECISION_RATIO)?,
self.unrealized_pnl_imf_factor,
margin_asset_weight,
)?
} else {
0
}
}
MarginRequirementType::Maintenance => self.unrealized_pnl_maintenance_asset_weight,
}
} else {
SPOT_WEIGHT_PRECISION
};
Ok(unrealized_asset_weight)
}
pub fn get_open_interest(&self) -> u128 {
self.amm
.base_asset_amount_long
.abs()
.max(self.amm.base_asset_amount_short.abs())
.unsigned_abs()
}
pub fn get_market_depth_for_funding_rate(&self) -> DriftResult<u64> {
// base amount used on user orders for funding calculation
let open_interest = self.get_open_interest();
let depth = self
.amm
.min_order_size
.safe_mul(1000)?
.max(open_interest.safe_div(500)?.cast::<u64>()?);
Ok(depth)
}
pub fn update_market_with_counterparty(
&mut self,
delta: &PositionDelta,
new_settled_base_asset_amount: i64,
) -> DriftResult {
if delta.remainder_base_asset_amount.is_some() {
// todo: name for this is confusing, but adding is correct as is
// definition: net position of users in the market that has the LP as a counterparty (which have NOT settled)
self.amm.base_asset_amount_with_unsettled_lp = self
.amm
.base_asset_amount_with_unsettled_lp
.safe_add(new_settled_base_asset_amount.cast()?)?;
}
Ok(())
}
}
#[cfg(test)]
impl PerpMarket {
pub fn default_test() -> Self {
let amm = AMM::default_test();
PerpMarket {
amm,
margin_ratio_initial: 1000,
margin_ratio_maintenance: 500,
..PerpMarket::default()
}
}
pub fn default_btc_test() -> Self {
let amm = AMM::default_btc_test();
PerpMarket {
amm,
margin_ratio_initial: 1000, // 10x
margin_ratio_maintenance: 500, // 5x
status: MarketStatus::Initialized,
..PerpMarket::default()
}
}
}
#[zero_copy(unsafe)]
#[derive(Default, Eq, PartialEq, Debug)]
#[repr(C)]
pub struct InsuranceClaim {
/// The amount of revenue last settled
/// Positive if funds left the perp market,
/// negative if funds were pulled into the perp market
/// precision: QUOTE_PRECISION
pub revenue_withdraw_since_last_settle: i64,
/// The max amount of revenue that can be withdrawn per period
/// precision: QUOTE_PRECISION
pub max_revenue_withdraw_per_period: u64,
/// The max amount of insurance that perp market can use to resolve bankruptcy and pnl deficits
/// precision: QUOTE_PRECISION
pub quote_max_insurance: u64,
/// The amount of insurance that has been used to resolve bankruptcy and pnl deficits
/// precision: QUOTE_PRECISION
pub quote_settled_insurance: u64,
/// The last time revenue was settled in/out of market
pub last_revenue_withdraw_ts: i64,
}
#[zero_copy(unsafe)]
#[derive(Default, Eq, PartialEq, Debug)]
#[repr(C)]
pub struct PoolBalance {
/// To get the pool's token amount, you must multiply the scaled balance by the market's cumulative
/// deposit interest
/// precision: SPOT_BALANCE_PRECISION
pub scaled_balance: u128,
/// The spot market the pool is for
pub market_index: u16,
pub padding: [u8; 6],
}
impl SpotBalance for PoolBalance {
fn market_index(&self) -> u16 {
self.market_index
}
fn balance_type(&self) -> &SpotBalanceType {
&SpotBalanceType::Deposit
}
fn balance(&self) -> u128 {
self.scaled_balance
}
fn increase_balance(&mut self, delta: u128) -> DriftResult {
self.scaled_balance = self.scaled_balance.safe_add(delta)?;
Ok(())
}
fn decrease_balance(&mut self, delta: u128) -> DriftResult {
self.scaled_balance = self.scaled_balance.safe_sub(delta)?;
Ok(())
}
fn update_balance_type(&mut self, _balance_type: SpotBalanceType) -> DriftResult {
Err(ErrorCode::CantUpdatePoolBalanceType)
}
}
#[assert_no_slop]
#[zero_copy(unsafe)]
#[derive(Debug, PartialEq, Eq)]
#[repr(C)]
pub struct AMM {
/// oracle price data public key
pub oracle: Pubkey,
/// stores historically witnessed oracle data
pub historical_oracle_data: HistoricalOracleData,
/// accumulated base asset amount since inception per lp share
pub base_asset_amount_per_lp: i128,
/// accumulated quote asset amount since inception per lp share
pub quote_asset_amount_per_lp: i128,
/// partition of fees from perp market trading moved from pnl settlements
pub fee_pool: PoolBalance,
/// `x` reserves for constant product mm formula (x * y = k)
pub base_asset_reserve: u128,
/// `y` reserves for constant product mm formula (x * y = k)
pub quote_asset_reserve: u128,
/// determines how close the min/max base asset reserve sit vs base reserves
/// allow for decreasing slippage without increasing liquidity and v.v.
pub concentration_coef: u128,
/// minimum base_asset_reserve allowed before AMM is unavailable
pub min_base_asset_reserve: u128,
/// maximum base_asset_reserve allowed before AMM is unavailable
pub max_base_asset_reserve: u128,
/// `sqrt(k)` in constant product mm formula (x * y = k). stored to avoid drift caused by integer math issues
pub sqrt_k: u128,
/// normalizing numerical factor for y, its use offers lowest slippage in cp-curve when market is balanced
pub peg_multiplier: u128,
/// y when market is balanced. stored to save computation
pub terminal_quote_asset_reserve: u128,
/// tracks number of total longs in market (regardless of counterparty)
pub base_asset_amount_long: i128,
/// tracks number of total shorts in market (regardless of counterparty)
pub base_asset_amount_short: i128,
/// tracks net position (longs-shorts) in market with AMM as counterparty
pub base_asset_amount_with_amm: i128,
/// tracks net position (longs-shorts) in market with LPs as counterparty
pub base_asset_amount_with_unsettled_lp: i128,
/// max allowed open interest, blocks trades that breach this value
pub max_open_interest: u128,
/// sum of all user's perp quote_asset_amount in market
pub quote_asset_amount: i128,
/// sum of all long user's quote_entry_amount in market
pub quote_entry_amount_long: i128,
/// sum of all short user's quote_entry_amount in market
pub quote_entry_amount_short: i128,
/// sum of all long user's quote_break_even_amount in market
pub quote_break_even_amount_long: i128,
/// sum of all short user's quote_break_even_amount in market
pub quote_break_even_amount_short: i128,
/// total user lp shares of sqrt_k (protocol owned liquidity = sqrt_k - last_funding_rate)
pub user_lp_shares: u128,
/// last funding rate in this perp market (unit is quote per base)
pub last_funding_rate: i64,
/// last funding rate for longs in this perp market (unit is quote per base)
pub last_funding_rate_long: i64,
/// last funding rate for shorts in this perp market (unit is quote per base)
pub last_funding_rate_short: i64,
/// estimate of last 24h of funding rate perp market (unit is quote per base)
pub last_24h_avg_funding_rate: i64,
/// total fees collected by this perp market
pub total_fee: i128,
/// total fees collected by the vAMM's bid/ask spread
pub total_mm_fee: i128,
/// total fees collected by exchange fee schedule
pub total_exchange_fee: u128,
/// total fees minus any recognized upnl and pool withdraws
pub total_fee_minus_distributions: i128,
/// sum of all fees from fee pool withdrawn to revenue pool
pub total_fee_withdrawn: u128,
/// all fees collected by market for liquidations
pub total_liquidation_fee: u128,
/// accumulated funding rate for longs since inception in market
pub cumulative_funding_rate_long: i128,
/// accumulated funding rate for shorts since inception in market
pub cumulative_funding_rate_short: i128,
/// accumulated social loss paid by users since inception in market
pub total_social_loss: u128,
/// transformed base_asset_reserve for users going long
pub ask_base_asset_reserve: u128,
/// transformed quote_asset_reserve for users going long
pub ask_quote_asset_reserve: u128,
/// transformed base_asset_reserve for users going short
pub bid_base_asset_reserve: u128,
/// transformed quote_asset_reserve for users going short
pub bid_quote_asset_reserve: u128,
/// the last seen oracle price partially shrunk toward the amm reserve price
/// precision: PRICE_PRECISION
pub last_oracle_normalised_price: i64,
/// the gap between the oracle price and the reserve price = y * peg_multiplier / x
pub last_oracle_reserve_price_spread_pct: i64,
/// average estimate of bid price over funding_period
/// precision: PRICE_PRECISION
pub last_bid_price_twap: u64,
/// average estimate of ask price over funding_period
/// precision: PRICE_PRECISION
pub last_ask_price_twap: u64,
/// average estimate of (bid+ask)/2 price over funding_period
/// precision: PRICE_PRECISION
pub last_mark_price_twap: u64,
/// average estimate of (bid+ask)/2 price over FIVE_MINUTES
pub last_mark_price_twap_5min: u64,
/// the last blockchain slot the amm was updated
pub last_update_slot: u64,
/// the pct size of the oracle confidence interval
/// precision: PERCENTAGE_PRECISION
pub last_oracle_conf_pct: u64,
/// the total_fee_minus_distribution change since the last funding update
/// precision: QUOTE_PRECISION
pub net_revenue_since_last_funding: i64,
/// the last funding rate update unix_timestamp
pub last_funding_rate_ts: i64,
/// the peridocity of the funding rate updates
pub funding_period: i64,
/// the base step size (increment) of orders
/// precision: BASE_PRECISION
pub order_step_size: u64,
/// the price tick size of orders
/// precision: PRICE_PRECISION
pub order_tick_size: u64,
/// the minimum base size of an order
/// precision: BASE_PRECISION
pub min_order_size: u64,
/// the max base size a single user can have
/// precision: BASE_PRECISION
pub max_position_size: u64,
/// estimated total of volume in market
/// QUOTE_PRECISION
pub volume_24h: u64,
/// the volume intensity of long fills against AMM
pub long_intensity_volume: u64,
/// the volume intensity of short fills against AMM
pub short_intensity_volume: u64,
/// the blockchain unix timestamp at the time of the last trade
pub last_trade_ts: i64,
/// estimate of standard deviation of the fill (mark) prices
/// precision: PRICE_PRECISION
pub mark_std: u64,
/// estimate of standard deviation of the oracle price at each update
/// precision: PRICE_PRECISION
pub oracle_std: u64,
/// the last unix_timestamp the mark twap was updated
pub last_mark_price_twap_ts: i64,
/// the minimum spread the AMM can quote. also used as step size for some spread logic increases.
pub base_spread: u32,
/// the maximum spread the AMM can quote
pub max_spread: u32,
/// the spread for asks vs the reserve price
pub long_spread: u32,
/// the spread for bids vs the reserve price
pub short_spread: u32,
/// the count intensity of long fills against AMM
pub long_intensity_count: u32,
/// the count intensity of short fills against AMM
pub short_intensity_count: u32,
/// the fraction of total available liquidity a single fill on the AMM can consume
pub max_fill_reserve_fraction: u16,
/// the maximum slippage a single fill on the AMM can push
pub max_slippage_ratio: u16,
/// the update intensity of AMM formulaic updates (adjusting k). 0-100
pub curve_update_intensity: u8,
/// the jit intensity of AMM. larger intensity means larger participation in jit. 0 means no jit participation.
/// (0, 100] is intensity for protocol-owned AMM. (100, 200] is intensity for user LP-owned AMM.
pub amm_jit_intensity: u8,
/// the oracle provider information. used to decode/scale the oracle public key
pub oracle_source: OracleSource,
/// tracks whether the oracle was considered valid at the last AMM update
pub last_oracle_valid: bool,
/// the target value for `base_asset_amount_per_lp`, used during AMM JIT with LP split
/// precision: BASE_PRECISION
pub target_base_asset_amount_per_lp: i32,
/// expo for unit of per_lp, base 10 (if per_lp_base=X, then per_lp unit is 10^X)
pub per_lp_base: i8,
pub padding1: u8,
pub padding2: u16,
pub total_fee_earned_per_lp: u64,
pub net_unsettled_funding_pnl: i64,
pub quote_asset_amount_with_unsettled_lp: i64,
pub reference_price_offset: i32,
pub padding: [u8; 12],
}
impl Default for AMM {
fn default() -> Self {
AMM {
oracle: Pubkey::default(),
historical_oracle_data: HistoricalOracleData::default(),
base_asset_amount_per_lp: 0,
quote_asset_amount_per_lp: 0,
fee_pool: PoolBalance::default(),
base_asset_reserve: 0,
quote_asset_reserve: 0,
concentration_coef: 0,
min_base_asset_reserve: 0,
max_base_asset_reserve: 0,
sqrt_k: 0,
peg_multiplier: 0,
terminal_quote_asset_reserve: 0,
base_asset_amount_long: 0,
base_asset_amount_short: 0,
base_asset_amount_with_amm: 0,
base_asset_amount_with_unsettled_lp: 0,
max_open_interest: 0,
quote_asset_amount: 0,
quote_entry_amount_long: 0,
quote_entry_amount_short: 0,
quote_break_even_amount_long: 0,
quote_break_even_amount_short: 0,
user_lp_shares: 0,
last_funding_rate: 0,
last_funding_rate_long: 0,
last_funding_rate_short: 0,
last_24h_avg_funding_rate: 0,
total_fee: 0,
total_mm_fee: 0,
total_exchange_fee: 0,
total_fee_minus_distributions: 0,
total_fee_withdrawn: 0,
total_liquidation_fee: 0,
cumulative_funding_rate_long: 0,
cumulative_funding_rate_short: 0,
total_social_loss: 0,
ask_base_asset_reserve: 0,
ask_quote_asset_reserve: 0,
bid_base_asset_reserve: 0,
bid_quote_asset_reserve: 0,
last_oracle_normalised_price: 0,
last_oracle_reserve_price_spread_pct: 0,
last_bid_price_twap: 0,
last_ask_price_twap: 0,
last_mark_price_twap: 0,
last_mark_price_twap_5min: 0,
last_update_slot: 0,
last_oracle_conf_pct: 0,
net_revenue_since_last_funding: 0,
last_funding_rate_ts: 0,
funding_period: 0,
order_step_size: 0,
order_tick_size: 0,
min_order_size: 1,
max_position_size: 0,
volume_24h: 0,
long_intensity_volume: 0,
short_intensity_volume: 0,
last_trade_ts: 0,
mark_std: 0,
oracle_std: 0,
last_mark_price_twap_ts: 0,
base_spread: 0,
max_spread: 0,
long_spread: 0,
short_spread: 0,
long_intensity_count: 0,
short_intensity_count: 0,
max_fill_reserve_fraction: 0,
max_slippage_ratio: 0,
curve_update_intensity: 0,
amm_jit_intensity: 0,
oracle_source: OracleSource::default(),
last_oracle_valid: false,
target_base_asset_amount_per_lp: 0,
per_lp_base: 0,
padding1: 0,
padding2: 0,
total_fee_earned_per_lp: 0,
net_unsettled_funding_pnl: 0,
quote_asset_amount_with_unsettled_lp: 0,
reference_price_offset: 0,
padding: [0; 12],
}
}
}
impl AMM {
pub fn get_fallback_price(
self,
direction: &PositionDirection,
amm_available_liquidity: u64,
oracle_price: i64,
seconds_til_order_expiry: i64,
) -> DriftResult<u64> {
// PRICE_PRECISION
if direction.eq(&PositionDirection::Long) {
// pick amm ask + buffer if theres liquidity
// otherwise be aggressive vs oracle + 1hr premium
if amm_available_liquidity >= self.min_order_size {
let reserve_price = self.reserve_price()?;
let amm_ask_price: i64 = self.ask_price(reserve_price)?.cast()?;
amm_ask_price
.safe_add(amm_ask_price / (seconds_til_order_expiry * 20).clamp(100, 200))?
.cast::<u64>()
} else {
oracle_price
.safe_add(
self.last_ask_price_twap
.cast::<i64>()?
.safe_sub(self.historical_oracle_data.last_oracle_price_twap)?
.max(0),
)?
.safe_add(oracle_price / (seconds_til_order_expiry * 2).clamp(10, 50))?
.cast::<u64>()
}
} else {
// pick amm bid - buffer if theres liquidity
// otherwise be aggressive vs oracle + 1hr bid premium
if amm_available_liquidity >= self.min_order_size {
let reserve_price = self.reserve_price()?;
let amm_bid_price: i64 = self.bid_price(reserve_price)?.cast()?;
amm_bid_price
.safe_sub(amm_bid_price / (seconds_til_order_expiry * 20).clamp(100, 200))?
.cast::<u64>()
} else {
oracle_price
.safe_add(
self.last_bid_price_twap
.cast::<i64>()?
.safe_sub(self.historical_oracle_data.last_oracle_price_twap)?
.min(0),
)?
.safe_sub(oracle_price / (seconds_til_order_expiry * 2).clamp(10, 50))?
.max(0)
.cast::<u64>()
}
}
}
pub fn get_lower_bound_sqrt_k(self) -> DriftResult<u128> {
Ok(self.sqrt_k.min(
self.user_lp_shares
.safe_add(self.user_lp_shares.safe_div(1000)?)?
.max(self.min_order_size.cast()?)
.max(self.base_asset_amount_with_amm.unsigned_abs().cast()?),
))
}
pub fn get_protocol_owned_position(self) -> DriftResult<i64> {
self.base_asset_amount_with_amm
.safe_add(self.base_asset_amount_with_unsettled_lp)?
.cast::<i64>()
}
pub fn get_max_reference_price_offset(self) -> DriftResult<i64> {
if self.curve_update_intensity <= 100 {
return Ok(0);
}
let lower_bound_multiplier: i64 =
self.curve_update_intensity.safe_sub(100)?.cast::<i64>()?;
// always allow 1-100 bps of price offset, up to a fifth of the market's max_spread
let lb_bps =
(PERCENTAGE_PRECISION.cast::<i64>()? / 10000).safe_mul(lower_bound_multiplier)?;
let max_offset = (self.max_spread.cast::<i64>()? / 5).max(lb_bps);
Ok(max_offset)
}
pub fn get_per_lp_base_unit(self) -> DriftResult<i128> {
let scalar: i128 = 10_i128.pow(self.per_lp_base.abs().cast()?);
if self.per_lp_base > 0 {
AMM_RESERVE_PRECISION_I128.safe_mul(scalar)
} else {
AMM_RESERVE_PRECISION_I128.safe_div(scalar)
}
}
pub fn calculate_lp_base_delta(
&self,
per_lp_delta_base: i128,
base_unit: i128,
) -> DriftResult<i128> {
// calculate dedicated for user lp shares
let lp_delta_base =
get_proportion_i128(per_lp_delta_base, self.user_lp_shares, base_unit.cast()?)?;
Ok(lp_delta_base)
}
pub fn calculate_per_lp_delta(
&self,
delta: &PositionDelta,
fee_to_market: i128,
liquidity_split: AMMLiquiditySplit,
base_unit: i128,
) -> DriftResult<(i128, i128, i128)> {
let total_lp_shares = if liquidity_split == AMMLiquiditySplit::LPOwned {
self.user_lp_shares
} else {
self.sqrt_k
};
// update Market per lp position
let per_lp_delta_base = get_proportion_i128(
delta.base_asset_amount.cast()?,
base_unit.cast()?,
total_lp_shares, //.safe_div_ceil(rebase_divisor.cast()?)?,
)?;
let mut per_lp_delta_quote = get_proportion_i128(
delta.quote_asset_amount.cast()?,
base_unit.cast()?,
total_lp_shares, //.safe_div_ceil(rebase_divisor.cast()?)?,
)?;
// user position delta is short => lp position delta is long
if per_lp_delta_base < 0 {
// add one => lp subtract 1
per_lp_delta_quote = per_lp_delta_quote.safe_add(1)?;
}
// 1/5 of fee auto goes to market
// the rest goes to lps/market proportional
let per_lp_fee: i128 = if fee_to_market > 0 {
get_proportion_i128(
fee_to_market,
LP_FEE_SLICE_NUMERATOR,
LP_FEE_SLICE_DENOMINATOR,
)?
.safe_mul(base_unit)?
.safe_div(total_lp_shares.cast::<i128>()?)?
} else {
0
};
Ok((per_lp_delta_base, per_lp_delta_quote, per_lp_fee))
}
pub fn get_target_base_asset_amount_per_lp(&self) -> DriftResult<i128> {
if self.target_base_asset_amount_per_lp == 0 {
return Ok(0_i128);
}
let target_base_asset_amount_per_lp: i128 = if self.per_lp_base > 0 {
let rebase_divisor = 10_i128.pow(self.per_lp_base.abs().cast()?);
self.target_base_asset_amount_per_lp
.cast::<i128>()?
.safe_mul(rebase_divisor)?
} else if self.per_lp_base < 0 {
let rebase_divisor = 10_i128.pow(self.per_lp_base.abs().cast()?);
self.target_base_asset_amount_per_lp
.cast::<i128>()?
.safe_div(rebase_divisor)?
} else {
self.target_base_asset_amount_per_lp.cast::<i128>()?
};
Ok(target_base_asset_amount_per_lp)
}