@drift-labs/sdk 0.1.17 → 0.1.18-orders.0

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 (103) hide show
  1. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts +5 -2
  2. package/lib/accounts/defaultClearingHouseAccountSubscriber.js +25 -0
  3. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +28 -0
  4. package/lib/accounts/defaultHistoryAccountSubscriber.js +110 -0
  5. package/lib/accounts/defaultUserAccountSubscriber.d.ts +3 -2
  6. package/lib/accounts/defaultUserAccountSubscriber.js +11 -0
  7. package/lib/accounts/types.d.ts +29 -3
  8. package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
  9. package/lib/addresses.d.ts +4 -1
  10. package/lib/addresses.js +28 -1
  11. package/lib/admin.d.ts +10 -4
  12. package/lib/admin.js +48 -3
  13. package/lib/assert/assert.d.ts +0 -1
  14. package/lib/clearingHouse.d.ts +21 -2
  15. package/lib/clearingHouse.js +238 -7
  16. package/lib/clearingHouseUser.d.ts +10 -17
  17. package/lib/clearingHouseUser.js +98 -84
  18. package/lib/config.d.ts +0 -1
  19. package/lib/config.js +1 -1
  20. package/lib/constants/markets.d.ts +0 -1
  21. package/lib/constants/numericConstants.d.ts +0 -1
  22. package/lib/examples/makeTradeExample.d.ts +0 -1
  23. package/lib/idl/clearing_house.json +956 -59
  24. package/lib/index.d.ts +3 -1
  25. package/lib/index.js +3 -0
  26. package/lib/math/amm.d.ts +1 -1
  27. package/lib/math/amm.js +31 -8
  28. package/lib/math/conversion.d.ts +0 -1
  29. package/lib/math/funding.d.ts +0 -1
  30. package/lib/math/insuranceFund.d.ts +0 -1
  31. package/lib/math/market.d.ts +2 -2
  32. package/lib/math/market.js +11 -1
  33. package/lib/math/orders.d.ts +3 -0
  34. package/lib/math/orders.js +30 -0
  35. package/lib/math/position.d.ts +4 -2
  36. package/lib/math/position.js +18 -4
  37. package/lib/math/trade.d.ts +0 -1
  38. package/lib/math/utils.d.ts +0 -1
  39. package/lib/mockUSDCFaucet.d.ts +0 -1
  40. package/lib/orderParams.d.ts +7 -0
  41. package/lib/orderParams.js +88 -0
  42. package/lib/orders.d.ts +5 -0
  43. package/lib/orders.js +136 -0
  44. package/lib/pythClient.d.ts +0 -1
  45. package/lib/tx/defaultTxSender.d.ts +0 -1
  46. package/lib/tx/types.d.ts +0 -1
  47. package/lib/tx/utils.d.ts +0 -1
  48. package/lib/types.d.ts +137 -2
  49. package/lib/types.js +36 -1
  50. package/lib/util/computeUnits.d.ts +0 -1
  51. package/lib/util/tps.d.ts +0 -1
  52. package/lib/wallet.d.ts +0 -1
  53. package/package.json +1 -1
  54. package/src/accounts/defaultClearingHouseAccountSubscriber.ts +47 -0
  55. package/src/accounts/defaultHistoryAccountSubscriber.ts +176 -0
  56. package/src/accounts/defaultUserAccountSubscriber.ts +29 -2
  57. package/src/accounts/types.ts +38 -1
  58. package/src/addresses.ts +35 -0
  59. package/src/admin.ts +84 -6
  60. package/src/clearingHouse.ts +338 -5
  61. package/src/clearingHouseUser.ts +154 -102
  62. package/src/config.ts +1 -1
  63. package/src/idl/clearing_house.json +956 -59
  64. package/src/index.ts +3 -0
  65. package/src/math/amm.ts +47 -14
  66. package/src/math/market.ts +28 -2
  67. package/src/math/orders.ts +39 -0
  68. package/src/math/position.ts +23 -3
  69. package/src/orderParams.ts +128 -0
  70. package/src/orders.ts +230 -0
  71. package/src/types.ts +121 -1
  72. package/tsconfig.json +0 -1
  73. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
  74. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
  75. package/lib/accounts/types.d.ts.map +0 -1
  76. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
  77. package/lib/addresses.d.ts.map +0 -1
  78. package/lib/admin.d.ts.map +0 -1
  79. package/lib/assert/assert.d.ts.map +0 -1
  80. package/lib/clearingHouse.d.ts.map +0 -1
  81. package/lib/clearingHouseUser.d.ts.map +0 -1
  82. package/lib/config.d.ts.map +0 -1
  83. package/lib/constants/markets.d.ts.map +0 -1
  84. package/lib/constants/numericConstants.d.ts.map +0 -1
  85. package/lib/examples/makeTradeExample.d.ts.map +0 -1
  86. package/lib/index.d.ts.map +0 -1
  87. package/lib/math/amm.d.ts.map +0 -1
  88. package/lib/math/conversion.d.ts.map +0 -1
  89. package/lib/math/funding.d.ts.map +0 -1
  90. package/lib/math/insuranceFund.d.ts.map +0 -1
  91. package/lib/math/market.d.ts.map +0 -1
  92. package/lib/math/position.d.ts.map +0 -1
  93. package/lib/math/trade.d.ts.map +0 -1
  94. package/lib/math/utils.d.ts.map +0 -1
  95. package/lib/mockUSDCFaucet.d.ts.map +0 -1
  96. package/lib/pythClient.d.ts.map +0 -1
  97. package/lib/tx/defaultTxSender.d.ts.map +0 -1
  98. package/lib/tx/types.d.ts.map +0 -1
  99. package/lib/tx/utils.d.ts.map +0 -1
  100. package/lib/types.d.ts.map +0 -1
  101. package/lib/util/computeUnits.d.ts.map +0 -1
  102. package/lib/util/tps.d.ts.map +0 -1
  103. package/lib/wallet.d.ts.map +0 -1
