@drift-labs/sdk 0.2.0-master.24 → 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 (193) 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 +8 -6
  7. package/lib/addresses/pda.js +35 -27
  8. package/lib/admin.d.ts +9 -6
  9. package/lib/admin.js +90 -54
  10. package/lib/clearingHouse.d.ts +71 -42
  11. package/lib/clearingHouse.js +765 -282
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +19 -17
  14. package/lib/clearingHouseUser.js +145 -123
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +21 -21
  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/eventSubscriber.d.ts +4 -2
  24. package/lib/events/eventSubscriber.js +16 -9
  25. package/lib/events/fetchLogs.d.ts +10 -1
  26. package/lib/events/fetchLogs.js +27 -7
  27. package/lib/events/pollingLogProvider.d.ts +2 -1
  28. package/lib/events/pollingLogProvider.js +6 -2
  29. package/lib/events/types.d.ts +4 -2
  30. package/lib/events/types.js +2 -0
  31. package/lib/examples/makeTradeExample.js +18 -6
  32. package/lib/idl/clearing_house.json +1128 -347
  33. package/lib/index.d.ts +6 -3
  34. package/lib/index.js +6 -3
  35. package/lib/math/amm.d.ts +5 -3
  36. package/lib/math/amm.js +42 -4
  37. package/lib/math/funding.d.ts +6 -6
  38. package/lib/math/funding.js +2 -1
  39. package/lib/math/margin.d.ts +4 -4
  40. package/lib/math/margin.js +18 -11
  41. package/lib/math/market.d.ts +10 -9
  42. package/lib/math/market.js +29 -6
  43. package/lib/math/oracles.d.ts +2 -1
  44. package/lib/math/oracles.js +11 -1
  45. package/lib/math/orders.d.ts +5 -5
  46. package/lib/math/position.d.ts +13 -13
  47. package/lib/math/position.js +19 -19
  48. package/lib/math/spotBalance.d.ts +19 -0
  49. package/lib/math/spotBalance.js +176 -0
  50. package/lib/math/spotMarket.d.ts +4 -0
  51. package/lib/math/spotMarket.js +8 -0
  52. package/lib/math/spotPosition.d.ts +2 -0
  53. package/lib/math/spotPosition.js +8 -0
  54. package/lib/math/state.js +2 -2
  55. package/lib/math/trade.d.ts +4 -4
  56. package/lib/orderParams.d.ts +4 -4
  57. package/lib/orderParams.js +12 -4
  58. package/lib/serum/serumSubscriber.d.ts +23 -0
  59. package/lib/serum/serumSubscriber.js +41 -0
  60. package/lib/serum/types.d.ts +11 -0
  61. package/lib/serum/types.js +2 -0
  62. package/lib/tx/retryTxSender.d.ts +1 -1
  63. package/lib/tx/retryTxSender.js +4 -2
  64. package/lib/tx/types.d.ts +1 -1
  65. package/lib/types.d.ts +148 -39
  66. package/lib/types.js +37 -9
  67. package/my-script/.env +7 -0
  68. package/my-script/getUserStats.ts +106 -0
  69. package/my-script/multiConnections.ts +119 -0
  70. package/my-script/test-regex.ts +11 -0
  71. package/my-script/utils.ts +52 -0
  72. package/package.json +1 -1
  73. package/src/accounts/bulkAccountLoader.js +249 -0
  74. package/src/accounts/bulkUserStatsSubscription.js +75 -0
  75. package/src/accounts/bulkUserSubscription.js +75 -0
  76. package/src/accounts/fetch.js +92 -0
  77. package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
  78. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
  79. package/src/accounts/pollingOracleSubscriber.js +156 -0
  80. package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
  81. package/src/accounts/pollingUserAccountSubscriber.js +208 -0
  82. package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
  83. package/src/accounts/types.js +28 -0
  84. package/src/accounts/types.ts +11 -9
  85. package/src/accounts/utils.js +7 -0
  86. package/src/accounts/webSocketAccountSubscriber.js +138 -0
  87. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
  88. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
  89. package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
  90. package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
  91. package/src/addresses/pda.js +186 -0
  92. package/src/addresses/pda.ts +56 -42
  93. package/src/admin.js +1284 -0
  94. package/src/admin.ts +149 -75
  95. package/src/assert/assert.js +1 -1
  96. package/src/clearingHouse.js +3433 -0
  97. package/src/clearingHouse.ts +1097 -380
  98. package/src/clearingHouseConfig.js +2 -0
  99. package/src/clearingHouseConfig.ts +2 -2
  100. package/src/clearingHouseUser.js +874 -0
  101. package/src/clearingHouseUser.ts +237 -172
  102. package/src/clearingHouseUserConfig.js +2 -0
  103. package/src/clearingHouseUserStats.js +115 -0
  104. package/src/clearingHouseUserStatsConfig.js +2 -0
  105. package/src/config.js +80 -0
  106. package/src/config.ts +30 -30
  107. package/src/constants/numericConstants.js +18 -11
  108. package/src/constants/numericConstants.ts +17 -15
  109. package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
  110. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  111. package/src/constants/spotMarkets.js +51 -0
  112. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  113. package/src/events/eventList.js +66 -23
  114. package/src/events/eventSubscriber.js +202 -0
  115. package/src/events/eventSubscriber.ts +20 -12
  116. package/src/events/fetchLogs.js +117 -0
  117. package/src/events/fetchLogs.ts +35 -8
  118. package/src/events/pollingLogProvider.js +113 -0
  119. package/src/events/pollingLogProvider.ts +10 -2
  120. package/src/events/sort.js +41 -0
  121. package/src/events/txEventCache.js +22 -19
  122. package/src/events/types.js +25 -0
  123. package/src/events/types.ts +7 -1
  124. package/src/events/webSocketLogProvider.js +76 -0
  125. package/src/examples/makeTradeExample.ts +27 -6
  126. package/src/factory/bigNum.js +183 -180
  127. package/src/factory/oracleClient.js +9 -9
  128. package/src/idl/clearing_house.json +1128 -347
  129. package/src/index.js +75 -0
  130. package/src/index.ts +6 -3
  131. package/src/math/amm.js +422 -0
  132. package/src/math/amm.ts +73 -5
  133. package/src/math/auction.js +10 -10
  134. package/src/math/conversion.js +4 -3
  135. package/src/math/funding.js +223 -175
  136. package/src/math/funding.ts +7 -7
  137. package/src/math/insurance.js +27 -0
  138. package/src/math/margin.js +77 -0
  139. package/src/math/margin.ts +34 -23
  140. package/src/math/market.js +105 -0
  141. package/src/math/market.ts +71 -19
  142. package/src/math/oracles.js +40 -10
  143. package/src/math/oracles.ts +18 -1
  144. package/src/math/orders.js +153 -0
  145. package/src/math/orders.ts +5 -5
  146. package/src/math/position.js +172 -0
  147. package/src/math/position.ts +31 -31
  148. package/src/math/repeg.js +40 -40
  149. package/src/math/spotBalance.js +176 -0
  150. package/src/math/spotBalance.ts +290 -0
  151. package/src/math/spotMarket.js +8 -0
  152. package/src/math/spotMarket.ts +9 -0
  153. package/src/math/spotPosition.js +8 -0
  154. package/src/math/spotPosition.ts +6 -0
  155. package/src/math/state.ts +2 -2
  156. package/src/math/trade.js +81 -74
  157. package/src/math/trade.ts +4 -4
  158. package/src/math/utils.js +8 -7
  159. package/src/oracles/oracleClientCache.js +10 -9
  160. package/src/oracles/pythClient.js +52 -17
  161. package/src/oracles/quoteAssetOracleClient.js +44 -13
  162. package/src/oracles/switchboardClient.js +69 -37
  163. package/src/oracles/types.js +1 -1
  164. package/src/orderParams.js +14 -6
  165. package/src/orderParams.ts +16 -8
  166. package/src/serum/serumSubscriber.js +102 -0
  167. package/src/serum/serumSubscriber.ts +80 -0
  168. package/src/serum/types.js +2 -0
  169. package/src/serum/types.ts +13 -0
  170. package/src/slot/SlotSubscriber.js +67 -20
  171. package/src/token/index.js +4 -4
  172. package/src/tokenFaucet.js +288 -154
  173. package/src/tx/retryTxSender.js +280 -0
  174. package/src/tx/retryTxSender.ts +5 -2
  175. package/src/tx/types.js +1 -1
  176. package/src/tx/types.ts +2 -1
  177. package/src/tx/utils.js +7 -6
  178. package/src/types.js +216 -0
  179. package/src/types.ts +131 -39
  180. package/src/userName.js +5 -5
  181. package/src/util/computeUnits.js +46 -11
  182. package/src/util/promiseTimeout.js +5 -5
  183. package/src/util/tps.js +46 -12
  184. package/src/wallet.js +55 -18
  185. package/lib/math/bankBalance.d.ts +0 -15
  186. package/lib/math/bankBalance.js +0 -150
  187. package/src/addresses/marketAddresses.js +0 -26
  188. package/src/constants/banks.js +0 -42
  189. package/src/examples/makeTradeExample.js +0 -80
  190. package/src/math/bankBalance.ts +0 -258
  191. package/src/math/state.js +0 -15
  192. package/src/math/utils.js.map +0 -1
  193. package/src/util/getTokenAddress.js +0 -9
