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

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 (93) hide show
  1. package/README.md +2 -1
  2. package/lib/accounts/bulkAccountLoader.d.ts +31 -0
  3. package/lib/accounts/bulkAccountLoader.js +175 -0
  4. package/lib/accounts/bulkUserSubscription.d.ts +7 -0
  5. package/lib/accounts/bulkUserSubscription.js +28 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
  8. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
  9. package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
  10. package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
  11. package/lib/accounts/pollingUserAccountSubscriber.js +133 -0
  12. package/lib/accounts/types.d.ts +34 -25
  13. package/lib/accounts/utils.d.ts +1 -0
  14. package/lib/accounts/utils.js +7 -0
  15. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -2
  16. package/lib/accounts/webSocketAccountSubscriber.js +43 -12
  17. package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +6 -4
  18. package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +5 -4
  19. package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +3 -1
  20. package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +10 -7
  21. package/lib/addresses.d.ts +2 -2
  22. package/lib/addresses.js +4 -4
  23. package/lib/admin.d.ts +3 -0
  24. package/lib/admin.js +61 -13
  25. package/lib/clearingHouse.d.ts +20 -4
  26. package/lib/clearingHouse.js +90 -10
  27. package/lib/clearingHouseUser.d.ts +12 -4
  28. package/lib/clearingHouseUser.js +35 -25
  29. package/lib/config.js +1 -1
  30. package/lib/constants/markets.d.ts +2 -1
  31. package/lib/constants/markets.js +35 -14
  32. package/lib/constants/numericConstants.d.ts +3 -1
  33. package/lib/constants/numericConstants.js +15 -17
  34. package/lib/examples/makeTradeExample.js +1 -1
  35. package/lib/factory/clearingHouse.d.ts +25 -0
  36. package/lib/factory/clearingHouse.js +64 -0
  37. package/lib/factory/clearingHouseUser.d.ts +19 -0
  38. package/lib/factory/clearingHouseUser.js +34 -0
  39. package/lib/idl/clearing_house.json +366 -17
  40. package/lib/index.d.ts +9 -3
  41. package/lib/index.js +10 -3
  42. package/lib/math/conversion.d.ts +1 -1
  43. package/lib/math/funding.js +4 -1
  44. package/lib/math/insuranceFund.d.ts +2 -1
  45. package/lib/math/insuranceFund.js +3 -6
  46. package/lib/math/position.d.ts +2 -1
  47. package/lib/math/position.js +2 -5
  48. package/lib/math/utils.d.ts +2 -1
  49. package/lib/math/utils.js +3 -3
  50. package/lib/mockUSDCFaucet.d.ts +2 -1
  51. package/lib/orderParams.d.ts +3 -3
  52. package/lib/orderParams.js +28 -8
  53. package/lib/orders.d.ts +2 -1
  54. package/lib/token/index.d.ts +3 -0
  55. package/lib/token/index.js +38 -0
  56. package/lib/types.d.ts +20 -8
  57. package/lib/types.js +2 -2
  58. package/package.json +11 -3
  59. package/src/accounts/bulkAccountLoader.ts +215 -0
  60. package/src/accounts/bulkUserSubscription.ts +28 -0
  61. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
  62. package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
  63. package/src/accounts/pollingUserAccountSubscriber.ts +186 -0
  64. package/src/accounts/types.ts +40 -30
  65. package/src/accounts/utils.ts +3 -0
  66. package/src/accounts/webSocketAccountSubscriber.ts +67 -17
  67. package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +9 -6
  68. package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +10 -5
  69. package/src/addresses.ts +6 -4
  70. package/src/admin.ts +73 -20
  71. package/src/clearingHouse.ts +125 -25
  72. package/src/clearingHouseUser.ts +28 -8
  73. package/src/config.ts +1 -1
  74. package/src/constants/markets.ts +25 -1
  75. package/src/constants/numericConstants.ts +2 -1
  76. package/src/examples/makeTradeExample.ts +4 -1
  77. package/src/factory/clearingHouse.ts +125 -0
  78. package/src/factory/clearingHouseUser.ts +73 -0
  79. package/src/idl/clearing_house.json +366 -17
  80. package/src/index.ts +9 -3
  81. package/src/math/conversion.ts +1 -1
  82. package/src/math/funding.ts +5 -1
  83. package/src/math/insuranceFund.ts +1 -1
  84. package/src/math/position.ts +1 -1
  85. package/src/math/utils.ts +1 -1
  86. package/src/mockUSDCFaucet.ts +1 -1
  87. package/src/orderParams.ts +30 -7
  88. package/src/orders.ts +1 -1
  89. package/src/token/index.ts +37 -0
  90. package/src/types.ts +17 -6
  91. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
  92. package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
  93. package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
