@drift-labs/sdk 0.1.36-master.0 → 0.1.36-master.3

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.
package/lib/math/amm.d.ts CHANGED
@@ -21,6 +21,7 @@ export declare type AssetType = 'quote' | 'base';
21
21
  * @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
22
22
  */
23
23
  export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
24
+ export declare function calculateSpread(amm: AMM, direction: PositionDirection): number;
24
25
  export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection): {
25
26
  baseAssetReserve: BN;
26
27
  quoteAssetReserve: BN;
package/lib/math/amm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateQuoteAssetAmountSwapped = exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.calculateRepegCost = exports.calculateAdjustKCost = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = void 0;
3
+ exports.calculateQuoteAssetAmountSwapped = exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.calculateRepegCost = exports.calculateAdjustKCost = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = void 0;
4
4
  const anchor_1 = require("@project-serum/anchor");
5
5
  const numericConstants_1 = require("../constants/numericConstants");
6
6
  const position_1 = require("./position");
@@ -51,14 +51,27 @@ function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDire
51
51
  return [newQuoteAssetReserve, newBaseAssetReserve];
52
52
  }
53
53
  exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
54
+ function calculateSpread(amm, direction) {
55
+ let spread;
56
+ // future logic
57
+ if ((0, types_1.isVariant)(direction, 'long')) {
58
+ spread = amm.baseSpread;
59
+ }
60
+ else {
61
+ spread = amm.baseSpread;
62
+ }
63
+ return spread;
64
+ }
65
+ exports.calculateSpread = calculateSpread;
54
66
  function calculateSpreadReserves(amm, direction) {
55
- if (amm.baseSpread === 0) {
67
+ const spread = calculateSpread(amm, direction);
68
+ if (spread === 0) {
56
69
  return {
57
70
  baseAssetReserve: amm.baseAssetReserve,
58
71
  quoteAssetReserve: amm.quoteAssetReserve,
59
72
  };
60
73
  }
61
- const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(amm.baseSpread / 4)));
74
+ const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(spread / 4)));
62
75
  let quoteAssetReserve;
63
76
  if ((0, types_1.isVariant)(direction, 'long')) {
64
77
  quoteAssetReserve = amm.quoteAssetReserve.add(quoteAsserReserveDelta);
@@ -0,0 +1,8 @@
1
+ import { StateAccount } from '../types';
2
+ /**
3
+ * Get the clearing house percent fee charged on notional of taking trades
4
+ *
5
+ * @param state
6
+ * @returns Precision : basis points (bps)
7
+ */
8
+ export declare function getExchangeFee(state: StateAccount): number;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getExchangeFee = void 0;
4
+ /**
5
+ * Get the clearing house percent fee charged on notional of taking trades
6
+ *
7
+ * @param state
8
+ * @returns Precision : basis points (bps)
9
+ */
10
+ function getExchangeFee(state) {
11
+ const exchangeFee = state.feeStructure.feeNumerator.toNumber() /
12
+ state.feeStructure.feeDenominator.toNumber();
13
+ return exchangeFee;
14
+ }
15
+ exports.getExchangeFee = getExchangeFee;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.36-master.0",
3
+ "version": "0.1.36-master.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -85,8 +85,14 @@ export class PollingClearingHouseAccountSubscriber
85
85
 
86
86
  await this.updateAccountsToPoll();
87
87
  await this.addToAccountLoader();
88
- await this.fetch();
89
- const subscriptionSucceeded = this.didSubscriptionSucceed();
88
+
89
+ let subscriptionSucceeded = false;
90
+ let retries = 0;
91
+ while (!subscriptionSucceeded && retries < 5) {
92
+ await this.fetch();
93
+ subscriptionSucceeded = this.didSubscriptionSucceed();
94
+ retries++;
95
+ }
90
96
 
91
97
  if (subscriptionSucceeded) {
92
98
  this.eventEmitter.emit('update');
@@ -237,6 +243,10 @@ export class PollingClearingHouseAccountSubscriber
237
243
  // @ts-ignore
238
244
  this.eventEmitter.emit(accountToPoll.eventType, account);
239
245
  this.eventEmitter.emit('update');
246
+
247
+ if (!this.isSubscribed) {
248
+ this.isSubscribed = this.didSubscriptionSucceed();
249
+ }
240
250
  }
241
251
  );
242
252
  }
@@ -37,11 +37,21 @@ export class PollingOracleSubscriber implements OracleSubscriber {
37
37
  }
38
38
 
39
39
  this.addToAccountLoader();
40
- await this.fetch();
41
- this.eventEmitter.emit('update');
42
40
 
43
- this.isSubscribed = true;
44
- return true;
41
+ let subscriptionSucceeded = false;
42
+ let retries = 0;
43
+ while (!subscriptionSucceeded && retries < 5) {
44
+ await this.fetch();
45
+ subscriptionSucceeded = this.didSubscriptionSucceed();
46
+ retries++;
47
+ }
48
+
49
+ if (subscriptionSucceeded) {
50
+ this.eventEmitter.emit('update');
51
+ }
52
+
53
+ this.isSubscribed = subscriptionSucceeded;
54
+ return subscriptionSucceeded;
45
55
  }
46
56
 
47
57
  addToAccountLoader(): void {
@@ -100,4 +110,8 @@ export class PollingOracleSubscriber implements OracleSubscriber {
100
110
  this.assertIsSubscribed();
101
111
  return this.oraclePriceData;
102
112
  }
113
+
114
+ didSubscriptionSucceed(): boolean {
115
+ return !!this.oraclePriceData;
116
+ }
103
117
  }
@@ -36,11 +36,20 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
36
36
  }
37
37
 
38
38
  this.addToAccountLoader();
39
- await this.fetch();
40
- this.eventEmitter.emit('update');
39
+ let subscriptionSucceeded = false;
40
+ let retries = 0;
41
+ while (!subscriptionSucceeded && retries < 5) {
42
+ await this.fetch();
43
+ subscriptionSucceeded = this.didSubscriptionSucceed();
44
+ retries++;
45
+ }
46
+
47
+ if (subscriptionSucceeded) {
48
+ this.eventEmitter.emit('update');
49
+ }
41
50
 
42
- this.isSubscribed = true;
43
- return true;
51
+ this.isSubscribed = subscriptionSucceeded;
52
+ return subscriptionSucceeded;
44
53
  }
45
54
 
46
55
  addToAccountLoader(): void {
@@ -96,4 +105,8 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
96
105
  this.assertIsSubscribed();
97
106
  return this.tokenAccount;
98
107
  }
108
+
109
+ didSubscriptionSucceed(): boolean {
110
+ return !!this.tokenAccount;
111
+ }
99
112
  }
@@ -52,9 +52,14 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
52
52
  }
53
53
 
54
54
  await this.addToAccountLoader();
55
- await this.fetchIfUnloaded();
56
55
 
57
- const subscriptionSucceeded = this.didSubscriptionSucceed();
56
+ let subscriptionSucceeded = false;
57
+ let retries = 0;
58
+ while (!subscriptionSucceeded && retries < 5) {
59
+ await this.fetchIfUnloaded();
60
+ subscriptionSucceeded = this.didSubscriptionSucceed();
61
+ retries++;
62
+ }
58
63
 
59
64
  if (subscriptionSucceeded) {
60
65
  this.eventEmitter.emit('update');