@drift-labs/sdk 0.2.0-master.11 → 0.2.0-master.14

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.
Files changed (114) hide show
  1. package/lib/accounts/fetch.d.ts +2 -1
  2. package/lib/accounts/fetch.js +9 -1
  3. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  4. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  5. package/lib/accounts/types.d.ts +14 -1
  6. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  7. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  8. package/lib/addresses/pda.d.ts +1 -0
  9. package/lib/addresses/pda.js +8 -1
  10. package/lib/admin.d.ts +2 -0
  11. package/lib/admin.js +18 -0
  12. package/lib/clearingHouse.d.ts +24 -3
  13. package/lib/clearingHouse.js +352 -51
  14. package/lib/clearingHouseConfig.d.ts +1 -0
  15. package/lib/clearingHouseUser.d.ts +15 -15
  16. package/lib/clearingHouseUser.js +185 -73
  17. package/lib/clearingHouseUserStats.d.ts +17 -0
  18. package/lib/clearingHouseUserStats.js +36 -0
  19. package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
  20. package/lib/clearingHouseUserStatsConfig.js +2 -0
  21. package/lib/config.js +1 -1
  22. package/lib/constants/banks.d.ts +2 -2
  23. package/lib/constants/banks.js +4 -3
  24. package/lib/constants/numericConstants.d.ts +2 -0
  25. package/lib/constants/numericConstants.js +3 -1
  26. package/lib/events/eventList.js +3 -0
  27. package/lib/events/types.d.ts +2 -1
  28. package/lib/factory/bigNum.d.ts +1 -0
  29. package/lib/factory/bigNum.js +37 -11
  30. package/lib/idl/clearing_house.json +692 -58
  31. package/lib/index.d.ts +1 -0
  32. package/lib/index.js +1 -0
  33. package/lib/math/amm.js +2 -2
  34. package/lib/math/bankBalance.d.ts +7 -1
  35. package/lib/math/bankBalance.js +76 -1
  36. package/lib/math/margin.d.ts +11 -0
  37. package/lib/math/margin.js +72 -0
  38. package/lib/math/market.d.ts +4 -1
  39. package/lib/math/market.js +35 -1
  40. package/lib/math/oracles.d.ts +3 -0
  41. package/lib/math/oracles.js +25 -5
  42. package/lib/math/position.d.ts +8 -0
  43. package/lib/math/position.js +43 -12
  44. package/lib/math/trade.js +2 -2
  45. package/lib/orders.d.ts +1 -2
  46. package/lib/orders.js +2 -77
  47. package/lib/tokenFaucet.d.ts +1 -0
  48. package/lib/tokenFaucet.js +23 -12
  49. package/lib/tx/retryTxSender.js +9 -2
  50. package/lib/types.d.ts +78 -10
  51. package/lib/types.js +12 -0
  52. package/lib/util/getTokenAddress.d.ts +2 -0
  53. package/lib/util/getTokenAddress.js +9 -0
  54. package/package.json +1 -1
  55. package/src/accounts/fetch.ts +27 -2
  56. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  57. package/src/accounts/types.ts +18 -0
  58. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  59. package/src/addresses/pda.ts +13 -0
  60. package/src/admin.ts +29 -1
  61. package/src/clearingHouse.ts +619 -62
  62. package/src/clearingHouseConfig.ts +1 -0
  63. package/src/clearingHouseUser.ts +317 -105
  64. package/src/clearingHouseUserStats.ts +53 -0
  65. package/src/clearingHouseUserStatsConfig.ts +18 -0
  66. package/src/config.ts +1 -1
  67. package/src/constants/banks.js +42 -0
  68. package/src/constants/banks.ts +6 -3
  69. package/src/constants/markets.js +42 -0
  70. package/src/constants/numericConstants.js +41 -0
  71. package/src/constants/numericConstants.ts +3 -0
  72. package/src/events/eventList.ts +3 -0
  73. package/src/events/types.ts +2 -0
  74. package/src/factory/bigNum.js +37 -11
  75. package/src/factory/bigNum.ts +43 -13
  76. package/src/idl/clearing_house.json +692 -58
  77. package/src/index.ts +1 -0
  78. package/src/math/amm.ts +8 -5
  79. package/src/math/bankBalance.ts +147 -1
  80. package/src/math/margin.ts +124 -0
  81. package/src/math/market.ts +66 -1
  82. package/src/math/oracles.ts +42 -5
  83. package/src/math/position.ts +60 -9
  84. package/src/math/trade.ts +2 -2
  85. package/src/orders.ts +4 -157
  86. package/src/tokenFaucet.js +189 -0
  87. package/src/tokenFaucet.ts +38 -15
  88. package/src/tx/retryTxSender.ts +11 -3
  89. package/src/types.js +12 -1
  90. package/src/types.ts +83 -10
  91. package/src/{accounts/fetch.js → util/computeUnits.js} +11 -13
  92. package/src/util/getTokenAddress.js +9 -0
  93. package/src/util/getTokenAddress.ts +18 -0
  94. package/tests/bn/test.ts +2 -0
  95. package/src/accounts/bulkAccountLoader.js +0 -197
  96. package/src/accounts/bulkUserSubscription.js +0 -33
  97. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  98. package/src/accounts/pollingOracleSubscriber.js +0 -93
  99. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  100. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  101. package/src/accounts/types.js +0 -10
  102. package/src/accounts/utils.js +0 -7
  103. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  104. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  105. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  106. package/src/addresses/pda.js +0 -104
  107. package/src/index.js +0 -100
  108. package/src/math/bankBalance.js +0 -75
  109. package/src/math/market.js +0 -57
  110. package/src/math/orders.js +0 -110
  111. package/src/math/position.js +0 -140
  112. package/src/mockUSDCFaucet.js +0 -280
  113. package/src/orders.js +0 -134
  114. package/src/tx/retryTxSender.js +0 -188
