@drift-labs/sdk 0.1.20 → 0.1.21-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.
Files changed (126) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +0 -1
  2. package/lib/accounts/bulkAccountLoader.js +17 -6
  3. package/lib/accounts/bulkUserSubscription.d.ts +0 -1
  4. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +5 -2
  5. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +23 -1
  6. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +0 -1
  7. package/lib/accounts/pollingUserAccountSubscriber.d.ts +3 -2
  8. package/lib/accounts/pollingUserAccountSubscriber.js +13 -0
  9. package/lib/accounts/types.d.ts +8 -3
  10. package/lib/accounts/utils.d.ts +0 -1
  11. package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
  12. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +5 -2
  13. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +25 -0
  14. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +3 -2
  15. package/lib/accounts/webSocketUserAccountSubscriber.js +11 -0
  16. package/lib/addresses.d.ts +4 -1
  17. package/lib/addresses.js +28 -1
  18. package/lib/admin.d.ts +10 -4
  19. package/lib/admin.js +48 -3
  20. package/lib/assert/assert.d.ts +0 -1
  21. package/lib/clearingHouse.d.ts +27 -2
  22. package/lib/clearingHouse.js +307 -7
  23. package/lib/clearingHouseUser.d.ts +16 -3
  24. package/lib/clearingHouseUser.js +79 -9
  25. package/lib/config.d.ts +0 -1
  26. package/lib/constants/markets.d.ts +0 -1
  27. package/lib/constants/markets.js +8 -0
  28. package/lib/constants/numericConstants.d.ts +1 -1
  29. package/lib/constants/numericConstants.js +2 -1
  30. package/lib/examples/makeTradeExample.d.ts +0 -1
  31. package/lib/factory/clearingHouse.d.ts +0 -1
  32. package/lib/factory/clearingHouseUser.d.ts +0 -1
  33. package/lib/idl/clearing_house.json +1077 -50
  34. package/lib/index.d.ts +3 -1
  35. package/lib/index.js +3 -0
  36. package/lib/math/amm.d.ts +3 -1
  37. package/lib/math/amm.js +121 -8
  38. package/lib/math/conversion.d.ts +0 -1
  39. package/lib/math/funding.d.ts +0 -1
  40. package/lib/math/insuranceFund.d.ts +0 -1
  41. package/lib/math/market.d.ts +2 -2
  42. package/lib/math/market.js +11 -1
  43. package/lib/math/orders.d.ts +3 -0
  44. package/lib/math/orders.js +32 -0
  45. package/lib/math/position.d.ts +4 -2
  46. package/lib/math/position.js +18 -4
  47. package/lib/math/trade.d.ts +0 -1
  48. package/lib/math/utils.d.ts +0 -1
  49. package/lib/mockUSDCFaucet.d.ts +0 -1
  50. package/lib/orderParams.d.ts +7 -0
  51. package/lib/orderParams.js +108 -0
  52. package/lib/orders.d.ts +6 -0
  53. package/lib/orders.js +136 -0
  54. package/lib/pythClient.d.ts +0 -1
  55. package/lib/token/index.d.ts +0 -1
  56. package/lib/tx/defaultTxSender.d.ts +0 -1
  57. package/lib/tx/types.d.ts +0 -1
  58. package/lib/tx/utils.d.ts +0 -1
  59. package/lib/types.d.ts +145 -2
  60. package/lib/types.js +36 -1
  61. package/lib/util/computeUnits.d.ts +0 -1
  62. package/lib/util/tps.d.ts +0 -1
  63. package/lib/wallet.d.ts +0 -1
  64. package/package.json +1 -1
  65. package/src/accounts/bulkAccountLoader.ts +21 -13
  66. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +33 -0
  67. package/src/accounts/pollingUserAccountSubscriber.ts +30 -2
  68. package/src/accounts/types.ts +11 -2
  69. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +47 -0
  70. package/src/accounts/webSocketUserAccountSubscriber.ts +29 -2
  71. package/src/addresses.ts +37 -0
  72. package/src/admin.ts +84 -6
  73. package/src/clearingHouse.ts +430 -5
  74. package/src/clearingHouseUser.ts +129 -12
  75. package/src/constants/markets.ts +8 -0
  76. package/src/constants/numericConstants.ts +1 -0
  77. package/src/idl/clearing_house.json +1077 -50
  78. package/src/index.ts +3 -0
  79. package/src/math/amm.ts +169 -14
  80. package/src/math/market.ts +28 -2
  81. package/src/math/orders.ts +44 -0
  82. package/src/math/position.ts +23 -3
  83. package/src/orderParams.ts +151 -0
  84. package/src/orders.ts +236 -0
  85. package/src/types.ts +129 -1
  86. package/tsconfig.json +0 -1
  87. package/lib/accounts/bulkAccountLoader.d.ts.map +0 -1
  88. package/lib/accounts/bulkUserSubscription.d.ts.map +0 -1
  89. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts.map +0 -1
  90. package/lib/accounts/pollingTokenAccountSubscriber.d.ts.map +0 -1
  91. package/lib/accounts/pollingUserAccountSubscriber.d.ts.map +0 -1
  92. package/lib/accounts/types.d.ts.map +0 -1
  93. package/lib/accounts/utils.d.ts.map +0 -1
  94. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
  95. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts.map +0 -1
  96. package/lib/accounts/webSocketUserAccountSubscriber.d.ts.map +0 -1
  97. package/lib/addresses.d.ts.map +0 -1
  98. package/lib/admin.d.ts.map +0 -1
  99. package/lib/assert/assert.d.ts.map +0 -1
  100. package/lib/clearingHouse.d.ts.map +0 -1
  101. package/lib/clearingHouseUser.d.ts.map +0 -1
  102. package/lib/config.d.ts.map +0 -1
  103. package/lib/constants/markets.d.ts.map +0 -1
  104. package/lib/constants/numericConstants.d.ts.map +0 -1
  105. package/lib/examples/makeTradeExample.d.ts.map +0 -1
  106. package/lib/factory/clearingHouse.d.ts.map +0 -1
  107. package/lib/factory/clearingHouseUser.d.ts.map +0 -1
  108. package/lib/index.d.ts.map +0 -1
  109. package/lib/math/amm.d.ts.map +0 -1
  110. package/lib/math/conversion.d.ts.map +0 -1
  111. package/lib/math/funding.d.ts.map +0 -1
  112. package/lib/math/insuranceFund.d.ts.map +0 -1
  113. package/lib/math/market.d.ts.map +0 -1
  114. package/lib/math/position.d.ts.map +0 -1
  115. package/lib/math/trade.d.ts.map +0 -1
  116. package/lib/math/utils.d.ts.map +0 -1
  117. package/lib/mockUSDCFaucet.d.ts.map +0 -1
  118. package/lib/pythClient.d.ts.map +0 -1
  119. package/lib/token/index.d.ts.map +0 -1
  120. package/lib/tx/defaultTxSender.d.ts.map +0 -1
  121. package/lib/tx/types.d.ts.map +0 -1
  122. package/lib/tx/utils.d.ts.map +0 -1
  123. package/lib/types.d.ts.map +0 -1
  124. package/lib/util/computeUnits.d.ts.map +0 -1
  125. package/lib/util/tps.d.ts.map +0 -1
  126. package/lib/wallet.d.ts.map +0 -1
