@drift-labs/sdk 0.2.0-master.32 → 0.2.0-master.34

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 (108) hide show
  1. package/lib/accounts/bulkAccountLoader.js +2 -1
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +2 -2
  3. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +8 -8
  4. package/lib/accounts/types.d.ts +1 -0
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +2 -2
  6. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +7 -7
  7. package/lib/addresses/marketAddresses.js +1 -1
  8. package/lib/addresses/pda.d.ts +4 -4
  9. package/lib/addresses/pda.js +23 -22
  10. package/lib/admin.d.ts +30 -32
  11. package/lib/admin.js +111 -119
  12. package/lib/clearingHouse.d.ts +43 -34
  13. package/lib/clearingHouse.js +353 -193
  14. package/lib/clearingHouseConfig.d.ts +2 -2
  15. package/lib/clearingHouseUser.d.ts +26 -5
  16. package/lib/clearingHouseUser.js +151 -51
  17. package/lib/config.d.ts +2 -0
  18. package/lib/config.js +5 -1
  19. package/lib/constants/numericConstants.d.ts +1 -0
  20. package/lib/constants/numericConstants.js +3 -2
  21. package/lib/dlob/DLOB.d.ts +18 -11
  22. package/lib/dlob/DLOB.js +179 -107
  23. package/lib/dlob/DLOBNode.js +2 -10
  24. package/lib/dlob/NodeList.js +1 -1
  25. package/lib/events/eventSubscriber.d.ts +1 -0
  26. package/lib/events/eventSubscriber.js +11 -4
  27. package/lib/events/fetchLogs.d.ts +3 -1
  28. package/lib/events/fetchLogs.js +13 -5
  29. package/lib/events/pollingLogProvider.js +1 -1
  30. package/lib/events/types.d.ts +1 -1
  31. package/lib/events/webSocketLogProvider.js +1 -1
  32. package/lib/factory/bigNum.d.ts +5 -4
  33. package/lib/factory/bigNum.js +36 -6
  34. package/lib/idl/clearing_house.json +1540 -1246
  35. package/lib/index.d.ts +3 -0
  36. package/lib/index.js +3 -0
  37. package/lib/math/amm.js +9 -9
  38. package/lib/math/exchangeStatus.d.ts +4 -0
  39. package/lib/math/exchangeStatus.js +18 -0
  40. package/lib/math/funding.js +10 -10
  41. package/lib/math/margin.js +6 -1
  42. package/lib/math/market.js +9 -9
  43. package/lib/math/orders.d.ts +7 -3
  44. package/lib/math/orders.js +39 -31
  45. package/lib/math/repeg.js +3 -3
  46. package/lib/math/spotBalance.js +3 -3
  47. package/lib/math/spotPosition.js +2 -2
  48. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  49. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  50. package/lib/serum/serumSubscriber.d.ts +4 -0
  51. package/lib/serum/serumSubscriber.js +16 -1
  52. package/lib/types.d.ts +60 -75
  53. package/lib/types.js +2 -1
  54. package/lib/userMap/userMap.d.ts +17 -1
  55. package/lib/userMap/userMap.js +12 -0
  56. package/lib/userName.d.ts +1 -0
  57. package/lib/userName.js +3 -2
  58. package/package.json +1 -1
  59. package/src/accounts/bulkAccountLoader.ts +5 -1
  60. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +9 -9
  61. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +8 -8
  62. package/src/addresses/marketAddresses.ts +2 -2
  63. package/src/addresses/pda.ts +20 -20
  64. package/src/admin.ts +246 -221
  65. package/src/assert/assert.js +9 -0
  66. package/src/clearingHouse.ts +556 -236
  67. package/src/clearingHouseConfig.ts +2 -2
  68. package/src/clearingHouseUser.ts +237 -87
  69. package/src/config.ts +8 -1
  70. package/src/constants/numericConstants.ts +5 -1
  71. package/src/dlob/DLOB.ts +257 -120
  72. package/src/dlob/DLOBNode.ts +2 -14
  73. package/src/dlob/NodeList.ts +1 -1
  74. package/src/events/eventList.js +77 -0
  75. package/src/events/eventSubscriber.ts +18 -4
  76. package/src/events/fetchLogs.ts +20 -5
  77. package/src/events/pollingLogProvider.ts +1 -1
  78. package/src/events/types.ts +2 -1
  79. package/src/events/webSocketLogProvider.ts +1 -1
  80. package/src/examples/makeTradeExample.js +157 -0
  81. package/src/factory/bigNum.ts +59 -6
  82. package/src/idl/clearing_house.json +1540 -1246
  83. package/src/idl/pyth.json +98 -2
  84. package/src/index.ts +3 -0
  85. package/src/math/amm.ts +9 -9
  86. package/src/math/exchangeStatus.ts +31 -0
  87. package/src/math/funding.ts +20 -10
  88. package/src/math/margin.ts +7 -1
  89. package/src/math/market.ts +9 -9
  90. package/src/math/orders.ts +44 -29
  91. package/src/math/repeg.ts +3 -3
  92. package/src/math/spotBalance.ts +4 -4
  93. package/src/math/spotPosition.ts +2 -2
  94. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  95. package/src/serum/serumSubscriber.ts +20 -1
  96. package/src/token/index.js +38 -0
  97. package/src/tx/types.js +2 -0
  98. package/src/tx/utils.js +17 -0
  99. package/src/types.ts +65 -51
  100. package/src/userMap/userMap.ts +25 -1
  101. package/src/userName.ts +2 -1
  102. package/src/util/computeUnits.js +27 -0
  103. package/src/util/getTokenAddress.js +9 -0
  104. package/src/util/promiseTimeout.js +14 -0
  105. package/src/util/tps.js +27 -0
  106. package/tests/bn/test.ts +22 -1
  107. package/tests/dlob/helpers.ts +252 -81
  108. package/tests/dlob/test.ts +1040 -219
