@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.26

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 (184) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
  3. package/lib/accounts/types.d.ts +8 -9
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
  6. package/lib/addresses/pda.d.ts +7 -6
  7. package/lib/addresses/pda.js +31 -27
  8. package/lib/admin.d.ts +9 -6
  9. package/lib/admin.js +83 -42
  10. package/lib/clearingHouse.d.ts +69 -42
  11. package/lib/clearingHouse.js +753 -277
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +16 -16
  14. package/lib/clearingHouseUser.js +139 -119
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +20 -20
  17. package/lib/constants/numericConstants.d.ts +12 -12
  18. package/lib/constants/numericConstants.js +13 -13
  19. package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
  20. package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
  21. package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
  22. package/lib/constants/{banks.js → spotMarkets.js} +16 -16
  23. package/lib/events/types.d.ts +2 -1
  24. package/lib/events/types.js +1 -0
  25. package/lib/examples/makeTradeExample.js +7 -7
  26. package/lib/idl/clearing_house.json +1008 -279
  27. package/lib/index.d.ts +5 -3
  28. package/lib/index.js +5 -3
  29. package/lib/math/amm.d.ts +2 -2
  30. package/lib/math/amm.js +1 -1
  31. package/lib/math/funding.d.ts +6 -6
  32. package/lib/math/funding.js +2 -1
  33. package/lib/math/margin.d.ts +4 -4
  34. package/lib/math/margin.js +18 -11
  35. package/lib/math/market.d.ts +10 -9
  36. package/lib/math/market.js +29 -6
  37. package/lib/math/oracles.d.ts +2 -1
  38. package/lib/math/oracles.js +11 -1
  39. package/lib/math/orders.d.ts +5 -5
  40. package/lib/math/position.d.ts +13 -13
  41. package/lib/math/position.js +19 -19
  42. package/lib/math/spotBalance.d.ts +19 -0
  43. package/lib/math/spotBalance.js +176 -0
  44. package/lib/math/spotMarket.d.ts +4 -0
  45. package/lib/math/spotMarket.js +8 -0
  46. package/lib/math/spotPosition.d.ts +2 -0
  47. package/lib/math/spotPosition.js +8 -0
  48. package/lib/math/state.js +2 -2
  49. package/lib/math/trade.d.ts +4 -4
  50. package/lib/orderParams.d.ts +4 -4
  51. package/lib/orderParams.js +12 -4
  52. package/lib/serum/serumSubscriber.d.ts +23 -0
  53. package/lib/serum/serumSubscriber.js +41 -0
  54. package/lib/serum/types.d.ts +11 -0
  55. package/lib/serum/types.js +2 -0
  56. package/lib/tx/retryTxSender.d.ts +1 -1
  57. package/lib/tx/retryTxSender.js +4 -2
  58. package/lib/tx/types.d.ts +1 -1
  59. package/lib/types.d.ts +123 -33
  60. package/lib/types.js +31 -9
  61. package/my-script/.env +7 -0
  62. package/my-script/getUserStats.ts +106 -0
  63. package/my-script/multiConnections.ts +119 -0
  64. package/my-script/test-regex.ts +11 -0
  65. package/my-script/utils.ts +52 -0
  66. package/package.json +1 -1
  67. package/src/accounts/bulkAccountLoader.js +249 -0
  68. package/src/accounts/bulkUserStatsSubscription.js +75 -0
  69. package/src/accounts/bulkUserSubscription.js +75 -0
  70. package/src/accounts/fetch.js +92 -0
  71. package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
  72. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
  73. package/src/accounts/pollingOracleSubscriber.js +156 -0
  74. package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
  75. package/src/accounts/pollingUserAccountSubscriber.js +208 -0
  76. package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
  77. package/src/accounts/types.js +28 -0
  78. package/src/accounts/types.ts +11 -9
  79. package/src/accounts/utils.js +7 -0
  80. package/src/accounts/webSocketAccountSubscriber.js +138 -0
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
  82. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
  83. package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
  84. package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
  85. package/src/addresses/pda.js +186 -0
  86. package/src/addresses/pda.ts +49 -44
  87. package/src/admin.js +1284 -0
  88. package/src/admin.ts +140 -47
  89. package/src/assert/assert.js +1 -1
  90. package/src/clearingHouse.js +3433 -0
  91. package/src/clearingHouse.ts +1083 -378
  92. package/src/clearingHouseConfig.js +2 -0
  93. package/src/clearingHouseConfig.ts +2 -2
  94. package/src/clearingHouseUser.js +874 -0
  95. package/src/clearingHouseUser.ts +232 -168
  96. package/src/clearingHouseUserConfig.js +2 -0
  97. package/src/clearingHouseUserStats.js +115 -0
  98. package/src/clearingHouseUserStatsConfig.js +2 -0
  99. package/src/config.js +80 -0
  100. package/src/config.ts +29 -29
  101. package/src/constants/numericConstants.js +18 -11
  102. package/src/constants/numericConstants.ts +17 -15
  103. package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
  104. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  105. package/src/constants/spotMarkets.js +51 -0
  106. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  107. package/src/events/eventList.js +66 -23
  108. package/src/events/eventSubscriber.js +202 -0
  109. package/src/events/fetchLogs.js +117 -0
  110. package/src/events/pollingLogProvider.js +113 -0
  111. package/src/events/sort.js +41 -0
  112. package/src/events/txEventCache.js +22 -19
  113. package/src/events/types.js +25 -0
  114. package/src/events/types.ts +3 -0
  115. package/src/events/webSocketLogProvider.js +76 -0
  116. package/src/examples/makeTradeExample.ts +10 -8
  117. package/src/factory/bigNum.js +183 -180
  118. package/src/factory/oracleClient.js +9 -9
  119. package/src/idl/clearing_house.json +1008 -279
  120. package/src/index.js +75 -0
  121. package/src/index.ts +5 -3
  122. package/src/math/amm.js +422 -0
  123. package/src/math/amm.ts +6 -3
  124. package/src/math/auction.js +10 -10
  125. package/src/math/conversion.js +4 -3
  126. package/src/math/funding.js +223 -175
  127. package/src/math/funding.ts +7 -7
  128. package/src/math/insurance.js +27 -0
  129. package/src/math/margin.js +77 -0
  130. package/src/math/margin.ts +34 -23
  131. package/src/math/market.js +105 -0
  132. package/src/math/market.ts +71 -19
  133. package/src/math/oracles.js +40 -10
  134. package/src/math/oracles.ts +18 -1
  135. package/src/math/orders.js +153 -0
  136. package/src/math/orders.ts +5 -5
  137. package/src/math/position.js +172 -0
  138. package/src/math/position.ts +31 -31
  139. package/src/math/repeg.js +40 -40
  140. package/src/math/spotBalance.js +176 -0
  141. package/src/math/spotBalance.ts +290 -0
  142. package/src/math/spotMarket.js +8 -0
  143. package/src/math/spotMarket.ts +9 -0
  144. package/src/math/spotPosition.js +8 -0
  145. package/src/math/spotPosition.ts +6 -0
  146. package/src/math/state.ts +2 -2
  147. package/src/math/trade.js +81 -74
  148. package/src/math/trade.ts +4 -4
  149. package/src/math/utils.js +8 -7
  150. package/src/oracles/oracleClientCache.js +10 -9
  151. package/src/oracles/pythClient.js +52 -17
  152. package/src/oracles/quoteAssetOracleClient.js +44 -13
  153. package/src/oracles/switchboardClient.js +69 -37
  154. package/src/oracles/types.js +1 -1
  155. package/src/orderParams.js +14 -6
  156. package/src/orderParams.ts +16 -8
  157. package/src/serum/serumSubscriber.js +102 -0
  158. package/src/serum/serumSubscriber.ts +80 -0
  159. package/src/serum/types.js +2 -0
  160. package/src/serum/types.ts +13 -0
  161. package/src/slot/SlotSubscriber.js +67 -20
  162. package/src/token/index.js +4 -4
  163. package/src/tokenFaucet.js +288 -154
  164. package/src/tx/retryTxSender.js +280 -0
  165. package/src/tx/retryTxSender.ts +5 -2
  166. package/src/tx/types.js +1 -1
  167. package/src/tx/types.ts +2 -1
  168. package/src/tx/utils.js +7 -6
  169. package/src/types.js +216 -0
  170. package/src/types.ts +110 -33
  171. package/src/userName.js +5 -5
  172. package/src/util/computeUnits.js +46 -11
  173. package/src/util/promiseTimeout.js +5 -5
  174. package/src/util/tps.js +46 -12
  175. package/src/wallet.js +55 -18
  176. package/lib/math/bankBalance.d.ts +0 -15
  177. package/lib/math/bankBalance.js +0 -150
  178. package/src/addresses/marketAddresses.js +0 -26
  179. package/src/constants/banks.js +0 -42
  180. package/src/examples/makeTradeExample.js +0 -80
  181. package/src/math/bankBalance.ts +0 -258
  182. package/src/math/state.js +0 -15
  183. package/src/math/utils.js.map +0 -1
  184. package/src/util/getTokenAddress.js +0 -9
