@drift-labs/sdk 0.2.0-master.11 → 0.2.0-master.14

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 (114) hide show
  1. package/lib/accounts/fetch.d.ts +2 -1
  2. package/lib/accounts/fetch.js +9 -1
  3. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  4. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  5. package/lib/accounts/types.d.ts +14 -1
  6. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  7. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  8. package/lib/addresses/pda.d.ts +1 -0
  9. package/lib/addresses/pda.js +8 -1
  10. package/lib/admin.d.ts +2 -0
  11. package/lib/admin.js +18 -0
  12. package/lib/clearingHouse.d.ts +24 -3
  13. package/lib/clearingHouse.js +352 -51
  14. package/lib/clearingHouseConfig.d.ts +1 -0
  15. package/lib/clearingHouseUser.d.ts +15 -15
  16. package/lib/clearingHouseUser.js +185 -73
  17. package/lib/clearingHouseUserStats.d.ts +17 -0
  18. package/lib/clearingHouseUserStats.js +36 -0
  19. package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
  20. package/lib/clearingHouseUserStatsConfig.js +2 -0
  21. package/lib/config.js +1 -1
  22. package/lib/constants/banks.d.ts +2 -2
  23. package/lib/constants/banks.js +4 -3
  24. package/lib/constants/numericConstants.d.ts +2 -0
  25. package/lib/constants/numericConstants.js +3 -1
  26. package/lib/events/eventList.js +3 -0
  27. package/lib/events/types.d.ts +2 -1
  28. package/lib/factory/bigNum.d.ts +1 -0
  29. package/lib/factory/bigNum.js +37 -11
  30. package/lib/idl/clearing_house.json +692 -58
  31. package/lib/index.d.ts +1 -0
  32. package/lib/index.js +1 -0
  33. package/lib/math/amm.js +2 -2
  34. package/lib/math/bankBalance.d.ts +7 -1
  35. package/lib/math/bankBalance.js +76 -1
  36. package/lib/math/margin.d.ts +11 -0
  37. package/lib/math/margin.js +72 -0
  38. package/lib/math/market.d.ts +4 -1
  39. package/lib/math/market.js +35 -1
  40. package/lib/math/oracles.d.ts +3 -0
  41. package/lib/math/oracles.js +25 -5
  42. package/lib/math/position.d.ts +8 -0
  43. package/lib/math/position.js +43 -12
  44. package/lib/math/trade.js +2 -2
  45. package/lib/orders.d.ts +1 -2
  46. package/lib/orders.js +2 -77
  47. package/lib/tokenFaucet.d.ts +1 -0
  48. package/lib/tokenFaucet.js +23 -12
  49. package/lib/tx/retryTxSender.js +9 -2
  50. package/lib/types.d.ts +78 -10
  51. package/lib/types.js +12 -0
  52. package/lib/util/getTokenAddress.d.ts +2 -0
  53. package/lib/util/getTokenAddress.js +9 -0
  54. package/package.json +1 -1
  55. package/src/accounts/fetch.ts +27 -2
  56. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  57. package/src/accounts/types.ts +18 -0
  58. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  59. package/src/addresses/pda.ts +13 -0
  60. package/src/admin.ts +29 -1
  61. package/src/clearingHouse.ts +619 -62
  62. package/src/clearingHouseConfig.ts +1 -0
  63. package/src/clearingHouseUser.ts +317 -105
  64. package/src/clearingHouseUserStats.ts +53 -0
  65. package/src/clearingHouseUserStatsConfig.ts +18 -0
  66. package/src/config.ts +1 -1
  67. package/src/constants/banks.js +42 -0
  68. package/src/constants/banks.ts +6 -3
  69. package/src/constants/markets.js +42 -0
  70. package/src/constants/numericConstants.js +41 -0
  71. package/src/constants/numericConstants.ts +3 -0
  72. package/src/events/eventList.ts +3 -0
  73. package/src/events/types.ts +2 -0
  74. package/src/factory/bigNum.js +37 -11
  75. package/src/factory/bigNum.ts +43 -13
  76. package/src/idl/clearing_house.json +692 -58
  77. package/src/index.ts +1 -0
  78. package/src/math/amm.ts +8 -5
  79. package/src/math/bankBalance.ts +147 -1
  80. package/src/math/margin.ts +124 -0
  81. package/src/math/market.ts +66 -1
  82. package/src/math/oracles.ts +42 -5
  83. package/src/math/position.ts +60 -9
  84. package/src/math/trade.ts +2 -2
  85. package/src/orders.ts +4 -157
  86. package/src/tokenFaucet.js +189 -0
  87. package/src/tokenFaucet.ts +38 -15
  88. package/src/tx/retryTxSender.ts +11 -3
  89. package/src/types.js +12 -1
  90. package/src/types.ts +83 -10
  91. package/src/{accounts/fetch.js → util/computeUnits.js} +11 -13
  92. package/src/util/getTokenAddress.js +9 -0
  93. package/src/util/getTokenAddress.ts +18 -0
  94. package/tests/bn/test.ts +2 -0
  95. package/src/accounts/bulkAccountLoader.js +0 -197
  96. package/src/accounts/bulkUserSubscription.js +0 -33
  97. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  98. package/src/accounts/pollingOracleSubscriber.js +0 -93
  99. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  100. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  101. package/src/accounts/types.js +0 -10
  102. package/src/accounts/utils.js +0 -7
  103. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  104. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  105. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  106. package/src/addresses/pda.js +0 -104
  107. package/src/index.js +0 -100
  108. package/src/math/bankBalance.js +0 -75
  109. package/src/math/market.js +0 -57
  110. package/src/math/orders.js +0 -110
  111. package/src/math/position.js +0 -140
  112. package/src/mockUSDCFaucet.js +0 -280
  113. package/src/orders.js +0 -134
  114. package/src/tx/retryTxSender.js +0 -188