@@ -6,4 +6,3 @@ export declare class DefaultTxSender implements TxSender {
6
6
  constructor(provider: Provider);
7
7
  send(tx: Transaction, additionalSigners?: Array<Signer>, opts?: ConfirmOptions): Promise<TransactionSignature>;
8
8
  }
9
- //# sourceMappingURL=defaultTxSender.d.ts.map
package/lib/tx/types.d.ts CHANGED
@@ -2,4 +2,3 @@ import { ConfirmOptions, Signer, Transaction, TransactionSignature } from '@sola
2
2
  export interface TxSender {
3
3
  send(tx: Transaction, additionalSigners?: Array<Signer>, opts?: ConfirmOptions): Promise<TransactionSignature>;
4
4
  }
5
- //# sourceMappingURL=types.d.ts.map
package/lib/tx/utils.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  import { Transaction, TransactionInstruction } from '@solana/web3.js';
2
2
  export declare function wrapInTx(instruction: TransactionInstruction): Transaction;
3
- //# sourceMappingURL=utils.d.ts.map
package/lib/types.d.ts CHANGED
@@ -25,6 +25,65 @@ export declare class OracleSource {
25
25
  switchboard: {};
26
26
  };
27
27
  }
28
+ export declare class OrderType {
29
+ static readonly LIMIT: {
30
+ limit: {};
31
+ };
32
+ static readonly TRIGGER_MARKET: {
33
+ triggerMarket: {};
34
+ };
35
+ static readonly TRIGGER_LIMIT: {
36
+ triggerLimit: {};
37
+ };
38
+ static readonly MARKET: {
39
+ market: {};
40
+ };
41
+ }
42
+ export declare class OrderStatus {
43
+ static readonly INIT: {
44
+ init: {};
45
+ };
46
+ static readonly OPEN: {
47
+ open: {};
48
+ };
49
+ }
50
+ export declare class OrderDiscountTier {
51
+ static readonly NONE: {
52
+ none: {};
53
+ };
54
+ static readonly FIRST: {
55
+ first: {};
56
+ };
57
+ static readonly SECOND: {
58
+ second: {};
59
+ };
60
+ static readonly THIRD: {
61
+ third: {};
62
+ };
63
+ static readonly FOURTH: {
64
+ fourth: {};
65
+ };
66
+ }
67
+ export declare class OrderAction {
68
+ static readonly PLACE: {
69
+ place: {};
70
+ };
71
+ static readonly CANCEL: {
72
+ cancel: {};
73
+ };
74
+ static readonly FILL: {
75
+ fill: {};
76
+ };
77
+ }
78
+ export declare class OrderTriggerCondition {
79
+ static readonly ABOVE: {
80
+ above: {};
81
+ };
82
+ static readonly BELOW: {
83
+ below: {};
84
+ };
85
+ }
86
+ export declare function isVariant(object: unknown, type: string): boolean;
28
87
  export declare enum TradeSide {
29
88
  None = 0,
30
89
  Buy = 1,
@@ -55,6 +114,11 @@ export declare type LiquidationHistoryAccount = {
55
114
  head: BN;
56
115
  liquidationRecords: LiquidationRecord[];
57
116
  };
117
+ export declare type OrderHistoryAccount = {
118
+ head: BN;
119
+ lastOrderId: BN;
120
+ orderRecords: OrderRecord[];
121
+ };
58
122
  export declare type DepositRecord = {
59
123
  ts: BN;
60
124
  recordId: BN;
@@ -148,6 +212,20 @@ export declare type LiquidationRecord = {
148
212
  unrealizedPnl: BN;
149
213
  marginRatio: BN;
150
214
  };
215
+ export declare type OrderRecord = {
216
+ ts: BN;
217
+ recordId: BN;
218
+ order: Order;
219
+ user: PublicKey;
220
+ authority: PublicKey;
221
+ action: OrderAction;
222
+ filler: PublicKey;
223
+ baseAssetAmountFilled: BN;
224
+ quoteAssetAmountFilled: BN;
225
+ fee: BN;
226
+ fillerReward: BN;
227
+ tradeRecordId: BN;
228
+ };
151
229
  export declare type StateAccount = {
152
230
  admin: PublicKey;
153
231
  fundingPaused: boolean;
@@ -185,8 +263,14 @@ export declare type StateAccount = {
185
263
  discountMint: PublicKey;
186
264
  oracleGuardRails: OracleGuardRails;
187
265
  maxDeposit: BN;
266
+ orderState: PublicKey;
188
267
  extendedCurveHistory: PublicKey;
189
268
  };
269
+ export declare type OrderStateAccount = {
270
+ orderHistory: PublicKey;
271
+ orderFillerRewardStructure: OrderFillerRewardStructure;
272
+ minOrderQuoteAssetAmount: BN;
273
+ };
190
274
  export declare type MarketsAccount = {
191
275
  accountIndex: BN;
192
276
  markets: Market[];
@@ -221,7 +305,8 @@ export declare type AMM = {
221
305
  totalFeeMinusDistributions: BN;
222
306
  totalFeeWithdrawn: BN;
223
307
  totalFee: BN;
224
- minimumTradeSize: BN;
308
+ minimumQuoteAssetTradeSize: BN;
309
+ minimumBaseAssetTradeSize: BN;
225
310
  lastOraclePrice: BN;
226
311
  };
227
312
  export declare type UserPosition = {
@@ -229,6 +314,7 @@ export declare type UserPosition = {
229
314
  lastCumulativeFundingRate: BN;
230
315
  marketIndex: BN;
231
316
  quoteAssetAmount: BN;
317
+ openOrders: BN;
232
318
  };
233
319
  export declare type UserPositionsAccount = {
234
320
  positions: UserPosition[];
@@ -240,6 +326,59 @@ export declare type UserAccount = {
240
326
  cumulativeDeposits: BN;
241
327
  positions: PublicKey;
242
328
  totalFeePaid: BN;
329
+ totalTokenDiscount: BN;
330
+ totalReferralReward: BN;
331
+ totalRefereeDiscount: BN;
332
+ };
333
+ export declare type UserOrdersAccount = {
334
+ orders: Order[];
335
+ user: PublicKey;
336
+ };
337
+ export declare type Order = {
338
+ status: OrderStatus;
339
+ orderType: OrderType;
340
+ ts: BN;
341
+ orderId: BN;
342
+ userOrderId: number;
343
+ marketIndex: BN;
344
+ price: BN;
345
+ userBaseAssetAmount: BN;
346
+ baseAssetAmount: BN;
347
+ baseAssetAmountFilled: BN;
348
+ quoteAssetAmount: BN;
349
+ quoteAssetAmountFilled: BN;
350
+ fee: BN;
351
+ direction: PositionDirection;
352
+ reduceOnly: boolean;
353
+ triggerPrice: BN;
354
+ triggerCondition: OrderTriggerCondition;
355
+ discountTier: OrderDiscountTier;
356
+ referrer: PublicKey;
357
+ postOnly: boolean;
358
+ immediateOrCancel: boolean;
359
+ oraclePriceOffset: BN;
360
+ };
361
+ export declare type OrderParams = {
362
+ orderType: OrderType;
363
+ userOrderId: number;
364
+ direction: PositionDirection;
365
+ quoteAssetAmount: BN;
366
+ baseAssetAmount: BN;
367
+ price: BN;
368
+ marketIndex: BN;
369
+ reduceOnly: boolean;
370
+ postOnly: boolean;
371
+ immediateOrCancel: boolean;
372
+ triggerPrice: BN;
373
+ triggerCondition: OrderTriggerCondition;
374
+ positionLimit: BN;
375
+ oraclePriceOffset: BN;
376
+ padding0: boolean;
377
+ padding1: BN;
378
+ optionalAccounts: {
379
+ discountToken: boolean;
380
+ referrer: boolean;
381
+ };
243
382
  };
244
383
  export interface IWallet {
245
384
  signTransaction(tx: Transaction): Promise<Transaction>;
@@ -290,4 +429,8 @@ export declare type OracleGuardRails = {
290
429
  };
291
430
  useForLiquidations: boolean;
292
431
  };
293
- //# sourceMappingURL=types.d.ts.map
432
+ export declare type OrderFillerRewardStructure = {
433
+ rewardNumerator: BN;
434
+ rewardDenominator: BN;
435
+ timeBasedRewardLowerBound: BN;
436
+ };
package/lib/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TradeSide = exports.OracleSource = exports.PositionDirection = exports.SwapDirection = void 0;
3
+ exports.TradeSide = exports.isVariant = exports.OrderTriggerCondition = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.PositionDirection = exports.SwapDirection = void 0;
4
4
  // # Utility Types / Enums / Constants
5
5
  class SwapDirection {
6
6
  }
@@ -17,6 +17,41 @@ class OracleSource {
17
17
  exports.OracleSource = OracleSource;
18
18
  OracleSource.PYTH = { pyth: {} };
19
19
  OracleSource.SWITCHBOARD = { switchboard: {} };
20
+ class OrderType {
21
+ }
22
+ exports.OrderType = OrderType;
23
+ OrderType.LIMIT = { limit: {} };
24
+ OrderType.TRIGGER_MARKET = { triggerMarket: {} };
25
+ OrderType.TRIGGER_LIMIT = { triggerLimit: {} };
26
+ OrderType.MARKET = { market: {} };
27
+ class OrderStatus {
28
+ }
29
+ exports.OrderStatus = OrderStatus;
30
+ OrderStatus.INIT = { init: {} };
31
+ OrderStatus.OPEN = { open: {} };
32
+ class OrderDiscountTier {
33
+ }
34
+ exports.OrderDiscountTier = OrderDiscountTier;
35
+ OrderDiscountTier.NONE = { none: {} };
36
+ OrderDiscountTier.FIRST = { first: {} };
37
+ OrderDiscountTier.SECOND = { second: {} };
38
+ OrderDiscountTier.THIRD = { third: {} };
39
+ OrderDiscountTier.FOURTH = { fourth: {} };
40
+ class OrderAction {
41
+ }
42
+ exports.OrderAction = OrderAction;
43
+ OrderAction.PLACE = { place: {} };
44
+ OrderAction.CANCEL = { cancel: {} };
45
+ OrderAction.FILL = { fill: {} };
46
+ class OrderTriggerCondition {
47
+ }
48
+ exports.OrderTriggerCondition = OrderTriggerCondition;
49
+ OrderTriggerCondition.ABOVE = { above: {} };
50
+ OrderTriggerCondition.BELOW = { below: {} };
51
+ function isVariant(object, type) {
52
+ return object.hasOwnProperty(type);
53
+ }
54
+ exports.isVariant = isVariant;
20
55
  var TradeSide;
21
56
  (function (TradeSide) {
22
57
  TradeSide[TradeSide["None"] = 0] = "None";
@@ -1,3 +1,2 @@
1
1
  import { Connection, Finality, PublicKey } from '@solana/web3.js';
2
2
  export declare function findComputeUnitConsumption(programId: PublicKey, connection: Connection, txSignature: string, commitment?: Finality): Promise<number[]>;
3
- //# sourceMappingURL=computeUnits.d.ts.map
package/lib/util/tps.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  import { Connection, PublicKey } from '@solana/web3.js';
2
2
  export declare function estimateTps(programId: PublicKey, connection: Connection, failed: boolean): Promise<number>;
3
- //# sourceMappingURL=tps.d.ts.map
package/lib/wallet.d.ts CHANGED
@@ -7,4 +7,3 @@ export declare class Wallet implements IWallet {
7
7
  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
8
8
  get publicKey(): PublicKey;
9
9
  }
10
- //# sourceMappingURL=wallet.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.20",
3
+ "version": "0.1.21-master.5",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -100,19 +100,27 @@ export class BulkAccountLoader {
100
100
  this.loadPromiseResolver = resolver;
101
101
  });
102
102
 
103
- const chunks = this.chunks(
104
- Array.from(this.accountsToLoad.values()),
105
- GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE
106
- );
107
-
108
- await Promise.all(
109
- chunks.map((chunk) => {
110
- return this.loadChunk(chunk);
111
- })
112
- );
113
-
114
- this.loadPromiseResolver();
115
- this.loadPromise = undefined;
103
+ try {
104
+ const chunks = this.chunks(
105
+ Array.from(this.accountsToLoad.values()),
106
+ GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE
107
+ );
108
+
109
+ await Promise.all(
110
+ chunks.map((chunk) => {
111
+ return this.loadChunk(chunk);
112
+ })
113
+ );
114
+ } catch (e) {
115
+ console.error(`Error in bulkAccountLoader.load()`);
116
+ console.error(e);
117
+ for (const [_, callback] of this.errorCallbacks) {
118
+ callback(e);
119
+ }
120
+ } finally {
121
+ this.loadPromiseResolver();
122
+ this.loadPromise = undefined;
123
+ }
116
124
  }
117
125
 
118
126
  async loadChunk(accountsToLoad: AccountToLoad[]): Promise<void> {
@@ -15,6 +15,8 @@ import {
15
15
  FundingRateHistoryAccount,
16
16
  LiquidationHistoryAccount,
17
17
  MarketsAccount,
18
+ OrderHistoryAccount,
19
+ OrderStateAccount,
18
20
  StateAccount,
19
21
  TradeHistoryAccount,
20
22
  } from '../types';
@@ -36,12 +38,14 @@ export class PollingClearingHouseAccountSubscriber
36
38
 
37
39
  state?: StateAccount;
38
40
  markets?: MarketsAccount;
41
+ orderState?: OrderStateAccount;
39
42
  tradeHistory?: TradeHistoryAccount;
40
43
  depositHistory?: DepositHistoryAccount;
41
44
  fundingPaymentHistory?: FundingPaymentHistoryAccount;
42
45
  fundingRateHistory?: FundingRateHistoryAccount;
43
46
  liquidationHistory?: LiquidationHistoryAccount;
44
47
  extendedCurveHistory: ExtendedCurveHistoryAccount;
48
+ orderHistory?: OrderHistoryAccount;
45
49
 
46
50
  optionalExtraSubscriptions: ClearingHouseAccountTypes[] = [];
47
51
 
@@ -114,6 +118,12 @@ export class PollingClearingHouseAccountSubscriber
114
118
  eventType: 'marketsAccountUpdate',
115
119
  });
116
120
 
121
+ this.accountsToPoll.set(state.orderState.toString(), {
122
+ key: 'orderState',
123
+ publicKey: state.orderState,
124
+ eventType: 'orderStateAccountUpdate',
125
+ });
126
+
117
127
  if (this.optionalExtraSubscriptions?.includes('tradeHistoryAccount')) {
118
128
  this.accountsToPoll.set(state.tradeHistory.toString(), {
119
129
  key: 'tradeHistory',
@@ -167,6 +177,18 @@ export class PollingClearingHouseAccountSubscriber
167
177
  eventType: 'liquidationHistoryAccountUpdate',
168
178
  });
169
179
  }
180
+
181
+ if (this.optionalExtraSubscriptions?.includes('orderHistoryAccount')) {
182
+ const orderState = (await this.program.account.orderState.fetch(
183
+ state.orderState
184
+ )) as OrderStateAccount;
185
+
186
+ this.accountsToPoll.set(orderState.orderHistory.toString(), {
187
+ key: 'orderHistory',
188
+ publicKey: orderState.orderHistory,
189
+ eventType: 'orderHistoryAccountUpdate',
190
+ });
191
+ }
170
192
  }
171
193
 
172
194
  async addToAccountLoader(): Promise<void> {
@@ -255,6 +277,11 @@ export class PollingClearingHouseAccountSubscriber
255
277
  return this.markets;
256
278
  }
257
279
 
280
+ public getOrderStateAccount(): OrderStateAccount {
281
+ this.assertIsSubscribed();
282
+ return this.orderState;
283
+ }
284
+
258
285
  public getTradeHistoryAccount(): TradeHistoryAccount {
259
286
  this.assertIsSubscribed();
260
287
  this.assertOptionalIsSubscribed('tradeHistoryAccount');
@@ -290,4 +317,10 @@ export class PollingClearingHouseAccountSubscriber
290
317
  this.assertOptionalIsSubscribed('liquidationHistoryAccount');
291
318
  return this.liquidationHistory;
292
319
  }
320
+
321
+ public getOrderHistoryAccount(): OrderHistoryAccount {
322
+ this.assertIsSubscribed();
323
+ this.assertOptionalIsSubscribed('orderHistoryAccount');
324
+ return this.orderHistory;
325
+ }
293
326
  }
@@ -8,8 +8,11 @@ import { Program } from '@project-serum/anchor';
8
8
  import StrictEventEmitter from 'strict-event-emitter-types';
9
9
  import { EventEmitter } from 'events';
10
10
  import { PublicKey } from '@solana/web3.js';
11
- import { getUserAccountPublicKey } from '../addresses';
12
- import { UserAccount, UserPositionsAccount } from '../types';
11
+ import {
12
+ getUserAccountPublicKey,
13
+ getUserOrdersAccountPublicKey,
14
+ } from '../addresses';
15
+ import { UserAccount, UserOrdersAccount, UserPositionsAccount } from '../types';
13
16
  import { BulkAccountLoader } from './bulkAccountLoader';
14
17
  import { capitalize } from './utils';
15
18
  import { ClearingHouseConfigType } from '../factory/clearingHouse';
@@ -26,6 +29,7 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
26
29
 
27
30
  user?: UserAccount;
28
31
  userPositions?: UserPositionsAccount;
32
+ userOrders?: UserOrdersAccount;
29
33
 
30
34
  type: ClearingHouseConfigType = 'polling';
31
35
 
@@ -80,6 +84,25 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
80
84
  eventType: 'userPositionsData',
81
85
  });
82
86
 
87
+ const userOrdersPublicKey = await getUserOrdersAccountPublicKey(
88
+ this.program.programId,
89
+ userPublicKey
90
+ );
91
+
92
+ const userOrdersExist =
93
+ (
94
+ await this.program.provider.connection.getParsedAccountInfo(
95
+ userOrdersPublicKey
96
+ )
97
+ ).value !== null;
98
+ if (userOrdersExist) {
99
+ this.accountsToPoll.set(userOrdersPublicKey.toString(), {
100
+ key: 'userOrders',
101
+ publicKey: userOrdersPublicKey,
102
+ eventType: 'userOrdersData',
103
+ });
104
+ }
105
+
83
106
  for (const [_, accountToPoll] of this.accountsToPoll) {
84
107
  accountToPoll.callbackId = this.accountLoader.addAccount(
85
108
  accountToPoll.publicKey,
@@ -163,4 +186,9 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
163
186
  this.assertIsSubscribed();
164
187
  return this.userPositions;
165
188
  }
189
+
190
+ public getUserOrdersAccount(): UserOrdersAccount {
191
+ this.assertIsSubscribed();
192
+ return this.userOrders;
193
+ }
166
194
  }
@@ -5,9 +5,12 @@ import {
5
5
  FundingRateHistoryAccount,
6
6
  LiquidationHistoryAccount,
7
7
  MarketsAccount,
8
+ OrderHistoryAccount,
9
+ OrderStateAccount,
8
10
  StateAccount,
9
11
  TradeHistoryAccount,
10
12
  UserAccount,
13
+ UserOrdersAccount,
11
14
  UserPositionsAccount,
12
15
  } from '../types';
13
16
  import StrictEventEmitter from 'strict-event-emitter-types';
@@ -38,6 +41,8 @@ export interface ClearingHouseAccountEvents {
38
41
  liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
39
42
  depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
40
43
  curveHistoryAccountUpdate: (payload: ExtendedCurveHistoryAccount) => void;
44
+ orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
45
+ orderStateAccountUpdate: (payload: OrderStateAccount) => void;
41
46
  update: void;
42
47
  error: (e: Error) => void;
43
48
  }
@@ -48,7 +53,8 @@ export type ClearingHouseAccountTypes =
48
53
  | 'fundingPaymentHistoryAccount'
49
54
  | 'fundingRateHistoryAccount'
50
55
  | 'curveHistoryAccount'
51
- | 'liquidationHistoryAccount';
56
+ | 'liquidationHistoryAccount'
57
+ | 'orderHistoryAccount';
52
58
 
53
59
  export interface ClearingHouseAccountSubscriber {
54
60
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
@@ -70,6 +76,8 @@ export interface ClearingHouseAccountSubscriber {
70
76
  getFundingRateHistoryAccount(): FundingRateHistoryAccount;
71
77
  getCurveHistoryAccount(): ExtendedCurveHistoryAccount;
72
78
  getLiquidationHistoryAccount(): LiquidationHistoryAccount;
79
+ getOrderStateAccount(): OrderStateAccount;
80
+ getOrderHistoryAccount(): OrderHistoryAccount;
73
81
 
74
82
  type: ClearingHouseConfigType;
75
83
  }
@@ -77,6 +85,7 @@ export interface ClearingHouseAccountSubscriber {
77
85
  export interface UserAccountEvents {
78
86
  userAccountData: (payload: UserAccount) => void;
79
87
  userPositionsData: (payload: UserPositionsAccount) => void;
88
+ userOrdersData: (payload: UserOrdersAccount) => void;
80
89
  update: void;
81
90
  error: (e: Error) => void;
82
91
  }
@@ -91,7 +100,7 @@ export interface UserAccountSubscriber {
91
100
 
92
101
  getUserAccount(): UserAccount;
93
102
  getUserPositionsAccount(): UserPositionsAccount;
94
-
103
+ getUserOrdersAccount(): UserOrdersAccount;
95
104
  type: ClearingHouseUserConfigType;
96
105
  }
97
106
 
@@ -11,6 +11,8 @@ import {
11
11
  FundingRateHistoryAccount,
12
12
  LiquidationHistoryAccount,
13
13
  MarketsAccount,
14
+ OrderHistoryAccount,
15
+ OrderStateAccount,
14
16
  StateAccount,
15
17
  TradeHistoryAccount,
16
18
  } from '../types';
@@ -35,6 +37,8 @@ export class WebSocketClearingHouseAccountSubscriber
35
37
  fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
36
38
  curveHistoryAccountSubscriber?: AccountSubscriber<ExtendedCurveHistoryAccount>;
37
39
  liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
40
+ orderStateAccountSubscriber?: AccountSubscriber<OrderStateAccount>;
41
+ orderHistoryAccountSubscriber?: AccountSubscriber<OrderHistoryAccount>;
38
42
 
39
43
  optionalExtraSubscriptions: ClearingHouseAccountTypes[] = [];
40
44
 
@@ -95,6 +99,21 @@ export class WebSocketClearingHouseAccountSubscriber
95
99
  this.eventEmitter.emit('update');
96
100
  });
97
101
 
102
+ this.orderStateAccountSubscriber = new WebSocketAccountSubscriber(
103
+ 'orderState',
104
+ this.program,
105
+ state.orderState
106
+ );
107
+
108
+ await this.orderStateAccountSubscriber.subscribe(
109
+ (data: OrderStateAccount) => {
110
+ this.eventEmitter.emit('orderStateAccountUpdate', data);
111
+ this.eventEmitter.emit('update');
112
+ }
113
+ );
114
+
115
+ const orderState = this.orderStateAccountSubscriber.data;
116
+
98
117
  // create subscribers for other state accounts
99
118
 
100
119
  this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
@@ -134,6 +153,12 @@ export class WebSocketClearingHouseAccountSubscriber
134
153
  state.extendedCurveHistory
135
154
  );
136
155
 
156
+ this.orderHistoryAccountSubscriber = new WebSocketAccountSubscriber(
157
+ 'orderHistory',
158
+ this.program,
159
+ orderState.orderHistory
160
+ );
161
+
137
162
  const extraSusbcribersToUse: {
138
163
  subscriber: AccountSubscriber<any>;
139
164
  eventType: keyof ClearingHouseAccountEvents;
@@ -175,6 +200,12 @@ export class WebSocketClearingHouseAccountSubscriber
175
200
  eventType: 'curveHistoryAccountUpdate',
176
201
  });