@@ -0,0 +1,465 @@
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
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
39
+ exports.PollingClearingHouseAccountSubscriber = void 0;
40
+ var types_1 = require("./types");
41
+ var events_1 = require("events");
42
+ var pda_1 = require("../addresses/pda");
43
+ var utils_1 = require("./utils");
44
+ var web3_js_1 = require("@solana/web3.js");
45
+ var oracleClientCache_1 = require("../oracles/oracleClientCache");
46
+ var quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
47
+ var PollingClearingHouseAccountSubscriber = /** @class */ (function () {
48
+ function PollingClearingHouseAccountSubscriber(program, accountLoader, perpMarketIndexes, spotMarketIndexes, oracleInfos) {
49
+ this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
50
+ this.accountsToPoll = new Map();
51
+ this.oraclesToPoll = new Map();
52
+ this.perpMarket = new Map();
53
+ this.spotMarket = new Map();
54
+ this.oracles = new Map();
55
+ this.isSubscribing = false;
56
+ this.isSubscribed = false;
57
+ this.program = program;
58
+ this.eventEmitter = new events_1.EventEmitter();
59
+ this.accountLoader = accountLoader;
60
+ this.perpMarketIndexes = perpMarketIndexes;
61
+ this.spotMarketIndexes = spotMarketIndexes;
62
+ this.oracleInfos = oracleInfos;
63
+ }
64
+ PollingClearingHouseAccountSubscriber.prototype.subscribe = function () {
65
+ return __awaiter(this, void 0, void 0, function () {
66
+ var subscriptionSucceeded, retries;
67
+ var _this = this;
68
+ return __generator(this, function (_a) {
69
+ switch (_a.label) {
70
+ case 0:
71
+ if (this.isSubscribed) {
72
+ return [2 /*return*/, true];
73
+ }
74
+ if (!this.isSubscribing) return [3 /*break*/, 2];
75
+ return [4 /*yield*/, this.subscriptionPromise];
76
+ case 1: return [2 /*return*/, _a.sent()];
77
+ case 2:
78
+ this.isSubscribing = true;
79
+ this.subscriptionPromise = new Promise(function (res) {
80
+ _this.subscriptionPromiseResolver = res;
81
+ });
82
+ return [4 /*yield*/, this.updateAccountsToPoll()];
83
+ case 3:
84
+ _a.sent();
85
+ return [4 /*yield*/, this.updateOraclesToPoll()];
86
+ case 4:
87
+ _a.sent();
88
+ return [4 /*yield*/, this.addToAccountLoader()];
89
+ case 5:
90
+ _a.sent();
91
+ subscriptionSucceeded = false;
92
+ retries = 0;
93
+ _a.label = 6;
94
+ case 6:
95
+ if (!(!subscriptionSucceeded && retries < 5)) return [3 /*break*/, 8];
96
+ return [4 /*yield*/, this.fetch()];
97
+ case 7:
98
+ _a.sent();
99
+ subscriptionSucceeded = this.didSubscriptionSucceed();
100
+ retries++;
101
+ return [3 /*break*/, 6];
102
+ case 8:
103
+ if (subscriptionSucceeded) {
104
+ this.eventEmitter.emit('update');
105
+ }
106
+ this.isSubscribing = false;
107
+ this.isSubscribed = subscriptionSucceeded;
108
+ this.subscriptionPromiseResolver(subscriptionSucceeded);
109
+ return [2 /*return*/, subscriptionSucceeded];
110
+ }
111
+ });
112
+ });
113
+ };
114
+ PollingClearingHouseAccountSubscriber.prototype.updateAccountsToPoll = function () {
115
+ return __awaiter(this, void 0, void 0, function () {
116
+ var accounts;
117
+ return __generator(this, function (_a) {
118
+ switch (_a.label) {
119
+ case 0:
120
+ if (this.accountsToPoll.size > 0) {
121
+ return [2 /*return*/];
122
+ }
123
+ return [4 /*yield*/, this.getClearingHouseAccounts()];
124
+ case 1:
125
+ accounts = _a.sent();
126
+ this.accountsToPoll.set(accounts.state.toString(), {
127
+ key: 'state',
128
+ publicKey: accounts.state,
129
+ eventType: 'stateAccountUpdate'
130
+ });
131
+ return [4 /*yield*/, this.updateMarketAccountsToPoll()];
132
+ case 2:
133
+ _a.sent();
134
+ return [4 /*yield*/, this.updateSpotMarketAccountsToPoll()];
135
+ case 3:
136
+ _a.sent();
137
+ return [2 /*return*/];
138
+ }
139
+ });
140
+ });
141
+ };
142
+ PollingClearingHouseAccountSubscriber.prototype.updateMarketAccountsToPoll = function () {
143
+ return __awaiter(this, void 0, void 0, function () {
144
+ var _i, _a, marketIndex;
145
+ return __generator(this, function (_b) {
146
+ switch (_b.label) {
147
+ case 0:
148
+ _i = 0, _a = this.perpMarketIndexes;
149
+ _b.label = 1;
150
+ case 1:
151
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
152
+ marketIndex = _a[_i];
153
+ return [4 /*yield*/, this.addMarketAccountToPoll(marketIndex)];
154
+ case 2:
155
+ _b.sent();
156
+ _b.label = 3;
157
+ case 3:
158
+ _i++;
159
+ return [3 /*break*/, 1];
160
+ case 4: return [2 /*return*/, true];
161
+ }
162
+ });
163
+ });
164
+ };
165
+ PollingClearingHouseAccountSubscriber.prototype.addMarketAccountToPoll = function (marketIndex) {
166
+ return __awaiter(this, void 0, void 0, function () {
167
+ var marketPublicKey;
168
+ return __generator(this, function (_a) {
169
+ switch (_a.label) {
170
+ case 0: return [4 /*yield*/, (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex)];
171
+ case 1:
172
+ marketPublicKey = _a.sent();
173
+ this.accountsToPoll.set(marketPublicKey.toString(), {
174
+ key: 'perpMarket',
175
+ publicKey: marketPublicKey,
176
+ eventType: 'perpMarketAccountUpdate',
177
+ mapKey: marketIndex.toNumber()
178
+ });
179
+ return [2 /*return*/, true];
180
+ }
181
+ });
182
+ });
183
+ };
184
+ PollingClearingHouseAccountSubscriber.prototype.updateSpotMarketAccountsToPoll = function () {
185
+ return __awaiter(this, void 0, void 0, function () {
186
+ var _i, _a, marketIndex;
187
+ return __generator(this, function (_b) {
188
+ switch (_b.label) {
189
+ case 0:
190
+ _i = 0, _a = this.spotMarketIndexes;
191
+ _b.label = 1;
192
+ case 1:
193
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
194
+ marketIndex = _a[_i];
195
+ return [4 /*yield*/, this.addSpotMarketAccountToPoll(marketIndex)];
196
+ case 2:
197
+ _b.sent();
198
+ _b.label = 3;
199
+ case 3:
200
+ _i++;
201
+ return [3 /*break*/, 1];
202
+ case 4: return [2 /*return*/, true];
203
+ }
204
+ });
205
+ });
206
+ };
207
+ PollingClearingHouseAccountSubscriber.prototype.addSpotMarketAccountToPoll = function (marketIndex) {
208
+ return __awaiter(this, void 0, void 0, function () {
209
+ var marketPublicKey;
210
+ return __generator(this, function (_a) {
211
+ switch (_a.label) {
212
+ case 0: return [4 /*yield*/, (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex)];
213
+ case 1:
214
+ marketPublicKey = _a.sent();
215
+ this.accountsToPoll.set(marketPublicKey.toString(), {
216
+ key: 'spotMarket',
217
+ publicKey: marketPublicKey,
218
+ eventType: 'spotMarketAccountUpdate',
219
+ mapKey: marketIndex.toNumber()
220
+ });
221
+ return [2 /*return*/, true];
222
+ }
223
+ });
224
+ });
225
+ };
226
+ PollingClearingHouseAccountSubscriber.prototype.updateOraclesToPoll = function () {
227
+ for (var _i = 0, _a = this.oracleInfos; _i < _a.length; _i++) {
228
+ var oracleInfo = _a[_i];
229
+ if (!oracleInfo.publicKey.equals(web3_js_1.PublicKey["default"])) {
230
+ this.addOracleToPoll(oracleInfo);
231
+ }
232
+ }
233
+ return true;
234
+ };
235
+ PollingClearingHouseAccountSubscriber.prototype.addOracleToPoll = function (oracleInfo) {
236
+ this.oraclesToPoll.set(oracleInfo.publicKey.toString(), {
237
+ publicKey: oracleInfo.publicKey,
238
+ source: oracleInfo.source
239
+ });
240
+ return true;
241
+ };
242
+ PollingClearingHouseAccountSubscriber.prototype.getClearingHouseAccounts = function () {
243
+ return __awaiter(this, void 0, void 0, function () {
244
+ var statePublicKey, accounts;
245
+ return __generator(this, function (_a) {
246
+ switch (_a.label) {
247
+ case 0: return [4 /*yield*/, (0, pda_1.getClearingHouseStateAccountPublicKey)(this.program.programId)];
248
+ case 1:
249
+ statePublicKey = _a.sent();
250
+ accounts = {
251
+ state: statePublicKey
252
+ };
253
+ return [2 /*return*/, accounts];
254
+ }
255
+ });
256
+ });
257
+ };
258
+ PollingClearingHouseAccountSubscriber.prototype.addToAccountLoader = function () {
259
+ return __awaiter(this, void 0, void 0, function () {
260
+ var _i, _a, _b, _1, accountToPoll, _c, _d, _e, _2, oracleToPoll;
261
+ var _this = this;
262
+ return __generator(this, function (_f) {
263
+ for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
264
+ _b = _a[_i], _1 = _b[0], accountToPoll = _b[1];
265
+ this.addAccountToAccountLoader(accountToPoll);
266
+ }
267
+ for (_c = 0, _d = this.oraclesToPoll; _c < _d.length; _c++) {
268
+ _e = _d[_c], _2 = _e[0], oracleToPoll = _e[1];
269
+ this.addOracleToAccountLoader(oracleToPoll);
270
+ }
271
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks(function (error) {
272
+ _this.eventEmitter.emit('error', error);
273
+ });
274
+ return [2 /*return*/];
275
+ });
276
+ });
277
+ };
278
+ PollingClearingHouseAccountSubscriber.prototype.addAccountToAccountLoader = function (accountToPoll) {
279
+ var _this = this;
280
+ accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, function (buffer, slot) {
281
+ if (!buffer)
282
+ return;
283
+ var account = _this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
284
+ var dataAndSlot = {
285
+ data: account,
286
+ slot: slot
287
+ };
288
+ if (accountToPoll.mapKey != undefined) {
289
+ _this[accountToPoll.key].set(accountToPoll.mapKey, dataAndSlot);
290
+ }
291
+ else {
292
+ _this[accountToPoll.key] = dataAndSlot;
293
+ }
294
+ // @ts-ignore
295
+ _this.eventEmitter.emit(accountToPoll.eventType, account);
296
+ _this.eventEmitter.emit('update');
297
+ if (!_this.isSubscribed) {
298
+ _this.isSubscribed = _this.didSubscriptionSucceed();
299
+ }
300
+ });
301
+ };
302
+ PollingClearingHouseAccountSubscriber.prototype.addOracleToAccountLoader = function (oracleToPoll) {
303
+ var _this = this;
304
+ var oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
305
+ oracleToPoll.callbackId = this.accountLoader.addAccount(oracleToPoll.publicKey, function (buffer, slot) {
306
+ if (!buffer)
307
+ return;
308
+ var oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
309
+ var dataAndSlot = {
310
+ data: oraclePriceData,
311
+ slot: slot
312
+ };
313
+ _this.oracles.set(oracleToPoll.publicKey.toString(), dataAndSlot);
314
+ _this.eventEmitter.emit('oraclePriceUpdate', oracleToPoll.publicKey, oraclePriceData);
315
+ _this.eventEmitter.emit('update');
316
+ });
317
+ };
318
+ PollingClearingHouseAccountSubscriber.prototype.fetch = function () {
319
+ return __awaiter(this, void 0, void 0, function () {
320
+ var _i, _a, _b, _3, accountToPoll, _c, buffer, slot, account, _d, _e, _f, _4, oracleToPoll, _g, buffer, slot, oracleClient, oraclePriceData;
321
+ return __generator(this, function (_h) {
322
+ switch (_h.label) {
323
+ case 0: return [4 /*yield*/, this.accountLoader.load()];
324
+ case 1:
325
+ _h.sent();
326
+ for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
327
+ _b = _a[_i], _3 = _b[0], accountToPoll = _b[1];
328
+ _c = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey), buffer = _c.buffer, slot = _c.slot;
329
+ if (buffer) {
330
+ account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
331
+ if (accountToPoll.mapKey != undefined) {
332
+ this[accountToPoll.key].set(accountToPoll.mapKey, {
333
+ data: account,
334
+ slot: slot
335
+ });
336
+ }
337
+ else {
338
+ this[accountToPoll.key] = {
339
+ data: account,
340
+ slot: slot
341
+ };
342
+ }
343
+ }
344
+ }
345
+ for (_d = 0, _e = this.oraclesToPoll; _d < _e.length; _d++) {
346
+ _f = _e[_d], _4 = _f[0], oracleToPoll = _f[1];
347
+ _g = this.accountLoader.getBufferAndSlot(oracleToPoll.publicKey), buffer = _g.buffer, slot = _g.slot;
348
+ if (buffer) {
349
+ oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
350
+ oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
351
+ this.oracles.set(oracleToPoll.publicKey.toString(), {
352
+ data: oraclePriceData,
353
+ slot: slot
354
+ });
355
+ }
356
+ }
357
+ return [2 /*return*/];
358
+ }
359
+ });
360
+ });
361
+ };
362
+ PollingClearingHouseAccountSubscriber.prototype.didSubscriptionSucceed = function () {
363
+ if (this.state)
364
+ return true;
365
+ return false;
366
+ };
367
+ PollingClearingHouseAccountSubscriber.prototype.unsubscribe = function () {
368
+ return __awaiter(this, void 0, void 0, function () {
369
+ var _i, _a, _b, _5, accountToPoll, _c, _d, _e, _6, oracleToPoll;
370
+ return __generator(this, function (_f) {
371
+ if (!this.isSubscribed) {
372
+ return [2 /*return*/];
373
+ }
374
+ for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
375
+ _b = _a[_i], _5 = _b[0], accountToPoll = _b[1];
376
+ this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
377
+ }
378
+ for (_c = 0, _d = this.oraclesToPoll; _c < _d.length; _c++) {
379
+ _e = _d[_c], _6 = _e[0], oracleToPoll = _e[1];
380
+ this.accountLoader.removeAccount(oracleToPoll.publicKey, oracleToPoll.callbackId);
381
+ }
382
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
383
+ this.errorCallbackId = undefined;
384
+ this.accountsToPoll.clear();
385
+ this.oraclesToPoll.clear();
386
+ this.isSubscribed = false;
387
+ return [2 /*return*/];
388
+ });
389
+ });
390
+ };
391
+ PollingClearingHouseAccountSubscriber.prototype.addSpotMarket = function (marketIndex) {
392
+ return __awaiter(this, void 0, void 0, function () {
393
+ var accountToPoll;
394
+ return __generator(this, function (_a) {
395
+ switch (_a.label) {
396
+ case 0: return [4 /*yield*/, this.addSpotMarketAccountToPoll(marketIndex)];
397
+ case 1:
398
+ _a.sent();
399
+ accountToPoll = this.accountsToPoll.get(marketIndex.toString());
400
+ this.addAccountToAccountLoader(accountToPoll);
401
+ return [2 /*return*/, true];
402
+ }
403
+ });
404
+ });
405
+ };
406
+ PollingClearingHouseAccountSubscriber.prototype.addPerpMarket = function (marketIndex) {
407
+ return __awaiter(this, void 0, void 0, function () {
408
+ var accountToPoll;
409
+ return __generator(this, function (_a) {
410
+ switch (_a.label) {
411
+ case 0: return [4 /*yield*/, this.addMarketAccountToPoll(marketIndex)];
412
+ case 1:
413
+ _a.sent();
414
+ accountToPoll = this.accountsToPoll.get(marketIndex.toString());
415
+ this.addAccountToAccountLoader(accountToPoll);
416
+ return [2 /*return*/, true];
417
+ }
418
+ });
419
+ });
420
+ };
421
+ PollingClearingHouseAccountSubscriber.prototype.addOracle = function (oracleInfo) {
422
+ return __awaiter(this, void 0, void 0, function () {
423
+ var oracleToPoll;
424
+ return __generator(this, function (_a) {
425
+ if (oracleInfo.publicKey.equals(web3_js_1.PublicKey["default"])) {
426
+ return [2 /*return*/, true];
427
+ }
428
+ this.addOracleToPoll(oracleInfo);
429
+ oracleToPoll = this.oraclesToPoll.get(oracleInfo.publicKey.toString());
430
+ this.addOracleToAccountLoader(oracleToPoll);
431
+ return [2 /*return*/, true];
432
+ });
433
+ });
434
+ };
435
+ PollingClearingHouseAccountSubscriber.prototype.assertIsSubscribed = function () {
436
+ if (!this.isSubscribed) {
437
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
438
+ }
439
+ };
440
+ PollingClearingHouseAccountSubscriber.prototype.getStateAccountAndSlot = function () {
441
+ this.assertIsSubscribed();
442
+ return this.state;
443
+ };
444
+ PollingClearingHouseAccountSubscriber.prototype.getMarketAccountAndSlot = function (marketIndex) {
445
+ return this.perpMarket.get(marketIndex.toNumber());
446
+ };
447
+ PollingClearingHouseAccountSubscriber.prototype.getMarketAccountsAndSlots = function () {
448
+ return Array.from(this.perpMarket.values());
449
+ };
450
+ PollingClearingHouseAccountSubscriber.prototype.getSpotMarketAccountAndSlot = function (marketIndex) {
451
+ return this.spotMarket.get(marketIndex.toNumber());
452
+ };
453
+ PollingClearingHouseAccountSubscriber.prototype.getOraclePriceDataAndSlot = function (oraclePublicKey) {
454
+ this.assertIsSubscribed();
455
+ if (oraclePublicKey.equals(web3_js_1.PublicKey["default"])) {
456
+ return {
457
+ data: quoteAssetOracleClient_1.QUOTE_ORACLE_PRICE_DATA,
458
+ slot: 0
459
+ };
460
+ }
461
+ return this.oracles.get(oraclePublicKey.toString());
462
+ };
463
+ return PollingClearingHouseAccountSubscriber;
464
+ }());
465
+ exports.PollingClearingHouseAccountSubscriber = PollingClearingHouseAccountSubscriber;
@@ -10,14 +10,14 @@ import { BN, Program } from '@project-serum/anchor';
10
10
  import StrictEventEmitter from 'strict-event-emitter-types';