@@ -9,21 +9,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.fetchUserAccounts = void 0;
13
- const pda_1 = require("../addresses/pda");
14
- function fetchUserAccounts(connection, program, authority, limit = 8) {
12
+ exports.findComputeUnitConsumption = void 0;
13
+ function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
15
14
  return __awaiter(this, void 0, void 0, function* () {
16
- const userAccountPublicKeys = new Array();
17
- for (let i = 0; i < limit; i++) {
18
- userAccountPublicKeys.push(yield pda_1.getUserAccountPublicKey(program.programId, authority, i));
19
- }
20
- const accountInfos = yield connection.getMultipleAccountsInfo(userAccountPublicKeys, 'confirmed');
21
- return accountInfos.map((accountInfo) => {
22
- if (!accountInfo) {
23
- return undefined;
15
+ const tx = yield connection.getTransaction(txSignature, { commitment });
16
+ const computeUnits = [];
17
+ const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
18
+ tx.meta.logMessages.forEach((logMessage) => {
19
+ const match = logMessage.match(regex);
20
+ if (match && match[1]) {
21
+ computeUnits.push(match[1]);
24
22
  }
25
- return program.account.user.coder.accounts.decode('User', accountInfo.data);
26
23
  });
24
+ return computeUnits;
27
25
  });
28
26
  }
29
- exports.fetchUserAccounts = fetchUserAccounts;
27
+ exports.findComputeUnitConsumption = findComputeUnitConsumption;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTokenAddress = void 0;
4
+ const spl_token_1 = require("@solana/spl-token");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const getTokenAddress = (mintAddress, userPubKey) => {
7
+ return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
8
+ };
9
+ exports.getTokenAddress = getTokenAddress;
@@ -0,0 +1,18 @@
1
+ import {
2
+ Token,
3
+ ASSOCIATED_TOKEN_PROGRAM_ID,
4
+ TOKEN_PROGRAM_ID,
5
+ } from '@solana/spl-token';
6
+ import { PublicKey } from '@solana/web3.js';
7
+
8
+ export const getTokenAddress = (
9
+ mintAddress: string,
10
+ userPubKey: string
11
+ ): Promise<PublicKey> => {
12
+ return Token.getAssociatedTokenAddress(
13
+ ASSOCIATED_TOKEN_PROGRAM_ID,
14
+ TOKEN_PROGRAM_ID,
15
+ new PublicKey(mintAddress),
16
+ new PublicKey(userPubKey)
17
+ );
18
+ };
package/tests/bn/test.ts CHANGED
@@ -125,6 +125,8 @@ describe('BigNum Tests', () => {
125
125
  expect(BigNum.fromPrint('1234567').toMillified(5)).to.equal('1.2345M');
126
126
  expect(BigNum.fromPrint('12345678').toMillified(5)).to.equal('12.345M');
127
127
  expect(BigNum.fromPrint('123456789').toMillified(5)).to.equal('123.45M');
128
+
129
+ expect(BigNum.from(-95, 2).print()).to.equal('-0.95');
128
130
  });
129
131
 
130
132
  it('can initialise from string values correctly', () => {
@@ -1,197 +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.BulkAccountLoader = void 0;
13
- const uuid_1 = require("uuid");
14
- const promiseTimeout_1 = require("../util/promiseTimeout");
15
- const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
16
- const oneMinute = 60 * 1000;
17
- class BulkAccountLoader {
18
- constructor(connection, commitment, pollingFrequency) {
19
- this.accountsToLoad = new Map();
20
- this.bufferAndSlotMap = new Map();
21
- this.errorCallbacks = new Map();
22
- this.lastTimeLoadingPromiseCleared = Date.now();
23
- this.mostRecentSlot = 0;
24
- this.connection = connection;
25
- this.commitment = commitment;
26
- this.pollingFrequency = pollingFrequency;
27
- }
28
- addAccount(publicKey, callback) {
29
- const existingSize = this.accountsToLoad.size;
30
- const callbackId = uuid_1.v4();
31
- const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
32
- if (existingAccountToLoad) {
33
- existingAccountToLoad.callbacks.set(callbackId, callback);
34
- }
35
- else {
36
- const callbacks = new Map();
37
- callbacks.set(callbackId, callback);
38
- const newAccountToLoad = {
39
- publicKey,
40
- callbacks,
41
- };
42
- this.accountsToLoad.set(publicKey.toString(), newAccountToLoad);
43
- }
44
- if (existingSize === 0) {
45
- this.startPolling();
46
- }
47
- // if a new account needs to be polled, remove the cached loadPromise in case client calls load immediately after
48
- this.loadPromise = undefined;
49
- return callbackId;
50
- }
51
- removeAccount(publicKey, callbackId) {
52
- const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
53
- if (existingAccountToLoad) {
54
- existingAccountToLoad.callbacks.delete(callbackId);
55
- if (existingAccountToLoad.callbacks.size === 0) {
56
- this.accountsToLoad.delete(existingAccountToLoad.publicKey.toString());
57
- }
58
- }
59
- if (this.accountsToLoad.size === 0) {
60
- this.stopPolling();
61
- }
62
- }
63
- addErrorCallbacks(callback) {
64
- const callbackId = uuid_1.v4();
65
- this.errorCallbacks.set(callbackId, callback);
66
- return callbackId;
67
- }
68
- removeErrorCallbacks(callbackId) {
69
- this.errorCallbacks.delete(callbackId);
70
- }
71
- chunks(array, size) {
72
- return new Array(Math.ceil(array.length / size))
73
- .fill(null)
74
- .map((_, index) => index * size)
75
- .map((begin) => array.slice(begin, begin + size));
76
- }
77
- load() {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- if (this.loadPromise) {
80
- const now = Date.now();
81
- if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
82
- this.loadPromise = undefined;
83
- }
84
- else {
85
- return this.loadPromise;
86
- }
87
- }
88
- this.loadPromise = new Promise((resolver) => {
89
- this.loadPromiseResolver = resolver;
90
- });
91
- this.lastTimeLoadingPromiseCleared = Date.now();
92
- try {
93
- const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
94
- yield Promise.all(chunks.map((chunk) => {
95
- return this.loadChunk(chunk);
96
- }));
97
- }
98
- catch (e) {
99
- console.error(`Error in bulkAccountLoader.load()`);
100
- console.error(e);
101
- for (const [_, callback] of this.errorCallbacks) {
102
- callback(e);
103
- }
104
- }
105
- finally {
106
- this.loadPromiseResolver();
107
- this.loadPromise = undefined;
108
- }
109
- });
110
- }
111
- loadChunk(accountsToLoad) {
112
- return __awaiter(this, void 0, void 0, function* () {
113
- if (accountsToLoad.length === 0) {
114
- return;
115
- }
116
- const args = [
117
- accountsToLoad.map((accountToLoad) => {
118
- return accountToLoad.publicKey.toBase58();
119
- }),
120
- { commitment: this.commitment },
121
- ];
122
- const rpcResponse = yield promiseTimeout_1.promiseTimeout(
123
- // @ts-ignore
124
- this.connection._rpcRequest('getMultipleAccounts', args), 10 * 1000 // 30 second timeout
125
- );
126
- if (rpcResponse === null) {
127
- this.log('request to rpc timed out');
128
- return;
129
- }
130
- const newSlot = rpcResponse.result.context.slot;
131
- if (newSlot > this.mostRecentSlot) {
132
- this.mostRecentSlot = newSlot;
133
- }
134
- for (const i in accountsToLoad) {
135
- const accountToLoad = accountsToLoad[i];
136
- const key = accountToLoad.publicKey.toString();
137
- const oldRPCResponse = this.bufferAndSlotMap.get(key);
138
- let newBuffer = undefined;
139
- if (rpcResponse.result.value[i]) {
140
- const raw = rpcResponse.result.value[i].data[0];
141
- const dataType = rpcResponse.result.value[i].data[1];
142
- newBuffer = Buffer.from(raw, dataType);
143
- }
144
- if (!oldRPCResponse) {
145
- this.bufferAndSlotMap.set(key, {
146
- slot: newSlot,
147
- buffer: newBuffer,
148
- });
149
- this.handleAccountCallbacks(accountToLoad, newBuffer, newSlot);
150
- continue;
151
- }
152
- if (newSlot <= oldRPCResponse.slot) {
153
- continue;
154
- }
155
- const oldBuffer = oldRPCResponse.buffer;
156
- if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
157
- this.bufferAndSlotMap.set(key, {
158
- slot: newSlot,
159
- buffer: newBuffer,
160
- });
161
- this.handleAccountCallbacks(accountToLoad, newBuffer, newSlot);
162
- }
163
- }
164
- });
165
- }
166
- handleAccountCallbacks(accountToLoad, buffer, slot) {
167
- for (const [_, callback] of accountToLoad.callbacks) {
168
- callback(buffer, slot);
169
- }
170
- }
171
- getBufferAndSlot(publicKey) {
172
- return this.bufferAndSlotMap.get(publicKey.toString());
173
- }
174
- startPolling() {
175
- if (this.intervalId) {
176
- return;
177
- }
178
- this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
179
- }
180
- stopPolling() {
181
- if (this.intervalId) {
182
- clearInterval(this.intervalId);
183
- this.intervalId = undefined;
184
- }
185
- }
186
- log(msg) {
187
- console.log(msg);
188
- }
189
- updatePollingFrequency(pollingFrequency) {
190
- this.stopPolling();
191
- this.pollingFrequency = pollingFrequency;
192
- if (this.accountsToLoad.size > 0) {
193
- this.startPolling();
194
- }
195
- }
196
- }
197
- exports.BulkAccountLoader = BulkAccountLoader;
@@ -1,33 +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.bulkPollingUserSubscribe = void 0;
13
- /**
14
- * @param users
15
- * @param accountLoader
16
- */
17
- function bulkPollingUserSubscribe(users, accountLoader) {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- if (users.length === 0) {
20
- yield accountLoader.load();
21
- return;
22
- }
23
- yield Promise.all(users.map((user) => {
24
- // Pull the keys from the authority map so we can skip fetching them in addToAccountLoader
25
- return user.accountSubscriber.addToAccountLoader();
26
- }));
27
- yield accountLoader.load();
28
- yield Promise.all(users.map((user) => __awaiter(this, void 0, void 0, function* () {
29
- return user.subscribe();
30
- })));
31
- });
32
- }
33
- exports.bulkPollingUserSubscribe = bulkPollingUserSubscribe;
@@ -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;