package/lib/types.d.ts CHANGED
@@ -24,6 +24,65 @@ export declare class OracleSource {
24
24
  switchboard: {};
25
25
  };
26
26
  }
27
+ export declare class OrderType {
28
+ static readonly LIMIT: {
29
+ limit: {};
30
+ };
31
+ static readonly STOP: {
32
+ stop: {};
33
+ };
34
+ static readonly STOP_LIMIT: {
35
+ stopLimit: {};
36
+ };
37
+ static readonly MARKET: {
38
+ market: {};
39
+ };
40
+ }
41
+ export declare class OrderStatus {
42
+ static readonly INIT: {
43
+ init: {};
44
+ };
45
+ static readonly OPEN: {
46
+ open: {};
47
+ };
48
+ }
49
+ export declare class OrderDiscountTier {
50
+ static readonly NONE: {
51
+ none: {};
52
+ };
53
+ static readonly FIRST: {
54
+ first: {};
55
+ };
56
+ static readonly SECOND: {
57
+ second: {};
58
+ };
59
+ static readonly THIRD: {
60
+ third: {};
61
+ };
62
+ static readonly FOURTH: {
63
+ fourth: {};
64
+ };
65
+ }
66
+ export declare class OrderAction {
67
+ static readonly PLACE: {
68
+ place: {};
69
+ };
70
+ static readonly CANCEL: {
71
+ cancel: {};
72
+ };
73
+ static readonly FILL: {
74
+ fill: {};
75
+ };
76
+ }
77
+ export declare class OrderTriggerCondition {
78
+ static readonly ABOVE: {
79
+ above: {};
80
+ };
81
+ static readonly BELOW: {
82
+ below: {};
83
+ };
84
+ }
85
+ export declare function isVariant(object: unknown, type: string): boolean;
27
86
  export declare enum TradeSide {
28
87
  None = 0,
29
88
  Buy = 1,
@@ -54,6 +113,11 @@ export declare type LiquidationHistoryAccount = {
54
113
  head: BN;
55
114
  liquidationRecords: LiquidationRecord[];
56
115
  };
116
+ export declare type OrderHistoryAccount = {
117
+ head: BN;
118
+ lastOrderId: BN;
119
+ orderRecords: OrderRecord[];
120
+ };
57
121
  export declare type DepositRecord = {
58
122
  ts: BN;
59
123
  recordId: BN;
@@ -145,6 +209,20 @@ export declare type LiquidationRecord = {
145
209
  unrealizedPnl: BN;
146
210
  marginRatio: BN;
147
211
  };
212
+ export declare type OrderRecord = {
213
+ ts: BN;
214
+ recordId: BN;
215
+ order: Order;
216
+ user: PublicKey;
217
+ authority: PublicKey;
218
+ action: OrderAction;
219
+ filler: PublicKey;
220
+ baseAssetAmountFilled: BN;
221
+ quoteAssetAmountFilled: BN;
222
+ fee: BN;
223
+ fillerReward: BN;
224
+ tradeRecordId: BN;
225
+ };
148
226
  export declare type StateAccount = {
149
227
  admin: PublicKey;
150
228
  fundingPaused: boolean;
@@ -182,6 +260,12 @@ export declare type StateAccount = {
182
260
  discountMint: PublicKey;
183
261
  oracleGuardRails: OracleGuardRails;
184
262
  maxDeposit: BN;
263
+ orderState: PublicKey;
264
+ };
265
+ export declare type OrderStateAccount = {
266
+ orderHistory: PublicKey;
267
+ orderFillerRewardStructure: OrderFillerRewardStructure;
268
+ minOrderQuoteAssetAmount: BN;
185
269
  };
186
270
  export declare type MarketsAccount = {
187
271
  accountIndex: BN;
@@ -217,13 +301,15 @@ export declare type AMM = {
217
301
  totalFeeMinusDistributions: BN;
218
302
  totalFeeWithdrawn: BN;
219
303
  totalFee: BN;
220
- minimumTradeSize: BN;
304
+ minimumQuoteAssetTradeSize: BN;
305
+ minimumBaseAssetTradeSize: BN;
221
306
  };
222
307
  export declare type UserPosition = {
223
308
  baseAssetAmount: BN;
224
309
  lastCumulativeFundingRate: BN;
225
310
  marketIndex: BN;
226
311
  quoteAssetAmount: BN;
312
+ openOrders: BN;
227
313
  };
228
314
  export declare type UserPositionsAccount = {
229
315
  positions: UserPosition[];
@@ -235,6 +321,51 @@ export declare type UserAccount = {
235
321
  cumulativeDeposits: BN;
236
322
  positions: PublicKey;
237
323
  totalFeePaid: BN;
324
+ totalTokenDiscount: BN;
325
+ totalReferralReward: BN;
326
+ totalRefereeDiscount: BN;
327
+ };
328
+ export declare type UserOrdersAccount = {
329
+ orders: Order[];
330
+ user: PublicKey;
331
+ };
332
+ export declare type Order = {
333
+ status: OrderStatus;
334
+ orderType: OrderType;
335
+ ts: BN;
336
+ orderId: BN;
337
+ marketIndex: BN;
338
+ price: BN;
339
+ baseAssetAmount: BN;
340
+ baseAssetAmountFilled: BN;
341
+ quoteAssetAmount: BN;
342
+ quoteAssetAmountFilled: BN;
343
+ fee: BN;
344
+ direction: PositionDirection;
345
+ reduceOnly: boolean;
346
+ triggerPrice: BN;
347
+ triggerCondition: OrderTriggerCondition;
348
+ discountTier: OrderDiscountTier;
349
+ referrer: PublicKey;
350
+ postOnly: boolean;
351
+ immediateOrCancel: boolean;
352
+ };
353
+ export declare type OrderParams = {
354
+ orderType: OrderType;
355
+ direction: PositionDirection;
356
+ quoteAssetAmount: BN;
357
+ baseAssetAmount: BN;
358
+ price: BN;
359
+ marketIndex: BN;
360
+ reduceOnly: boolean;
361
+ postOnly: boolean;
362
+ immediateOrCancel: boolean;
363
+ triggerPrice: BN;
364
+ triggerCondition: OrderTriggerCondition;
365
+ optionalAccounts: {
366
+ discountToken: boolean;
367
+ referrer: boolean;
368
+ };
238
369
  };
239
370
  export interface IWallet {
240
371
  signTransaction(tx: Transaction): Promise<Transaction>;
@@ -285,4 +416,8 @@ export declare type OracleGuardRails = {
285
416
  };
286
417
  useForLiquidations: boolean;
287
418
  };
288
- //# sourceMappingURL=types.d.ts.map
419
+ export declare type OrderFillerRewardStructure = {
420
+ rewardNumerator: BN;
421
+ rewardDenominator: BN;
422
+ timeBasedRewardLowerBound: BN;
423
+ };
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.STOP = { stop: {} };
25
+ OrderType.STOP_LIMIT = { stopLimit: {} };
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.17",
3
+ "version": "0.1.18-orders.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -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';
@@ -34,6 +36,8 @@ export class DefaultClearingHouseAccountSubscriber
34
36
  fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
35
37
  curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
36
38
  liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
39
+ orderStateAccountSubscriber?: AccountSubscriber<OrderStateAccount>;
40
+ orderHistoryAccountSubscriber?: AccountSubscriber<OrderHistoryAccount>;
37
41
 
38
42
  optionalExtraSubscriptions: ClearingHouseAccountTypes[] = [];
39
43
 
@@ -92,6 +96,21 @@ export class DefaultClearingHouseAccountSubscriber
92
96
  this.eventEmitter.emit('update');
93
97
  });
94
98
 
99
+ this.orderStateAccountSubscriber = new WebSocketAccountSubscriber(
100
+ 'orderState',
101
+ this.program,
102
+ state.orderState
103
+ );
104
+
105
+ await this.orderStateAccountSubscriber.subscribe(
106
+ (data: OrderStateAccount) => {
107
+ this.eventEmitter.emit('orderStateAccountUpdate', data);
108
+ this.eventEmitter.emit('update');
109
+ }
110
+ );
111
+
112
+ const orderState = this.orderStateAccountSubscriber.data;
113
+
95
114
  // create subscribers for other state accounts
96
115
 
97
116
  this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
@@ -131,6 +150,12 @@ export class DefaultClearingHouseAccountSubscriber
131
150
  state.curveHistory
132
151
  );
133
152
 
153
+ this.orderHistoryAccountSubscriber = new WebSocketAccountSubscriber(
154
+ 'orderHistory',
155
+ this.program,
156
+ orderState.orderHistory
157
+ );
158
+
134
159
  const extraSusbcribersToUse: {
135
160
  subscriber: AccountSubscriber<any>;
136
161
  eventType: keyof ClearingHouseAccountEvents;
@@ -172,6 +197,12 @@ export class DefaultClearingHouseAccountSubscriber
172
197
  eventType: 'curveHistoryAccountUpdate',
173
198
  });
174
199
 
200
+ if (optionalSubscriptions?.includes('orderHistoryAccount'))
201
+ extraSusbcribersToUse.push({
202
+ subscriber: this.orderHistoryAccountSubscriber,
203
+ eventType: 'orderHistoryAccountUpdate',
204
+ });
205
+
175
206
  this.optionalExtraSubscriptions = optionalSubscriptions ?? [];
176
207
 
177
208
  // await all subcriptions in parallel to boost performance
@@ -219,6 +250,7 @@ export class DefaultClearingHouseAccountSubscriber
219
250
 
220
251
  await this.stateAccountSubscriber.unsubscribe();
221
252
  await this.marketsAccountSubscriber.unsubscribe();
253
+ await this.orderStateAccountSubscriber.unsubscribe();
222
254
 
223
255
  if (this.optionalExtraSubscriptions.includes('tradeHistoryAccount')) {
224
256
  await this.tradeHistoryAccountSubscriber.unsubscribe();
@@ -246,6 +278,10 @@ export class DefaultClearingHouseAccountSubscriber
246
278
  await this.liquidationHistoryAccountSubscriber.unsubscribe();
247
279
  }
248
280
 
281
+ if (this.optionalExtraSubscriptions.includes('orderHistoryAccount')) {
282
+ await this.orderHistoryAccountSubscriber.unsubscribe();
283
+ }
284
+
249
285
  this.isSubscribed = false;
250
286
  }
