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

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 +2 -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 +3 -0
  9. package/lib/clearingHouse.js +19 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +184 -108
  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 +123 -203
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +3 -2
  23. package/lib/math/market.js +17 -12
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/position.d.ts +3 -3
  27. package/lib/math/position.js +23 -16
  28. package/lib/math/repeg.js +8 -0
  29. package/lib/math/spotBalance.d.ts +3 -0
  30. package/lib/math/spotBalance.js +18 -1
  31. package/lib/math/spotPosition.d.ts +5 -1
  32. package/lib/math/spotPosition.js +16 -1
  33. package/lib/types.d.ts +25 -24
  34. package/lib/types.js +9 -3
  35. package/lib/userMap/userMap.d.ts +25 -0
  36. package/lib/userMap/userMap.js +73 -0
  37. package/lib/userMap/userStatsMap.d.ts +19 -0
  38. package/lib/userMap/userStatsMap.js +68 -0
  39. package/package.json +6 -3
  40. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  41. package/src/accounts/types.ts +1 -0
  42. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  43. package/src/admin.ts +50 -8
  44. package/src/clearingHouse.ts +30 -1
  45. package/src/clearingHouseUser.ts +366 -167
  46. package/src/config.ts +1 -1
  47. package/src/dlob/DLOB.ts +884 -0
  48. package/src/dlob/DLOBNode.ts +163 -0
  49. package/src/dlob/NodeList.ts +185 -0
  50. package/src/idl/clearing_house.json +123 -203
  51. package/src/index.ts +5 -0
  52. package/src/math/market.ts +22 -13
  53. package/src/math/orders.ts +29 -21
  54. package/src/math/position.ts +36 -22
  55. package/src/math/repeg.ts +9 -0
  56. package/src/math/spotBalance.ts +26 -0
  57. package/src/math/spotPosition.ts +42 -1
  58. package/src/types.ts +26 -24
  59. package/src/userMap/userMap.ts +100 -0
  60. package/src/userMap/userStatsMap.ts +110 -0
  61. package/tests/bn/test.ts +2 -3
  62. package/tests/dlob/helpers.ts +322 -0
  63. package/tests/dlob/test.ts +2865 -0
  64. package/my-script/.env +0 -7
  65. package/my-script/getUserStats.ts +0 -106
  66. package/my-script/multiConnections.ts +0 -119
  67. package/my-script/test-regex.ts +0 -11
  68. package/my-script/utils.ts +0 -52
  69. package/src/accounts/bulkAccountLoader.js +0 -249
  70. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  71. package/src/accounts/bulkUserSubscription.js +0 -75
  72. package/src/accounts/fetch.js +0 -92
  73. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  74. package/src/accounts/pollingOracleSubscriber.js +0 -156
  75. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  76. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  77. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  78. package/src/accounts/types.js +0 -28
  79. package/src/accounts/utils.js +0 -7
  80. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  82. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  83. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  84. package/src/addresses/pda.js +0 -186
  85. package/src/admin.js +0 -1284
  86. package/src/assert/assert.js +0 -9
  87. package/src/clearingHouse.js +0 -3433
  88. package/src/clearingHouseConfig.js +0 -2
  89. package/src/clearingHouseUser.js +0 -874
  90. package/src/clearingHouseUserConfig.js +0 -2
  91. package/src/clearingHouseUserStats.js +0 -115
  92. package/src/clearingHouseUserStatsConfig.js +0 -2
  93. package/src/config.js +0 -80
  94. package/src/constants/numericConstants.js +0 -48
  95. package/src/constants/perpMarkets.js +0 -42
  96. package/src/constants/spotMarkets.js +0 -51
  97. package/src/events/eventList.js +0 -120
  98. package/src/events/eventSubscriber.js +0 -202
  99. package/src/events/fetchLogs.js +0 -117
  100. package/src/events/pollingLogProvider.js +0 -113
  101. package/src/events/sort.js +0 -41
  102. package/src/events/txEventCache.js +0 -74
  103. package/src/events/types.js +0 -25
  104. package/src/events/webSocketLogProvider.js +0 -76
  105. package/src/factory/bigNum.js +0 -393
  106. package/src/factory/oracleClient.js +0 -20
  107. package/src/index.js +0 -75
  108. package/src/math/amm.js +0 -422
  109. package/src/math/auction.js +0 -42
  110. package/src/math/conversion.js +0 -12
  111. package/src/math/funding.js +0 -296
  112. package/src/math/insurance.js +0 -27
  113. package/src/math/margin.js +0 -77
  114. package/src/math/market.js +0 -105
  115. package/src/math/oracles.js +0 -56
  116. package/src/math/orders.js +0 -153
  117. package/src/math/position.js +0 -172
  118. package/src/math/repeg.js +0 -128
  119. package/src/math/spotBalance.js +0 -176
  120. package/src/math/spotMarket.js +0 -8
  121. package/src/math/spotPosition.js +0 -8
  122. package/src/math/trade.js +0 -260
  123. package/src/math/utils.js +0 -27
  124. package/src/oracles/oracleClientCache.js +0 -20
  125. package/src/oracles/pythClient.js +0 -81
  126. package/src/oracles/quoteAssetOracleClient.js +0 -63
  127. package/src/oracles/switchboardClient.js +0 -101
  128. package/src/oracles/types.js +0 -2
  129. package/src/orderParams.js +0 -28
  130. package/src/serum/serumSubscriber.js +0 -102
  131. package/src/serum/types.js +0 -2
  132. package/src/slot/SlotSubscriber.js +0 -86
  133. package/src/token/index.js +0 -38
  134. package/src/tokenFaucet.js +0 -323
  135. package/src/tx/retryTxSender.js +0 -280
  136. package/src/tx/types.js +0 -2
  137. package/src/tx/utils.js +0 -18
  138. package/src/types.js +0 -216
  139. package/src/userName.js +0 -20
  140. package/src/util/computeUnits.js +0 -62
  141. package/src/util/promiseTimeout.js +0 -14
  142. package/src/util/tps.js +0 -61
  143. package/src/wallet.js +0 -72
