@drift-labs/sdk 0.1.34-master.1 → 0.1.34-master.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2538,7 +2538,11 @@
2538
2538
  },
2539
2539
  {
2540
2540
  "name": "totalFeePaid",
2541
- "type": "u128"
2541
+ "type": "u64"
2542
+ },
2543
+ {
2544
+ "name": "totalFeeRebate",
2545
+ "type": "u64"
2542
2546
  },
2543
2547
  {
2544
2548
  "name": "totalTokenDiscount",
@@ -3369,7 +3373,7 @@
3369
3373
  },
3370
3374
  {
3371
3375
  "name": "fee",
3372
- "type": "u128"
3376
+ "type": "i128"
3373
3377
  },
3374
3378
  {
3375
3379
  "name": "fillerReward",
@@ -3620,7 +3624,7 @@
3620
3624
  },
3621
3625
  {
3622
3626
  "name": "fee",
3623
- "type": "u128"
3627
+ "type": "i128"
3624
3628
  },
3625
3629
  {
3626
3630
  "name": "quoteAssetAmountSurplus",
@@ -3772,7 +3776,7 @@
3772
3776
  },
3773
3777
  {
3774
3778
  "name": "fee",
3775
- "type": "u128"
3779
+ "type": "i128"
3776
3780
  },
3777
3781
  {
3778
3782
  "name": "direction",
@@ -9,7 +9,7 @@ import { OraclePriceData } from '../oracles/types';
9
9
  * @param periodAdjustment
10
10
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
11
11
  */
12
- export declare function calculateAllEstimatedFundingRate(market: Market, oraclePriceData: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN, BN]>;
12
+ export declare function calculateAllEstimatedFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN, BN]>;
13
13
  /**
14
14
  *
15
15
  * @param market
@@ -18,7 +18,7 @@ export declare function calculateAllEstimatedFundingRate(market: Market, oracleP
18
18
  * @param estimationMethod
19
19
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
20
20
  */
21
- export declare function calculateEstimatedFundingRate(market: Market, oraclePriceData: OraclePriceData, periodAdjustment: BN, estimationMethod: 'interpolated' | 'lowerbound' | 'capped'): Promise<BN>;
21
+ export declare function calculateEstimatedFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN, estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'): Promise<BN>;
22
22
  /**
23
23
  *
24
24
  * @param market
@@ -26,7 +26,7 @@ export declare function calculateEstimatedFundingRate(market: Market, oraclePric
26
26
  * @param periodAdjustment
27
27
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
28
28
  */
29
- export declare function calculateLongShortFundingRate(market: Market, oraclePriceData: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN]>;
29
+ export declare function calculateLongShortFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN]>;
30
30
  /**
31
31
  *
32
32
  * @param market
@@ -34,7 +34,7 @@ export declare function calculateLongShortFundingRate(market: Market, oraclePric
34
34
  * @param periodAdjustment
35
35
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
36
36
  */
