@drift-labs/sdk 0.2.0-temp.1 → 2.0.0

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 (274) hide show
  1. package/README.md +27 -29
  2. package/lib/accounts/bulkAccountLoader.d.ts +2 -0
  3. package/lib/accounts/bulkAccountLoader.js +36 -29
  4. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  5. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  6. package/lib/accounts/bulkUserSubscription.d.ts +2 -2
  7. package/lib/accounts/bulkUserSubscription.js +0 -1
  8. package/lib/accounts/fetch.d.ts +2 -1
  9. package/lib/accounts/fetch.js +9 -1
  10. package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
  11. package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
  12. package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
  13. package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
  14. package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
  15. package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
  16. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  17. package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
  18. package/lib/accounts/types.d.ts +26 -15
  19. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
  20. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
  21. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  22. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  23. package/lib/addresses/marketAddresses.d.ts +1 -3
  24. package/lib/addresses/marketAddresses.js +1 -1
  25. package/lib/addresses/pda.d.ts +15 -9
  26. package/lib/addresses/pda.js +73 -35
  27. package/lib/adminClient.d.ts +65 -0
  28. package/lib/adminClient.js +637 -0
  29. package/lib/config.d.ts +10 -10
  30. package/lib/config.js +26 -22
  31. package/lib/constants/numericConstants.d.ts +29 -12
  32. package/lib/constants/numericConstants.js +40 -21
  33. package/lib/constants/perpMarkets.d.ts +18 -0
  34. package/lib/constants/{markets.js → perpMarkets.js} +20 -9
  35. package/lib/constants/spotMarkets.d.ts +19 -0
  36. package/lib/constants/spotMarkets.js +63 -0
  37. package/lib/dlob/DLOB.d.ts +82 -0
  38. package/lib/dlob/DLOB.js +694 -0
  39. package/lib/dlob/DLOBNode.d.ts +54 -0
  40. package/lib/dlob/DLOBNode.js +77 -0
  41. package/lib/dlob/NodeList.d.ts +27 -0
  42. package/lib/dlob/NodeList.js +144 -0
  43. package/lib/driftClient.d.ts +234 -0
  44. package/lib/driftClient.js +2108 -0
  45. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
  46. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  47. package/lib/events/eventList.js +3 -0
  48. package/lib/events/eventSubscriber.d.ts +5 -2
  49. package/lib/events/eventSubscriber.js +25 -11
  50. package/lib/events/fetchLogs.d.ts +13 -2
  51. package/lib/events/fetchLogs.js +45 -14
  52. package/lib/events/pollingLogProvider.d.ts +2 -1
  53. package/lib/events/pollingLogProvider.js +7 -3
  54. package/lib/events/sort.js +8 -11
  55. package/lib/events/types.d.ts +11 -3
  56. package/lib/events/types.js +8 -0
  57. package/lib/events/webSocketLogProvider.js +1 -1
  58. package/lib/examples/makeTradeExample.js +30 -18
  59. package/lib/factory/bigNum.d.ts +8 -4
  60. package/lib/factory/bigNum.js +109 -19
  61. package/lib/idl/drift.json +8392 -0
  62. package/lib/index.d.ts +29 -12
  63. package/lib/index.js +29 -12
  64. package/lib/math/amm.d.ts +9 -6
  65. package/lib/math/amm.js +91 -38
  66. package/lib/math/conversion.js +1 -1
  67. package/lib/math/exchangeStatus.d.ts +4 -0
  68. package/lib/math/exchangeStatus.js +18 -0
  69. package/lib/math/funding.d.ts +6 -6
  70. package/lib/math/funding.js +23 -21
  71. package/lib/math/insurance.d.ts +4 -0
  72. package/lib/math/insurance.js +27 -0
  73. package/lib/math/margin.d.ts +11 -0
  74. package/lib/math/margin.js +82 -0
  75. package/lib/math/market.d.ts +14 -9
  76. package/lib/math/market.js +70 -10
  77. package/lib/math/oracles.d.ts +4 -0
  78. package/lib/math/oracles.js +36 -8
  79. package/lib/math/orders.d.ts +16 -6
  80. package/lib/math/orders.js +97 -17
  81. package/lib/math/position.d.ts +27 -13
  82. package/lib/math/position.js +91 -37
  83. package/lib/math/repeg.js +17 -8
  84. package/lib/math/spotBalance.d.ts +22 -0
  85. package/lib/math/spotBalance.js +192 -0
  86. package/lib/math/spotMarket.d.ts +4 -0
  87. package/lib/math/spotMarket.js +8 -0
  88. package/lib/math/spotPosition.d.ts +6 -0
  89. package/lib/math/spotPosition.js +23 -0
  90. package/lib/math/trade.d.ts +10 -10
  91. package/lib/math/trade.js +27 -31
  92. package/lib/oracles/pythClient.js +1 -1
  93. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  94. package/lib/oracles/switchboardClient.js +1 -1
  95. package/lib/orderParams.d.ts +4 -4
  96. package/lib/orderParams.js +12 -4
  97. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  98. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  99. package/lib/serum/serumSubscriber.d.ts +27 -0
  100. package/lib/serum/serumSubscriber.js +56 -0
  101. package/lib/serum/types.d.ts +11 -0
  102. package/{src/oracles → lib/serum}/types.js +0 -0
  103. package/lib/tokenFaucet.d.ts +1 -0
  104. package/lib/tokenFaucet.js +23 -12
  105. package/lib/tx/retryTxSender.d.ts +1 -1
  106. package/lib/tx/retryTxSender.js +13 -4
  107. package/lib/tx/types.d.ts +1 -1
  108. package/lib/types.d.ts +631 -222
  109. package/lib/types.js +137 -24
  110. package/lib/user.d.ts +228 -0
  111. package/lib/user.js +959 -0
  112. package/lib/userConfig.d.ts +14 -0
  113. package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
  114. package/lib/userMap/userMap.d.ts +41 -0
  115. package/lib/userMap/userMap.js +85 -0
  116. package/lib/userMap/userStatsMap.d.ts +19 -0
  117. package/lib/userMap/userStatsMap.js +68 -0
  118. package/lib/userName.d.ts +1 -0
  119. package/lib/userName.js +3 -2
  120. package/lib/userStats.d.ts +18 -0
  121. package/lib/userStats.js +49 -0
  122. package/lib/userStatsConfig.d.ts +14 -0
  123. package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
  124. package/lib/util/getTokenAddress.d.ts +2 -0
  125. package/lib/util/getTokenAddress.js +9 -0
  126. package/package.json +12 -5
  127. package/src/accounts/bulkAccountLoader.ts +44 -34
  128. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  129. package/src/accounts/bulkUserSubscription.ts +2 -3
  130. package/src/accounts/fetch.ts +27 -2
  131. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
  132. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  133. package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
  134. package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
  135. package/src/accounts/types.ts +35 -15
  136. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  137. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  138. package/src/addresses/marketAddresses.ts +3 -4
  139. package/src/addresses/pda.ts +105 -33
  140. package/src/adminClient.ts +1207 -0
  141. package/src/config.ts +41 -34
  142. package/src/constants/numericConstants.ts +59 -26
  143. package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
  144. package/src/constants/spotMarkets.ts +83 -0
  145. package/src/dlob/DLOB.ts +1120 -0
  146. package/src/dlob/DLOBNode.ts +155 -0
  147. package/src/dlob/NodeList.ts +195 -0
  148. package/src/driftClient.ts +3594 -0
  149. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  150. package/src/events/eventList.ts +3 -0
  151. package/src/events/eventSubscriber.ts +36 -14
  152. package/src/events/fetchLogs.ts +60 -15
  153. package/src/events/pollingLogProvider.ts +11 -3
  154. package/src/events/sort.ts +11 -15
  155. package/src/events/types.ts +27 -2
  156. package/src/events/webSocketLogProvider.ts +1 -1
  157. package/src/examples/makeTradeExample.js +152 -75
  158. package/src/examples/makeTradeExample.ts +44 -28
  159. package/src/factory/bigNum.ts +150 -22
  160. package/src/idl/drift.json +8392 -0
  161. package/src/idl/pyth.json +98 -2
  162. package/src/index.ts +29 -12
  163. package/src/math/amm.ts +161 -48
  164. package/src/math/conversion.ts +2 -2
  165. package/src/math/exchangeStatus.ts +31 -0
  166. package/src/math/funding.ts +41 -31
  167. package/src/math/insurance.ts +35 -0
  168. package/src/math/margin.ts +133 -0
  169. package/src/math/market.ts +143 -14
  170. package/src/math/oracles.ts +63 -9
  171. package/src/math/orders.ts +168 -26
  172. package/src/math/position.ts +133 -59
  173. package/src/math/repeg.ts +19 -9
  174. package/src/math/spotBalance.ts +319 -0
  175. package/src/math/spotMarket.ts +9 -0
  176. package/src/math/spotPosition.ts +47 -0
  177. package/src/math/trade.ts +33 -37
  178. package/src/oracles/pythClient.ts +2 -2
  179. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  180. package/src/oracles/switchboardClient.ts +2 -2
  181. package/src/orderParams.ts +16 -8
  182. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  183. package/src/serum/serumSubscriber.ts +99 -0
  184. package/src/serum/types.ts +13 -0
  185. package/src/tokenFaucet.ts +38 -15
  186. package/src/tx/retryTxSender.ts +16 -5
  187. package/src/tx/types.ts +2 -1
  188. package/src/types.ts +594 -195
  189. package/src/user.ts +1599 -0
  190. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  191. package/src/userMap/userMap.ts +124 -0
  192. package/src/userMap/userStatsMap.ts +108 -0
  193. package/src/userName.ts +2 -1
  194. package/src/userStats.ts +75 -0
  195. package/src/userStatsConfig.ts +18 -0
  196. package/src/util/getTokenAddress.ts +18 -0
  197. package/tests/bn/test.ts +46 -11
  198. package/tests/dlob/helpers.ts +619 -0
  199. package/tests/dlob/test.ts +4586 -0
  200. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  201. package/lib/admin.d.ts +0 -44
  202. package/lib/admin.js +0 -438
  203. package/lib/clearingHouse.d.ts +0 -146
  204. package/lib/clearingHouse.js +0 -1154
  205. package/lib/clearingHouseUser.d.ts +0 -187
  206. package/lib/clearingHouseUser.js +0 -634
  207. package/lib/clearingHouseUserConfig.d.ts +0 -14
  208. package/lib/constants/banks.d.ts +0 -16
  209. package/lib/constants/banks.js +0 -41
  210. package/lib/constants/markets.d.ts +0 -19
  211. package/lib/idl/clearing_house.json +0 -4464
  212. package/lib/math/bankBalance.d.ts +0 -9
  213. package/lib/math/bankBalance.js +0 -75
  214. package/lib/math/state.d.ts +0 -8
  215. package/lib/math/state.js +0 -15
  216. package/lib/orders.d.ts +0 -8
  217. package/lib/orders.js +0 -134
  218. package/src/accounts/bulkAccountLoader.js +0 -197
  219. package/src/accounts/bulkUserSubscription.js +0 -33
  220. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  221. package/src/accounts/pollingOracleSubscriber.js +0 -93
  222. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  223. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  224. package/src/accounts/types.js +0 -10
  225. package/src/accounts/utils.js +0 -7
  226. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  227. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  228. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  229. package/src/addresses/marketAddresses.js +0 -26
  230. package/src/admin.js +0 -517
  231. package/src/admin.ts +0 -730
  232. package/src/clearingHouse.ts +0 -1828
  233. package/src/clearingHouseUser.ts +0 -978
  234. package/src/clearingHouseUserConfig.js +0 -2
  235. package/src/config.js +0 -67
  236. package/src/constants/banks.js +0 -42
  237. package/src/constants/banks.ts +0 -50
  238. package/src/constants/markets.js +0 -42
  239. package/src/constants/numericConstants.js +0 -41
  240. package/src/events/eventSubscriber.js +0 -139
  241. package/src/events/fetchLogs.js +0 -50
  242. package/src/events/pollingLogProvider.js +0 -64
  243. package/src/events/sort.js +0 -44
  244. package/src/events/txEventCache.js +0 -71
  245. package/src/events/types.js +0 -20
  246. package/src/events/webSocketLogProvider.js +0 -41
  247. package/src/factory/bigNum.js +0 -390
  248. package/src/factory/oracleClient.js +0 -20
  249. package/src/idl/clearing_house.json +0 -4464
  250. package/src/index.js +0 -69
  251. package/src/math/amm.js +0 -369
  252. package/src/math/auction.js +0 -42
  253. package/src/math/bankBalance.ts +0 -112
  254. package/src/math/conversion.js +0 -11
  255. package/src/math/funding.js +0 -248
  256. package/src/math/oracles.js +0 -26
  257. package/src/math/repeg.js +0 -128
  258. package/src/math/state.js +0 -15
  259. package/src/math/state.ts +0 -14
  260. package/src/math/trade.js +0 -253
  261. package/src/math/utils.js +0 -26
  262. package/src/math/utils.js.map +0 -1
  263. package/src/mockUSDCFaucet.js +0 -280
  264. package/src/oracles/oracleClientCache.js +0 -19
  265. package/src/oracles/pythClient.js +0 -46
  266. package/src/oracles/quoteAssetOracleClient.js +0 -32
  267. package/src/oracles/switchboardClient.js +0 -69
  268. package/src/orderParams.js +0 -20
  269. package/src/orders.ts +0 -245
  270. package/src/slot/SlotSubscriber.js +0 -39
  271. package/src/tokenFaucet.js +0 -189
  272. package/src/types.js +0 -125
  273. package/src/userName.js +0 -20
  274. package/src/wallet.js +0 -35