package/my-script/.env DELETED
@@ -1,7 +0,0 @@
1
- #ANCHOR_WALLET=/Users/ww/.config/solana/id.json
2
-
3
- #ANCHOR_WALLET=/Users/ww/.config/solana/bot2RzYy9oY5s3z71Yubo4MmRxyDBQP3qNNCqyqLcPp.json
4
-
5
- ANCHOR_WALLET=./ww18NdhuLSQPCrHSx7V68eZJpe2y311heWeXJfSmP3Q.json
6
- #ANCHOR_WALLET=./ww2z7N9TG1PLLUQGQF2VKzCFaPtQ5FBhRfeEAuy6c5C.json
7
- #ANCHOR_WALLET=./ww3StJtTubhwssqAhvSSAc5ifCgKjzmF8hz7Gt2DmSa.json
@@ -1,106 +0,0 @@
1
- import * as anchor from '@project-serum/anchor';
2
- import { PublicKey, clusterApiUrl } from '@solana/web3.js';
3
-
4
- import {
5
- initialize,
6
- BulkAccountLoader,
7
- EventSubscriber,
8
- DevnetMarkets,
9
- ClearingHouse,
10
- } from '..';
11
- import { printUserStats } from './utils';
12
-
13
- require('dotenv').config();
14
-
15
-
16
- async function main(provider: anchor.AnchorProvider) {
17
- const connection = provider.connection;
18
- const config = initialize({ env: 'devnet' });
19
- const clearingHousePublicKey = new PublicKey(
20
- config.CLEARING_HOUSE_PROGRAM_ID
21
- );
22
-
23
- const bulkAccountLoader = new BulkAccountLoader(connection, 'confirmed', 1000);
24
- const clearingHouse = new ClearingHouse({
25
- connection,
26
- wallet: provider.wallet,
27
- programID: clearingHousePublicKey,
28
- env: 'devnet',
29
- // accountSubscription: {
30
- // type: 'websocket',
31
- // }
32
- accountSubscription: {
33
- type: 'polling',
34
- accountLoader: bulkAccountLoader,
35
- }
36
- });
37
-
38
- console.log(`clearingHouseProgramID: ${clearingHousePublicKey}`);
39
- console.log(`provider.wallet: ${provider.wallet.publicKey.toBase58()}`);
40
-
41
- const eventSubscriber = new EventSubscriber(connection, clearingHouse.program, {
42
- maxTx: 8192,
43
- maxEventsPerType: 4096,
44
- orderBy: 'blockchain',
45
- orderDir: 'desc',
46
- commitment: 'confirmed',
47
- logProviderConfig: {
48
- type: 'websocket',
49
- },
50
- });
51
-
52
- if (!(await clearingHouse.getUser().exists())) {
53
- console.error(`ClearingHouseUser for ${provider.wallet.publicKey} does not exist`);
54
- console.info(`Creating ClearingHouseUser for ${provider.wallet.publicKey}`);
55
- const [txSig] = await clearingHouse.initializeUserAccount();
56
- console.log(`Initialized user account in transaction: ${txSig}`);
57
- }
58
-
59
- if (!await clearingHouse.subscribe()) {
60
- throw new Error("fail clearingHouse.subscribe");
61
- }
62
- if (!await clearingHouse.accountSubscriber.subscribe()) {
63
- throw new Error("fail clearingHouse.accountSubscriber.subscribe");
64
- }
65
- if (!eventSubscriber.subscribe()) {
66
- throw new Error("fail eventSubscriber.subscribe");
67
- }
68
-
69
- const chUser = clearingHouse.getUser();
70
- const chUserAccount = clearingHouse.getUserAccount();
71
- console.log(`chUserAcc.authority: ${chUserAccount!.authority.toBase58()}`);
72
- console.log(` chUserAccount pubkey: ${chUser.userAccountPublicKey.toBase58()}`);
73
-
74
- const keyToSymbol = new Map<string, string>();
75
- const marketIndexToSymbol = new Map<number, string>();
76
- for (const market of DevnetMarkets) {
77
- keyToSymbol.set(market.oracle.toBase58(), market.baseAssetSymbol);
78
- marketIndexToSymbol.set(market.marketIndex.toNumber(), market.symbol);
79
- }
80
-
81
- const run = async () => {
82
- await clearingHouse.fetchAccounts();
83
- await chUser.fetchAccounts();
84
- printUserStats(chUserAccount!, marketIndexToSymbol);
85
- };
86
- await run();
87
- setInterval(run, 1000);
88
- }
89
-
90
-
91
- try {
92
- if (!process.env.ANCHOR_WALLET) {
93
- throw new Error('ANCHOR_WALLET must be set.');
94
- }
95
- main(
96
- anchor.AnchorProvider.local(clusterApiUrl("devnet"), {
97
- // anchor.AnchorProvider.local('https://devnet.genesysgo.net', {
98
- preflightCommitment: 'confirmed',
99
- skipPreflight: false,
100
- commitment: 'confirmed',
101
- })
102
- );
103
- // anchor.AnchorProvider.local('https://psytrbhymqlkfrhudd.dev.genesysgo.net:8899/');
104
- } catch (e) {
105
- console.error(e);
106
- }
@@ -1,119 +0,0 @@
1
- import * as anchor from '@project-serum/anchor';
2
- import { PublicKey, clusterApiUrl } from '@solana/web3.js';
3
-
4
- import {
5
- initialize,
6
- BulkAccountLoader,
7
- EventSubscriber,
8
- DevnetMarkets,
9
- ClearingHouse,
10
- } from '..';
11
- import { printUserStats } from './utils';
12
-
13
- require('dotenv').config();
14
-
15
-
16
- async function main(provider: anchor.AnchorProvider) {
17
- const connection = provider.connection;
18
- const config = initialize({ env: 'devnet' });
19
- const clearingHousePublicKey = new PublicKey(
20
- config.CLEARING_HOUSE_PROGRAM_ID
21
- );
22
-
23
- const bulkAccountLoader = new BulkAccountLoader(
24
- connection,
25
- 'confirmed',
26
- 1000
27
- );
28
- const clearingHouse = new ClearingHouse({
29
- connection,
30
- wallet: provider.wallet,
31
- programID: clearingHousePublicKey,
32
- env: 'devnet',
33
- // accountSubscription: {
34
- // type: 'websocket',
35
- // }
36
- accountSubscription: {
37
- type: 'polling',
38
- accountLoader: bulkAccountLoader,
39
- },
40
- });
41
-
42
- console.log(`clearingHouseProgramID: ${clearingHousePublicKey}`);
43
- console.log(
44
- `provider.wallet: ${provider.wallet.publicKey.toBase58()}`
45
- );
46
-
47
- const eventSubscriber = new EventSubscriber(
48
- connection,
49
- clearingHouse.program,
50
- {
51
- maxTx: 8192,
52
- maxEventsPerType: 4096,
53
- orderBy: 'blockchain',
54
- orderDir: 'desc',
55
- commitment: 'confirmed',
56
- logProviderConfig: {
57
- type: 'websocket',
58
- },
59
- }
60
- );
61
-
62
- if (!(await clearingHouse.getUser().exists())) {
63
- console.error(
64
- `ClearingHouseUser for ${provider.wallet.publicKey} does not exist`
65
- );
66
- console.info(`Creating ClearingHouseUser for ${provider.wallet.publicKey}`);
67
- const [txSig] = await clearingHouse.initializeUserAccount();
68
- console.log(`Initialized user account in transaction: ${txSig}`);
69
- }
70
-
71
- if (!(await clearingHouse.subscribe())) {
72
- throw new Error('fail clearingHouse.subscribe');
73
- }
74
- if (!(await clearingHouse.accountSubscriber.subscribe())) {
75
- throw new Error('fail clearingHouse.accountSubscriber.subscribe');
76
- }
77
- if (!eventSubscriber.subscribe()) {
78
- throw new Error('fail eventSubscriber.subscribe');
79
- }
80
-
81
- const chUser = clearingHouse.getUser();
82
- const chUserAccount = clearingHouse.getUserAccount();
83
- console.log(`chUserAcc.authority: ${chUserAccount!.authority.toBase58()}`);
84
- console.log(
85
- ` chUserAccount pubkey: ${chUser.userAccountPublicKey.toBase58()}`
86
- );
87
-
88
- const keyToSymbol = new Map<string, string>();
89
- const marketIndexToSymbol = new Map<number, string>();
90
- for (const market of DevnetMarkets) {
91
- keyToSymbol.set(market.oracle.toBase58(), market.baseAssetSymbol);
92
- marketIndexToSymbol.set(market.marketIndex.toNumber(), market.symbol);
93
- }
94
-
95
- const run = async () => {
96
- await clearingHouse.fetchAccounts();
97
- await chUser.fetchAccounts();
98
- printUserStats(chUserAccount!, marketIndexToSymbol);
99
- };
100
- await run();
101
- setInterval(run, 1000);
102
- }
103
-
104
- try {
105
- if (!process.env.ANCHOR_WALLET) {
106
- throw new Error('ANCHOR_WALLET must be set.');
107
- }
108
- main(
109
- anchor.AnchorProvider.local(clusterApiUrl('devnet'), {
110
- // anchor.AnchorProvider.local('https://devnet.genesysgo.net', {
111
- preflightCommitment: 'confirmed',
112
- skipPreflight: false,
113
- commitment: 'confirmed',
114
- })
115
- );
116
- // anchor.AnchorProvider.local('https://psytrbhymqlkfrhudd.dev.genesysgo.net:8899/');
117
- } catch (e) {
118
- console.error(e);
119
- }
@@ -1,11 +0,0 @@
1
- try {
2
- // const msg = 'Program 65sz7dRiWDRPZjiRxcTxPM7AE6VK4Nag9HEK6oBJXhJn failed: custom program error: 0x179e'
3
- const msg = 'Program log: AnchorError occurred. Error Code: OrderDoesNotExist. Error Number: 6046. Error Message: Order does not exist.';
4
- const code = msg.match(
5
- /Program log: AnchorError occurred. Error Code: ([0-9,a-z,A-Z]+). Error Number/
6
- );
7
- console.log(code[1]);
8
- } catch (e) {
9
- console.log("nothing");
10
- // no problem if couldn't match error code
11
- }
@@ -1,52 +0,0 @@
1
- import {
2
- ClearingHouse,
3
- UserAccount,
4
- } from '..';
5
-
6
- export function printUserStats(chUserAccount: UserAccount, marketIndexToSymbol: Map<number, string>) {
7
-
8
- console.log("");
9
- console.log(`${new Date(Date.now())}`);
10
- console.log("Open positions:");
11
- let openPositions = 0;
12
- for (const p of chUserAccount!.positions) {
13
- if (p.baseAssetAmount.isZero()) {
14
- continue;
15
- }
16
- console.log(` [${marketIndexToSymbol.get(p.marketIndex.toNumber())}]`);
17
- console.log(` . baseAssetAmount: ${p.baseAssetAmount.toString()}`);
18
- console.log(` . quoteAssetAmount: ${p.quoteAssetAmount.toString()}`);
19
- console.log(` . quoteEntryAmount: ${p.quoteEntryAmount.toString()}`);
20
- console.log(` . lastCumulativeFundingRate: ${p.lastCumulativeFundingRate.toString()}`);
21
- console.log(` . openOrders: ${p.openOrders}`);
22
- console.log(` . openBids: ${p.openBids.toString()}`);
23
- console.log(` . openAsks: ${p.openAsks.toString()}`);
24
- openPositions++;
25
- }
26
- console.log(`count: ${openPositions}`);
27
-
28
- let openOrders = 0;
29
- console.log("");
30
- console.log("Open orders:");
31
- for (const o of chUserAccount!.orders) {
32
- if (o.baseAssetAmount.isZero()) {
33
- continue;
34
- }
35
- console.log(` . [OrderId: ${o.orderId.toString()}, ${JSON.stringify(o.status)} ${JSON.stringify(o.direction)}, mktIdx: ${o.marketIndex.toString()}]: ${o.baseAssetAmountFilled.toString()}/${o.baseAssetAmount.toString()}`);
36
- openOrders++;
37
- }
38
- console.log(`count: ${openOrders}`);
39
-
40
- return;
41
- }
42
-
43
- export async function cancelOpenOrders(clearingHouse: ClearingHouse, chUserAccount: UserAccount) {
44
- for (const o of chUserAccount!.orders) {
45
- if (o.baseAssetAmount.isZero()) {
46
- continue;
47
- }
48
-
49
- const tx = await clearingHouse.cancelOrder(o.orderId);
50
- console.log(`Canceled orderID ${o.orderId.toString()} on market ${o.marketIndex.toString()}, tx: ${tx}`);
51
- }
52
- }
@@ -1,249 +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.BulkAccountLoader = void 0;
40
- var uuid_1 = require("uuid");
41
- var promiseTimeout_1 = require("../util/promiseTimeout");
42
- var GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
43
- var oneMinute = 60 * 1000;
44
- var BulkAccountLoader = /** @class */ (function () {
45
- function BulkAccountLoader(connection, commitment, pollingFrequency) {
46
- this.accountsToLoad = new Map();
47
- this.bufferAndSlotMap = new Map();
48
- this.errorCallbacks = new Map();
49
- this.lastTimeLoadingPromiseCleared = Date.now();
50
- this.mostRecentSlot = 0;
51
- this.connection = connection;
52
- this.commitment = commitment;
53
- this.pollingFrequency = pollingFrequency;
54
- }
55
- BulkAccountLoader.prototype.addAccount = function (publicKey, callback) {
56
- var existingSize = this.accountsToLoad.size;
57
- var callbackId = (0, uuid_1.v4)();
58
- var existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
59
- if (existingAccountToLoad) {
60
- existingAccountToLoad.callbacks.set(callbackId, callback);
61
- }
62
- else {
63
- var callbacks = new Map();
64
- callbacks.set(callbackId, callback);
65
- var newAccountToLoad = {
66
- publicKey: publicKey,
67
- callbacks: callbacks
68
- };
69
- this.accountsToLoad.set(publicKey.toString(), newAccountToLoad);
70
- }
71
- if (existingSize === 0) {
72
- this.startPolling();
73
- }
74
- // if a new account needs to be polled, remove the cached loadPromise in case client calls load immediately after
75
- this.loadPromise = undefined;
76
- return callbackId;
77
- };
78
- BulkAccountLoader.prototype.removeAccount = function (publicKey, callbackId) {
79
- var existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
80
- if (existingAccountToLoad) {
81
- existingAccountToLoad.callbacks["delete"](callbackId);
82
- if (existingAccountToLoad.callbacks.size === 0) {
83
- this.accountsToLoad["delete"](existingAccountToLoad.publicKey.toString());
84
- }
85
- }
86
- if (this.accountsToLoad.size === 0) {
87
- this.stopPolling();
88
- }
89
- };
90
- BulkAccountLoader.prototype.addErrorCallbacks = function (callback) {
91
- var callbackId = (0, uuid_1.v4)();
92
- this.errorCallbacks.set(callbackId, callback);
93
- return callbackId;
94
- };
95
- BulkAccountLoader.prototype.removeErrorCallbacks = function (callbackId) {
96
- this.errorCallbacks["delete"](callbackId);
97
- };
98
- BulkAccountLoader.prototype.chunks = function (array, size) {
99
- return new Array(Math.ceil(array.length / size))
100
- .fill(null)
101
- .map(function (_, index) { return index * size; })
102
- .map(function (begin) { return array.slice(begin, begin + size); });
103
- };
104
- BulkAccountLoader.prototype.load = function () {
105
- return __awaiter(this, void 0, void 0, function () {
106
- var now, chunks, e_1, _i, _a, _b, _1, callback;
107
- var _this = this;
108
- return __generator(this, function (_c) {
109
- switch (_c.label) {
110
- case 0:
111
- if (this.loadPromise) {
112
- now = Date.now();
113
- if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
114
- this.loadPromise = undefined;
115
- }
116
- else {
117
- return [2 /*return*/, this.loadPromise];
118
- }
119
- }
120
- this.loadPromise = new Promise(function (resolver) {
121
- _this.loadPromiseResolver = resolver;
122
- });
123
- this.lastTimeLoadingPromiseCleared = Date.now();
124
- _c.label = 1;
125
- case 1:
126
- _c.trys.push([1, 3, 4, 5]);
127
- chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
128
- return [4 /*yield*/, Promise.all(chunks.map(function (chunk) {
129
- return _this.loadChunk(chunk);
130
- }))];
131
- case 2:
132
- _c.sent();
133
- return [3 /*break*/, 5];
134
- case 3:
135
- e_1 = _c.sent();
136
- console.error("Error in bulkAccountLoader.load()");
137
- console.error(e_1);
138
- for (_i = 0, _a = this.errorCallbacks; _i < _a.length; _i++) {
139
- _b = _a[_i], _1 = _b[0], callback = _b[1];
140
- callback(e_1);
141
- }
142
- return [3 /*break*/, 5];
143
- case 4:
144
- this.loadPromiseResolver();
145
- this.loadPromise = undefined;
146
- return [7 /*endfinally*/];
147
- case 5: return [2 /*return*/];
148
- }
149
- });
150
- });
151
- };
152
- BulkAccountLoader.prototype.loadChunk = function (accountsToLoad) {
153
- return __awaiter(this, void 0, void 0, function () {
154
- var args, rpcResponse, newSlot, i, accountToLoad, key, oldRPCResponse, newBuffer, raw, dataType, oldBuffer;
155
- return __generator(this, function (_a) {
156
- switch (_a.label) {
157
- case 0:
158
- if (accountsToLoad.length === 0) {
159
- return [2 /*return*/];
160
- }
161
- args = [
162
- accountsToLoad.map(function (accountToLoad) {
163
- return accountToLoad.publicKey.toBase58();
164
- }),
165
- { commitment: this.commitment },
166
- ];
167
- return [4 /*yield*/, (0, promiseTimeout_1.promiseTimeout)(
168
- // @ts-ignore
169
- this.connection._rpcRequest('getMultipleAccounts', args), 10 * 1000 // 30 second timeout
170
- )];
171
- case 1:
172
- rpcResponse = _a.sent();
173
- if (rpcResponse === null) {
174
- this.log('request to rpc timed out');
175
- return [2 /*return*/];
176
- }
177
- newSlot = rpcResponse.result.context.slot;
178
- if (newSlot > this.mostRecentSlot) {
179
- this.mostRecentSlot = newSlot;
180
- }
181
- for (i in accountsToLoad) {
182
- accountToLoad = accountsToLoad[i];
183
- key = accountToLoad.publicKey.toString();
184
- oldRPCResponse = this.bufferAndSlotMap.get(key);
185
- newBuffer = undefined;
186
- if (rpcResponse.result.value[i]) {
187
- raw = rpcResponse.result.value[i].data[0];
188
- dataType = rpcResponse.result.value[i].data[1];
189
- newBuffer = Buffer.from(raw, dataType);
190
- }
191
- if (!oldRPCResponse) {
192
- this.bufferAndSlotMap.set(key, {
193
- slot: newSlot,
194
- buffer: newBuffer
195
- });
196
- this.handleAccountCallbacks(accountToLoad, newBuffer, newSlot);
197
- continue;
198
- }
199
- if (newSlot <= oldRPCResponse.slot) {
200
- continue;
201
- }
202
- oldBuffer = oldRPCResponse.buffer;
203
- if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
204
- this.bufferAndSlotMap.set(key, {
205
- slot: newSlot,
206
- buffer: newBuffer
207
- });
208
- this.handleAccountCallbacks(accountToLoad, newBuffer, newSlot);
209
- }
210
- }
211
- return [2 /*return*/];
212
- }
213
- });
214
- });
215
- };
216
- BulkAccountLoader.prototype.handleAccountCallbacks = function (accountToLoad, buffer, slot) {
217
- for (var _i = 0, _a = accountToLoad.callbacks; _i < _a.length; _i++) {
218
- var _b = _a[_i], _2 = _b[0], callback = _b[1];
219
- callback(buffer, slot);
220
- }
221
- };
222
- BulkAccountLoader.prototype.getBufferAndSlot = function (publicKey) {
223
- return this.bufferAndSlotMap.get(publicKey.toString());
224
- };
225
- BulkAccountLoader.prototype.startPolling = function () {
226
- if (this.intervalId) {
227
- return;
228
- }
229
- this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
230
- };
231
- BulkAccountLoader.prototype.stopPolling = function () {
232
- if (this.intervalId) {
233
- clearInterval(this.intervalId);
234
- this.intervalId = undefined;
235
- }
236
- };
237
- BulkAccountLoader.prototype.log = function (msg) {
238
- console.log(msg);
239
- };
240
- BulkAccountLoader.prototype.updatePollingFrequency = function (pollingFrequency) {
241
- this.stopPolling();
242
- this.pollingFrequency = pollingFrequency;
243
- if (this.accountsToLoad.size > 0) {
244
- this.startPolling();
245
- }
246
- };
247
- return BulkAccountLoader;
248
- }());
249
- exports.BulkAccountLoader = BulkAccountLoader;
@@ -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.bulkPollingUserStatsSubscribe = void 0;
40
- /**
41
- * @param userStats
42
- * @param accountLoader
43
- */
44
- function bulkPollingUserStatsSubscribe(userStats, 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 (!(userStats.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(userStats.map(function (userStat) {
56
- return userStat.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(userStats.map(function (userStat) { return __awaiter(_this, void 0, void 0, function () {
64
- return __generator(this, function (_a) {
65
- return [2 /*return*/, userStat.subscribe()];
66
- });
67
- }); }))];
68
- case 5:
69
- _a.sent();
70
- return [2 /*return*/];
71
- }
72
- });
73
- });
74
- }
75
- exports.bulkPollingUserStatsSubscribe = bulkPollingUserStatsSubscribe;