177
202
 
203
+ if (optionalSubscriptions?.includes('orderHistoryAccount'))
204
+ extraSusbcribersToUse.push({
205
+ subscriber: this.orderHistoryAccountSubscriber,
206
+ eventType: 'orderHistoryAccountUpdate',
207
+ });
208
+
178
209
  this.optionalExtraSubscriptions = optionalSubscriptions ?? [];
179
210
 
180
211
  // await all subcriptions in parallel to boost performance
@@ -222,6 +253,7 @@ export class WebSocketClearingHouseAccountSubscriber
222
253
 
223
254
  await this.stateAccountSubscriber.unsubscribe();
224
255
  await this.marketsAccountSubscriber.unsubscribe();
256
+ await this.orderStateAccountSubscriber.unsubscribe();
225
257
 
226
258
  if (this.optionalExtraSubscriptions.includes('tradeHistoryAccount')) {
227
259
  await this.tradeHistoryAccountSubscriber.unsubscribe();
@@ -249,6 +281,10 @@ export class WebSocketClearingHouseAccountSubscriber
249
281
  await this.liquidationHistoryAccountSubscriber.unsubscribe();
250
282
  }
251
283
 
284
+ if (this.optionalExtraSubscriptions.includes('orderHistoryAccount')) {
285
+ await this.orderHistoryAccountSubscriber.unsubscribe();
286
+ }
287
+
252
288
  this.isSubscribed = false;