@@ -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(
@@ -0,0 +1,156 @@
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.PollingOracleSubscriber = void 0;
40
+ var types_1 = require("./types");
41
+ var events_1 = require("events");
42
+ var PollingOracleSubscriber = /** @class */ (function () {
43
+ function PollingOracleSubscriber(publicKey, oracleClient, accountLoader) {
44
+ this.isSubscribed = false;
45
+ this.publicKey = publicKey;
46
+ this.oracleClient = oracleClient;
47
+ this.accountLoader = accountLoader;
48
+ this.eventEmitter = new events_1.EventEmitter();
49
+ }
50
+ PollingOracleSubscriber.prototype.subscribe = function () {
51
+ return __awaiter(this, void 0, void 0, function () {
52
+ var subscriptionSucceeded, retries;
53
+ return __generator(this, function (_a) {
54
+ switch (_a.label) {
55
+ case 0:
56
+ if (this.isSubscribed) {
57
+ return [2 /*return*/, true];
58
+ }
59
+ this.addToAccountLoader();
60
+ subscriptionSucceeded = false;
61
+ retries = 0;
62
+ _a.label = 1;
63
+ case 1:
64
+ if (!(!subscriptionSucceeded && retries < 5)) return [3 /*break*/, 3];
65
+ return [4 /*yield*/, this.fetch()];
66
+ case 2:
67
+ _a.sent();
68
+ subscriptionSucceeded = this.didSubscriptionSucceed();
69
+ retries++;
70
+ return [3 /*break*/, 1];
71
+ case 3:
72
+ if (subscriptionSucceeded) {
73
+ this.eventEmitter.emit('update');
74
+ }
75
+ this.isSubscribed = subscriptionSucceeded;
76
+ return [2 /*return*/, subscriptionSucceeded];
77
+ }
78
+ });
79
+ });
80
+ };
81
+ PollingOracleSubscriber.prototype.addToAccountLoader = function () {
82
+ var _this = this;
83
+ if (this.callbackId) {
84
+ return;
85
+ }
86
+ this.callbackId = this.accountLoader.addAccount(this.publicKey, function (buffer, slot) { return __awaiter(_this, void 0, void 0, function () {
87
+ var oraclePriceData;
88
+ return __generator(this, function (_a) {
89
+ switch (_a.label) {
90
+ case 0: return [4 /*yield*/, this.oracleClient.getOraclePriceDataFromBuffer(buffer)];
91
+ case 1:
92
+ oraclePriceData = _a.sent();
93
+ this.oraclePriceData = { data: oraclePriceData, slot: slot };
94
+ // @ts-ignore
95
+ this.eventEmitter.emit('oracleUpdate', oraclePriceData);
96
+ this.eventEmitter.emit('update');
97
+ return [2 /*return*/];
98
+ }
99
+ });
100
+ }); });
101
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks(function (error) {
102
+ _this.eventEmitter.emit('error', error);
103
+ });
104
+ };
105
+ PollingOracleSubscriber.prototype.fetch = function () {
106
+ return __awaiter(this, void 0, void 0, function () {
107
+ var _a, buffer, slot, _b;
108
+ var _c;
109
+ return __generator(this, function (_d) {
110
+ switch (_d.label) {
111
+ case 0: return [4 /*yield*/, this.accountLoader.load()];
112
+ case 1:
113
+ _d.sent();
114
+ _a = this.accountLoader.getBufferAndSlot(this.publicKey), buffer = _a.buffer, slot = _a.slot;
115
+ _b = this;
116
+ _c = {};
117
+ return [4 /*yield*/, this.oracleClient.getOraclePriceDataFromBuffer(buffer)];
118
+ case 2:
119
+ _b.oraclePriceData = (_c.data = _d.sent(),
120
+ _c.slot = slot,
121
+ _c);
122
+ return [2 /*return*/];
123
+ }
124
+ });
125
+ });
126
+ };
127
+ PollingOracleSubscriber.prototype.unsubscribe = function () {
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ return __generator(this, function (_a) {
130
+ if (!this.isSubscribed) {
131
+ return [2 /*return*/];
132
+ }
133
+ this.accountLoader.removeAccount(this.publicKey, this.callbackId);
134
+ this.callbackId = undefined;
135
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
136
+ this.errorCallbackId = undefined;
137
+ this.isSubscribed = false;
138
+ return [2 /*return*/];
139
+ });
140
+ });
141
+ };
142
+ PollingOracleSubscriber.prototype.assertIsSubscribed = function () {
143
+ if (!this.isSubscribed) {
144
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
145
+ }
146
+ };
147
+ PollingOracleSubscriber.prototype.getOraclePriceData = function () {
148
+ this.assertIsSubscribed();
149
+ return this.oraclePriceData;
150
+ };
151
+ PollingOracleSubscriber.prototype.didSubscriptionSucceed = function () {
152
+ return !!this.oraclePriceData;
153
+ };
154
+ return PollingOracleSubscriber;
155
+ }());
156
+ exports.PollingOracleSubscriber = PollingOracleSubscriber;
@@ -0,0 +1,141 @@
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.PollingTokenAccountSubscriber = void 0;
40
+ var types_1 = require("./types");
41
+ var events_1 = require("events");
42
+ var token_1 = require("../token");
43
+ var PollingTokenAccountSubscriber = /** @class */ (function () {
44
+ function PollingTokenAccountSubscriber(publicKey, accountLoader) {
45
+ this.isSubscribed = false;
46
+ this.publicKey = publicKey;
47
+ this.accountLoader = accountLoader;
48
+ this.eventEmitter = new events_1.EventEmitter();
49
+ }
50
+ PollingTokenAccountSubscriber.prototype.subscribe = function () {
51
+ return __awaiter(this, void 0, void 0, function () {
52
+ var subscriptionSucceeded, retries;
53
+ return __generator(this, function (_a) {
54
+ switch (_a.label) {
55
+ case 0:
56
+ if (this.isSubscribed) {
57
+ return [2 /*return*/, true];
58
+ }
59
+ this.addToAccountLoader();
60
+ subscriptionSucceeded = false;
61
+ retries = 0;
62
+ _a.label = 1;
63
+ case 1:
64
+ if (!(!subscriptionSucceeded && retries < 5)) return [3 /*break*/, 3];
65
+ return [4 /*yield*/, this.fetch()];
66
+ case 2:
67
+ _a.sent();
68
+ subscriptionSucceeded = this.didSubscriptionSucceed();
69
+ retries++;
70
+ return [3 /*break*/, 1];
71
+ case 3:
72
+ if (subscriptionSucceeded) {
73
+ this.eventEmitter.emit('update');
74
+ }
75
+ this.isSubscribed = subscriptionSucceeded;
76
+ return [2 /*return*/, subscriptionSucceeded];
77
+ }
78
+ });
79
+ });
80
+ };
81
+ PollingTokenAccountSubscriber.prototype.addToAccountLoader = function () {
82
+ var _this = this;
83
+ if (this.callbackId) {
84
+ return;
85
+ }
86
+ this.callbackId = this.accountLoader.addAccount(this.publicKey, function (buffer, slot) {
87
+ var tokenAccount = (0, token_1.parseTokenAccount)(buffer);
88
+ _this.tokenAccountAndSlot = { data: tokenAccount, slot: slot };
89
+ // @ts-ignore
90
+ _this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
91
+ _this.eventEmitter.emit('update');
92
+ });
93
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks(function (error) {
94
+ _this.eventEmitter.emit('error', error);
95
+ });
96
+ };
97
+ PollingTokenAccountSubscriber.prototype.fetch = function () {
98
+ return __awaiter(this, void 0, void 0, function () {
99
+ var _a, buffer, slot;
100
+ return __generator(this, function (_b) {
101
+ switch (_b.label) {
102
+ case 0: return [4 /*yield*/, this.accountLoader.load()];
103
+ case 1:
104
+ _b.sent();
105
+ _a = this.accountLoader.getBufferAndSlot(this.publicKey), buffer = _a.buffer, slot = _a.slot;
106
+ this.tokenAccountAndSlot = { data: (0, token_1.parseTokenAccount)(buffer), slot: slot };
107
+ return [2 /*return*/];
108
+ }
109
+ });
110
+ });
111
+ };
112
+ PollingTokenAccountSubscriber.prototype.unsubscribe = function () {
113
+ return __awaiter(this, void 0, void 0, function () {
114
+ return __generator(this, function (_a) {
115
+ if (!this.isSubscribed) {
116
+ return [2 /*return*/];
117
+ }
118
+ this.accountLoader.removeAccount(this.publicKey, this.callbackId);
119
+ this.callbackId = undefined;
120
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
121
+ this.errorCallbackId = undefined;
122
+ this.isSubscribed = false;
123
+ return [2 /*return*/];
124
+ });
125
+ });
126
+ };
127
+ PollingTokenAccountSubscriber.prototype.assertIsSubscribed = function () {
128
+ if (!this.isSubscribed) {
129
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
130
+ }
131
+ };
132
+ PollingTokenAccountSubscriber.prototype.getTokenAccountAndSlot = function () {
133
+ this.assertIsSubscribed();
134
+ return this.tokenAccountAndSlot;
135
+ };
136
+ PollingTokenAccountSubscriber.prototype.didSubscriptionSucceed = function () {
137
+ return !!this.tokenAccountAndSlot;
138
+ };
139
+ return PollingTokenAccountSubscriber;
140
+ }());
141
+ exports.PollingTokenAccountSubscriber = PollingTokenAccountSubscriber;
@@ -0,0 +1,208 @@
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.PollingUserAccountSubscriber = void 0;
40
+ var types_1 = require("./types");
41
+ var events_1 = require("events");
42
+ var utils_1 = require("./utils");
43
+ var PollingUserAccountSubscriber = /** @class */ (function () {
44
+ function PollingUserAccountSubscriber(program, userAccountPublicKey, accountLoader) {
45
+ this.accountsToPoll = new Map();
46
+ this.isSubscribed = false;
47
+ this.program = program;
48
+ this.accountLoader = accountLoader;
49
+ this.eventEmitter = new events_1.EventEmitter();
50
+ this.userAccountPublicKey = userAccountPublicKey;
51
+ }
52
+ PollingUserAccountSubscriber.prototype.subscribe = function () {
53
+ return __awaiter(this, void 0, void 0, function () {
54
+ var subscriptionSucceeded, retries;
55
+ return __generator(this, function (_a) {
56
+ switch (_a.label) {
57
+ case 0:
58
+ if (this.isSubscribed) {
59
+ return [2 /*return*/, true];
60
+ }
61
+ return [4 /*yield*/, this.addToAccountLoader()];
62
+ case 1:
63
+ _a.sent();
64
+ subscriptionSucceeded = false;
65
+ retries = 0;
66
+ _a.label = 2;
67
+ case 2:
68
+ if (!(!subscriptionSucceeded && retries < 5)) return [3 /*break*/, 4];
69
+ return [4 /*yield*/, this.fetchIfUnloaded()];
70
+ case 3:
71
+ _a.sent();
72
+ subscriptionSucceeded = this.didSubscriptionSucceed();
73
+ retries++;
74
+ return [3 /*break*/, 2];
75
+ case 4:
76
+ if (subscriptionSucceeded) {
77
+ this.eventEmitter.emit('update');
78
+ }
79
+ this.isSubscribed = subscriptionSucceeded;
80
+ return [2 /*return*/, subscriptionSucceeded];
81
+ }
82
+ });
83
+ });
84
+ };
85
+ PollingUserAccountSubscriber.prototype.addToAccountLoader = function () {
86
+ return __awaiter(this, void 0, void 0, function () {
87
+ var _loop_1, this_1, _i, _a, _b, _1, accountToPoll;
88
+ var _this = this;
89
+ return __generator(this, function (_c) {
90
+ if (this.accountsToPoll.size > 0) {
91
+ return [2 /*return*/];
92
+ }
93
+ this.accountsToPoll.set(this.userAccountPublicKey.toString(), {
94
+ key: 'user',
95
+ publicKey: this.userAccountPublicKey,
96
+ eventType: 'userAccountUpdate'
97
+ });
98
+ _loop_1 = function (_1, accountToPoll) {
99
+ accountToPoll.callbackId = this_1.accountLoader.addAccount(accountToPoll.publicKey, function (buffer, slot) {
100
+ if (!buffer) {
101
+ return;
102
+ }
103
+ var account = _this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
104
+ _this[accountToPoll.key] = { data: account, slot: slot };
105
+ // @ts-ignore
106
+ _this.eventEmitter.emit(accountToPoll.eventType, account);
107
+ _this.eventEmitter.emit('update');
108
+ });
109
+ };
110
+ this_1 = this;
111
+ for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
112
+ _b = _a[_i], _1 = _b[0], accountToPoll = _b[1];
113
+ _loop_1(_1, accountToPoll);
114
+ }
115
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks(function (error) {
116
+ _this.eventEmitter.emit('error', error);
117
+ });
118
+ return [2 /*return*/];
119
+ });
120
+ });
121
+ };
122
+ PollingUserAccountSubscriber.prototype.fetchIfUnloaded = function () {
123
+ return __awaiter(this, void 0, void 0, function () {
124
+ var shouldFetch, _i, _a, _b, _2, accountToPoll;
125
+ return __generator(this, function (_c) {
126
+ switch (_c.label) {
127
+ case 0:
128
+ shouldFetch = false;
129
+ for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
130
+ _b = _a[_i], _2 = _b[0], accountToPoll = _b[1];
131
+ if (this[accountToPoll.key] === undefined) {
132
+ shouldFetch = true;
133
+ break;
134
+ }
135
+ }
136
+ if (!shouldFetch) return [3 /*break*/, 2];
137
+ return [4 /*yield*/, this.fetch()];
138
+ case 1:
139
+ _c.sent();
140
+ _c.label = 2;
141
+ case 2: return [2 /*return*/];
142
+ }
143
+ });
144
+ });
145
+ };
146
+ PollingUserAccountSubscriber.prototype.fetch = function () {
147
+ return __awaiter(this, void 0, void 0, function () {
148
+ var _i, _a, _b, _3, accountToPoll, _c, buffer, slot, account;
149
+ return __generator(this, function (_d) {
150
+ switch (_d.label) {
151
+ case 0: return [4 /*yield*/, this.accountLoader.load()];
152
+ case 1:
153
+ _d.sent();
154
+ for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
155
+ _b = _a[_i], _3 = _b[0], accountToPoll = _b[1];
156
+ _c = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey), buffer = _c.buffer, slot = _c.slot;
157
+ if (buffer) {
158
+ account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
159
+ this[accountToPoll.key] = { data: account, slot: slot };
160
+ }
161
+ }
162
+ return [2 /*return*/];
163
+ }
164
+ });
165
+ });
166
+ };
167
+ PollingUserAccountSubscriber.prototype.didSubscriptionSucceed = function () {
168
+ var success = true;
169
+ for (var _i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
170
+ var _b = _a[_i], _4 = _b[0], accountToPoll = _b[1];
171
+ if (!this[accountToPoll.key]) {
172
+ success = false;
173
+ break;
174
+ }
175
+ }
176
+ return success;
177
+ };
178
+ PollingUserAccountSubscriber.prototype.unsubscribe = function () {
179
+ return __awaiter(this, void 0, void 0, function () {
180
+ var _i, _a, _b, _5, accountToPoll;
181
+ return __generator(this, function (_c) {
182
+ if (!this.isSubscribed) {
183
+ return [2 /*return*/];
184
+ }
185
+ for (_i = 0, _a = this.accountsToPoll; _i < _a.length; _i++) {
186
+ _b = _a[_i], _5 = _b[0], accountToPoll = _b[1];
187
+ this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
188
+ }
189
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
190
+ this.errorCallbackId = undefined;
191
+ this.accountsToPoll.clear();
192
+ this.isSubscribed = false;
193
+ return [2 /*return*/];
194
+ });
195
+ });
196
+ };
197
+ PollingUserAccountSubscriber.prototype.assertIsSubscribed = function () {
198
+ if (!this.isSubscribed) {
199
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
200
+ }
201
+ };
202
+ PollingUserAccountSubscriber.prototype.getUserAccountAndSlot = function () {
203
+ this.assertIsSubscribed();
204
+ return this.user;
205
+ };
206
+ return PollingUserAccountSubscriber;
207
+ }());
208
+ exports.PollingUserAccountSubscriber = PollingUserAccountSubscriber;