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

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 (143) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
  3. package/lib/accounts/types.d.ts +1 -0
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
  6. package/lib/admin.d.ts +5 -2
  7. package/lib/admin.js +30 -4
  8. package/lib/clearingHouse.d.ts +2 -0
  9. package/lib/clearingHouse.js +14 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +182 -106
  12. package/lib/config.js +1 -1
  13. package/lib/dlob/DLOB.d.ts +73 -0
  14. package/lib/dlob/DLOB.js +557 -0
  15. package/lib/dlob/DLOBNode.d.ts +52 -0
  16. package/lib/dlob/DLOBNode.js +82 -0
  17. package/lib/dlob/NodeList.d.ts +26 -0
  18. package/lib/dlob/NodeList.js +138 -0
  19. package/lib/idl/clearing_house.json +122 -202
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +1 -1
  23. package/lib/math/market.js +1 -1
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/spotBalance.d.ts +3 -0
  27. package/lib/math/spotBalance.js +18 -1
  28. package/lib/math/spotPosition.d.ts +5 -1
  29. package/lib/math/spotPosition.js +16 -1
  30. package/lib/types.d.ts +24 -23
  31. package/lib/types.js +9 -3
  32. package/lib/userMap/userMap.d.ts +25 -0
  33. package/lib/userMap/userMap.js +73 -0
  34. package/lib/userMap/userStatsMap.d.ts +19 -0
  35. package/lib/userMap/userStatsMap.js +68 -0
  36. package/package.json +6 -3
  37. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  38. package/src/accounts/types.ts +1 -0
  39. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  40. package/src/addresses/marketAddresses.js +26 -0
  41. package/src/admin.ts +50 -8
  42. package/src/assert/assert.js +1 -1
  43. package/src/clearingHouse.ts +23 -1
  44. package/src/clearingHouseUser.ts +364 -165
  45. package/src/config.ts +1 -1
  46. package/src/constants/banks.js +42 -0
  47. package/src/constants/{perpMarkets.js → markets.js} +11 -11
  48. package/src/dlob/DLOB.ts +884 -0
  49. package/src/dlob/DLOBNode.ts +163 -0
  50. package/src/dlob/NodeList.ts +185 -0
  51. package/src/events/eventList.js +23 -66
  52. package/src/events/txEventCache.js +19 -22
  53. package/src/examples/makeTradeExample.js +157 -0
  54. package/src/factory/bigNum.js +180 -183
  55. package/src/factory/oracleClient.js +9 -9
  56. package/src/idl/clearing_house.json +122 -202
  57. package/src/index.ts +5 -0
  58. package/src/math/auction.js +10 -10
  59. package/src/math/conversion.js +3 -4
  60. package/src/math/funding.js +175 -223
  61. package/src/math/market.ts +1 -1
  62. package/src/math/orders.ts +29 -21
  63. package/src/math/repeg.js +40 -40
  64. package/src/math/spotBalance.ts +26 -0
  65. package/src/math/spotPosition.ts +42 -1
  66. package/src/math/trade.js +74 -81
  67. package/src/math/utils.js +7 -8
  68. package/src/math/utils.js.map +1 -0
  69. package/src/oracles/oracleClientCache.js +9 -10
  70. package/src/oracles/pythClient.js +17 -52
  71. package/src/oracles/quoteAssetOracleClient.js +13 -44
  72. package/src/oracles/switchboardClient.js +37 -69
  73. package/src/oracles/types.js +1 -1
  74. package/src/token/index.js +4 -4
  75. package/src/tx/types.js +1 -1
  76. package/src/tx/utils.js +6 -7
  77. package/src/types.ts +25 -23
  78. package/src/userMap/userMap.ts +100 -0
  79. package/src/userMap/userStatsMap.ts +110 -0
  80. package/src/userName.js +5 -5
  81. package/src/util/computeUnits.js +11 -46
  82. package/src/util/getTokenAddress.js +9 -0
  83. package/src/util/promiseTimeout.js +5 -5
  84. package/src/util/tps.js +12 -46
  85. package/src/wallet.js +18 -55
  86. package/tests/bn/test.ts +2 -3
  87. package/tests/dlob/helpers.ts +322 -0
  88. package/tests/dlob/test.ts +2865 -0
  89. package/my-script/.env +0 -7
  90. package/my-script/getUserStats.ts +0 -106
  91. package/my-script/multiConnections.ts +0 -119
  92. package/my-script/test-regex.ts +0 -11
  93. package/my-script/utils.ts +0 -52
  94. package/src/accounts/bulkAccountLoader.js +0 -249
  95. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  96. package/src/accounts/bulkUserSubscription.js +0 -75
  97. package/src/accounts/fetch.js +0 -92
  98. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  99. package/src/accounts/pollingOracleSubscriber.js +0 -156
  100. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  101. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  102. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  103. package/src/accounts/types.js +0 -28
  104. package/src/accounts/utils.js +0 -7
  105. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  106. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  107. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  108. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  109. package/src/addresses/pda.js +0 -186
  110. package/src/admin.js +0 -1284
  111. package/src/clearingHouse.js +0 -3433
  112. package/src/clearingHouseConfig.js +0 -2
  113. package/src/clearingHouseUser.js +0 -874
  114. package/src/clearingHouseUserConfig.js +0 -2
  115. package/src/clearingHouseUserStats.js +0 -115
  116. package/src/clearingHouseUserStatsConfig.js +0 -2
  117. package/src/config.js +0 -80
  118. package/src/constants/numericConstants.js +0 -48
  119. package/src/constants/spotMarkets.js +0 -51
  120. package/src/events/eventSubscriber.js +0 -202
  121. package/src/events/fetchLogs.js +0 -117
  122. package/src/events/pollingLogProvider.js +0 -113
  123. package/src/events/sort.js +0 -41
  124. package/src/events/types.js +0 -25
  125. package/src/events/webSocketLogProvider.js +0 -76
  126. package/src/index.js +0 -75
  127. package/src/math/amm.js +0 -422
  128. package/src/math/insurance.js +0 -27
  129. package/src/math/margin.js +0 -77
  130. package/src/math/market.js +0 -105
  131. package/src/math/oracles.js +0 -56
  132. package/src/math/orders.js +0 -153
  133. package/src/math/position.js +0 -172
  134. package/src/math/spotBalance.js +0 -176
  135. package/src/math/spotMarket.js +0 -8
  136. package/src/math/spotPosition.js +0 -8
  137. package/src/orderParams.js +0 -28
  138. package/src/serum/serumSubscriber.js +0 -102
  139. package/src/serum/types.js +0 -2
  140. package/src/slot/SlotSubscriber.js +0 -86
  141. package/src/tokenFaucet.js +0 -323
  142. package/src/tx/retryTxSender.js +0 -280
  143. package/src/types.js +0 -216