253
289
  }
254
290
 
@@ -321,4 +357,15 @@ export class WebSocketClearingHouseAccountSubscriber
321
357
  this.assertOptionalIsSubscribed('liquidationHistoryAccount');
322
358
  return this.liquidationHistoryAccountSubscriber.data;
323
359
  }
360
+
361
+ public getOrderHistoryAccount(): OrderHistoryAccount {
362
+ this.assertIsSubscribed();
363
+ this.assertOptionalIsSubscribed('orderHistoryAccount');
364
+ return this.orderHistoryAccountSubscriber.data;
365
+ }
366
+
367
+ public getOrderStateAccount(): OrderStateAccount {
368
+ this.assertIsSubscribed();
369
+ return this.orderStateAccountSubscriber.data;
370
+ }
324
371
  }
@@ -8,9 +8,12 @@ import { Program } from '@project-serum/anchor';
8
8
  import StrictEventEmitter from 'strict-event-emitter-types';
9
9
  import { EventEmitter } from 'events';
10
10
  import { PublicKey } from '@solana/web3.js';
11
- import { getUserAccountPublicKey } from '../addresses';
11
+ import {
12
+ getUserAccountPublicKey,
13
+ getUserOrdersAccountPublicKey,
14
+ } from '../addresses';
12
15
  import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