251
287
 
@@ -318,4 +354,15 @@ export class DefaultClearingHouseAccountSubscriber
318
354
  this.assertOptionalIsSubscribed('liquidationHistoryAccount');
319
355
  return this.liquidationHistoryAccountSubscriber.data;
320
356
  }
357
+
358
+ public getOrderHistoryAccount(): OrderHistoryAccount {
359
+ this.assertIsSubscribed();
360
+ this.assertOptionalIsSubscribed('orderHistoryAccount');
361
+ return this.orderHistoryAccountSubscriber.data;
362
+ }
363
+
364
+ public getOrderStateAccount(): OrderStateAccount {
365
+ this.assertIsSubscribed();
366
+ return this.orderStateAccountSubscriber.data;
367
+ }
321
368
  }
@@ -0,0 +1,176 @@
1
+ import { ClearingHouseAccountEvents, HistoryAccountSubscriber } from './types';
2
+ import { AccountSubscriber, NotSubscribedError } from './types';
3
+ import {
4
+ CurveHistoryAccount,
5
+ DepositHistoryAccount,
6
+ FundingPaymentHistoryAccount,
7
+ FundingRateHistoryAccount,
8
+ LiquidationHistoryAccount,
9
+ TradeHistoryAccount,
10
+ } from '../types';
11
+ import { Program } from '@project-serum/anchor';
12
+ import StrictEventEmitter from 'strict-event-emitter-types';
13
+ import { EventEmitter } from 'events';
14
+ import { getClearingHouseStateAccountPublicKey } from '../addresses';
15
+ import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
16
+
17
+ export class DefaultHistoryAccountSubscriber
18
+ implements HistoryAccountSubscriber
19
+ {
20
+ isSubscribed: boolean;
21
+ program: Program;
22
+ eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
23
+ tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
24
+ depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
25
+ fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
26
+ fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
27
+ curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
28
+ liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
29
+
30
+ public constructor(program: Program) {
31
+ this.isSubscribed = false;
32
+ this.program = program;
33
+ this.eventEmitter = new EventEmitter();
34
+ }
35
+
36
+ public async subscribe(): Promise<boolean> {
37
+ if (this.isSubscribed) {
38
+ return true;
39
+ }
40
+
41
+ const statePublicKey = await getClearingHouseStateAccountPublicKey(
42
+ this.program.programId
43
+ );
44
+ const state: any = await this.program.account.state.fetch(statePublicKey);
45
+
46
+ this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
47
+ 'tradeHistory',
48
+ this.program,
49
+ state.tradeHistory
50
+ );
51
+ await this.tradeHistoryAccountSubscriber.subscribe(
52
+ (data: TradeHistoryAccount) => {
53
+ this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
54
+ this.eventEmitter.emit('update');
55
+ }
56
+ );
57
+
58
+ this.depositHistoryAccountSubscriber = new WebSocketAccountSubscriber(
59
+ 'depositHistory',
60
+ this.program,
61
+ state.depositHistory
62
+ );
63
+ await this.depositHistoryAccountSubscriber.subscribe(
64
+ (data: DepositHistoryAccount) => {
65
+ this.eventEmitter.emit('depositHistoryAccountUpdate', data);
66
+ this.eventEmitter.emit('update');
67
+ }
68
+ );
69
+
70
+ this.fundingPaymentHistoryAccountSubscriber =
71
+ new WebSocketAccountSubscriber(
72
+ 'fundingPaymentHistory',
73
+ this.program,
74
+ state.fundingPaymentHistory
75
+ );
76
+ await this.fundingPaymentHistoryAccountSubscriber.subscribe(
77
+ (data: FundingPaymentHistoryAccount) => {
78
+ this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
79
+ this.eventEmitter.emit('update');
80
+ }
81
+ );
82
+
83
+ this.fundingRateHistoryAccountSubscriber = new WebSocketAccountSubscriber(
84
+ 'fundingRateHistory',
85
+ this.program,
86
+ state.fundingRateHistory
87
+ );
88
+ await this.fundingRateHistoryAccountSubscriber.subscribe(
89
+ (data: FundingRateHistoryAccount) => {
90
+ this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
91
+ this.eventEmitter.emit('update');
92
+ }
93
+ );
94
+
95
+ this.liquidationHistoryAccountSubscriber = new WebSocketAccountSubscriber(
96
+ 'liquidationHistory',
97
+ this.program,
98
+ state.liquidationHistory
99
+ );
100
+ await this.liquidationHistoryAccountSubscriber.subscribe(
101
+ (data: LiquidationHistoryAccount) => {
102
+ this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
103
+ this.eventEmitter.emit('update');
104
+ }
105
+ );
106
+
107
+ this.curveHistoryAccountSubscriber = new WebSocketAccountSubscriber(
108
+ 'curveHistory',
109
+ this.program,
110
+ state.curveHistory
111
+ );
112
+ await this.curveHistoryAccountSubscriber.subscribe(
113
+ (data: CurveHistoryAccount) => {
114
+ this.eventEmitter.emit('curveHistoryAccountUpdate', data);
115
+ this.eventEmitter.emit('update');
116
+ }
117
+ );
118
+
119
+ this.eventEmitter.emit('update');
120
+
121
+ this.isSubscribed = true;
122
+ return true;
123
+ }
124
+
125
+ public async unsubscribe(): Promise<void> {
126
+ if (!this.isSubscribed) {
127
+ return;
128
+ }
129
+
130
+ await this.tradeHistoryAccountSubscriber.unsubscribe();
131
+ await this.fundingRateHistoryAccountSubscriber.unsubscribe();
132
+ await this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
133
+ await this.depositHistoryAccountSubscriber.unsubscribe();
134
+ await this.curveHistoryAccountSubscriber.unsubscribe();
135
+ await this.liquidationHistoryAccountSubscriber.unsubscribe();
136
+ this.isSubscribed = false;
137
+ }
138
+
139
+ assertIsSubscribed(): void {
140
+ if (!this.isSubscribed) {
141
+ throw new NotSubscribedError(
142
+ 'You must call `subscribe` before using this function'
143
+ );
144
+ }
145
+ }
146
+
147
+ public getTradeHistoryAccount(): TradeHistoryAccount {
148
+ this.assertIsSubscribed();
149
+ return this.tradeHistoryAccountSubscriber.data;
150
+ }
151
+
152
+ public getDepositHistoryAccount(): DepositHistoryAccount {
153
+ this.assertIsSubscribed();
154
+ return this.depositHistoryAccountSubscriber.data;
155
+ }
156
+
157
+ public getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount {
158
+ this.assertIsSubscribed();
159
+ return this.fundingPaymentHistoryAccountSubscriber.data;
160
+ }
161
+
162
+ public getFundingRateHistoryAccount(): FundingRateHistoryAccount {
163
+ this.assertIsSubscribed();
164
+ return this.fundingRateHistoryAccountSubscriber.data;
165
+ }
166
+
167
+ public getCurveHistoryAccount(): CurveHistoryAccount {
168
+ this.assertIsSubscribed();
169
+ return this.curveHistoryAccountSubscriber.data;
170
+ }
171
+
172
+ public getLiquidationHistoryAccount(): LiquidationHistoryAccount {
173
+ this.assertIsSubscribed();
174
+ return this.liquidationHistoryAccountSubscriber.data;
175
+ }
176
+ }
@@ -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
 
