@drift-labs/sdk 0.1.34-master.2 → 0.1.34-master.5

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.
@@ -221,4 +221,15 @@ exports.Markets = [
221
221
  launchTs: 1651017354000,
222
222
  oracleSource: __1.OracleSource.SWITCHBOARD,
223
223
  },
224
+ {
225
+ fullName: 'Fantom',
226
+ category: ['L1', 'Infra'],
227
+ symbol: 'FTM-PERP',
228
+ baseAssetSymbol: 'FTM',
229
+ marketIndex: new __1.BN(20),
230
+ devnetPublicKey: 'BTwrLU4so1oJMViWA3BTzh8YmFwiLZ6CL4U3JryG7Q5S',
231
+ mainnetPublicKey: '7Dn52EY5EGE8Nvvw98KVMGPWTiTGn3PF4y24TVLyXdT9',
232
+ launchTs: 1651559653000,
233
+ oracleSource: __1.OracleSource.PYTH,
234
+ },
224
235
  ];
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.34-master.2",
3
+ "version": "0.1.34-master.5",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -231,4 +231,15 @@ export const Markets: MarketConfig[] = [
231
231
  launchTs: 1651017354000,
232
232
  oracleSource: OracleSource.SWITCHBOARD,
233
233
  },
234
+ {
235
+ fullName: 'Fantom',
236
+ category: ['L1', 'Infra'],
237
+ symbol: 'FTM-PERP',
238
+ baseAssetSymbol: 'FTM',
239
+ marketIndex: new BN(20),
240
+ devnetPublicKey: 'BTwrLU4so1oJMViWA3BTzh8YmFwiLZ6CL4U3JryG7Q5S',
241
+ mainnetPublicKey: '7Dn52EY5EGE8Nvvw98KVMGPWTiTGn3PF4y24TVLyXdT9',
242
+ launchTs: 1651559653000,
243
+ oracleSource: OracleSource.PYTH,
244
+ },
234
245
  ];
@@ -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] =