13
- import { UserAccount, UserPositionsAccount } from '../types';
16
+ import { UserAccount, UserOrdersAccount, UserPositionsAccount } from '../types';
14
17
  import { ClearingHouseConfigType } from '../factory/clearingHouse';
15
18
 
16
19
  export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
@@ -21,6 +24,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
21
24
 
22
25
  userDataAccountSubscriber: AccountSubscriber<UserAccount>;
23
26
  userPositionsAccountSubscriber: AccountSubscriber<UserPositionsAccount>;
27
+ userOrdersAccountSubscriber: AccountSubscriber<UserOrdersAccount>;
24
28
 
25
29
  type: ClearingHouseConfigType = 'websocket';
26
30
 
@@ -64,6 +68,23 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
64
68
  }
65
69
  );
66
70
 
71
+ const userOrdersPublicKey = await getUserOrdersAccountPublicKey(
72
+ this.program.programId,
73
+ userPublicKey
74
+ );
75
+
76
+ this.userOrdersAccountSubscriber = new WebSocketAccountSubscriber(
77
+ 'userOrders',
78
+ this.program,
79
+ userOrdersPublicKey
80
+ );
81
+ await this.userOrdersAccountSubscriber.subscribe(
82
+ (data: UserOrdersAccount) => {
83
+ this.eventEmitter.emit('userOrdersData', data);
84
+ this.eventEmitter.emit('update');
85
+ }
86
+ );
87
+
67
88
  this.eventEmitter.emit('update');
68
89
  this.isSubscribed = true;
69
90
  return true;
@@ -73,6 +94,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
73
94
  await Promise.all([
74
95
  this.userDataAccountSubscriber.fetch(),
75
96
  this.userPositionsAccountSubscriber.fetch(),
97
+ this.userOrdersAccountSubscriber.fetch(),
76
98
  ]);
77
99
  }
78
100
 
@@ -84,6 +106,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
84
106
  await Promise.all([
85
107
  this.userDataAccountSubscriber.unsubscribe(),
86
108
  this.userPositionsAccountSubscriber.unsubscribe(),
109
+ await this.userOrdersAccountSubscriber.unsubscribe(),
87
110
  ]);
88
111
 
89
112
  this.isSubscribed = false;
@@ -106,4 +129,8 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
106
129
  this.assertIsSubscribed();
107
130
  return this.userPositionsAccountSubscriber.data;
108
131
  }
132
+
133
+ public getUserOrdersAccount(): UserOrdersAccount {
134
+ return this.userOrdersAccountSubscriber.data;
135
+ }
109
136
  }