@drift-labs/sdk 0.2.0-master.31 → 0.2.0-master.33

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 (99) 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 +19 -11
  22. package/lib/dlob/DLOB.js +208 -107
  23. package/lib/dlob/DLOBNode.js +2 -10
  24. package/lib/dlob/NodeList.d.ts +1 -0
  25. package/lib/dlob/NodeList.js +6 -1
  26. package/lib/events/eventSubscriber.d.ts +1 -0
  27. package/lib/events/eventSubscriber.js +11 -4
  28. package/lib/events/fetchLogs.d.ts +3 -1
  29. package/lib/events/fetchLogs.js +13 -5
  30. package/lib/events/pollingLogProvider.js +1 -1
  31. package/lib/events/types.d.ts +1 -1
  32. package/lib/events/webSocketLogProvider.js +1 -1
  33. package/lib/factory/bigNum.d.ts +5 -4
  34. package/lib/factory/bigNum.js +36 -6
  35. package/lib/idl/clearing_house.json +1527 -1242
  36. package/lib/index.d.ts +3 -0
  37. package/lib/index.js +3 -0
  38. package/lib/math/amm.js +9 -9
  39. package/lib/math/exchangeStatus.d.ts +4 -0
  40. package/lib/math/exchangeStatus.js +18 -0
  41. package/lib/math/funding.js +10 -10
  42. package/lib/math/margin.js +6 -1
  43. package/lib/math/market.js +9 -9
  44. package/lib/math/orders.d.ts +7 -3
  45. package/lib/math/orders.js +39 -31
  46. package/lib/math/repeg.js +3 -3
  47. package/lib/math/spotBalance.js +3 -3
  48. package/lib/math/spotPosition.js +2 -2
  49. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  50. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  51. package/lib/serum/serumSubscriber.d.ts +4 -0
  52. package/lib/serum/serumSubscriber.js +16 -1
  53. package/lib/types.d.ts +70 -85
  54. package/lib/types.js +12 -11
  55. package/lib/userMap/userMap.d.ts +17 -1
  56. package/lib/userMap/userMap.js +12 -0
  57. package/lib/userName.d.ts +1 -0
  58. package/lib/userName.js +3 -2
  59. package/package.json +1 -1
  60. package/src/accounts/bulkAccountLoader.ts +5 -1
  61. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +9 -9
  62. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +8 -8
  63. package/src/addresses/marketAddresses.ts +2 -2
  64. package/src/addresses/pda.ts +20 -20
  65. package/src/admin.ts +246 -221
  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 +290 -120
  72. package/src/dlob/DLOBNode.ts +2 -14
  73. package/src/dlob/NodeList.ts +7 -1
  74. package/src/events/eventSubscriber.ts +18 -4
  75. package/src/events/fetchLogs.ts +20 -5
  76. package/src/events/pollingLogProvider.ts +1 -1
  77. package/src/events/types.ts +2 -1
  78. package/src/events/webSocketLogProvider.ts +1 -1
  79. package/src/factory/bigNum.ts +59 -6
  80. package/src/idl/clearing_house.json +1527 -1242
  81. package/src/idl/pyth.json +98 -2
  82. package/src/index.ts +3 -0
  83. package/src/math/amm.ts +9 -9
  84. package/src/math/exchangeStatus.ts +31 -0
  85. package/src/math/funding.ts +20 -10
  86. package/src/math/margin.ts +7 -1
  87. package/src/math/market.ts +9 -9
  88. package/src/math/orders.ts +44 -29
  89. package/src/math/repeg.ts +3 -3
  90. package/src/math/spotBalance.ts +4 -4
  91. package/src/math/spotPosition.ts +2 -2
  92. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  93. package/src/serum/serumSubscriber.ts +20 -1
  94. package/src/types.ts +75 -61
  95. package/src/userMap/userMap.ts +25 -1
  96. package/src/userName.ts +2 -1
  97. package/tests/bn/test.ts +22 -1
  98. package/tests/dlob/helpers.ts +252 -81
  99. package/tests/dlob/test.ts +1115 -215