15
18
  export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
16
19
  isSubscribed: boolean;
@@ -20,6 +23,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
20
23
 
21
24
  userDataAccountSubscriber: AccountSubscriber<UserAccount>;
22
25
  userPositionsAccountSubscriber: AccountSubscriber<UserPositionsAccount>;
26
+ userOrdersAccountSubscriber: AccountSubscriber<UserOrdersAccount>;
23
27
 
24
28
  public constructor(program: Program, authority: PublicKey) {
25
29
  this.isSubscribed = false;
@@ -61,6 +65,23 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
61
65
  }
62
66
  );
63
67
 
68
+ const userOrdersPublicKey = await getUserOrdersAccountPublicKey(
69
+ this.program.programId,
70
+ this.authority
71
+ );
72
+
73
+ this.userOrdersAccountSubscriber = new WebSocketAccountSubscriber(
74
+ 'userOrders',
75
+ this.program,
76
+ userOrdersPublicKey
77
+ );
78
+ await this.userOrdersAccountSubscriber.subscribe(
79
+ (data: UserOrdersAccount) => {
80
+ this.eventEmitter.emit('userOrdersData', data);
81
+ this.eventEmitter.emit('update');
82
+ }
83
+ );
84
+
64
85
  this.eventEmitter.emit('update');