11
11
  import { EventEmitter } from 'events';
12
12
  import {
13
- BankAccount,
14
- MarketAccount,
13
+ SpotMarketAccount,
14
+ PerpMarketAccount,
15
15
  StateAccount,
16
16
  UserAccount,
17
17
  } from '../types';
18
18
  import {
19
19
  getClearingHouseStateAccountPublicKey,
20
- getBankPublicKey,
20
+ getSpotMarketPublicKey,
21
21
  getMarketPublicKey,
22
22
  } from '../addresses/pda';
23
23
  import { BulkAccountLoader } from './bulkAccountLoader';
@@ -32,8 +32,8 @@ export class PollingClearingHouseAccountSubscriber
32
32
  {
33
33
  isSubscribed: boolean;
34
34
  program: Program;
35
- marketIndexes: BN[];
36
- bankIndexes: BN[];
35
+ perpMarketIndexes: BN[];
36
+ spotMarketIndexes: BN[];
37
37
  oracleInfos: OracleInfo[];
38
38
  oracleClientCache = new OracleClientCache();
39
39
 
@@ -45,8 +45,8 @@ export class PollingClearingHouseAccountSubscriber
45
45
  errorCallbackId?: string;
46
46
 
47
47
  state?: DataAndSlot<StateAccount>;
48
- market = new Map<number, DataAndSlot<MarketAccount>>();
49
- bank = new Map<number, DataAndSlot<BankAccount>>();
48
+ perpMarket = new Map<number, DataAndSlot<PerpMarketAccount>>();
49
+ spotMarket = new Map<number, DataAndSlot<SpotMarketAccount>>();
50
50
  oracles = new Map<string, DataAndSlot<OraclePriceData>>();
51
51
  user?: DataAndSlot<UserAccount>;
52
52
 
@@ -57,16 +57,16 @@ export class PollingClearingHouseAccountSubscriber
57
57
  public constructor(
58
58
  program: Program,
59
59
  accountLoader: BulkAccountLoader,
60
- marketIndexes: BN[],
61
- bankIndexes: BN[],
60
+ perpMarketIndexes: BN[],
61
+ spotMarketIndexes: BN[],
62
62
  oracleInfos: OracleInfo[]
63
63
  ) {
64
64
  this.isSubscribed = false;
65
65
  this.program = program;
66
66
  this.eventEmitter = new EventEmitter();
67
67
  this.accountLoader = accountLoader;
68
- this.marketIndexes = marketIndexes;
69
- this.bankIndexes = bankIndexes;
68
+ this.perpMarketIndexes = perpMarketIndexes;
69
+ this.spotMarketIndexes = spotMarketIndexes;
70
70
  this.oracleInfos = oracleInfos;
71
71
  }
72
72
 
@@ -122,11 +122,11 @@ export class PollingClearingHouseAccountSubscriber
122
122
  });
