@drift-labs/sdk 0.1.30-master.0 → 0.1.30-master.3

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 (82) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +2 -2
  2. package/lib/accounts/bulkAccountLoader.js +21 -24
  3. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +15 -0
  4. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +50 -23
  5. package/lib/clearingHouse.d.ts +0 -1
  6. package/lib/clearingHouse.js +0 -14
  7. package/lib/constants/accounts.d.ts +15 -0
  8. package/lib/constants/accounts.js +18 -0
  9. package/lib/constants/markets.js +11 -8
  10. package/lib/factory/clearingHouse.d.ts +14 -4
  11. package/lib/factory/clearingHouse.js +23 -6
  12. package/lib/idl/clearing_house.json +2 -33
  13. package/lib/index.d.ts +0 -1
  14. package/lib/index.js +0 -1
  15. package/lib/math/amm.d.ts +20 -0
  16. package/lib/math/amm.js +151 -1
  17. package/lib/util/promiseTimeout.d.ts +1 -0
  18. package/lib/util/promiseTimeout.js +14 -0
  19. package/package.json +1 -1
  20. package/src/accounts/bulkAccountLoader.ts +22 -32
  21. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +73 -30
  22. package/src/clearingHouse.ts +0 -13
  23. package/src/constants/accounts.ts +26 -0
  24. package/src/constants/markets.ts +11 -8
  25. package/src/factory/clearingHouse.ts +47 -7
  26. package/src/idl/clearing_house.json +2 -33
  27. package/src/index.ts +0 -1
  28. package/src/math/amm.ts +212 -1
  29. package/src/util/promiseTimeout.ts +14 -0
  30. package/lib/math/repeg.d.ts +0 -32
  31. package/lib/math/repeg.js +0 -178
  32. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -210
  33. package/src/accounts/pollingClearingHouseAccountSubscriber.js.map +0 -1
  34. package/src/accounts/pollingOracleSubscriber.js +0 -65
  35. package/src/accounts/pollingOracleSubscriber.js.map +0 -1
  36. package/src/accounts/pollingTokenAccountSubscriber.js +0 -65
  37. package/src/accounts/pollingTokenAccountSubscriber.js.map +0 -1
  38. package/src/accounts/utils.js +0 -8
  39. package/src/accounts/utils.js.map +0 -1
  40. package/src/accounts/webSocketAccountSubscriber.js +0 -64
  41. package/src/accounts/webSocketAccountSubscriber.js.map +0 -1
  42. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -212
  43. package/src/accounts/webSocketClearingHouseAccountSubscriber.js.map +0 -1
  44. package/src/assert/assert.js +0 -10
  45. package/src/assert/assert.js.map +0 -1
  46. package/src/constants/markets.js +0 -167
  47. package/src/constants/markets.js.map +0 -1
  48. package/src/constants/numericConstants.js +0 -22
  49. package/src/constants/numericConstants.js.map +0 -1
  50. package/src/math/conversion.js +0 -16
  51. package/src/math/conversion.js.map +0 -1
  52. package/src/math/funding.js +0 -223
  53. package/src/math/funding.js.map +0 -1
  54. package/src/math/insuranceFund.js +0 -23
  55. package/src/math/insuranceFund.js.map +0 -1
  56. package/src/math/market.js +0 -30
  57. package/src/math/market.js.map +0 -1
  58. package/src/math/orders.js +0 -73
  59. package/src/math/orders.js.map +0 -1
  60. package/src/math/position.js +0 -121
  61. package/src/math/position.js.map +0 -1
  62. package/src/math/repeg.ts +0 -253
  63. package/src/math/trade.js +0 -182
  64. package/src/math/trade.js.map +0 -1
  65. package/src/math/utils.js +0 -27
  66. package/src/math/utils.js.map +0 -1
  67. package/src/oracles/switchboardClient.js +0 -60
  68. package/src/oracles/switchboardClient.js.map +0 -1
  69. package/src/oracles/types.js +0 -3
  70. package/src/oracles/types.js.map +0 -1
  71. package/src/token/index.js +0 -39
  72. package/src/token/index.js.map +0 -1
  73. package/src/tx/defaultTxSender.js +0 -13
  74. package/src/tx/defaultTxSender.js.map +0 -1
  75. package/src/tx/types.js +0 -3
  76. package/src/tx/types.js.map +0 -1
  77. package/src/tx/utils.js +0 -9
  78. package/src/tx/utils.js.map +0 -1
  79. package/src/util/computeUnits.js +0 -17
  80. package/src/util/computeUnits.js.map +0 -1
  81. package/src/util/tps.js +0 -17
  82. package/src/util/tps.js.map +0 -1