65
86
  this.isSubscribed = true;
66
87
  return true;
@@ -70,6 +91,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
70
91
  await Promise.all([
71
92
  this.userDataAccountSubscriber.fetch(),
72
93
  this.userPositionsAccountSubscriber.fetch(),
94
+ this.userOrdersAccountSubscriber.fetch(),
73
95
  ]);
74
96
  }
75
97
 
@@ -80,6 +102,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
80
102
 
81
103
  this.userDataAccountSubscriber.unsubscribe();
82
104
  this.userPositionsAccountSubscriber.unsubscribe();
105
+ this.userOrdersAccountSubscriber.unsubscribe();
83
106
 
84
107
  this.isSubscribed = false;
85
108
  }
@@ -101,4 +124,8 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
101
124
  this.assertIsSubscribed();
102
125
  return this.userPositionsAccountSubscriber.data;
103
126
  }
127
+
128
+ public getUserOrdersAccount(): UserOrdersAccount {
129
+ return this.userOrdersAccountSubscriber.data;
130
+ }
104
131
  }
@@ -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';
@@ -35,6 +38,8 @@ export interface ClearingHouseAccountEvents {
35
38
  liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
36
39
  depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
37
40
  curveHistoryAccountUpdate: (payload: CurveHistoryAccount) => void;
41
+ orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
42
+ orderStateAccountUpdate: (payload: OrderStateAccount) => void;
38
43
  update: void;
39
44
  }