123
123
 
124
124
  await this.updateMarketAccountsToPoll();
125
- await this.updateBankAccountsToPoll();
125
+ await this.updateSpotMarketAccountsToPoll();
126
126
  }
127
127
 
128
128
  async updateMarketAccountsToPoll(): Promise<boolean> {
129
- for (const marketIndex of this.marketIndexes) {
129
+ for (const marketIndex of this.perpMarketIndexes) {
130
130
  await this.addMarketAccountToPoll(marketIndex);
131
131
  }
132
132
  return true;
@@ -139,34 +139,34 @@ export class PollingClearingHouseAccountSubscriber
139
139
  );
140
140
 
141
141
  this.accountsToPoll.set(marketPublicKey.toString(), {
142
- key: 'market',
142
+ key: 'perpMarket',
143
143
  publicKey: marketPublicKey,
144
- eventType: 'marketAccountUpdate',
144
+ eventType: 'perpMarketAccountUpdate',
145
145
  mapKey: marketIndex.toNumber(),
146
146
  });
147
147
 
148
148
  return true;
149
149
  }
150
150
 
151
- async updateBankAccountsToPoll(): Promise<boolean> {
152
- for (const bankIndex of this.bankIndexes) {
153
- await this.addBankAccountToPoll(bankIndex);
151
+ async updateSpotMarketAccountsToPoll(): Promise<boolean> {
152
+ for (const marketIndex of this.spotMarketIndexes) {
153
+ await this.addSpotMarketAccountToPoll(marketIndex);
154
154
  }
155
155
 
156
156
  return true;
157
157
  }
158
158
 
159
- async addBankAccountToPoll(bankIndex: BN): Promise<boolean> {
160
- const bankPublicKey = await getBankPublicKey(
159
+ async addSpotMarketAccountToPoll(marketIndex: BN): Promise<boolean> {
160
+ const marketPublicKey = await getSpotMarketPublicKey(
161
161
  this.program.programId,
162
- bankIndex
162
+ marketIndex
163
163
  );
164
164
 
165
- this.accountsToPoll.set(bankPublicKey.toString(), {
166
- key: 'bank',
167
- publicKey: bankPublicKey,
168
- eventType: 'bankAccountUpdate',
169
- mapKey: bankIndex.toNumber(),
165
+ this.accountsToPoll.set(marketPublicKey.toString(), {
166
+ key: 'spotMarket',
167
+ publicKey: marketPublicKey,
168
+ eventType: 'spotMarketAccountUpdate',
169
+ mapKey: marketIndex.toNumber(),
170
170
  });
171
171
  return true;
172
172
  }
@@ -353,14 +353,14 @@ export class PollingClearingHouseAccountSubscriber
353
353
  this.isSubscribed = false;
354
354
  }
355
355
 
356
- async addBank(bankIndex: BN): Promise<boolean> {
357
- await this.addBankAccountToPoll(bankIndex);
358
- const accountToPoll = this.accountsToPoll.get(bankIndex.toString());
356
+ async addSpotMarket(marketIndex: BN): Promise<boolean> {
357
+ await this.addSpotMarketAccountToPoll(marketIndex);
358
+ const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
359
359
  this.addAccountToAccountLoader(accountToPoll);
360
360
  return true;
361
361
  }
362
362
 
363
- async addMarket(marketIndex: BN): Promise<boolean> {
363
+ async addPerpMarket(marketIndex: BN): Promise<boolean> {
364
364
  await this.addMarketAccountToPoll(marketIndex);
365
365
  const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
366
366
  this.addAccountToAccountLoader(accountToPoll);
@@ -395,18 +395,18 @@ export class PollingClearingHouseAccountSubscriber
395
395
 
396
396
  public getMarketAccountAndSlot(
397
397
  marketIndex: BN
398
- ): DataAndSlot<MarketAccount> | undefined {
399
- return this.market.get(marketIndex.toNumber());
398
+ ): DataAndSlot<PerpMarketAccount> | undefined {
399
+ return this.perpMarket.get(marketIndex.toNumber());
400
400
  }
401
401
 
402
- public getMarketAccountsAndSlots(): DataAndSlot<MarketAccount>[] {
403
- return Array.from(this.market.values());
402
+ public getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[] {
403
+ return Array.from(this.perpMarket.values());
404
404
  }
405
405
 
406
- public getBankAccountAndSlot(
407
- bankIndex: BN
408
- ): DataAndSlot<BankAccount> | undefined {
409
- return this.bank.get(bankIndex.toNumber());
406
+ public getSpotMarketAccountAndSlot(
407
+ marketIndex: BN
408
+ ): DataAndSlot<SpotMarketAccount> | undefined {
409
+ return this.spotMarket.get(marketIndex.toNumber());
410
410
  }
411
411
 
412
412
  public getOraclePriceDataAndSlot(