@@ -14,10 +14,12 @@ export function getLimitOrderParams(
14
14
  price: BN,
15
15
  reduceOnly: boolean,
16
16
  discountToken = false,
17
- referrer = false
17
+ referrer = false,
18
+ userOrderId = 0
18
19
  ): OrderParams {
19
20
  return {
20
21
  orderType: OrderType.LIMIT,
22
+ userOrderId,
21
23
  marketIndex,
22
24
  direction,
23
25
  quoteAssetAmount: ZERO,
@@ -26,16 +28,20 @@ export function getLimitOrderParams(
26
28
  reduceOnly,
27
29
  postOnly: false,
28
30
  immediateOrCancel: false,
31
+ positionLimit: ZERO,
32
+ padding0: true,
33
+ padding1: ZERO,
29
34
  optionalAccounts: {
30
35
  discountToken,
31
36
  referrer,
32
37
  },
33
38
  triggerCondition: OrderTriggerCondition.ABOVE,
34
39
  triggerPrice: ZERO,
40
+ oraclePriceOffset: ZERO,
35
41
  };
36
42
  }
37
43
 
38
- export function getStopOrderParams(
44
+ export function getTriggerMarketOrderParams(
39
45
  marketIndex: BN,
40
46
  direction: PositionDirection,
41
47
  baseAssetAmount: BN,
@@ -43,10 +49,12 @@ export function getStopOrderParams(
43
49
  triggerCondition: OrderTriggerCondition,
44
50
  reduceOnly: boolean,
45
51
  discountToken = false,
46
- referrer = false
52
+ referrer = false,
53
+ userOrderId = 0
47
54
  ): OrderParams {
48
55
  return {
49
- orderType: OrderType.STOP,
56
+ orderType: OrderType.TRIGGER_MARKET,
57
+ userOrderId,
50
58
  marketIndex,
51
59
  direction,
52
60
  quoteAssetAmount: ZERO,
@@ -55,16 +63,20 @@ export function getStopOrderParams(
55
63
  reduceOnly,
56
64
  postOnly: false,
57
65
  immediateOrCancel: false,
66
+ positionLimit: ZERO,
67
+ padding0: true,
68
+ padding1: ZERO,
58
69
  optionalAccounts: {
59
70
  discountToken,
60
71
  referrer,
61
72
  },
62
73
  triggerCondition,
63
74
  triggerPrice,
75
+ oraclePriceOffset: ZERO,
64
76
  };
65
77
  }
66
78
 
67
- export function getStopLimitOrderParams(
79
+ export function getTriggerLimitOrderParams(
68
80
  marketIndex: BN,
69
81
  direction: PositionDirection,
70
82
  baseAssetAmount: BN,
@@ -73,10 +85,12 @@ export function getStopLimitOrderParams(
73
85
  triggerCondition: OrderTriggerCondition,
74
86
  reduceOnly: boolean,
75
87
  discountToken = false,
76
- referrer = false
88
+ referrer = false,
89
+ userOrderId = 0
77
90
  ): OrderParams {
78
91
  return {
79
- orderType: OrderType.STOP_LIMIT,
92
+ orderType: OrderType.TRIGGER_LIMIT,
93
+ userOrderId,
80
94
  marketIndex,
81
95
  direction,
82
96
  quoteAssetAmount: ZERO,
@@ -85,12 +99,16 @@ export function getStopLimitOrderParams(
85
99
  reduceOnly,
86
100
  postOnly: false,
87
101
  immediateOrCancel: false,
102
+ positionLimit: ZERO,
103
+ padding0: true,
104
+ padding1: ZERO,
88
105
  optionalAccounts: {
89
106
  discountToken,
90
107
  referrer,
91
108
  },
92
109
  triggerCondition,
93
110
  triggerPrice,
111
+ oraclePriceOffset: ZERO,
94
112
  };
95
113
  }
96
114
 
@@ -110,6 +128,7 @@ export function getMarketOrderParams(
110
128
 
111
129
  return {
112
130
  orderType: OrderType.MARKET,
131
+ userOrderId: 0,
113
132
  marketIndex,
114
133
  direction,
115
134
  quoteAssetAmount,
@@ -118,11 +137,15 @@ export function getMarketOrderParams(
118
137
  reduceOnly,
119
138
  postOnly: false,
120
139
  immediateOrCancel: false,
140
+ positionLimit: ZERO,
141
+ padding0: true,
142
+ padding1: ZERO,
121
143
  optionalAccounts: {
122
144
  discountToken,
123
145
  referrer,
124
146
  },
125
147
  triggerCondition: OrderTriggerCondition.ABOVE,
126
148
  triggerPrice: ZERO,
149
+ oraclePriceOffset: ZERO,
127
150
  };
128
151
  }
package/src/orders.ts CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  UserAccount,
7
7
  UserPosition,
8
8
  } from './types';
9
- import BN from 'bn.js';
9
+ import { BN } from '.';
10
10
  import {
11
11
  calculateMarkPrice,
12
12
  calculateNewMarketAfterTrade,
@@ -0,0 +1,37 @@
1
+ import { AccountInfo, AccountLayout, u64 } from '@solana/spl-token';
2
+ import { PublicKey } from '@solana/web3.js';
3
+
4
+ export function parseTokenAccount(data: Buffer): AccountInfo {
5
+ const accountInfo = AccountLayout.decode(data);
6
+ accountInfo.mint = new PublicKey(accountInfo.mint);
7
+ accountInfo.owner = new PublicKey(accountInfo.owner);
8
+ accountInfo.amount = u64.fromBuffer(accountInfo.amount);
9
+
10
+ if (accountInfo.delegateOption === 0) {
11
+ accountInfo.delegate = null;
12
+ // eslint-disable-next-line new-cap
13
+ accountInfo.delegatedAmount = new u64(0);
14
+ } else {
15
+ accountInfo.delegate = new PublicKey(accountInfo.delegate);
16
+ accountInfo.delegatedAmount = u64.fromBuffer(accountInfo.delegatedAmount);
17
+ }
18
+
19
+ accountInfo.isInitialized = accountInfo.state !== 0;
20
+ accountInfo.isFrozen = accountInfo.state === 2;
21
+
22
+ if (accountInfo.isNativeOption === 1) {
23
+ accountInfo.rentExemptReserve = u64.fromBuffer(accountInfo.isNative);
24
+ accountInfo.isNative = true;
25
+ } else {
26
+ accountInfo.rentExemptReserve = null;
27
+ accountInfo.isNative = false;
28
+ }
29
+
30
+ if (accountInfo.closeAuthorityOption === 0) {
31
+ accountInfo.closeAuthority = null;
32
+ } else {
33
+ accountInfo.closeAuthority = new PublicKey(accountInfo.closeAuthority);
34
+ }
35
+
36
+ return accountInfo;
37
+ }
package/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { PublicKey, Transaction } from '@solana/web3.js';
2
- import BN from 'bn.js';
2
+ import { BN } from '.';
3
3
 
4
4
  // # Utility Types / Enums / Constants
5
5
  export class SwapDirection {
@@ -19,8 +19,8 @@ export class OracleSource {
19
19
 
20
20
  export class OrderType {
21
21
  static readonly LIMIT = { limit: {} };
22
- static readonly STOP = { stop: {} };
23
- static readonly STOP_LIMIT = { stopLimit: {} };
22
+ static readonly TRIGGER_MARKET = { triggerMarket: {} };
23
+ static readonly TRIGGER_LIMIT = { triggerLimit: {} };
24
24
  static readonly MARKET = { market: {} };
25
25
  }
26
26
 
@@ -79,9 +79,9 @@ export type DepositHistoryAccount = {
79
79
  depositRecords: DepositRecord[];
80
80
  };
81
81
 
82
- export type CurveHistoryAccount = {
82
+ export type ExtendedCurveHistoryAccount = {
83
83
  head: BN;
84
- curveRecords: CurveRecord[];
84
+ curveRecords: ExtendedCurveRecord[];
85
85
  };
86
86
 
87
87
  export type FundingRateHistoryAccount = {
@@ -119,7 +119,7 @@ export type DepositRecord = {
119
119
  amount: BN;
120
120
  };
121
121
 
122
- export type CurveRecord = {
122
+ export type ExtendedCurveRecord = {
123
123
  ts: BN;
124
124
  recordId: BN;
125
125
  marketIndex: BN;
@@ -135,6 +135,8 @@ export type CurveRecord = {
135
135
  baseAssetAmountShort: BN;
136
136
  baseAssetAmount: BN;
137
137
  openInterest: BN;
138
+ oraclePrice: BN;
139
+ tradeId: BN;
138
140
  };
139
141
 
140
142
  export type TradeRecord = {
@@ -255,6 +257,7 @@ export type StateAccount = {
255
257
  oracleGuardRails: OracleGuardRails;
256
258
  maxDeposit: BN;
257
259
  orderState: PublicKey;
260
+ extendedCurveHistory: PublicKey;
258
261
  };
259
262
 
260
263
  export type OrderStateAccount = {
@@ -301,6 +304,7 @@ export type AMM = {
301
304
  totalFee: BN;
302
305
  minimumQuoteAssetTradeSize: BN;
303
306
  minimumBaseAssetTradeSize: BN;
307
+ lastOraclePrice: BN;
304
308
  };
305
309
 
306
310
  // # User Account Types
@@ -338,8 +342,10 @@ export type Order = {
338
342
  orderType: OrderType;
339
343
  ts: BN;
340
344
  orderId: BN;
345
+ userOrderId: number;
341
346
  marketIndex: BN;
342
347
  price: BN;
348
+ userBaseAssetAmount: BN;
343
349
  baseAssetAmount: BN;
344
350
  baseAssetAmountFilled: BN;
345
351
  quoteAssetAmount: BN;
@@ -357,6 +363,7 @@ export type Order = {
357
363
 
358
364
  export type OrderParams = {
359
365
  orderType: OrderType;
366
+ userOrderId: number;
360
367
  direction: PositionDirection;
361
368
  quoteAssetAmount: BN;
362
369
  baseAssetAmount: BN;
@@ -367,6 +374,10 @@ export type OrderParams = {
367
374
  immediateOrCancel: boolean;
368
375
  triggerPrice: BN;
369
376
  triggerCondition: OrderTriggerCondition;
377
+ positionLimit: BN;
378
+ oraclePriceOffset: BN;
379
+ padding0: boolean;
380
+ padding1: BN;
370
381
  optionalAccounts: {
371
382
  discountToken: boolean;
372
383
  referrer: boolean;
@@ -1,28 +0,0 @@
1
- /// <reference types="node" />
2
- import { ClearingHouseAccountEvents, HistoryAccountSubscriber } from './types';
3
- import { AccountSubscriber } from './types';
4
- import { CurveHistoryAccount, DepositHistoryAccount, FundingPaymentHistoryAccount, FundingRateHistoryAccount, LiquidationHistoryAccount, TradeHistoryAccount } from '../types';
5
- import { Program } from '@project-serum/anchor';
6
- import StrictEventEmitter from 'strict-event-emitter-types';
7
- import { EventEmitter } from 'events';
8
- export declare class DefaultHistoryAccountSubscriber implements HistoryAccountSubscriber {
9
- isSubscribed: boolean;
10
- program: Program;
11
- eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
12
- tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
13
- depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
14
- fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
15
- fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
16
- curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
17
- liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
18
- constructor(program: Program);
19
- subscribe(): Promise<boolean>;
20
- unsubscribe(): Promise<void>;
21
- assertIsSubscribed(): void;
22
- getTradeHistoryAccount(): TradeHistoryAccount;
23
- getDepositHistoryAccount(): DepositHistoryAccount;
24
- getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
25
- getFundingRateHistoryAccount(): FundingRateHistoryAccount;
26
- getCurveHistoryAccount(): CurveHistoryAccount;
27
- getLiquidationHistoryAccount(): LiquidationHistoryAccount;
28
- }
@@ -1,110 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DefaultHistoryAccountSubscriber = void 0;
13
- const types_1 = require("./types");
14
- const events_1 = require("events");
15
- const addresses_1 = require("../addresses");
16
- const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
17
- class DefaultHistoryAccountSubscriber {
18
- constructor(program) {
19
- this.isSubscribed = false;
20
- this.program = program;
21
- this.eventEmitter = new events_1.EventEmitter();
22
- }
23
- subscribe() {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- if (this.isSubscribed) {
26
- return true;
27
- }
28
- const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
29
- const state = yield this.program.account.state.fetch(statePublicKey);
30
- this.tradeHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('tradeHistory', this.program, state.tradeHistory);
31
- yield this.tradeHistoryAccountSubscriber.subscribe((data) => {
32
- this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
33
- this.eventEmitter.emit('update');
34
- });
35
- this.depositHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('depositHistory', this.program, state.depositHistory);
36
- yield this.depositHistoryAccountSubscriber.subscribe((data) => {
37
- this.eventEmitter.emit('depositHistoryAccountUpdate', data);
38
- this.eventEmitter.emit('update');
39
- });
40
- this.fundingPaymentHistoryAccountSubscriber =
41
- new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingPaymentHistory', this.program, state.fundingPaymentHistory);
42
- yield this.fundingPaymentHistoryAccountSubscriber.subscribe((data) => {
43
- this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
44
- this.eventEmitter.emit('update');
45
- });
46
- this.fundingRateHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingRateHistory', this.program, state.fundingRateHistory);
47
- yield this.fundingRateHistoryAccountSubscriber.subscribe((data) => {
48
- this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
49
- this.eventEmitter.emit('update');
50
- });
51
- this.liquidationHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('liquidationHistory', this.program, state.liquidationHistory);
52
- yield this.liquidationHistoryAccountSubscriber.subscribe((data) => {
53
- this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
54
- this.eventEmitter.emit('update');
55
- });
56
- this.curveHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('curveHistory', this.program, state.curveHistory);
57
- yield this.curveHistoryAccountSubscriber.subscribe((data) => {
58
- this.eventEmitter.emit('curveHistoryAccountUpdate', data);
59
- this.eventEmitter.emit('update');
60
- });
61
- this.eventEmitter.emit('update');
62
- this.isSubscribed = true;
63
- return true;
64
- });
65
- }
66
- unsubscribe() {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- if (!this.isSubscribed) {
69
- return;
70
- }
71
- yield this.tradeHistoryAccountSubscriber.unsubscribe();
72
- yield this.fundingRateHistoryAccountSubscriber.unsubscribe();
73
- yield this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
74
- yield this.depositHistoryAccountSubscriber.unsubscribe();
75
- yield this.curveHistoryAccountSubscriber.unsubscribe();
76
- yield this.liquidationHistoryAccountSubscriber.unsubscribe();
77
- this.isSubscribed = false;
78
- });
79
- }
80
- assertIsSubscribed() {
81
- if (!this.isSubscribed) {
82
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
83
- }
84
- }
85
- getTradeHistoryAccount() {
86
- this.assertIsSubscribed();
87
- return this.tradeHistoryAccountSubscriber.data;
88
- }
89
- getDepositHistoryAccount() {
90
- this.assertIsSubscribed();
91
- return this.depositHistoryAccountSubscriber.data;
92
- }
93
- getFundingPaymentHistoryAccount() {
94
- this.assertIsSubscribed();
95
- return this.fundingPaymentHistoryAccountSubscriber.data;
96
- }
97
- getFundingRateHistoryAccount() {
98
- this.assertIsSubscribed();
99
- return this.fundingRateHistoryAccountSubscriber.data;
100
- }
101
- getCurveHistoryAccount() {
102
- this.assertIsSubscribed();
103
- return this.curveHistoryAccountSubscriber.data;
104
- }
105
- getLiquidationHistoryAccount() {
106
- this.assertIsSubscribed();
107
- return this.liquidationHistoryAccountSubscriber.data;
108
- }
109
- }
110
- exports.DefaultHistoryAccountSubscriber = DefaultHistoryAccountSubscriber;
@@ -1,176 +0,0 @@
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
- }