@@ -68,15 +68,16 @@ export declare class BigNum {
68
68
  * @param fixedPrecision
69
69
  * @returns
70
70
  */
71
- toFixed(fixedPrecision: number): string;
71
+ toFixed(fixedPrecision: number, rounded?: boolean): string;
72
72
  private getZeroes;
73
+ toRounded(roundingPrecision: number): BigNum;
73
74
  /**
74
75
  * Pretty print to the specified number of significant figures
75
76
  * @param fixedPrecision
76
77
  * @returns
77
78
  */
78
- toPrecision(fixedPrecision: number, trailingZeroes?: boolean): string;
79
- toTradePrecision(): string;
79
+ toPrecision(fixedPrecision: number, trailingZeroes?: boolean, rounded?: boolean): string;
80
+ toTradePrecision(rounded?: boolean): string;
80
81
  /**
81
82
  * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
82
83
  * @param useTradePrecision
@@ -84,7 +85,7 @@ export declare class BigNum {
84
85
  * @returns
85
86
  */
86
87
  toNotional(useTradePrecision?: boolean, precisionOverride?: number): string;
87
- toMillified(precision?: number): string;
88
+ toMillified(precision?: number, rounded?: boolean): string;
88
89
  toJSON(): {
89
90
  val: string;
90
91
  precision: string;
@@ -201,7 +201,10 @@ class BigNum {
201
201
  * @param fixedPrecision
202
202
  * @returns
203
203
  */
204
- toFixed(fixedPrecision) {
204
+ toFixed(fixedPrecision, rounded = false) {
205
+ if (rounded) {
206
+ return this.toRounded(fixedPrecision).toFixed(fixedPrecision);
207
+ }
205
208
  const printString = this.print();
206
209
  const [leftSide, rightSide] = printString.split(BigNum.delim);
207
210
  const filledRightSide = [
@@ -213,14 +216,38 @@ class BigNum {
213
216
  return `${leftSide}${BigNum.delim}${filledRightSide}`;
214
217
  }
215
218
  getZeroes(count) {
216
- return new Array(count).fill('0').join('');
219
+ return new Array(Math.max(count, 0)).fill('0').join('');
220
+ }
221
+ toRounded(roundingPrecision) {
222
+ const printString = this.toString();
223
+ let shouldRoundUp = false;
224
+ const roundingDigitChar = printString[roundingPrecision];
225
+ if (roundingDigitChar) {
226
+ const roundingDigitVal = Number(roundingDigitChar);
227
+ if (roundingDigitVal >= 5)
228
+ shouldRoundUp = true;
229
+ }
230
+ if (shouldRoundUp) {
231
+ const valueWithRoundedPrecisionAdded = this.add(BigNum.from(new anchor_1.BN(10).pow(new anchor_1.BN(printString.length - roundingPrecision)), this.precision));
232
+ const roundedUpPrintString = valueWithRoundedPrecisionAdded.toString().slice(0, roundingPrecision) +
233
+ this.getZeroes(printString.length - roundingPrecision);
234
+ return BigNum.from(roundedUpPrintString, this.precision);
235
+ }
236
+ else {
237
+ const roundedDownPrintString = printString.slice(0, roundingPrecision) +
238
+ this.getZeroes(printString.length - roundingPrecision);
239
+ return BigNum.from(roundedDownPrintString, this.precision);
240
+ }
217
241
  }
218
242
  /**
219
243
  * Pretty print to the specified number of significant figures
220
244
  * @param fixedPrecision
221
245
  * @returns
222
246
  */
223
- toPrecision(fixedPrecision, trailingZeroes = false) {
247
+ toPrecision(fixedPrecision, trailingZeroes = false, rounded = false) {
248
+ if (rounded) {
249
+ return this.toRounded(fixedPrecision).toPrecision(fixedPrecision, trailingZeroes);
250
+ }
224
251
  const printString = this.print();
225
252
  let precisionPrintString = printString.slice(0, fixedPrecision + 1);
226
253
  const thisString = this.toString();
@@ -269,8 +296,8 @@ class BigNum {
269
296
  }
270
297
  return precisionPrintString;
271
298
  }
272
- toTradePrecision() {
273
- return this.toPrecision(6, true);
299
+ toTradePrecision(rounded = false) {
300
+ return this.toPrecision(6, true, rounded);
274
301
  }
275
302
  /**
276
303
  * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
@@ -302,7 +329,10 @@ class BigNum {
302
329
  }
303
330
  return `${prefix}${val.replace('-', '')}`;
304
331
  }
305
- toMillified(precision = 3) {
332
+ toMillified(precision = 3, rounded = false) {
333
+ if (rounded) {
334
+ return this.toRounded(precision).toMillified(precision);
335
+ }
306
336
  const stringVal = this.print();
307
337
  const [leftSide] = stringVal.split(BigNum.delim);
308
338
  if (!leftSide) {