@@ -40,6 +40,9 @@ class EventList {
40
40
  if (currentNode.next !== undefined) {
41
41
  newNode.next.prev = newNode;
42
42
  }
43
+ else {
44
+ this.tail = newNode;
45
+ }
43
46
  currentNode.next = newNode;
44
47
  newNode.prev = currentNode;
45
48
  }
@@ -1,5 +1,5 @@
1
1
  import { Commitment, TransactionSignature } from '@solana/web3.js';
2
- import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, OrderRecord } from '../index';
2
+ import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, OrderRecord, SettlePnlRecord } from '../index';
3
3
  export declare type EventSubscriptionOptions = {
4
4
  eventTypes?: EventType[];
5
5
  maxEventsPerType?: number;
@@ -27,6 +27,7 @@ export declare type EventMap = {
27
27
  LiquidationRecord: Event<LiquidationRecord>;
28
28
  FundingRateRecord: Event<FundingRateRecord>;
29
29
  OrderRecord: Event<OrderRecord>;
30
+ SettlePnlRecord: Event<SettlePnlRecord>;
30
31
  };
31
32
  export declare type EventType = keyof EventMap;
32
33
  export interface EventSubscriberEvents {
@@ -6,6 +6,7 @@ export declare class BigNum {
6
6
  static delim: string;
7
7
  static spacer: string;
8
8
  constructor(val: BN | number | string, precisionVal?: BN | number | string);
9
+ private bigNumFromParam;
9
10
  add(bn: BigNum): BigNum;
10
11
  sub(bn: BigNum): BigNum;
11
12
  mul(bn: BigNum | BN): BigNum;
@@ -10,6 +10,9 @@ class BigNum {
10
10
  this.val = new anchor_1.BN(val);
11
11
  this.precision = new anchor_1.BN(precisionVal);
12
12
  }
13
+ bigNumFromParam(bn) {
14
+ return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
15
+ }
13
16
  add(bn) {
14
17
  (0, assert_1.assert)(bn.precision.eq(this.precision), 'Adding unequal precisions');
15
18
  return BigNum.from(this.val.add(bn.val), this.precision);
@@ -19,7 +22,7 @@ class BigNum {
19
22
  return BigNum.from(this.val.sub(bn.val), this.precision);
20
23
  }
21
24
  mul(bn) {
22
- const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
25
+ const mulVal = this.bigNumFromParam(bn);
23
26
  return BigNum.from(this.val.mul(mulVal.val), this.precision.add(mulVal.precision));
24
27
  }
25
28
  /**
@@ -28,12 +31,12 @@ class BigNum {
28
31
  * @returns
29
32
  */
30
33
  scalarMul(bn) {
31
- if (bn instanceof anchor_1.BN)
34
+ if (anchor_1.BN.isBN(bn))
32
35
  return BigNum.from(this.val.mul(bn), this.precision);
33
36
  return BigNum.from(this.val.mul(bn.val), this.precision.add(bn.precision)).shift(bn.precision.neg());
34
37
  }
35
38
  div(bn) {
36
- if (bn instanceof anchor_1.BN)
39
+ if (anchor_1.BN.isBN(bn))
37
40
  return BigNum.from(this.val.div(bn), this.precision);
38
41
  return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
39
42
  }
@@ -73,35 +76,35 @@ class BigNum {
73
76
  .toPrecision(precision);
74
77
  }
75
78
  gt(bn, ignorePrecision) {
76
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
79
+ const comparisonVal = this.bigNumFromParam(bn);
77
80
  if (!ignorePrecision && !comparisonVal.eq(numericConstants_1.ZERO)) {
78
81
  (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
79
82
  }
80
83
  return this.val.gt(comparisonVal.val);
81
84
  }
82
85
  lt(bn, ignorePrecision) {
83
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
86
+ const comparisonVal = this.bigNumFromParam(bn);
84
87
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
85
88
  (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
86
89
  }
87
90
  return this.val.lt(comparisonVal.val);
88
91
  }
89
92
  gte(bn, ignorePrecision) {
90
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
93
+ const comparisonVal = this.bigNumFromParam(bn);
91
94
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
92
95
  (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
93
96
  }
94
97
  return this.val.gte(comparisonVal.val);
95
98
  }
96
99
  lte(bn, ignorePrecision) {
97
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
100
+ const comparisonVal = this.bigNumFromParam(bn);
98
101
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
99
102
  (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
100
103
  }
101
104
  return this.val.lte(comparisonVal.val);
102
105
  }
103
106
  eq(bn, ignorePrecision) {
104
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
107
+ const comparisonVal = this.bigNumFromParam(bn);
105
108
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
106
109
  (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
107
110
  }
@@ -149,8 +152,14 @@ class BigNum {
149
152
  // remove leading zeroes
150
153
  printString = printString.replace(/^0+/, '');
151
154
  // add zero if leading delim
152
- if (printString[0] === BigNum.delim)
153
- printString = `0${printString}`;
155
+ if (this.isNeg()) {
156
+ if (printString[1] === BigNum.delim)
157
+ printString = printString.replace('-.', '-0.');
158
+ }
159
+ else {
160
+ if (printString[0] === BigNum.delim)
161
+ printString = `0${printString}`;
162
+ }
154
163
  // remove trailing delim
155
164
  if (printString[printString.length - 1] === BigNum.delim)
156
165
  printString = printString.slice(0, printString.length - 1);
@@ -261,10 +270,27 @@ class BigNum {
261
270
  * @returns
262
271
  */
263
272
  toNotional(useTradePrecision, precisionOverride) {
273
+ var _a;
264
274
  const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
265
- const val = useTradePrecision || precisionOverride
275
+ const usingCustomPrecision = true && (useTradePrecision || precisionOverride);
276
+ let val = usingCustomPrecision
266
277
  ? this.prettyPrint(useTradePrecision, precisionOverride)
267
278
  : BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
279
+ // Append two trailing zeroes if not using custom precision
280
+ if (!usingCustomPrecision) {
281
+ const [_, rightSide] = val.split(BigNum.delim);
282
+ const trailingLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
283
+ if (trailingLength < 2) {
284
+ const numHasDecimals = this.print().includes(BigNum.delim);
285
+ // Handle case where pretty print won't include the decimal point
286
+ if (trailingLength === 0 && numHasDecimals) {
287
+ val = `${val}.00`;
288
+ }
289
+ else {
290
+ val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
291
+ }
292
+ }
293
+ }
268
294
  return `${prefix}${val.replace('-', '')}`;
269
295
  }
270
296
  toMillified(precision = 3) {