37
- export declare function calculateLongShortFundingRateAndLiveTwaps(market: Market, oraclePriceData: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN]>;
37
+ export declare function calculateLongShortFundingRateAndLiveTwaps(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN]>;
38
38
  /**
39
39
  *
40
40
  * @param market
@@ -28,6 +28,7 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
28
28
  // 24 * 365.25: annualized
29
29
  const secondsInHour = new anchor_1.BN(3600);
30
30
  const hoursInDay = new anchor_1.BN(24);
31
+ const ONE = new anchor_1.BN(1);
31
32
  if (!market.initialized) {
32
33
  return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
33
34
  }
@@ -51,30 +52,34 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
51
52
  const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
52
53
  const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
53
54
  const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
54
- const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, secondsInHour.sub(timeSinceLastOracleTwapUpdate));
55
- const oraclePrice = oraclePriceData.price;
55
+ const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
56
56
  let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
57
- const oracleLiveVsTwap = oraclePrice
58
- .sub(lastOracleTwapWithMantissa)
59
- .abs()
60
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
61
- .mul(new anchor_1.BN(100))
62
- .div(lastOracleTwapWithMantissa);
63
- // verify pyth live input is within 10% of last twap for live update
64
- if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
65
- oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
66
- .mul(lastOracleTwapWithMantissa)
67
- .add(timeSinceLastMarkChange.mul(oraclePrice))
68
- .add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
69
- .div(timeSinceLastMarkChange
70
- .add(oracleTwapTimeSinceLastUpdate)
71
- .add(oracleInvalidDuration));
72
- }
73
- const twapSpread = lastMarkTwapWithMantissa.sub(lastOracleTwapWithMantissa);
57
+ // if passing live oracle data, improve predicted calc estimate
58
+ if (oraclePriceData) {
59
+ const oraclePrice = oraclePriceData.price;
60
+ const oracleLiveVsTwap = oraclePrice
61
+ .sub(lastOracleTwapWithMantissa)
62
+ .abs()
63
+ .mul(numericConstants_1.MARK_PRICE_PRECISION)
64
+ .mul(new anchor_1.BN(100))
65
+ .div(lastOracleTwapWithMantissa);
66
+ // verify pyth live input is within 10% of last twap for live update
67
+ if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
68
+ oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
69
+ .mul(lastOracleTwapWithMantissa)
70
+ .add(timeSinceLastMarkChange.mul(oraclePrice))
71
+ .div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
72
+ }
73
+ }
74
+ const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
75
+ .mul(lastOracleTwapWithMantissa)
76
+ .add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
77
+ .div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
78
+ const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
74
79
  const twapSpreadPct = twapSpread
75
80
  .mul(numericConstants_1.MARK_PRICE_PRECISION)
76
81
  .mul(new anchor_1.BN(100))
77
- .div(oracleTwapWithMantissa);
82
+ .div(shrunkLastOracleTwapwithMantissa);
78
83
  const lowerboundEst = twapSpreadPct
79
84
  .mul(payFreq)
80
85
  .mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
package/lib/types.d.ts CHANGED
@@ -333,6 +333,7 @@ export declare type UserAccount = {
333
333
  cumulativeDeposits: BN;
334
334
  positions: PublicKey;
335
335
  totalFeePaid: BN;
336
+ totalFeeRebate: BN;
336
337
  totalTokenDiscount: BN;
337
338
  totalReferralReward: BN;
338
339
  totalRefereeDiscount: BN;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.34-master.1",
3
+ "version": "0.1.34-master.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -2538,7 +2538,11 @@
2538
2538
  },
2539
2539
  {
2540
2540
  "name": "totalFeePaid",
2541
- "type": "u128"
2541
+ "type": "u64"
2542
+ },
2543
+ {
2544
+ "name": "totalFeeRebate",
2545
+ "type": "u64"
2542
2546
  },
2543
2547
  {
2544
2548
  "name": "totalTokenDiscount",
@@ -3369,7 +3373,7 @@
3369
3373
  },
3370
3374
  {
3371
3375
  "name": "fee",
3372
- "type": "u128"
3376
+ "type": "i128"
3373
3377
  },
3374
3378
  {
3375
3379
  "name": "fillerReward",
@@ -3620,7 +3624,7 @@
3620
3624
  },
3621
3625
  {
3622
3626
  "name": "fee",
3623
- "type": "u128"
3627
+ "type": "i128"
3624
3628
  },
3625
3629
  {
3626
3630
  "name": "quoteAssetAmountSurplus",
@@ -3772,7 +3776,7 @@
3772
3776
  },
3773
3777
  {
3774
3778
  "name": "fee",
3775
- "type": "u128"
3779
+ "type": "i128"
3776
3780
  },
3777
3781
  {
3778
3782
  "name": "direction",
@@ -18,7 +18,7 @@ import { OraclePriceData } from '../oracles/types';
18
18
  */