@@ -1,210 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PollingClearingHouseAccountSubscriber = void 0;
4
- const types_1 = require("./types");
5
- const events_1 = require("events");
6
- const addresses_1 = require("../addresses");
7
- const utils_1 = require("./utils");
8
- class PollingClearingHouseAccountSubscriber {
9
- constructor(program, accountLoader) {
10
- this.accountsToPoll = new Map();
11
- this.optionalExtraSubscriptions = [];
12
- this.type = 'polling';
13
- this.isSubscribing = false;
14
- this.isSubscribed = false;
15
- this.program = program;
16
- this.eventEmitter = new events_1.EventEmitter();
17
- this.accountLoader = accountLoader;
18
- }
19
- async subscribe(optionalSubscriptions) {
20
- if (this.isSubscribed) {
21
- return true;
22
- }
23
- if (this.isSubscribing) {
24
- return await this.subscriptionPromise;
25
- }
26
- this.optionalExtraSubscriptions = optionalSubscriptions;
27
- this.isSubscribing = true;
28
- this.subscriptionPromise = new Promise((res) => {
29
- this.subscriptionPromiseResolver = res;
30
- });
31
- await this.updateAccountsToPoll();
32
- await this.addToAccountLoader();
33
- await this.fetch();
34
- this.eventEmitter.emit('update');
35
- this.isSubscribing = false;
36
- this.isSubscribed = true;
37
- this.subscriptionPromiseResolver(true);
38
- return true;
39
- }
40
- async updateAccountsToPoll() {
41
- var _a, _b, _c, _d, _e, _f, _g;
42
- if (this.accountsToPoll.size > 0) {
43
- return;
44
- }
45
- const statePublicKey = await (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
46
- const state = (await this.program.account.state.fetch(statePublicKey));
47
- this.accountsToPoll.set(statePublicKey.toString(), {
48
- key: 'state',
49
- publicKey: statePublicKey,
50
- eventType: 'stateAccountUpdate',
51
- });
52
- this.accountsToPoll.set(state.markets.toString(), {
53
- key: 'markets',
54
- publicKey: state.markets,
55
- eventType: 'marketsAccountUpdate',
56
- });
57
- this.accountsToPoll.set(state.orderState.toString(), {
58
- key: 'orderState',
59
- publicKey: state.orderState,
60
- eventType: 'orderStateAccountUpdate',
61
- });
62
- if ((_a = this.optionalExtraSubscriptions) === null || _a === void 0 ? void 0 : _a.includes('tradeHistoryAccount')) {
63
- this.accountsToPoll.set(state.tradeHistory.toString(), {
64
- key: 'tradeHistory',
65
- publicKey: state.tradeHistory,
66
- eventType: 'tradeHistoryAccountUpdate',
67
- });
68
- }
69
- if ((_b = this.optionalExtraSubscriptions) === null || _b === void 0 ? void 0 : _b.includes('depositHistoryAccount')) {
70
- this.accountsToPoll.set(state.depositHistory.toString(), {
71
- key: 'depositHistory',
72
- publicKey: state.depositHistory,
73
- eventType: 'depositHistoryAccountUpdate',
74
- });
75
- }
76
- if ((_c = this.optionalExtraSubscriptions) === null || _c === void 0 ? void 0 : _c.includes('fundingPaymentHistoryAccount')) {
77
- this.accountsToPoll.set(state.fundingPaymentHistory.toString(), {
78
- key: 'fundingPaymentHistory',
79
- publicKey: state.fundingPaymentHistory,
80
- eventType: 'fundingPaymentHistoryAccountUpdate',
81
- });
82
- }
83
- if ((_d = this.optionalExtraSubscriptions) === null || _d === void 0 ? void 0 : _d.includes('fundingRateHistoryAccount')) {
84
- this.accountsToPoll.set(state.fundingRateHistory.toString(), {
85
- key: 'fundingRateHistory',
86
- publicKey: state.fundingRateHistory,
87
- eventType: 'fundingRateHistoryAccountUpdate',
88
- });
89
- }
90
- if ((_e = this.optionalExtraSubscriptions) === null || _e === void 0 ? void 0 : _e.includes('curveHistoryAccount')) {
91
- this.accountsToPoll.set(state.extendedCurveHistory.toString(), {
92
- key: 'extendedCurveHistory',
93
- publicKey: state.extendedCurveHistory,
94
- eventType: 'curveHistoryAccountUpdate',
95
- });
96
- }
97
- if ((_f = this.optionalExtraSubscriptions) === null || _f === void 0 ? void 0 : _f.includes('liquidationHistoryAccount')) {
98
- this.accountsToPoll.set(state.liquidationHistory.toString(), {
99
- key: 'liquidationHistory',
100
- publicKey: state.liquidationHistory,
101
- eventType: 'liquidationHistoryAccountUpdate',
102
- });
103
- }
104
- if ((_g = this.optionalExtraSubscriptions) === null || _g === void 0 ? void 0 : _g.includes('orderHistoryAccount')) {
105
- const orderState = (await this.program.account.orderState.fetch(state.orderState));
106
- this.accountsToPoll.set(orderState.orderHistory.toString(), {
107
- key: 'orderHistory',
108
- publicKey: orderState.orderHistory,
109
- eventType: 'orderHistoryAccountUpdate',
110
- });
111
- }
112
- }
113
- async addToAccountLoader() {
114
- for (const [_, accountToPoll] of this.accountsToPoll) {
115
- accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
116
- const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
117
- this[accountToPoll.key] = account;
118
- // @ts-ignore
119
- this.eventEmitter.emit(accountToPoll.eventType, account);
120
- this.eventEmitter.emit('update');
121
- });
122
- }
123
- this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
124
- this.eventEmitter.emit('error', error);
125
- });
126
- }
127
- async fetch() {
128
- await this.accountLoader.load();
129
- for (const [_, accountToPoll] of this.accountsToPoll) {
130
- const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
131
- if (buffer) {
132
- this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
133
- }
134
- }
135
- }
136
- async unsubscribe() {
137
- if (!this.isSubscribed) {
138
- return;
139
- }
140
- for (const [_, accountToPoll] of this.accountsToPoll) {
141
- this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
142
- }
143
- this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
144
- this.errorCallbackId = undefined;
145
- this.accountsToPoll.clear();
146
- this.isSubscribed = false;
147
- }
148
- assertIsSubscribed() {
149
- if (!this.isSubscribed) {
150
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
151
- }
152
- }
153
- assertOptionalIsSubscribed(optionalSubscription) {
154
- if (!this.isSubscribed) {
155
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
156
- }
157
- if (!this.optionalExtraSubscriptions.includes(optionalSubscription)) {
158
- throw new types_1.NotSubscribedError(`You need to subscribe to the optional Clearing House account "${optionalSubscription}" to use this method`);
159
- }
160
- }
161
- getStateAccount() {
162
- this.assertIsSubscribed();
163
- return this.state;
164
- }
165
- getMarketsAccount() {
166
- this.assertIsSubscribed();
167
- return this.markets;
168
- }
169
- getOrderStateAccount() {
170
- this.assertIsSubscribed();
171
- return this.orderState;
172
- }
173
- getTradeHistoryAccount() {
174
- this.assertIsSubscribed();
175
- this.assertOptionalIsSubscribed('tradeHistoryAccount');
176
- return this.tradeHistory;
177
- }
178
- getDepositHistoryAccount() {
179
- this.assertIsSubscribed();
180
- this.assertOptionalIsSubscribed('depositHistoryAccount');
181
- return this.depositHistory;
182
- }
183
- getFundingPaymentHistoryAccount() {
184
- this.assertIsSubscribed();
185
- this.assertOptionalIsSubscribed('fundingPaymentHistoryAccount');
186
- return this.fundingPaymentHistory;
187
- }
188
- getFundingRateHistoryAccount() {
189
- this.assertIsSubscribed();
190
- this.assertOptionalIsSubscribed('fundingRateHistoryAccount');
191
- return this.fundingRateHistory;
192
- }
193
- getCurveHistoryAccount() {
194
- this.assertIsSubscribed();
195
- this.assertOptionalIsSubscribed('curveHistoryAccount');
196
- return this.extendedCurveHistory;
197
- }
198
- getLiquidationHistoryAccount() {
199
- this.assertIsSubscribed();
200
- this.assertOptionalIsSubscribed('liquidationHistoryAccount');
201
- return this.liquidationHistory;
202
- }
203
- getOrderHistoryAccount() {
204
- this.assertIsSubscribed();
205
- this.assertOptionalIsSubscribed('orderHistoryAccount');
206
- return this.orderHistory;
207
- }
208
- }
209
- exports.PollingClearingHouseAccountSubscriber = PollingClearingHouseAccountSubscriber;
210
- //# sourceMappingURL=pollingClearingHouseAccountSubscriber.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pollingClearingHouseAccountSubscriber.js","sourceRoot":"","sources":["pollingClearingHouseAccountSub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0BAA0B,CAAC,2BAA2B,CAAC,CAAC;QAC7D,OAAO,IAAI,CAAC,kBAAkB,CAAC;IAChC,CAAC;IAEM,sBAAsB;QAC5B,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,IAAI,CAAC,0BAA0B,CAAC,qBAAqB,CAAC,CAAC;QACvD,OAAO,IAAI,CAAC,oBAAoB,CAAC;IAClC,CAAC;IAEM,4BAA4B;QAClC,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,IAAI,CAAC,0BAA0B,CAAC,2BAA2B,CAAC,CAAC;QAC7D,OAAO,IAAI,CAAC,kBAAkB,CAAC;IAChC,CAAC;IAEM,sBAAsB;QAC5B,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,IAAI,CAAC,0BAA0B,CAAC,qBAAqB,CAAC,CAAC;QACvD,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;CACD;AA1SD,sFA0SC"}
@@ -1,65 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PollingOracleSubscriber = void 0;
4
- const types_1 = require("./types");
5
- const events_1 = require("events");
6
- class PollingOracleSubscriber {
7
- constructor(publicKey, oracleClient, accountLoader) {
8
- this.isSubscribed = false;
9
- this.publicKey = publicKey;
10
- this.oracleClient = oracleClient;
11
- this.accountLoader = accountLoader;
12
- this.eventEmitter = new events_1.EventEmitter();
13
- }
14
- async subscribe() {
15
- if (this.isSubscribed) {
16
- return true;
17
- }
18
- this.addToAccountLoader();
19
- await this.fetch();
20
- this.eventEmitter.emit('update');
21
- this.isSubscribed = true;
22
- return true;
23
- }
24
- addToAccountLoader() {
25
- if (this.callbackId) {
26
- return;
27
- }
28
- this.callbackId = this.accountLoader.addAccount(this.publicKey, async (buffer) => {
29
- const oraclePriceData = await this.oracleClient.getOraclePriceDataFromBuffer(buffer);
30
- this.oraclePriceData = oraclePriceData;
31
- // @ts-ignore
32
- this.eventEmitter.emit('oracleUpdate', oraclePriceData);
33
- this.eventEmitter.emit('update');
34
- });
35
- this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
36
- this.eventEmitter.emit('error', error);
37
- });
38
- }
39
- async fetch() {
40
- await this.accountLoader.load();
41
- const buffer = this.accountLoader.getAccountData(this.publicKey);
42
- this.oraclePriceData = await this.oracleClient.getOraclePriceDataFromBuffer(buffer);
43
- }
44
- async unsubscribe() {
45
- if (!this.isSubscribed) {
46
- return;
47
- }
48
- this.accountLoader.removeAccount(this.publicKey, this.callbackId);
49
- this.callbackId = undefined;
50
- this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
51
- this.errorCallbackId = undefined;
52
- this.isSubscribed = false;
53
- }
54
- assertIsSubscribed() {
55
- if (!this.isSubscribed) {
56
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
57
- }
58
- }
59
- getOraclePriceData() {
60
- this.assertIsSubscribed();
61
- return this.oraclePriceData;
62
- }
63
- }
64
- exports.PollingOracleSubscriber = PollingOracleSubscriber;
65
- //# sourceMappingURL=pollingOracleSubscriber.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pollingOracleSubscriber.js","sourceRoot":"","sources":["pollingOracleSubscriber.ts"],"names":[],"mappings":";;;AAAA,mCAA6E;AAG7E,mCAAsC;AAKtC,MAAa,uBAAuB;IAanC,YACC,SAAoB,EACpB,YAA0B,EAC1B,aAAgC;QAEhC,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC1B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,YAAY,GAAG,IAAI,qBAAY,EAAE,CAAC;IACxC,CAAC;IAED,KAAK,CAAC,SAAS;QACd,IAAI,IAAI,CAAC,YAAY,EAAE;YACtB,OAAO,IAAI,CAAC;SACZ;QAED,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,MAAM,I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aAAa,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QAClE,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAE5B,IAAI,CAAC,aAAa,CAAC,oBAAoB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAC9D,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QAEjC,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED,kBAAkB;QACjB,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACvB,MAAM,IAAI,0BAAkB,CAC3B,sDAAsD,CACtD,CAAC;SACF;IACF,CAAC;IAEM,kBAAkB;QACxB,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC,eAAe,CAAC;IAC7B,CAAC;CACD;AA9FD,0DA8FC"}
@@ -1,65 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PollingTokenAccountSubscriber = void 0;
4
- const types_1 = require("./types");
5
- const events_1 = require("events");
6
- const token_1 = require("../token");
7
- class PollingTokenAccountSubscriber {
8
- constructor(publicKey, accountLoader) {
9
- this.isSubscribed = false;
10
- this.publicKey = publicKey;
11
- this.accountLoader = accountLoader;
12
- this.eventEmitter = new events_1.EventEmitter();
13
- }
14
- async subscribe() {
15
- if (this.isSubscribed) {
16
- return true;
17
- }
18
- this.addToAccountLoader();
19
- await this.fetch();
20
- this.eventEmitter.emit('update');
21
- this.isSubscribed = true;
22
- return true;
23
- }
24
- addToAccountLoader() {
25
- if (this.callbackId) {
26
- return;
27
- }
28
- this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer) => {
29
- const tokenAccount = (0, token_1.parseTokenAccount)(buffer);
30
- this.tokenAccount = tokenAccount;
31
- // @ts-ignore
32
- this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
33
- this.eventEmitter.emit('update');
34
- });
35
- this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
36
- this.eventEmitter.emit('error', error);
37
- });
38
- }
39
- async fetch() {
40
- await this.accountLoader.load();
41
- const buffer = this.accountLoader.getAccountData(this.publicKey);
42
- this.tokenAccount = (0, token_1.parseTokenAccount)(buffer);
43
- }
44
- async unsubscribe() {
45
- if (!this.isSubscribed) {
46
- return;
47
- }
48
- this.accountLoader.removeAccount(this.publicKey, this.callbackId);
49
- this.callbackId = undefined;
50
- this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
51
- this.errorCallbackId = undefined;
52
- this.isSubscribed = false;
53
- }
54
- assertIsSubscribed() {
55
- if (!this.isSubscribed) {
56
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
57
- }
58
- }
59
- getTokenAccount() {
60
- this.assertIsSubscribed();
61
- return this.tokenAccount;
62
- }
63
- }
64
- exports.PollingTokenAccountSubscriber = PollingTokenAccountSubscriber;
65
- //# sourceMappingURL=pollingTokenAccountSubscriber.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pollingTokenAccountSubscriber.js","sourceRoot":"","sources":["pollingTokenAccountSubscriber.ts"],"names":[],"mappings":";;;AAAA,mCAIiB;AAGjB,mCAAsC;AAItC,oCAA6C;AAE7C,MAAa,6BAA6B;IAYzC,YAAmB,SAAoB,EAAE,aAAgC;QACxE,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC1B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,YAAY,GAAG,IAAI,qBAAY,EAAE,CAAC;IACxC,CAAC;IAED,KAAK,CAAC,SAAS;QACd,IAAI,IAAI,CAAC,YAAY,EAAE;YACtB,OAAO,IAAI,CAAC;SACZ;QAED,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACnB,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAEjC,IA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AAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAC9D,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QAEjC,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED,kBAAkB;QACjB,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACvB,MAAM,IAAI,0BAAkB,CAC3B,sDAAsD,CACtD,CAAC;SACF;IACF,CAAC;IAEM,eAAe;QACrB,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;CACD;AArFD,sEAqFC"}
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.capitalize = void 0;
4
- function capitalize(value) {
5
- return value[0].toUpperCase() + value.slice(1);
6
- }
7
- exports.capitalize = capitalize;
8
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;AAAA,SAAgB,UAAU,CAAC,KAAa;IACvC,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAChD,CAAC;AAFD,gCAEC"}
@@ -1,64 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebSocketAccountSubscriber = void 0;
4
- const utils_1 = require("./utils");
5
- class WebSocketAccountSubscriber {
6
- constructor(accountName, program, accountPublicKey) {
7
- this.accountName = accountName;
8
- this.program = program;
9
- this.accountPublicKey = accountPublicKey;
10
- }
11
- async subscribe(onChange) {
12
- if (this.listenerId) {
13
- return;
14
- }
15
- this.onChange = onChange;
16
- await this.fetch();
17
- this.listenerId = this.program.provider.connection.onAccountChange(this.accountPublicKey, (accountInfo, context) => {
18
- this.handleRpcResponse(context, accountInfo);
19
- }, this.program.provider.opts.commitment);
20
- }
21
- async fetch() {
22
- const rpcResponse = await this.program.provider.connection.getAccountInfoAndContext(this.accountPublicKey, this.program.provider.opts.commitment);
23
- this.handleRpcResponse(rpcResponse.context, rpcResponse === null || rpcResponse === void 0 ? void 0 : rpcResponse.value);
24
- }
25
- handleRpcResponse(context, accountInfo) {
26
- const newSlot = context.slot;
27
- let newBuffer = undefined;
28
- if (accountInfo) {
29
- newBuffer = accountInfo.data;
30
- }
31
- if (!this.accountData) {
32
- this.accountData = {
33
- buffer: newBuffer,
34
- slot: newSlot,
35
- };
36
- if (newBuffer) {
37
- this.data = this.program.account[this.accountName].coder.accounts.decode((0, utils_1.capitalize)(this.accountName), newBuffer);
38
- this.onChange(this.data);
39
- }
40
- return;
41
- }
42
- if (newSlot <= this.accountData.slot) {
43
- return;
44
- }
45
- const oldBuffer = this.accountData.buffer;
46
- if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
47
- this.accountData = {
48
- buffer: newBuffer,
49
- slot: newSlot,
50
- };
51
- this.data = this.program.account[this.accountName].coder.accounts.decode((0, utils_1.capitalize)(this.accountName), newBuffer);
52
- this.onChange(this.data);
53
- }
54
- }
55
- unsubscribe() {
56
- if (this.listenerId) {
57
- const promise = this.program.provider.connection.removeAccountChangeListener(this.listenerId);
58
- this.listenerId = undefined;
59
- return promise;
60
- }
61
- }
62
- }
63
- exports.WebSocketAccountSubscriber = WebSocketAccountSubscriber;
64
- //# sourceMappingURL=webSocketAccountSubscriber.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"webSocketAccountSubscriber.js","sourceRoot":"","sources":["webSocketAccountSubscriber.ts"],"names":[],"mappings":";;;AAGA,mCAAqC;AAGrC,MAAa,0BAA0B;IAStC,YACC,WAAmB,EACnB,OAAgB,EAChB,gBAA2B;QAE3B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC1C,CAAC;I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OAAO,CAC/B,IAAI,CAAC,WAAW,CAChB,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,WAAW,CAAC,EAAE,SAAS,CAAC,CAAC;gBACjE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACzB;YACD,OAAO;SACP;QAED,IAAI,OAAO,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE;YACrC,OAAO;SACP;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;QAC1C,IAAI,SAAS,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE;YAC9D,IAAI,CAAC,WAAW,GAAG;gBAClB,MAAM,EAAE,SAAS;gBACjB,IAAI,EAAE,OAAO;aACb,CAAC;YACF,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CACvE,IAAA,kBAAU,EAAC,IAAI,CAAC,WAAW,CAAC,EAC5B,SAAS,CACT,CAAC;YACF,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACzB;IACF,CAAC;IAED,WAAW;QACV,IAAI,IAAI,CAAC,UAAU,EAAE;YACpB,MAAM,OAAO,GACZ,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,2BAA2B,CAC3D,IAAI,CAAC,UAAU,CACf,CAAC;YACH,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;YAC5B,OAAO,OAAO,CAAC;SACf;IACF,CAAC;CACD;AA9FD,gEA8FC"}
@@ -1,212 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebSocketClearingHouseAccountSubscriber = void 0;
4
- const types_1 = require("./types");
5
- const events_1 = require("events");
6
- const addresses_1 = require("../addresses");
7
- const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
8
- class WebSocketClearingHouseAccountSubscriber {
9
- constructor(program) {
10
- this.optionalExtraSubscriptions = [];
11
- this.type = 'websocket';
12
- this.isSubscribing = false;
13
- this.isSubscribed = false;
14
- this.program = program;
15
- this.eventEmitter = new events_1.EventEmitter();
16
- }
17
- async subscribe(optionalSubscriptions) {
18
- if (this.isSubscribed) {
19
- return true;
20
- }
21
- if (this.isSubscribing) {
22
- return await this.subscriptionPromise;
23
- }
24
- this.isSubscribing = true;
25
- this.subscriptionPromise = new Promise((res) => {
26
- this.subscriptionPromiseResolver = res;
27
- });
28
- const statePublicKey = await (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
29
- // create and activate main state account subscription
30
- this.stateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('state', this.program, statePublicKey);
31
- await this.stateAccountSubscriber.subscribe((data) => {
32
- this.eventEmitter.emit('stateAccountUpdate', data);
33
- this.eventEmitter.emit('update');
34
- });
35
- const state = this.stateAccountSubscriber.data;
36
- this.marketsAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('markets', this.program, state.markets);
37
- await this.marketsAccountSubscriber.subscribe((data) => {
38
- this.eventEmitter.emit('marketsAccountUpdate', data);
39
- this.eventEmitter.emit('update');
40
- });
41
- this.orderStateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('orderState', this.program, state.orderState);
42
- await this.orderStateAccountSubscriber.subscribe((data) => {
43
- this.eventEmitter.emit('orderStateAccountUpdate', data);
44
- this.eventEmitter.emit('update');
45
- });
46
- const orderState = this.orderStateAccountSubscriber.data;
47
- // create subscribers for other state accounts
48
- this.tradeHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('tradeHistory', this.program, state.tradeHistory);
49
- this.depositHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('depositHistory', this.program, state.depositHistory);
50
- this.fundingPaymentHistoryAccountSubscriber =
51
- new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingPaymentHistory', this.program, state.fundingPaymentHistory);
52
- this.fundingRateHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingRateHistory', this.program, state.fundingRateHistory);
53
- this.liquidationHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('liquidationHistory', this.program, state.liquidationHistory);
54
- this.curveHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('extendedCurveHistory', this.program, state.extendedCurveHistory);
55
- this.orderHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('orderHistory', this.program, orderState.orderHistory);
56
- const extraSusbcribersToUse = [];
57
- if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('tradeHistoryAccount'))
58
- extraSusbcribersToUse.push({
59
- subscriber: this.tradeHistoryAccountSubscriber,
60
- eventType: 'tradeHistoryAccountUpdate',
61
- });
62
- if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('depositHistoryAccount'))
63
- extraSusbcribersToUse.push({
64
- subscriber: this.depositHistoryAccountSubscriber,
65
- eventType: 'depositHistoryAccountUpdate',
66
- });
67
- if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('fundingPaymentHistoryAccount'))
68
- extraSusbcribersToUse.push({
69
- subscriber: this.fundingPaymentHistoryAccountSubscriber,
70
- eventType: 'fundingPaymentHistoryAccountUpdate',
71
- });
72
- if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('fundingRateHistoryAccount'))
73
- extraSusbcribersToUse.push({
74
- subscriber: this.fundingRateHistoryAccountSubscriber,
75
- eventType: 'fundingRateHistoryAccountUpdate',
76
- });
77
- if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('liquidationHistoryAccount'))
78
- extraSusbcribersToUse.push({
79
- subscriber: this.liquidationHistoryAccountSubscriber,
80
- eventType: 'liquidationHistoryAccountUpdate',
81
- });
82
- if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('curveHistoryAccount'))
83
- extraSusbcribersToUse.push({
84
- subscriber: this.curveHistoryAccountSubscriber,
85
- eventType: 'curveHistoryAccountUpdate',
86
- });
87
- if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('orderHistoryAccount'))
88
- extraSusbcribersToUse.push({
89
- subscriber: this.orderHistoryAccountSubscriber,
90
- eventType: 'orderHistoryAccountUpdate',
91
- });
92
- this.optionalExtraSubscriptions = optionalSubscriptions !== null && optionalSubscriptions !== void 0 ? optionalSubscriptions : [];
93
- // await all subcriptions in parallel to boost performance
94
- //// the state account subscription above can't happen in here, because some of these susbcriptions are dependent on clearing house state being available
95
- await Promise.all(extraSusbcribersToUse.map(({ subscriber, eventType }) => subscriber.subscribe((data) => {
96
- this.eventEmitter.emit(eventType, data);
97
- this.eventEmitter.emit('update');
98
- })));
99
- this.eventEmitter.emit('update');
100
- this.isSubscribing = false;
101
- this.isSubscribed = true;
102
- this.subscriptionPromiseResolver(true);
103
- return true;
104
- }
105
- async fetch() {
106
- if (!this.isSubscribed) {
107
- return;
108
- }
109
- const promises = this.optionalExtraSubscriptions
110
- .map((optionalSubscription) => {
111
- const subscriber = `${optionalSubscription}Subscriber`;
112
- return this[subscriber].fetch();
113
- })
114
- .concat([
115
- this.stateAccountSubscriber.fetch(),
116
- this.marketsAccountSubscriber.fetch(),
117
- ]);
118
- await Promise.all(promises);
119
- }
120
- async unsubscribe() {
121
- if (!this.isSubscribed) {
122
- return;
123
- }
124
- await this.stateAccountSubscriber.unsubscribe();
125
- await this.marketsAccountSubscriber.unsubscribe();
126
- await this.orderStateAccountSubscriber.unsubscribe();
127
- if (this.optionalExtraSubscriptions.includes('tradeHistoryAccount')) {
128
- await this.tradeHistoryAccountSubscriber.unsubscribe();
129
- }
130
- if (this.optionalExtraSubscriptions.includes('fundingRateHistoryAccount')) {
131
- await this.fundingRateHistoryAccountSubscriber.unsubscribe();
132
- }
133
- if (this.optionalExtraSubscriptions.includes('fundingPaymentHistoryAccount')) {
134
- await this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
135
- }
136
- if (this.optionalExtraSubscriptions.includes('depositHistoryAccount')) {
137
- await this.depositHistoryAccountSubscriber.unsubscribe();
138
- }
139
- if (this.optionalExtraSubscriptions.includes('curveHistoryAccount')) {
140
- await this.curveHistoryAccountSubscriber.unsubscribe();
141
- }
142
- if (this.optionalExtraSubscriptions.includes('liquidationHistoryAccount')) {
143
- await this.liquidationHistoryAccountSubscriber.unsubscribe();
144
- }
145
- if (this.optionalExtraSubscriptions.includes('orderHistoryAccount')) {
146
- await this.orderHistoryAccountSubscriber.unsubscribe();
147
- }
148
- this.isSubscribed = false;
149
- }
150
- assertIsSubscribed() {
151
- if (!this.isSubscribed) {
152
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
153
- }
154
- }
155
- assertOptionalIsSubscribed(optionalSubscription) {
156
- if (!this.isSubscribed) {
157
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
158
- }
159
- if (!this.optionalExtraSubscriptions.includes(optionalSubscription)) {
160
- throw new types_1.NotSubscribedError(`You need to subscribe to the optional Clearing House account "${optionalSubscription}" to use this method`);
161
- }
162
- }
163
- getStateAccount() {
164
- this.assertIsSubscribed();
165
- return this.stateAccountSubscriber.data;
166
- }
167
- getMarketsAccount() {
168
- this.assertIsSubscribed();
169
- return this.marketsAccountSubscriber.data;
170
- }
171
- getTradeHistoryAccount() {
172
- this.assertIsSubscribed();
173
- this.assertOptionalIsSubscribed('tradeHistoryAccount');
174
- return this.tradeHistoryAccountSubscriber.data;
175
- }
176
- getDepositHistoryAccount() {
177
- this.assertIsSubscribed();
178
- this.assertOptionalIsSubscribed('depositHistoryAccount');
179
- return this.depositHistoryAccountSubscriber.data;
180
- }
181
- getFundingPaymentHistoryAccount() {
182
- this.assertIsSubscribed();
183
- this.assertOptionalIsSubscribed('fundingPaymentHistoryAccount');
184
- return this.fundingPaymentHistoryAccountSubscriber.data;
185
- }
186
- getFundingRateHistoryAccount() {
187
- this.assertIsSubscribed();
188
- this.assertOptionalIsSubscribed('fundingRateHistoryAccount');
189
- return this.fundingRateHistoryAccountSubscriber.data;
190
- }
191
- getCurveHistoryAccount() {
192
- this.assertIsSubscribed();
193
- this.assertOptionalIsSubscribed('curveHistoryAccount');
194
- return this.curveHistoryAccountSubscriber.data;
195
- }
196
- getLiquidationHistoryAccount() {
197
- this.assertIsSubscribed();
198
- this.assertOptionalIsSubscribed('liquidationHistoryAccount');
199
- return this.liquidationHistoryAccountSubscriber.data;
200
- }
201
- getOrderHistoryAccount() {
202
- this.assertIsSubscribed();
203
- this.assertOptionalIsSubscribed('orderHistoryAccount');
204
- return this.orderHistoryAccountSubscriber.data;
205
- }
206
- getOrderStateAccount() {
207
- this.assertIsSubscribed();
208
- return this.orderStateAccountSubscriber.data;
209
- }
210
- }
211
- exports.WebSocketClearingHouseAccountSubscriber = WebSocketClearingHouseAccountSubscriber;
212
- //# sourceMappingURL=webSocketClearingHouseAccountSubscriber.js.map