@@ -1,311 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.PollingClearingHouseAccountSubscriber = void 0;
13
- const types_1 = require("./types");
14
- const events_1 = require("events");
15
- const pda_1 = require("../addresses/pda");
16
- const utils_1 = require("./utils");
17
- const web3_js_1 = require("@solana/web3.js");
18
- const oracleClientCache_1 = require("../oracles/oracleClientCache");
19
- const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
20
- class PollingClearingHouseAccountSubscriber {
21
- constructor(program, accountLoader, marketIndexes, bankIndexes, oracleInfos) {
22
- this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
23
- this.accountsToPoll = new Map();
24
- this.oraclesToPoll = new Map();
25
- this.market = new Map();
26
- this.bank = new Map();
27
- this.oracles = new Map();
28
- this.isSubscribing = false;
29
- this.isSubscribed = false;
30
- this.program = program;
31
- this.eventEmitter = new events_1.EventEmitter();
32
- this.accountLoader = accountLoader;
33
- this.marketIndexes = marketIndexes;
34
- this.bankIndexes = bankIndexes;
35
- this.oracleInfos = oracleInfos;
36
- }
37
- subscribe() {
38
- return __awaiter(this, void 0, void 0, function* () {
39
- if (this.isSubscribed) {
40
- return true;
41
- }
42
- if (this.isSubscribing) {
43
- return yield this.subscriptionPromise;
44
- }
45
- this.isSubscribing = true;
46
- this.subscriptionPromise = new Promise((res) => {
47
- this.subscriptionPromiseResolver = res;
48
- });
49
- yield this.updateAccountsToPoll();
50
- yield this.updateOraclesToPoll();
51
- yield this.addToAccountLoader();
52
- let subscriptionSucceeded = false;
53
- let retries = 0;
54
- while (!subscriptionSucceeded && retries < 5) {
55
- yield this.fetch();
56
- subscriptionSucceeded = this.didSubscriptionSucceed();
57
- retries++;
58
- }
59
- if (subscriptionSucceeded) {
60
- this.eventEmitter.emit('update');
61
- }
62
- this.isSubscribing = false;
63
- this.isSubscribed = subscriptionSucceeded;
64
- this.subscriptionPromiseResolver(subscriptionSucceeded);
65
- return subscriptionSucceeded;
66
- });
67
- }
68
- updateAccountsToPoll() {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- if (this.accountsToPoll.size > 0) {
71
- return;
72
- }
73
- const accounts = yield this.getClearingHouseAccounts();
74
- this.accountsToPoll.set(accounts.state.toString(), {
75
- key: 'state',
76
- publicKey: accounts.state,
77
- eventType: 'stateAccountUpdate',
78
- });
79
- yield this.updateMarketAccountsToPoll();
80
- yield this.updateBankAccountsToPoll();
81
- });
82
- }
83
- updateMarketAccountsToPoll() {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- for (const marketIndex of this.marketIndexes) {
86
- yield this.addMarketAccountToPoll(marketIndex);
87
- }
88
- return true;
89
- });
90
- }
91
- addMarketAccountToPoll(marketIndex) {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- const marketPublicKey = yield pda_1.getMarketPublicKey(this.program.programId, marketIndex);
94
- this.accountsToPoll.set(marketPublicKey.toString(), {
95
- key: 'market',
96
- publicKey: marketPublicKey,
97
- eventType: 'marketAccountUpdate',
98
- mapKey: marketIndex.toNumber(),
99
- });
100
- return true;
101
- });
102
- }
103
- updateBankAccountsToPoll() {
104
- return __awaiter(this, void 0, void 0, function* () {
105
- for (const bankIndex of this.bankIndexes) {
106
- yield this.addBankAccountToPoll(bankIndex);
107
- }
108
- return true;
109
- });
110
- }
111
- addBankAccountToPoll(bankIndex) {
112
- return __awaiter(this, void 0, void 0, function* () {
113
- const bankPublicKey = yield pda_1.getBankPublicKey(this.program.programId, bankIndex);
114
- this.accountsToPoll.set(bankPublicKey.toString(), {
115
- key: 'bank',
116
- publicKey: bankPublicKey,
117
- eventType: 'bankAccountUpdate',
118
- mapKey: bankIndex.toNumber(),
119
- });
120
- return true;
121
- });
122
- }
123
- updateOraclesToPoll() {
124
- for (const oracleInfo of this.oracleInfos) {
125
- if (!oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
126
- this.addOracleToPoll(oracleInfo);
127
- }
128
- }
129
- return true;
130
- }
131
- addOracleToPoll(oracleInfo) {
132
- this.oraclesToPoll.set(oracleInfo.publicKey.toString(), {
133
- publicKey: oracleInfo.publicKey,
134
- source: oracleInfo.source,
135
- });
136
- return true;
137
- }
138
- getClearingHouseAccounts() {
139
- return __awaiter(this, void 0, void 0, function* () {
140
- const statePublicKey = yield pda_1.getClearingHouseStateAccountPublicKey(this.program.programId);
141
- const accounts = {
142
- state: statePublicKey,
143
- };
144
- return accounts;
145
- });
146
- }
147
- addToAccountLoader() {
148
- return __awaiter(this, void 0, void 0, function* () {
149
- for (const [_, accountToPoll] of this.accountsToPoll) {
150
- this.addAccountToAccountLoader(accountToPoll);
151
- }
152
- for (const [_, oracleToPoll] of this.oraclesToPoll) {
153
- this.addOracleToAccountLoader(oracleToPoll);
154
- }
155
- this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
156
- this.eventEmitter.emit('error', error);
157
- });
158
- });
159
- }
160
- addAccountToAccountLoader(accountToPoll) {
161
- accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer, slot) => {
162
- if (!buffer)
163
- return;
164
- const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
165
- const dataAndSlot = {
166
- data: account,
167
- slot,
168
- };
169
- if (accountToPoll.mapKey != undefined) {
170
- this[accountToPoll.key].set(accountToPoll.mapKey, dataAndSlot);
171
- }
172
- else {
173
- this[accountToPoll.key] = dataAndSlot;
174
- }
175
- // @ts-ignore
176
- this.eventEmitter.emit(accountToPoll.eventType, account);
177
- this.eventEmitter.emit('update');
178
- if (!this.isSubscribed) {
179
- this.isSubscribed = this.didSubscriptionSucceed();
180
- }
181
- });
182
- }
183
- addOracleToAccountLoader(oracleToPoll) {
184
- const oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
185
- oracleToPoll.callbackId = this.accountLoader.addAccount(oracleToPoll.publicKey, (buffer, slot) => {
186
- if (!buffer)
187
- return;
188
- const oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
189
- const dataAndSlot = {
190
- data: oraclePriceData,
191
- slot,
192
- };
193
- this.oracles.set(oracleToPoll.publicKey.toString(), dataAndSlot);
194
- this.eventEmitter.emit('oraclePriceUpdate', oracleToPoll.publicKey, oraclePriceData);
195
- this.eventEmitter.emit('update');
196
- });
197
- }
198
- fetch() {
199
- return __awaiter(this, void 0, void 0, function* () {
200
- yield this.accountLoader.load();
201
- for (const [_, accountToPoll] of this.accountsToPoll) {
202
- const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
203
- if (buffer) {
204
- const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
205
- if (accountToPoll.mapKey != undefined) {
206
- this[accountToPoll.key].set(accountToPoll.mapKey, {
207
- data: account,
208
- slot,
209
- });
210
- }
211
- else {
212
- this[accountToPoll.key] = {
213
- data: account,
214
- slot,
215
- };
216
- }
217
- }
218
- }
219
- for (const [_, oracleToPoll] of this.oraclesToPoll) {
220
- const { buffer, slot } = this.accountLoader.getBufferAndSlot(oracleToPoll.publicKey);
221
- if (buffer) {
222
- const oracleClient = this.oracleClientCache.get(oracleToPoll.source, this.program.provider.connection);
223
- const oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(buffer);
224
- this.oracles.set(oracleToPoll.publicKey.toString(), {
225
- data: oraclePriceData,
226
- slot,
227
- });
228
- }
229
- }
230
- });
231
- }
232
- didSubscriptionSucceed() {
233
- if (this.state)
234
- return true;
235
- return false;
236
- }
237
- unsubscribe() {
238
- return __awaiter(this, void 0, void 0, function* () {
239
- if (!this.isSubscribed) {
240
- return;
241
- }
242
- for (const [_, accountToPoll] of this.accountsToPoll) {
243
- this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
244
- }
245
- for (const [_, oracleToPoll] of this.oraclesToPoll) {
246
- this.accountLoader.removeAccount(oracleToPoll.publicKey, oracleToPoll.callbackId);
247
- }
248
- this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
249
- this.errorCallbackId = undefined;
250
- this.accountsToPoll.clear();
251
- this.oraclesToPoll.clear();
252
- this.isSubscribed = false;
253
- });
254
- }
255
- addBank(bankIndex) {
256
- return __awaiter(this, void 0, void 0, function* () {
257
- yield this.addBankAccountToPoll(bankIndex);
258
- const accountToPoll = this.accountsToPoll.get(bankIndex.toString());
259
- this.addAccountToAccountLoader(accountToPoll);
260
- return true;
261
- });
262
- }
263
- addMarket(marketIndex) {
264
- return __awaiter(this, void 0, void 0, function* () {
265
- yield this.addMarketAccountToPoll(marketIndex);
266
- const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
267
- this.addAccountToAccountLoader(accountToPoll);
268
- return true;
269
- });
270
- }
271
- addOracle(oracleInfo) {
272
- return __awaiter(this, void 0, void 0, function* () {
273
- if (oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
274
- return true;
275
- }
276
- this.addOracleToPoll(oracleInfo);
277
- const oracleToPoll = this.oraclesToPoll.get(oracleInfo.publicKey.toString());
278
- this.addOracleToAccountLoader(oracleToPoll);
279
- return true;
280
- });
281
- }
282
- assertIsSubscribed() {
283
- if (!this.isSubscribed) {
284
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
285
- }
286
- }
287
- getStateAccountAndSlot() {
288
- this.assertIsSubscribed();
289
- return this.state;
290
- }
291
- getMarketAccountAndSlot(marketIndex) {
292
- return this.market.get(marketIndex.toNumber());
293
- }
294
- getMarketAccountsAndSlots() {
295
- return Array.from(this.market.values());
296
- }
297
- getBankAccountAndSlot(bankIndex) {
298
- return this.bank.get(bankIndex.toNumber());
299
- }
300
- getOraclePriceDataAndSlot(oraclePublicKey) {
301
- this.assertIsSubscribed();
302
- if (oraclePublicKey.equals(web3_js_1.PublicKey.default)) {
303
- return {
304
- data: quoteAssetOracleClient_1.QUOTE_ORACLE_PRICE_DATA,
305
- slot: 0,
306
- };
307
- }
308
- return this.oracles.get(oraclePublicKey.toString());
309
- }
310
- }
311
- exports.PollingClearingHouseAccountSubscriber = PollingClearingHouseAccountSubscriber;
@@ -1,93 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.PollingOracleSubscriber = void 0;
13
- const types_1 = require("./types");
14
- const events_1 = require("events");
15
- class PollingOracleSubscriber {
16
- constructor(publicKey, oracleClient, accountLoader) {
17
- this.isSubscribed = false;
18
- this.publicKey = publicKey;
19
- this.oracleClient = oracleClient;
20
- this.accountLoader = accountLoader;
21
- this.eventEmitter = new events_1.EventEmitter();
22
- }
23
- subscribe() {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- if (this.isSubscribed) {
26
- return true;
27
- }
28
- this.addToAccountLoader();
29
- let subscriptionSucceeded = false;
30
- let retries = 0;
31
- while (!subscriptionSucceeded && retries < 5) {
32
- yield this.fetch();
33
- subscriptionSucceeded = this.didSubscriptionSucceed();
34
- retries++;
35
- }
36
- if (subscriptionSucceeded) {
37
- this.eventEmitter.emit('update');
38
- }
39
- this.isSubscribed = subscriptionSucceeded;
40
- return subscriptionSucceeded;
41
- });
42
- }
43
- addToAccountLoader() {
44
- if (this.callbackId) {
45
- return;
46
- }
47
- this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer, slot) => __awaiter(this, void 0, void 0, function* () {
48
- const oraclePriceData = yield this.oracleClient.getOraclePriceDataFromBuffer(buffer);
49
- this.oraclePriceData = { data: oraclePriceData, slot };
50
- // @ts-ignore
51
- this.eventEmitter.emit('oracleUpdate', oraclePriceData);
52
- this.eventEmitter.emit('update');
53
- }));
54
- this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
55
- this.eventEmitter.emit('error', error);
56
- });
57
- }
58
- fetch() {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- yield this.accountLoader.load();
61
- const { buffer, slot } = this.accountLoader.getBufferAndSlot(this.publicKey);
62
- this.oraclePriceData = {
63
- data: yield this.oracleClient.getOraclePriceDataFromBuffer(buffer),
64
- slot,
65
- };
66
- });
67
- }
68
- unsubscribe() {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- if (!this.isSubscribed) {
71
- return;
72
- }
73
- this.accountLoader.removeAccount(this.publicKey, this.callbackId);
74
- this.callbackId = undefined;
75
- this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
76
- this.errorCallbackId = undefined;
77
- this.isSubscribed = false;
78
- });
79
- }
80
- assertIsSubscribed() {
81
- if (!this.isSubscribed) {
82
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
83
- }
84
- }
85
- getOraclePriceData() {
86
- this.assertIsSubscribed();
87
- return this.oraclePriceData;
88
- }
89
- didSubscriptionSucceed() {
90
- return !!this.oraclePriceData;
91
- }
92
- }
93
- exports.PollingOracleSubscriber = PollingOracleSubscriber;
@@ -1,90 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.PollingTokenAccountSubscriber = void 0;
13
- const types_1 = require("./types");
14
- const events_1 = require("events");
15
- const token_1 = require("../token");
16
- class PollingTokenAccountSubscriber {
17
- constructor(publicKey, accountLoader) {
18
- this.isSubscribed = false;
19
- this.publicKey = publicKey;
20
- this.accountLoader = accountLoader;
21
- this.eventEmitter = new events_1.EventEmitter();
22
- }
23
- subscribe() {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- if (this.isSubscribed) {
26
- return true;
27
- }
28
- this.addToAccountLoader();
29
- let subscriptionSucceeded = false;
30
- let retries = 0;
31
- while (!subscriptionSucceeded && retries < 5) {
32
- yield this.fetch();
33
- subscriptionSucceeded = this.didSubscriptionSucceed();
34
- retries++;
35
- }
36
- if (subscriptionSucceeded) {
37
- this.eventEmitter.emit('update');
38
- }
39
- this.isSubscribed = subscriptionSucceeded;
40
- return subscriptionSucceeded;
41
- });
42
- }
43
- addToAccountLoader() {
44
- if (this.callbackId) {
45
- return;
46
- }
47
- this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer, slot) => {
48
- const tokenAccount = token_1.parseTokenAccount(buffer);
49
- this.tokenAccountAndSlot = { data: tokenAccount, slot };
50
- // @ts-ignore
51
- this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
52
- this.eventEmitter.emit('update');
53
- });
54
- this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
55
- this.eventEmitter.emit('error', error);
56
- });
57
- }
58
- fetch() {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- yield this.accountLoader.load();
61
- const { buffer, slot } = this.accountLoader.getBufferAndSlot(this.publicKey);
62
- this.tokenAccountAndSlot = { data: token_1.parseTokenAccount(buffer), slot };
63
- });
64
- }
65
- unsubscribe() {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- if (!this.isSubscribed) {
68
- return;
69
- }
70
- this.accountLoader.removeAccount(this.publicKey, this.callbackId);
71
- this.callbackId = undefined;
72
- this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
73
- this.errorCallbackId = undefined;
74
- this.isSubscribed = false;
75
- });
76
- }
77
- assertIsSubscribed() {
78
- if (!this.isSubscribed) {
79
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
80
- }
81
- }
82
- getTokenAccountAndSlot() {
83
- this.assertIsSubscribed();
84
- return this.tokenAccountAndSlot;
85
- }
86
- didSubscriptionSucceed() {
87
- return !!this.tokenAccountAndSlot;
88
- }
89
- }
90
- exports.PollingTokenAccountSubscriber = PollingTokenAccountSubscriber;
@@ -1,132 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.PollingUserAccountSubscriber = void 0;
13
- const types_1 = require("./types");
14
- const events_1 = require("events");
15
- const utils_1 = require("./utils");
16
- class PollingUserAccountSubscriber {
17
- constructor(program, userAccountPublicKey, accountLoader) {
18
- this.accountsToPoll = new Map();
19
- this.isSubscribed = false;
20
- this.program = program;
21
- this.accountLoader = accountLoader;
22
- this.eventEmitter = new events_1.EventEmitter();
23
- this.userAccountPublicKey = userAccountPublicKey;
24
- }
25
- subscribe() {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- if (this.isSubscribed) {
28
- return true;
29
- }
30
- yield this.addToAccountLoader();
31
- let subscriptionSucceeded = false;
32
- let retries = 0;
33
- while (!subscriptionSucceeded && retries < 5) {
34
- yield this.fetchIfUnloaded();
35
- subscriptionSucceeded = this.didSubscriptionSucceed();
36
- retries++;
37
- }
38
- if (subscriptionSucceeded) {
39
- this.eventEmitter.emit('update');
40
- }
41
- this.isSubscribed = subscriptionSucceeded;
42
- return subscriptionSucceeded;
43
- });
44
- }
45
- addToAccountLoader() {
46
- return __awaiter(this, void 0, void 0, function* () {
47
- if (this.accountsToPoll.size > 0) {
48
- return;
49
- }
50
- this.accountsToPoll.set(this.userAccountPublicKey.toString(), {
51
- key: 'user',
52
- publicKey: this.userAccountPublicKey,
53
- eventType: 'userAccountUpdate',
54
- });
55
- for (const [_, accountToPoll] of this.accountsToPoll) {
56
- accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer, slot) => {
57
- if (!buffer) {
58
- return;
59
- }
60
- const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
61
- this[accountToPoll.key] = { data: account, slot };
62
- // @ts-ignore
63
- this.eventEmitter.emit(accountToPoll.eventType, account);
64
- this.eventEmitter.emit('update');
65
- });
66
- }
67
- this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
68
- this.eventEmitter.emit('error', error);
69
- });
70
- });
71
- }
72
- fetchIfUnloaded() {
73
- return __awaiter(this, void 0, void 0, function* () {
74
- let shouldFetch = false;
75
- for (const [_, accountToPoll] of this.accountsToPoll) {
76
- if (this[accountToPoll.key] === undefined) {
77
- shouldFetch = true;
78
- break;
79
- }
80
- }
81
- if (shouldFetch) {
82
- yield this.fetch();
83
- }
84
- });
85
- }
86
- fetch() {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- yield this.accountLoader.load();
89
- for (const [_, accountToPoll] of this.accountsToPoll) {
90
- const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
91
- if (buffer) {
92
- const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
93
- this[accountToPoll.key] = { data: account, slot };
94
- }
95
- }
96
- });
97
- }
98
- didSubscriptionSucceed() {
99
- let success = true;
100
- for (const [_, accountToPoll] of this.accountsToPoll) {
101
- if (!this[accountToPoll.key]) {
102
- success = false;
103
- break;
104
- }
105
- }
106
- return success;
107
- }
108
- unsubscribe() {
109
- return __awaiter(this, void 0, void 0, function* () {
110
- if (!this.isSubscribed) {
111
- return;
112
- }
113
- for (const [_, accountToPoll] of this.accountsToPoll) {
114
- this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
115
- }
116
- this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
117
- this.errorCallbackId = undefined;
118
- this.accountsToPoll.clear();
119
- this.isSubscribed = false;
120
- });
121
- }
122
- assertIsSubscribed() {
123
- if (!this.isSubscribed) {
124
- throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
125
- }
126
- }
127
- getUserAccountAndSlot() {
128
- this.assertIsSubscribed();
129
- return this.user;
130
- }
131
- }
132
- exports.PollingUserAccountSubscriber = PollingUserAccountSubscriber;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotSubscribedError = void 0;
4
- class NotSubscribedError extends Error {
5
- constructor() {
6
- super(...arguments);
7
- this.name = 'NotSubscribedError';
8
- }
9
- }
10
- exports.NotSubscribedError = NotSubscribedError;
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.capitalize = void 0;
4
- function capitalize(value) {
5
- return value[0].toUpperCase() + value.slice(1);
6
- }
7
- exports.capitalize = capitalize;