40
45
 
@@ -44,7 +49,8 @@ export type ClearingHouseAccountTypes =
44
49
  | 'fundingPaymentHistoryAccount'
45
50
  | 'fundingRateHistoryAccount'
46
51
  | 'curveHistoryAccount'
47
- | 'liquidationHistoryAccount';
52
+ | 'liquidationHistoryAccount'
53
+ | 'orderHistoryAccount';
48
54
 
49
55
  export interface ClearingHouseAccountSubscriber {
50
56
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
@@ -66,11 +72,41 @@ export interface ClearingHouseAccountSubscriber {
66
72
  getFundingRateHistoryAccount(): FundingRateHistoryAccount;
67
73
  getCurveHistoryAccount(): CurveHistoryAccount;
68
74
  getLiquidationHistoryAccount(): LiquidationHistoryAccount;
75
+ getOrderStateAccount(): OrderStateAccount;
76
+ getOrderHistoryAccount(): OrderHistoryAccount;
77
+ }
78
+
79
+ export interface HistoryAccountEvents {
80
+ fundingPaymentHistoryAccountUpdate: (
81
+ payload: FundingPaymentHistoryAccount
82
+ ) => void;
83
+ fundingRateHistoryAccountUpdate: (payload: FundingRateHistoryAccount) => void;
84
+ tradeHistoryAccountUpdate: (payload: TradeHistoryAccount) => void;
85
+ liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
86
+ depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
87
+ curveHistoryAccountUpdate: (payload: CurveHistoryAccount) => void;
88
+ update: void;
89
+ }
90
+
91
+ export interface HistoryAccountSubscriber {
92
+ eventEmitter: StrictEventEmitter<EventEmitter, HistoryAccountEvents>;
93
+ isSubscribed: boolean;
94
+
95
+ subscribe(): Promise<boolean>;
96
+ unsubscribe(): Promise<void>;
97
+
98
+ getTradeHistoryAccount(): TradeHistoryAccount;
99
+ getDepositHistoryAccount(): DepositHistoryAccount;
100
+ getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
101
+ getFundingRateHistoryAccount(): FundingRateHistoryAccount;
102
+ getCurveHistoryAccount(): CurveHistoryAccount;
103
+ getLiquidationHistoryAccount(): LiquidationHistoryAccount;
69
104
  }
70
105
 
71
106
  export interface UserAccountEvents {
72
107
  userAccountData: (payload: UserAccount) => void;
73
108
  userPositionsData: (payload: UserPositionsAccount) => void;
109
+ userOrdersData: (payload: UserOrdersAccount) => void;
74
110
  update: void;
75
111
  }
76
112
 
@@ -84,4 +120,5 @@ export interface UserAccountSubscriber {
84
120
 
85
121
  getUserAccount(): UserAccount;
86
122
  getUserPositionsAccount(): UserPositionsAccount;
123
+ getUserOrdersAccount(): UserOrdersAccount;
87
124
  }