19
19
  export async function calculateAllEstimatedFundingRate(
20
20
  market: Market,
21
- oraclePriceData: OraclePriceData,
21
+ oraclePriceData?: OraclePriceData,
22
22
  periodAdjustment: BN = new BN(1)
23
23
  ): Promise<[BN, BN, BN, BN, BN]> {
24
24
  // periodAdjustment
@@ -27,6 +27,7 @@ export async function calculateAllEstimatedFundingRate(
27
27
  // 24 * 365.25: annualized
28
28
  const secondsInHour = new BN(3600);
29
29
  const hoursInDay = new BN(24);
30
+ const ONE = new BN(1);
30
31
 
31
32
  if (!market.initialized) {
32
33
  return [ZERO, ZERO, ZERO, ZERO, ZERO];
@@ -66,39 +67,47 @@ export async function calculateAllEstimatedFundingRate(
66
67
 
67
68
  const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
68
69
  const oracleTwapTimeSinceLastUpdate = BN.max(
69
- secondsInHour,
70
- secondsInHour.sub(timeSinceLastOracleTwapUpdate)
70
+ ONE,
71
+ BN.min(
72
+ secondsInHour,
73
+ BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))
74
+ )
71
75
  );
72
-
73
- const oraclePrice = oraclePriceData.price;
74
76
  let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
75
77
 
76
- const oracleLiveVsTwap = oraclePrice
77
- .sub(lastOracleTwapWithMantissa)
78
- .abs()
79
- .mul(MARK_PRICE_PRECISION)
80
- .mul(new BN(100))
81
- .div(lastOracleTwapWithMantissa);
82
-
83
- // verify pyth live input is within 10% of last twap for live update
84
- if (oracleLiveVsTwap.lte(MARK_PRICE_PRECISION.mul(new BN(10)))) {
85
- oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
86
- .mul(lastOracleTwapWithMantissa)
87
- .add(timeSinceLastMarkChange.mul(oraclePrice))
88
- .add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
89
- .div(
90
- timeSinceLastMarkChange
91
- .add(oracleTwapTimeSinceLastUpdate)
92
- .add(oracleInvalidDuration)
93
- );
78
+ // if passing live oracle data, improve predicted calc estimate
79
+ if (oraclePriceData) {
80
+ const oraclePrice = oraclePriceData.price;
81
+
82
+ const oracleLiveVsTwap = oraclePrice
83
+ .sub(lastOracleTwapWithMantissa)
84
+ .abs()
85
+ .mul(MARK_PRICE_PRECISION)
86
+ .mul(new BN(100))
87
+ .div(lastOracleTwapWithMantissa);
88
+
89
+ // verify pyth live input is within 10% of last twap for live update
90
+ if (oracleLiveVsTwap.lte(MARK_PRICE_PRECISION.mul(new BN(10)))) {
91
+ oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
92
+ .mul(lastOracleTwapWithMantissa)
93
+ .add(timeSinceLastMarkChange.mul(oraclePrice))
94
+ .div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
95
+ }
94
96
  }
95
97
 
96
- const twapSpread = lastMarkTwapWithMantissa.sub(lastOracleTwapWithMantissa);
98
+ const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
99
+ .mul(lastOracleTwapWithMantissa)
100
+ .add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
101
+ .div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
102
+
103
+ const twapSpread = lastMarkTwapWithMantissa.sub(
104
+ shrunkLastOracleTwapwithMantissa
105
+ );
97
106
 
98
107
  const twapSpreadPct = twapSpread
99
108
  .mul(MARK_PRICE_PRECISION)
100
109
  .mul(new BN(100))
101
- .div(oracleTwapWithMantissa);
110
+ .div(shrunkLastOracleTwapwithMantissa);
102
111
 
103
112
  const lowerboundEst = twapSpreadPct
104
113
  .mul(payFreq)
@@ -197,9 +206,9 @@ export async function calculateAllEstimatedFundingRate(
197
206
  */
198
207
  export async function calculateEstimatedFundingRate(
199
208
  market: Market,
200
- oraclePriceData: OraclePriceData,
209
+ oraclePriceData?: OraclePriceData,
201
210
  periodAdjustment: BN = new BN(1),
202
- estimationMethod: 'interpolated' | 'lowerbound' | 'capped'
211
+ estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'
203
212
  ): Promise<BN> {
204
213
  const [_1, _2, lowerboundEst, cappedAltEst, interpEst] =
205
214
  await calculateAllEstimatedFundingRate(
@@ -227,7 +236,7 @@ export async function calculateEstimatedFundingRate(
227
236
  */
228
237
  export async function calculateLongShortFundingRate(
229
238
  market: Market,
230
- oraclePriceData: OraclePriceData,
239
+ oraclePriceData?: OraclePriceData,
231
240
  periodAdjustment: BN = new BN(1)
232
241
  ): Promise<[BN, BN]> {
233
242
  const [_1, _2, _, cappedAltEst, interpEst] =
@@ -255,7 +264,7 @@ export async function calculateLongShortFundingRate(
255
264
  */
256
265
  export async function calculateLongShortFundingRateAndLiveTwaps(
257
266
  market: Market,
258
- oraclePriceData: OraclePriceData,
267
+ oraclePriceData?: OraclePriceData,
259
268
  periodAdjustment: BN = new BN(1)
260
269
  ): Promise<[BN, BN, BN, BN]> {
261
270
  const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] =
package/src/types.ts CHANGED
@@ -332,6 +332,7 @@ export type UserAccount = {
332
332
  cumulativeDeposits: BN;
333
333
  positions: PublicKey;
334
334
  totalFeePaid: BN;
335
+ totalFeeRebate: BN;
335
336
  totalTokenDiscount: BN;
336
337
  totalReferralReward: BN;
337
338
  totalRefereeDiscount: BN;