@@ -1,6 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LogParser = exports.fetchLogs = void 0;
4
+ function mapTransactionResponseToLog(transaction) {
5
+ return {
6
+ txSig: transaction.transaction.signatures[0],
7
+ slot: transaction.slot,
8
+ logs: transaction.meta.logMessages,
9
+ };
10
+ }
4
11
  async function fetchLogs(connection, programId, finality, beforeTx, untilTx, limit) {
5
12
  const signatures = await connection.getSignaturesForAddress(programId, {
6
13
  before: beforeTx,
@@ -16,11 +23,7 @@ async function fetchLogs(connection, programId, finality, beforeTx, untilTx, lim
16
23
  const transactionLogs = (await Promise.all(chunkedSignatures.map(async (chunk) => {
17
24
  const transactions = await connection.getTransactions(chunk.map((confirmedSignature) => confirmedSignature.signature), finality);
18
25
  return transactions.map((transaction) => {
19
- return {
20
- txSig: transaction.transaction.signatures[0],
21
- slot: transaction.slot,
22
- logs: transaction.meta.logMessages,
23
- };
26
+ return mapTransactionResponseToLog(transaction);
24
27
  });
25
28
  }))).flat();
26
29
  const earliest = filteredSignatures[0];
@@ -31,6 +34,7 @@ async function fetchLogs(connection, programId, finality, beforeTx, untilTx, lim
31
34
  mostRecentTx: mostRecent.signature,
32
35
  earliestSlot: earliest.slot,
33
36
  mostRecentSlot: mostRecent.slot,
37
+ mostRecentBlockTime: mostRecent.blockTime,
34
38
  };
35
39
  }
36
40
  exports.fetchLogs = fetchLogs;
@@ -44,6 +48,10 @@ class LogParser {
44
48
  constructor(program) {
45
49
  this.program = program;
46
50
  }
51
+ parseEventsFromTransaction(transaction) {
52
+ const transactionLogObject = mapTransactionResponseToLog(transaction);
53
+ return this.parseEventsFromLogs(transactionLogObject);
54
+ }
47
55
  parseEventsFromLogs(event) {
48
56
  const records = [];
49
57
  // @ts-ignore
@@ -29,7 +29,7 @@ class PollingLogProvider {
29
29
  }
30
30
  const { mostRecentTx, transactionLogs } = response;
31
31
  for (const { txSig, slot, logs } of transactionLogs) {
32
- callback(txSig, slot, logs);
32
+ callback(txSig, slot, logs, response.mostRecentBlockTime);
33
33
  }
34
34
  this.mostRecentSeenTx = mostRecentTx;
35
35
  }
@@ -39,7 +39,7 @@ export interface EventSubscriberEvents {
39
39
  newEvent: (event: WrappedEvent<EventType>) => void;
40
40
  }
41
41
  export declare type SortFn = (currentRecord: EventMap[EventType], newRecord: EventMap[EventType]) => 'less than' | 'greater than';
42
- export declare type logProviderCallback = (txSig: TransactionSignature, slot: number, logs: string[]) => void;
42
+ export declare type logProviderCallback = (txSig: TransactionSignature, slot: number, logs: string[], mostRecentBlockTime: number | undefined) => void;
43
43
  export interface LogProvider {
44
44
  isSubscribed(): boolean;
45
45
  subscribe(callback: logProviderCallback, skipHistory?: boolean): boolean;
@@ -12,7 +12,7 @@ class WebSocketLogProvider {
12
12
  return true;
13
13
  }
14
14
  this.subscriptionId = this.connection.onLogs(this.programId, (logs, ctx) => {
15
- callback(logs.signature, ctx.slot, logs.logs);
15
+ callback(logs.signature, ctx.slot, logs.logs, undefined);
16
16
  }, this.commitment);
17
17
  return true;
18
18
  }
@@ -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) {