@@ -1,75 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- 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.bulkPollingUserSubscribe = void 0;
40
- /**
41
- * @param users
42
- * @param accountLoader
43
- */
44
- function bulkPollingUserSubscribe(users, accountLoader) {
45
- return __awaiter(this, void 0, void 0, function () {
46
- var _this = this;
47
- return __generator(this, function (_a) {
48
- switch (_a.label) {
49
- case 0:
50
- if (!(users.length === 0)) return [3 /*break*/, 2];
51
- return [4 /*yield*/, accountLoader.load()];
52
- case 1:
53
- _a.sent();
54
- return [2 /*return*/];
55
- case 2: return [4 /*yield*/, Promise.all(users.map(function (user) {
56
- return user.accountSubscriber.addToAccountLoader();
57
- }))];
58
- case 3:
59
- _a.sent();
60
- return [4 /*yield*/, accountLoader.load()];
61
- case 4:
62
- _a.sent();
63
- return [4 /*yield*/, Promise.all(users.map(function (user) { return __awaiter(_this, void 0, void 0, function () {
64
- return __generator(this, function (_a) {
65
- return [2 /*return*/, user.subscribe()];
66
- });
67
- }); }))];
68
- case 5:
69
- _a.sent();
70
- return [2 /*return*/];
71
- }
72
- });
73
- });
74
- }
75
- exports.bulkPollingUserSubscribe = bulkPollingUserSubscribe;
@@ -1,92 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- 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.fetchUserStatsAccount = exports.fetchUserAccounts = void 0;
40
- var pda_1 = require("../addresses/pda");
41
- function fetchUserAccounts(connection, program, authority, limit) {
42
- if (limit === void 0) { limit = 8; }
43
- return __awaiter(this, void 0, void 0, function () {
44
- var userAccountPublicKeys, i, _a, _b, accountInfos;
45
- return __generator(this, function (_c) {
46
- switch (_c.label) {
47
- case 0:
48
- userAccountPublicKeys = new Array();
49
- i = 0;
50
- _c.label = 1;
51
- case 1:
52
- if (!(i < limit)) return [3 /*break*/, 4];
53
- _b = (_a = userAccountPublicKeys).push;
54
- return [4 /*yield*/, (0, pda_1.getUserAccountPublicKey)(program.programId, authority, i)];
55
- case 2:
56
- _b.apply(_a, [_c.sent()]);
57
- _c.label = 3;
58
- case 3:
59
- i++;
60
- return [3 /*break*/, 1];
61
- case 4: return [4 /*yield*/, connection.getMultipleAccountsInfo(userAccountPublicKeys, 'confirmed')];
62
- case 5:
63
- accountInfos = _c.sent();
64
- return [2 /*return*/, accountInfos.map(function (accountInfo) {
65
- if (!accountInfo) {
66
- return undefined;
67
- }
68
- return program.account.user.coder.accounts.decode('User', accountInfo.data);
69
- })];
70
- }
71
- });
72
- });
73
- }
74
- exports.fetchUserAccounts = fetchUserAccounts;
75
- function fetchUserStatsAccount(connection, program, authority) {
76
- return __awaiter(this, void 0, void 0, function () {
77
- var userStatsPublicKey, accountInfo;
78
- return __generator(this, function (_a) {
79
- switch (_a.label) {
80
- case 0:
81
- userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(program.programId, authority);
82
- return [4 /*yield*/, connection.getAccountInfo(userStatsPublicKey, 'confirmed')];
83
- case 1:
84
- accountInfo = _a.sent();
85
- return [2 /*return*/, accountInfo
86
- ? program.account.user.coder.accounts.decode('UserStats', accountInfo.data)
87
- : undefined];
88
- }
89
- });
90
- });
91
- }
92
- exports.fetchUserStatsAccount = fetchUserStatsAccount;
@@ -1,465 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- 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;