@velocity-exchange/sdk 0.4.0 → 0.6.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 (323) hide show
  1. package/README.md +3 -3
  2. package/lib/browser/adminClient.d.ts +35 -0
  3. package/lib/browser/adminClient.js +65 -0
  4. package/lib/browser/config.js +6 -7
  5. package/lib/browser/constants/numericConstants.d.ts +1 -1
  6. package/lib/browser/constants/numericConstants.js +1 -1
  7. package/lib/browser/decode/user.js +5 -1
  8. package/lib/browser/idl/velocity.d.ts +146 -3
  9. package/lib/browser/idl/velocity.json +147 -4
  10. package/lib/browser/orderSubscriber/OrderSubscriber.js +3 -0
  11. package/lib/browser/pyth/pythLazerSubscriber.js +37 -27
  12. package/lib/browser/types.d.ts +4 -0
  13. package/lib/browser/user.d.ts +15 -0
  14. package/lib/browser/user.js +37 -1
  15. package/lib/browser/velocityClient.d.ts +46 -10
  16. package/lib/browser/velocityClient.js +107 -35
  17. package/lib/node/adminClient.d.ts +35 -0
  18. package/lib/node/adminClient.d.ts.map +1 -1
  19. package/lib/node/adminClient.js +65 -0
  20. package/lib/node/config.d.ts.map +1 -1
  21. package/lib/node/config.js +6 -7
  22. package/lib/node/constants/numericConstants.d.ts +1 -1
  23. package/lib/node/constants/numericConstants.d.ts.map +1 -1
  24. package/lib/node/constants/numericConstants.js +1 -1
  25. package/lib/node/decode/user.d.ts.map +1 -1
  26. package/lib/node/decode/user.js +5 -1
  27. package/lib/node/idl/velocity.d.ts +146 -3
  28. package/lib/node/idl/velocity.d.ts.map +1 -1
  29. package/lib/node/idl/velocity.json +147 -4
  30. package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
  31. package/lib/node/orderSubscriber/OrderSubscriber.js +3 -0
  32. package/lib/node/pyth/pythLazerSubscriber.d.ts.map +1 -1
  33. package/lib/node/pyth/pythLazerSubscriber.js +37 -27
  34. package/lib/node/types.d.ts +4 -0
  35. package/lib/node/types.d.ts.map +1 -1
  36. package/lib/node/user.d.ts +15 -0
  37. package/lib/node/user.d.ts.map +1 -1
  38. package/lib/node/user.js +37 -1
  39. package/lib/node/velocityClient.d.ts +46 -10
  40. package/lib/node/velocityClient.d.ts.map +1 -1
  41. package/lib/node/velocityClient.js +107 -35
  42. package/package.json +7 -3
  43. package/.prettierignore +0 -1
  44. package/CHANGELOG.md +0 -132
  45. package/build-browser.js +0 -58
  46. package/bunfig.toml +0 -3
  47. package/esbuild-shims.js +0 -12
  48. package/scripts/deposit-isolated-positions.ts +0 -110
  49. package/scripts/find-flagged-users.ts +0 -213
  50. package/scripts/grpc-client-test-comparison.ts +0 -372
  51. package/scripts/grpc-multiuser-client-test-comparison.ts +0 -158
  52. package/scripts/postbuild.js +0 -95
  53. package/scripts/single-grpc-client-test.ts +0 -284
  54. package/scripts/withdraw-isolated-positions.ts +0 -174
  55. package/src/accounts/README_WebSocketAccountSubscriberV2.md +0 -95
  56. package/src/accounts/README_WebSocketProgramAccountSubscriberV2.md +0 -135
  57. package/src/accounts/basicUserAccountSubscriber.ts +0 -79
  58. package/src/accounts/basicUserStatsAccountSubscriber.ts +0 -87
  59. package/src/accounts/bulkAccountLoader.ts +0 -358
  60. package/src/accounts/bulkUserStatsSubscription.ts +0 -40
  61. package/src/accounts/bulkUserSubscription.ts +0 -39
  62. package/src/accounts/customizedCadenceBulkAccountLoader.ts +0 -250
  63. package/src/accounts/fetch.ts +0 -200
  64. package/src/accounts/grpcAccountSubscriber.ts +0 -225
  65. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -73
  66. package/src/accounts/grpcMultiAccountSubscriber.ts +0 -566
  67. package/src/accounts/grpcMultiUserAccountSubscriber.ts +0 -328
  68. package/src/accounts/grpcProgramAccountSubscriber.ts +0 -264
  69. package/src/accounts/grpcUserAccountSubscriber.ts +0 -63
  70. package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -65
  71. package/src/accounts/grpcVelocityClientAccountSubscriber.ts +0 -257
  72. package/src/accounts/grpcVelocityClientAccountSubscriberV2.ts +0 -941
  73. package/src/accounts/laserProgramAccountSubscriber.ts +0 -266
  74. package/src/accounts/oneShotUserAccountSubscriber.ts +0 -88
  75. package/src/accounts/oneShotUserStatsAccountSubscriber.ts +0 -94
  76. package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +0 -223
  77. package/src/accounts/pollingOracleAccountSubscriber.ts +0 -151
  78. package/src/accounts/pollingTokenAccountSubscriber.ts +0 -147
  79. package/src/accounts/pollingUserAccountSubscriber.ts +0 -198
  80. package/src/accounts/pollingUserStatsAccountSubscriber.ts +0 -180
  81. package/src/accounts/pollingVelocityClientAccountSubscriber.ts +0 -774
  82. package/src/accounts/testBulkAccountLoader.ts +0 -59
  83. package/src/accounts/types.ts +0 -428
  84. package/src/accounts/utils.ts +0 -90
  85. package/src/accounts/webSocketAccountSubscriber.ts +0 -304
  86. package/src/accounts/webSocketAccountSubscriberV2.ts +0 -536
  87. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +0 -171
  88. package/src/accounts/webSocketProgramAccountSubscriber.ts +0 -244
  89. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +0 -1048
  90. package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +0 -1042
  91. package/src/accounts/webSocketUserAccountSubscriber.ts +0 -139
  92. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +0 -131
  93. package/src/accounts/webSocketVelocityClientAccountSubscriber.ts +0 -865
  94. package/src/accounts/webSocketVelocityClientAccountSubscriberV2.ts +0 -897
  95. package/src/accounts/websocketProgramUserAccountSubscriber.ts +0 -127
  96. package/src/addresses/marketAddresses.ts +0 -28
  97. package/src/addresses/pda.ts +0 -620
  98. package/src/adminClient.ts +0 -8136
  99. package/src/assert/assert.ts +0 -5
  100. package/src/auctionSubscriber/auctionSubscriber.ts +0 -86
  101. package/src/auctionSubscriber/auctionSubscriberGrpc.ts +0 -99
  102. package/src/auctionSubscriber/index.ts +0 -9
  103. package/src/auctionSubscriber/types.ts +0 -30
  104. package/src/bankrun/bankrunConnection.ts +0 -630
  105. package/src/blockhashSubscriber/BlockhashSubscriber.ts +0 -160
  106. package/src/blockhashSubscriber/index.ts +0 -6
  107. package/src/blockhashSubscriber/types.ts +0 -13
  108. package/src/clock/clockSubscriber.ts +0 -146
  109. package/src/config.ts +0 -312
  110. package/src/constants/index.ts +0 -9
  111. package/src/constants/numericConstants.ts +0 -194
  112. package/src/constants/perpMarkets.ts +0 -127
  113. package/src/constants/spotMarkets.ts +0 -104
  114. package/src/constants/txConstants.ts +0 -2
  115. package/src/constituentMap/constituentMap.ts +0 -305
  116. package/src/constituentMap/pollingConstituentAccountSubscriber.ts +0 -99
  117. package/src/constituentMap/webSocketConstituentAccountSubscriber.ts +0 -121
  118. package/src/core/VelocityCore.ts +0 -606
  119. package/src/core/index.ts +0 -12
  120. package/src/core/instructions/deposit.ts +0 -64
  121. package/src/core/instructions/fill.ts +0 -51
  122. package/src/core/instructions/funding.ts +0 -33
  123. package/src/core/instructions/liquidation.ts +0 -55
  124. package/src/core/instructions/orders.ts +0 -84
  125. package/src/core/instructions/perpOrders.ts +0 -312
  126. package/src/core/instructions/settlement.ts +0 -40
  127. package/src/core/instructions/trigger.ts +0 -41
  128. package/src/core/instructions/withdraw.ts +0 -65
  129. package/src/core/remainingAccounts.ts +0 -320
  130. package/src/core/signedMsg.ts +0 -109
  131. package/src/decode/customCoder.ts +0 -148
  132. package/src/decode/user.ts +0 -401
  133. package/src/dlob/DLOB.ts +0 -2793
  134. package/src/dlob/DLOBNode.ts +0 -305
  135. package/src/dlob/DLOBSubscriber.ts +0 -269
  136. package/src/dlob/NodeList.ts +0 -244
  137. package/src/dlob/orderBookLevels.ts +0 -695
  138. package/src/dlob/types.ts +0 -37
  139. package/src/events/eventList.ts +0 -114
  140. package/src/events/eventSubscriber.ts +0 -521
  141. package/src/events/eventsServerLogProvider.ts +0 -189
  142. package/src/events/fetchLogs.ts +0 -219
  143. package/src/events/parse.ts +0 -271
  144. package/src/events/pollingLogProvider.ts +0 -113
  145. package/src/events/sort.ts +0 -51
  146. package/src/events/txEventCache.ts +0 -92
  147. package/src/events/types.ts +0 -337
  148. package/src/events/webSocketLogProvider.ts +0 -152
  149. package/src/factory/bigNum.ts +0 -709
  150. package/src/factory/oracleClient.ts +0 -73
  151. package/src/idl/pyth.json +0 -142
  152. package/src/idl/pyth.ts +0 -97
  153. package/src/idl/pyth_solana_receiver.json +0 -628
  154. package/src/idl/token_faucet.json +0 -229
  155. package/src/idl/token_faucet.ts +0 -197
  156. package/src/idl/velocity.json +0 -23974
  157. package/src/idl/velocity.ts +0 -23980
  158. package/src/index.ts +0 -150
  159. package/src/indicative-quotes/indicativeQuotesSender.ts +0 -233
  160. package/src/isomorphic/README.md +0 -19
  161. package/src/isomorphic/anchor.browser.ts +0 -44
  162. package/src/isomorphic/anchor.node.ts +0 -1
  163. package/src/isomorphic/anchor.ts +0 -1
  164. package/src/isomorphic/anchor29.browser.ts +0 -24
  165. package/src/isomorphic/anchor29.node.ts +0 -1
  166. package/src/isomorphic/anchor29.ts +0 -1
  167. package/src/isomorphic/grpc.browser.ts +0 -4
  168. package/src/isomorphic/grpc.node.ts +0 -130
  169. package/src/isomorphic/grpc.ts +0 -1
  170. package/src/jupiter/jupiterClient.ts +0 -502
  171. package/src/keypair.ts +0 -32
  172. package/src/margin/README.md +0 -138
  173. package/src/marginCalculation.ts +0 -407
  174. package/src/marinade/idl/idl.json +0 -1962
  175. package/src/marinade/index.ts +0 -73
  176. package/src/marinade/types.ts +0 -3925
  177. package/src/math/amm.ts +0 -1758
  178. package/src/math/auction.ts +0 -480
  179. package/src/math/bankruptcy.ts +0 -138
  180. package/src/math/builder.ts +0 -130
  181. package/src/math/conversion.ts +0 -45
  182. package/src/math/exchangeStatus.ts +0 -261
  183. package/src/math/funding.ts +0 -498
  184. package/src/math/insurance.ts +0 -171
  185. package/src/math/liquidation.ts +0 -433
  186. package/src/math/margin.ts +0 -327
  187. package/src/math/market.ts +0 -487
  188. package/src/math/oracles.ts +0 -477
  189. package/src/math/orders.ts +0 -500
  190. package/src/math/position.ts +0 -381
  191. package/src/math/repeg.ts +0 -223
  192. package/src/math/spotBalance.ts +0 -996
  193. package/src/math/spotMarket.ts +0 -115
  194. package/src/math/spotPosition.ts +0 -263
  195. package/src/math/state.ts +0 -71
  196. package/src/math/superStake.ts +0 -602
  197. package/src/math/tiers.ts +0 -73
  198. package/src/math/trade.ts +0 -879
  199. package/src/math/utils.ts +0 -183
  200. package/src/memcmp.ts +0 -279
  201. package/src/oracles/oracleClientCache.ts +0 -41
  202. package/src/oracles/oracleId.ts +0 -99
  203. package/src/oracles/prelaunchOracleClient.ts +0 -63
  204. package/src/oracles/pythClient.ts +0 -125
  205. package/src/oracles/pythLazerClient.ts +0 -141
  206. package/src/oracles/quoteAssetOracleClient.ts +0 -43
  207. package/src/oracles/strictOraclePrice.ts +0 -40
  208. package/src/oracles/types.ts +0 -82
  209. package/src/oracles/utils.ts +0 -45
  210. package/src/orderParams.ts +0 -117
  211. package/src/orderSubscriber/OrderSubscriber.ts +0 -378
  212. package/src/orderSubscriber/PollingSubscription.ts +0 -50
  213. package/src/orderSubscriber/WebsocketSubscription.ts +0 -137
  214. package/src/orderSubscriber/grpcSubscription.ts +0 -155
  215. package/src/orderSubscriber/index.ts +0 -7
  216. package/src/orderSubscriber/types.ts +0 -64
  217. package/src/priorityFee/averageOverSlotsStrategy.ts +0 -17
  218. package/src/priorityFee/averageStrategy.ts +0 -13
  219. package/src/priorityFee/ewmaStrategy.ts +0 -51
  220. package/src/priorityFee/heliusPriorityFeeMethod.ts +0 -68
  221. package/src/priorityFee/index.ts +0 -16
  222. package/src/priorityFee/maxOverSlotsStrategy.ts +0 -18
  223. package/src/priorityFee/maxStrategy.ts +0 -8
  224. package/src/priorityFee/priorityFeeSubscriber.ts +0 -328
  225. package/src/priorityFee/priorityFeeSubscriberMap.ts +0 -134
  226. package/src/priorityFee/solanaPriorityFeeMethod.ts +0 -46
  227. package/src/priorityFee/types.ts +0 -82
  228. package/src/priorityFee/velocityPriorityFeeMethod.ts +0 -53
  229. package/src/pyth/constants.ts +0 -9
  230. package/src/pyth/index.ts +0 -15
  231. package/src/pyth/pythLazerSubscriber.ts +0 -365
  232. package/src/pyth/types.ts +0 -4451
  233. package/src/pyth/utils.ts +0 -13
  234. package/src/slot/SlotSubscriber.ts +0 -126
  235. package/src/slot/SlothashSubscriber.ts +0 -163
  236. package/src/swap/UnifiedSwapClient.ts +0 -315
  237. package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +0 -81
  238. package/src/swift/index.ts +0 -10
  239. package/src/swift/signedMsgUserAccountSubscriber.ts +0 -251
  240. package/src/swift/swiftOrderSubscriber.ts +0 -351
  241. package/src/testClient.ts +0 -42
  242. package/src/titan/titanClient.ts +0 -438
  243. package/src/token/index.ts +0 -13
  244. package/src/tokenFaucet.ts +0 -296
  245. package/src/tx/baseTxSender.ts +0 -651
  246. package/src/tx/blockhashFetcher/baseBlockhashFetcher.ts +0 -31
  247. package/src/tx/blockhashFetcher/cachedBlockhashFetcher.ts +0 -105
  248. package/src/tx/blockhashFetcher/types.ts +0 -9
  249. package/src/tx/fastSingleTxSender.ts +0 -200
  250. package/src/tx/forwardOnlyTxSender.ts +0 -193
  251. package/src/tx/priorityFeeCalculator.ts +0 -117
  252. package/src/tx/reportTransactionError.ts +0 -176
  253. package/src/tx/retryTxSender.ts +0 -177
  254. package/src/tx/txHandler.ts +0 -1009
  255. package/src/tx/txParamProcessor.ts +0 -225
  256. package/src/tx/types.ts +0 -117
  257. package/src/tx/utils.ts +0 -133
  258. package/src/tx/whileValidTxSender.ts +0 -377
  259. package/src/types.ts +0 -2575
  260. package/src/user.ts +0 -5316
  261. package/src/userConfig.ts +0 -38
  262. package/src/userMap/PollingSubscription.ts +0 -61
  263. package/src/userMap/WebsocketSubscription.ts +0 -93
  264. package/src/userMap/grpcSubscription.ts +0 -93
  265. package/src/userMap/referrerMap.ts +0 -360
  266. package/src/userMap/revenueShareEscrowMap.ts +0 -342
  267. package/src/userMap/userMap.ts +0 -784
  268. package/src/userMap/userMapConfig.ts +0 -82
  269. package/src/userMap/userStatsMap.ts +0 -393
  270. package/src/userName.ts +0 -37
  271. package/src/userStats.ts +0 -159
  272. package/src/userStatsConfig.ts +0 -35
  273. package/src/util/TransactionConfirmationManager.ts +0 -333
  274. package/src/util/chainClock.ts +0 -65
  275. package/src/util/computeUnits.ts +0 -101
  276. package/src/util/digest.ts +0 -33
  277. package/src/util/ed25519Utils.ts +0 -95
  278. package/src/util/promiseTimeout.ts +0 -23
  279. package/src/velocityClient.ts +0 -14802
  280. package/src/velocityClientConfig.ts +0 -119
  281. package/src/wallet.ts +0 -91
  282. package/tests/VelocityCore/builder_escrow.test.ts +0 -121
  283. package/tests/VelocityCore/decode.test.ts +0 -16
  284. package/tests/VelocityCore/fill_trigger.test.ts +0 -73
  285. package/tests/VelocityCore/instructions.test.ts +0 -46
  286. package/tests/VelocityCore/pdas.test.ts +0 -30
  287. package/tests/VelocityCore/perp_orders.test.ts +0 -205
  288. package/tests/VelocityCore/remainingAccounts.test.ts +0 -72
  289. package/tests/VelocityCore/settlement_liquidation.test.ts +0 -69
  290. package/tests/accounts/customizedCadenceBulkAccountLoader.test.ts +0 -211
  291. package/tests/amm/test.ts +0 -2402
  292. package/tests/auctions/test.ts +0 -81
  293. package/tests/bn/test.ts +0 -355
  294. package/tests/builder/builderFee.test.ts +0 -42
  295. package/tests/ci/verifyConstants.ts +0 -393
  296. package/tests/decode/test.ts +0 -262
  297. package/tests/decode/userAccountBufferStrings.ts +0 -102
  298. package/tests/dlob/helpers.ts +0 -748
  299. package/tests/dlob/test.ts +0 -6969
  300. package/tests/dlob/tickSizeStandardization.ts +0 -545
  301. package/tests/events/parseLogsForCuUsage.ts +0 -139
  302. package/tests/exchangeStatus/test.ts +0 -45
  303. package/tests/insurance/test.ts +0 -40
  304. package/tests/liquidation/test.ts +0 -125
  305. package/tests/oracles/mmOracleGate.test.ts +0 -379
  306. package/tests/oracles/pythPegSnap.test.ts +0 -76
  307. package/tests/sdkParity/enumParity.test.ts +0 -84
  308. package/tests/sdkParity/marginCategoryFill.test.ts +0 -143
  309. package/tests/sdkParity/memcmpOffsets.test.ts +0 -139
  310. package/tests/spot/test.ts +0 -281
  311. package/tests/tx/TransactionConfirmationManager.test.ts +0 -312
  312. package/tests/tx/cachedBlockhashFetcher.test.ts +0 -96
  313. package/tests/tx/priorityFeeCalculator.ts +0 -77
  314. package/tests/tx/priorityFeeStrategy.ts +0 -95
  315. package/tests/user/bankruptcy.ts +0 -165
  316. package/tests/user/feeAndWithdrawLimits.ts +0 -284
  317. package/tests/user/getMarginCalculation.ts +0 -586
  318. package/tests/user/helpers.ts +0 -184
  319. package/tests/user/liquidations.ts +0 -129
  320. package/tests/user/marginCalculations.test.ts +0 -321
  321. package/tests/user/test.ts +0 -833
  322. package/tsconfig.browser.json +0 -18
  323. package/tsconfig.json +0 -36
@@ -1,1042 +0,0 @@
1
- import { BufferAndSlot, ProgramAccountSubscriber, ResubOpts } from './types';
2
- import { AnchorProvider } from '../isomorphic/anchor';
3
- import { VelocityProgram } from '../config';
4
- import { Commitment, Context, MemcmpFilter, PublicKey } from '@solana/web3.js';
5
- import {
6
- AccountInfoBase,
7
- AccountInfoWithBase58EncodedData,
8
- AccountInfoWithBase64EncodedData,
9
- createSolanaClient,
10
- isAddress,
11
- Lamports,
12
- Slot,
13
- Address,
14
- Commitment as GillCommitment,
15
- } from 'gill';
16
- import bs58 from 'bs58';
17
-
18
- type ProgramAccountSubscriptionAsyncIterable = AsyncIterable<
19
- Readonly<{
20
- context: Readonly<{
21
- slot: Slot;
22
- }>;
23
- value: Readonly<{
24
- account: Readonly<{
25
- executable: boolean;
26
- lamports: Lamports;
27
- owner: Address;
28
- rentEpoch: bigint;
29
- space: bigint;
30
- }> &
31
- Readonly<any>;
32
- pubkey: Address;
33
- }>;
34
- }>
35
- >;
36
- /**
37
- * WebSocketProgramAccountsSubscriberV2
38
- *
39
- * High-level overview
40
- * - WebSocket-first subscriber for Solana program accounts that also layers in
41
- * targeted polling to detect missed updates reliably.
42
- * - Emits decoded account updates via the provided `onChange` callback.
43
- * - Designed to focus extra work on the specific accounts the consumer cares
44
- * about ("monitored accounts") while keeping baseline WS behavior for the
45
- * full program subscription.
46
- *
47
- * Why polling if this is a WebSocket subscriber?
48
- * - WS infra can stall, drop, or reorder notifications under network stress or
49
- * provider hiccups. When that happens, critical account changes can be missed.
50
- * - To mitigate this, the class accepts a set of accounts (provided via constructor) to monitor
51
- * and uses light polling to verify whether a WS change was missed.
52
- * - If polling detects a newer slot with different data than the last seen
53
- * buffer, a centralized resubscription is triggered to restore a clean stream.
54
- *
55
- * Initial fetch (on subscribe)
56
- * - On `subscribe()`, we first perform a single batched fetch of all monitored
57
- * accounts ("initial monitor fetch").
58
- * - Purpose: seed the internal `bufferAndSlotMap` and emit the latest state so
59
- * consumers have up-to-date data immediately, even before WS events arrive.
60
- * - This step does not decide resubscription; it only establishes ground truth.
61
- *
62
- * Continuous polling (only for monitored accounts)
63
- * - After seeding, each monitored account is put into a monitoring cycle:
64
- * 1) If no WS notification for an account is observed for `pollingIntervalMs`,
65
- * we enqueue it for a batched fetch (buffered for a short window).
66
- * 2) Once an account enters the "currently polling" set, a shared batch poll
67
- * runs every `pollingIntervalMs` across all such accounts.
68
- * 3) If WS notifications resume for an account, that account is removed from
69
- * the polling set and returns to passive monitoring.
70
- * - Polling compares the newly fetched buffer with the last stored buffer at a
71
- * later slot. A difference indicates a missed update; we schedule a single
72
- * resubscription (coalesced across accounts) to re-sync.
73
- *
74
- * Accounts the consumer cares about
75
- * - Provide accounts up-front via the constructor `accountsToMonitor`, or add
76
- * them dynamically with `addAccountToMonitor()` and remove with
77
- * `removeAccountFromMonitor()`.
78
- * - Only these accounts incur additional polling safeguards; other accounts are
79
- * still processed from the WS stream normally.
80
- *
81
- * Resubscription strategy
82
- * - Missed updates from any monitored account are coalesced and trigger a single
83
- * resubscription after a short delay. This avoids rapid churn.
84
- * - If `resubOpts.resubTimeoutMs` is set, an inactivity timer also performs a
85
- * batch check of monitored accounts. If a missed update is found, the same
86
- * centralized resubscription flow is used.
87
- *
88
- * Tuning knobs
89
- * - `setPollingInterval(ms)`: adjust how often monitoring/polling runs
90
- * (default 30s). Shorter = faster detection, higher RPC load.
91
- * - Debounced immediate poll (~100ms): batches accounts added to polling right after inactivity.
92
- * - Batch size for `getMultipleAccounts` is limited to 100, requests are chunked
93
- * and processed concurrently.
94
- */
95
-
96
- export class WebSocketProgramAccountsSubscriberV2<T>
97
- implements ProgramAccountSubscriber<T>
98
- {
99
- subscriptionName: string;
100
- accountDiscriminator: string;
101
- bufferAndSlotMap: Map<string, BufferAndSlot> = new Map();
102
- program: VelocityProgram;
103
- decodeBuffer: (accountName: string, ix: Buffer) => T;
104
- private _onChange?: (
105
- accountId: PublicKey,
106
- data: T,
107
- context: Context,
108
- buffer: Buffer
109
- ) => void;
110
- get onChange(): (
111
- accountId: PublicKey,
112
- data: T,
113
- context: Context,
114
- buffer: Buffer
115
- ) => void {
116
- if (!this._onChange) {
117
- throw new Error('onChange callback function must be set');
118
- }
119
- return this._onChange;
120
- }
121
- set onChange(
122
- onChange: (
123
- accountId: PublicKey,
124
- data: T,
125
- context: Context,
126
- buffer: Buffer
127
- ) => void
128
- ) {
129
- this._onChange = onChange;
130
- }
131
- listenerId?: number;
132
- resubOpts: ResubOpts;
133
- isUnsubscribing = false;
134
- timeoutId?: ReturnType<typeof setTimeout>;
135
- options: { filters: MemcmpFilter[]; commitment?: Commitment };
136
-
137
- receivingData = false;
138
-
139
- // Gill client components
140
- private rpc: ReturnType<typeof createSolanaClient>['rpc'];
141
- private rpcSubscriptions: ReturnType<
142
- typeof createSolanaClient
143
- >['rpcSubscriptions'];
144
- private abortController?: AbortController;
145
-
146
- // Polling logic for specific accounts
147
- private accountsToMonitor: Set<string> = new Set();
148
- private pollingIntervalMs: number = 30000; // 30 seconds
149
- private pollingTimeouts: Map<string, ReturnType<typeof setTimeout>> =
150
- new Map();
151
- private lastWsNotificationTime: Map<string, number> = new Map(); // Track last WS notification time per account
152
- private accountsCurrentlyPolling: Set<string> = new Set(); // Track which accounts are being polled
153
- private batchPollingTimeout?: ReturnType<typeof setTimeout>; // Single timeout for batch polling
154
-
155
- // Debounced immediate poll to batch multiple additions within a short window
156
- private debouncedImmediatePollTimeout?: ReturnType<typeof setTimeout>;
157
- private debouncedImmediatePollMs: number = 100; // configurable short window
158
-
159
- // Centralized resubscription handling
160
- private missedChangeDetected = false; // Flag to track if any missed change was detected
161
- private resubscriptionTimeout?: ReturnType<typeof setTimeout>; // Timeout for delayed resubscription
162
- private accountsWithMissedUpdates: Set<string> = new Set(); // Track which accounts had missed updates
163
-
164
- /**
165
- * @param subscriptionName Human-readable name for logging.
166
- * @param accountDiscriminator Anchor account type name passed to `decodeBufferFn` for each update.
167
- * @param program Anchor program whose accounts to watch (filtered to `program.programId` as owner); its connection's RPC endpoint is reused to build the `gill` client.
168
- * @param decodeBufferFn Decode function for each account's raw buffer.
169
- * @param options `filters` narrowing which program accounts are watched; `commitment` for the WS subscription and RPC polls.
170
- * @param resubOpts Resubscription/polling options; defaults to `{ resubTimeoutMs: 30000, usePollingInsteadOfResub: true }` if omitted (stricter than the v1 `WebSocketProgramAccountSubscriber`, which defaults to no watchdog at all).
171
- * @param accountsToMonitor Initial set of accounts to apply the polling safeguard to; more can be added later via `addAccountToMonitor`.
172
- */
173
- public constructor(
174
- subscriptionName: string,
175
- accountDiscriminator: string,
176
- program: VelocityProgram,
177
- decodeBufferFn: (accountName: string, ix: Buffer) => T,
178
- options: { filters: MemcmpFilter[]; commitment?: Commitment } = {
179
- filters: [],
180
- },
181
- resubOpts?: ResubOpts,
182
- accountsToMonitor?: PublicKey[] // Optional list of accounts to poll
183
- ) {
184
- this.subscriptionName = subscriptionName;
185
- this.accountDiscriminator = accountDiscriminator;
186
- this.program = program;
187
- this.decodeBuffer = decodeBufferFn;
188
- this.resubOpts = resubOpts ?? {
189
- resubTimeoutMs: 30000,
190
- usePollingInsteadOfResub: true,
191
- logResubMessages: false,
192
- };
193
- if (
194
- this.resubOpts.resubTimeoutMs != null &&
195
- this.resubOpts.resubTimeoutMs < 1000
196
- ) {
197
- console.log(
198
- 'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
199
- );
200
- }
201
- this.options = options;
202
- this.receivingData = false;
203
-
204
- // Initialize accounts to monitor
205
- if (accountsToMonitor) {
206
- accountsToMonitor.forEach((account) => {
207
- this.accountsToMonitor.add(account.toBase58());
208
- });
209
- }
210
-
211
- // Initialize gill client using the same RPC URL as the program provider
212
- const rpcUrl = (this.program.provider as AnchorProvider).connection
213
- .rpcEndpoint;
214
- const { rpc, rpcSubscriptions } = createSolanaClient({
215
- urlOrMoniker: rpcUrl,
216
- });
217
- this.rpc = rpc;
218
- this.rpcSubscriptions = rpcSubscriptions;
219
- }
220
-
221
- private async handleNotificationLoop(
222
- notificationPromise: Promise<ProgramAccountSubscriptionAsyncIterable>
223
- ) {
224
- try {
225
- const subscriptionIterable = await notificationPromise;
226
- for await (const notification of subscriptionIterable) {
227
- try {
228
- if (this.resubOpts?.resubTimeoutMs) {
229
- this.receivingData = true;
230
- clearTimeout(this.timeoutId);
231
- this.handleRpcResponse(
232
- notification.context,
233
- notification.value.pubkey,
234
- notification.value.account.data
235
- );
236
- this.setTimeout();
237
- } else {
238
- this.handleRpcResponse(
239
- notification.context,
240
- notification.value.pubkey,
241
- notification.value.account.data
242
- );
243
- }
244
- } catch (error) {
245
- console.error(
246
- `Error handling RPC response for pubkey ${notification.value.pubkey}:`,
247
- error
248
- );
249
- }
250
- }
251
- } catch (error) {
252
- console.error(
253
- `[${this.subscriptionName}] Error in notification loop:`,
254
- error
255
- );
256
- }
257
- }
258
-
259
- async subscribe(
260
- onChange: (
261
- accountId: PublicKey,
262
- data: T,
263
- context: Context,
264
- buffer: Buffer
265
- ) => void
266
- ): Promise<void> {
267
- /**
268
- * Start the WebSocket subscription and initialize polling safeguards.
269
- *
270
- * Flow
271
- * - Seeds all monitored accounts with a single batched RPC fetch and emits
272
- * their current state.
273
- * - Subscribes to program notifications via WS using gill.
274
- * - If `resubOpts.resubTimeoutMs` is set, starts an inactivity timer that
275
- * batch-checks monitored accounts when WS goes quiet.
276
- * - Begins monitoring for accounts that may need polling when WS
277
- * notifications are not observed within `pollingIntervalMs`.
278
- *
279
- * @param onChange Callback invoked with decoded account data when an update
280
- * is detected (via WS or batch RPC fetch).
281
- */
282
- const startTime = performance.now();
283
- if (this.listenerId != null || this.isUnsubscribing) {
284
- return;
285
- }
286
-
287
- if (this.resubOpts?.logResubMessages) {
288
- console.log(
289
- `[${this.subscriptionName}] initializing subscription. This many monitored accounts: ${this.accountsToMonitor.size}`
290
- );
291
- }
292
-
293
- this.onChange = onChange;
294
-
295
- // initial fetch of monitored data - only fetch and populate, don't check for missed changes
296
- await this.fetchAndPopulateAllMonitoredAccounts();
297
-
298
- // Create abort controller for proper cleanup
299
- const abortController = new AbortController();
300
- this.abortController = abortController;
301
-
302
- this.listenerId = Math.random(); // Unique ID for logging purposes
303
-
304
- if (this.resubOpts?.resubTimeoutMs) {
305
- this.receivingData = true;
306
- this.setTimeout();
307
- }
308
-
309
- // Subscribe to program account changes using gill's rpcSubscriptions
310
- const programId = this.program.programId.toBase58();
311
- if (isAddress(programId)) {
312
- const subscriptionPromise = this.rpcSubscriptions
313
- .programNotifications(programId, {
314
- commitment: this.options.commitment as GillCommitment,
315
- encoding: 'base64',
316
- filters: this.options.filters.map((filter) => {
317
- // Convert filter bytes from base58 to base64 if needed
318
- let bytes = filter.memcmp.bytes;
319
- if (
320
- typeof bytes === 'string' &&
321
- /^[1-9A-HJ-NP-Za-km-z]+$/.test(bytes)
322
- ) {
323
- // Looks like base58 - convert to base64
324
- const decoded = bs58.decode(bytes);
325
- bytes = Buffer.from(decoded).toString('base64');
326
- }
327
-
328
- return {
329
- memcmp: {
330
- offset: BigInt(filter.memcmp.offset),
331
- bytes: bytes as any,
332
- encoding: 'base64' as const,
333
- },
334
- };
335
- }),
336
- })
337
- .subscribe({
338
- abortSignal: abortController.signal,
339
- });
340
-
341
- // Start notification loop without awaiting
342
- this.handleNotificationLoop(subscriptionPromise);
343
- // Start monitoring for accounts that may need polling if no WS event is received
344
- this.startMonitoringForAccounts();
345
- }
346
- const endTime = performance.now();
347
- console.log(
348
- `[PROFILING] ${this.subscriptionName}.subscribe() completed in ${
349
- endTime - startTime
350
- }ms`
351
- );
352
- }
353
-
354
- protected setTimeout(): void {
355
- if (!this._onChange) {
356
- throw new Error('onChange callback function must be set');
357
- }
358
- this.timeoutId = setTimeout(
359
- async () => {
360
- if (this.isUnsubscribing) {
361
- // If we are in the process of unsubscribing, do not attempt to resubscribe
362
- return;
363
- }
364
-
365
- if (this.receivingData) {
366
- if (this.resubOpts?.logResubMessages) {
367
- console.log(
368
- `No ws data from ${this.subscriptionName} in ${this.resubOpts?.resubTimeoutMs}ms, checking for missed changes`
369
- );
370
- }
371
-
372
- // Check for missed changes in monitored accounts
373
- const missedChangeDetected = await this.fetchAllMonitoredAccounts();
374
-
375
- if (missedChangeDetected) {
376
- // Signal missed change with a generic identifier since we don't have specific account IDs from this context
377
- this.signalMissedChange('timeout-check');
378
- } else {
379
- // No missed changes, continue monitoring
380
- this.receivingData = false;
381
- this.setTimeout();
382
- }
383
- }
384
- },
385
- this.resubOpts?.resubTimeoutMs
386
- );
387
- }
388
-
389
- /**
390
- * Applies a raw notification (or polled response) for one account: decodes and stores it
391
- * (updating that account's entry in `bufferAndSlotMap` and invoking `onChange`) only if the
392
- * slot is not older than the cached one for that specific account and the buffer's bytes
393
- * actually changed. Also records the WS notification time and, if this account was in the
394
- * polling set, removes it from polling now that a fresh WS update arrived.
395
- */
396
- handleRpcResponse(
397
- context: { slot: bigint },
398
- accountId: Address,
399
- accountInfo?: AccountInfoBase &
400
- (
401
- | AccountInfoWithBase58EncodedData
402
- | AccountInfoWithBase64EncodedData
403
- )['data']
404
- ): void {
405
- const newSlot = Number(context.slot);
406
- let newBuffer: Buffer | undefined = undefined;
407
-
408
- if (accountInfo) {
409
- // Handle different data formats from gill
410
- if (Array.isArray(accountInfo)) {
411
- // If it's a tuple [data, encoding]
412
- const [data, encoding] = accountInfo;
413
-
414
- if (encoding === ('base58' as any)) {
415
- // Convert base58 to buffer using bs58
416
- newBuffer = Buffer.from(bs58.decode(data));
417
- } else {
418
- newBuffer = Buffer.from(data, 'base64');
419
- }
420
- }
421
- }
422
-
423
- const accountIdString = accountId.toString();
424
- const existingBufferAndSlot = this.bufferAndSlotMap.get(accountIdString);
425
-
426
- // Track WebSocket notification time for this account
427
- this.lastWsNotificationTime.set(accountIdString, Date.now());
428
-
429
- // If this account was being polled, stop polling it if the buffer has changed
430
- if (
431
- this.accountsCurrentlyPolling.has(accountIdString) &&
432
- (!existingBufferAndSlot?.buffer ||
433
- (newBuffer != null && !existingBufferAndSlot.buffer.equals(newBuffer)))
434
- ) {
435
- this.accountsCurrentlyPolling.delete(accountIdString);
436
-
437
- // If no more accounts are being polled, stop batch polling
438
- if (
439
- this.accountsCurrentlyPolling.size === 0 &&
440
- this.batchPollingTimeout
441
- ) {
442
- clearTimeout(this.batchPollingTimeout);
443
- this.batchPollingTimeout = undefined;
444
- }
445
- }
446
-
447
- if (!existingBufferAndSlot) {
448
- if (newBuffer) {
449
- this.updateBufferAndHandleChange(newBuffer, newSlot, accountIdString);
450
- }
451
- return;
452
- }
453
-
454
- if (newSlot < existingBufferAndSlot.slot) {
455
- return;
456
- }
457
-
458
- const oldBuffer = existingBufferAndSlot.buffer;
459
- if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
460
- this.updateBufferAndHandleChange(newBuffer, newSlot, accountIdString);
461
- }
462
- }
463
-
464
- private startMonitoringForAccounts(): void {
465
- // Clear any existing polling timeouts
466
- this.clearPollingTimeouts();
467
-
468
- // Start monitoring for each account in the accountsToMonitor set
469
- this.accountsToMonitor.forEach((accountIdString) => {
470
- this.startMonitoringForAccount(accountIdString);
471
- });
472
- }
473
-
474
- private startMonitoringForAccount(accountIdString: string): void {
475
- // Clear existing timeout for this account
476
- const existingTimeout = this.pollingTimeouts.get(accountIdString);
477
- if (existingTimeout) {
478
- clearTimeout(existingTimeout);
479
- }
480
-
481
- // Set up monitoring timeout - only start polling if no WS notification in 30s
482
- const timeoutId = setTimeout(async () => {
483
- // Check if we've received a WS notification for this account recently
484
- const lastNotificationTime =
485
- this.lastWsNotificationTime.get(accountIdString) || 0;
486
- const currentTime = Date.now();
487
-
488
- if (
489
- !lastNotificationTime ||
490
- currentTime - lastNotificationTime >= this.pollingIntervalMs
491
- ) {
492
- if (this.resubOpts?.logResubMessages) {
493
- console.debug(
494
- `[${this.subscriptionName}] No recent WS notification for ${accountIdString}, adding to polling set`
495
- );
496
- }
497
- // No recent WS notification: add to polling and schedule debounced poll
498
- this.accountsCurrentlyPolling.add(accountIdString);
499
- this.scheduleDebouncedImmediatePoll();
500
- } else {
501
- // We received a WS notification recently, continue monitoring
502
- this.startMonitoringForAccount(accountIdString);
503
- }
504
- }, this.pollingIntervalMs);
505
-
506
- this.pollingTimeouts.set(accountIdString, timeoutId);
507
- }
508
-
509
- private scheduleDebouncedImmediatePoll(): void {
510
- if (this.debouncedImmediatePollTimeout) {
511
- clearTimeout(this.debouncedImmediatePollTimeout);
512
- }
513
- this.debouncedImmediatePollTimeout = setTimeout(async () => {
514
- try {
515
- await this.pollAllAccounts();
516
- // After the immediate poll, ensure continuous batch polling is active
517
- if (
518
- !this.batchPollingTimeout &&
519
- this.accountsCurrentlyPolling.size > 0
520
- ) {
521
- this.startBatchPolling();
522
- }
523
- } catch (e) {
524
- if (this.resubOpts?.logResubMessages) {
525
- console.log(
526
- `[${this.subscriptionName}] Error during debounced immediate poll:`,
527
- e
528
- );
529
- }
530
- }
531
- }, this.debouncedImmediatePollMs);
532
- }
533
-
534
- private startBatchPolling(): void {
535
- if (this.resubOpts?.logResubMessages) {
536
- console.debug(`[${this.subscriptionName}] Scheduling batch polling`);
537
- }
538
- // Clear existing batch polling timeout
539
- if (this.batchPollingTimeout) {
540
- clearTimeout(this.batchPollingTimeout);
541
- }
542
-
543
- // Set up batch polling interval
544
- this.batchPollingTimeout = setTimeout(async () => {
545
- await this.pollAllAccounts();
546
- // Schedule next batch poll
547
- this.startBatchPolling();
548
- }, this.pollingIntervalMs);
549
- }
550
-
551
- private async pollAllAccounts(): Promise<void> {
552
- try {
553
- // Get all accounts currently being polled
554
- const accountsToPoll = Array.from(this.accountsCurrentlyPolling);
555
- if (accountsToPoll.length === 0) {
556
- return;
557
- }
558
-
559
- if (this.resubOpts?.logResubMessages) {
560
- console.debug(
561
- `[${this.subscriptionName}] Polling all accounts`,
562
- accountsToPoll.length,
563
- 'accounts'
564
- );
565
- }
566
-
567
- // Use the shared batch fetch method
568
- await this.fetchAccountsBatch(accountsToPoll);
569
- } catch (error) {
570
- if (this.resubOpts?.logResubMessages) {
571
- console.log(
572
- `[${this.subscriptionName}] Error batch polling accounts:`,
573
- error
574
- );
575
- }
576
- }
577
- }
578
-
579
- /**
580
- * Fetches and populates all monitored accounts data without checking for missed changes
581
- * This is used during initial subscription to populate data
582
- */
583
- private async fetchAndPopulateAllMonitoredAccounts(): Promise<void> {
584
- try {
585
- // Get all accounts currently being polled
586
- const accountsToMonitor = Array.from(this.accountsToMonitor);
587
- if (accountsToMonitor.length === 0) {
588
- return;
589
- }
590
-
591
- // Fetch all accounts in a single batch request
592
- const accountAddresses = accountsToMonitor.map(
593
- (accountId) => accountId as Address
594
- );
595
- const rpcResponse = await this.rpc
596
- .getMultipleAccounts(accountAddresses, {
597
- commitment: this.options.commitment as GillCommitment,
598
- encoding: 'base64',
599
- })
600
- .send();
601
-
602
- const currentSlot = Number(rpcResponse.context.slot);
603
-
604
- // Process each account response
605
- for (let i = 0; i < accountsToMonitor.length; i++) {
606
- const accountIdString = accountsToMonitor[i];
607
- const accountInfo = rpcResponse.value[i];
608
-
609
- if (!accountInfo) {
610
- continue;
611
- }
612
-
613
- const existingBufferAndSlot =
614
- this.bufferAndSlotMap.get(accountIdString);
615
-
616
- if (!existingBufferAndSlot) {
617
- // Account not in our map yet, add it
618
- let newBuffer: Buffer | undefined = undefined;
619
- if (accountInfo) {
620
- if (Array.isArray(accountInfo.data)) {
621
- const [data, encoding] = accountInfo.data;
622
- newBuffer = Buffer.from(data, encoding);
623
- }
624
- }
625
-
626
- if (newBuffer) {
627
- this.updateBufferAndHandleChange(
628
- newBuffer,
629
- currentSlot,
630
- accountIdString
631
- );
632
- }
633
- continue;
634
- }
635
-
636
- // For initial population, just update the slot if we have newer data
637
- if (currentSlot > existingBufferAndSlot.slot) {
638
- let newBuffer: Buffer | undefined = undefined;
639
- if (accountInfo.data) {
640
- if (Array.isArray(accountInfo.data)) {
641
- const [data, encoding] = accountInfo.data;
642
- if (encoding === ('base58' as any)) {
643
- newBuffer = Buffer.from(bs58.decode(data));
644
- } else {
645
- newBuffer = Buffer.from(data, 'base64');
646
- }
647
- }
648
- }
649
-
650
- // Update with newer data if available
651
- if (newBuffer) {
652
- this.updateBufferAndHandleChange(
653
- newBuffer,
654
- currentSlot,
655
- accountIdString
656
- );
657
- }
658
- }
659
- }
660
- } catch (error) {
661
- if (this.resubOpts?.logResubMessages) {
662
- console.log(
663
- `[${this.subscriptionName}] Error fetching and populating monitored accounts:`,
664
- error
665
- );
666
- }
667
- }
668
- }
669
-
670
- /**
671
- * Fetches all monitored accounts and checks for missed changes
672
- * Returns true if a missed change was detected and resubscription is needed
673
- */
674
- private async fetchAllMonitoredAccounts(): Promise<boolean> {
675
- try {
676
- // Get all accounts currently being polled
677
- const accountsToMonitor = Array.from(this.accountsToMonitor);
678
- if (accountsToMonitor.length === 0) {
679
- return false;
680
- }
681
-
682
- // Fetch all accounts in a single batch request
683
- const accountAddresses = accountsToMonitor.map(
684
- (accountId) => accountId as Address
685
- );
686
- const rpcResponse = await this.rpc
687
- .getMultipleAccounts(accountAddresses, {
688
- commitment: this.options.commitment as GillCommitment,
689
- encoding: 'base64',
690
- })
691
- .send();
692
-
693
- const currentSlot = Number(rpcResponse.context.slot);
694
-
695
- // Process each account response
696
- for (let i = 0; i < accountsToMonitor.length; i++) {
697
- const accountIdString = accountsToMonitor[i];
698
- const accountInfo = rpcResponse.value[i];
699
-
700
- if (!accountInfo) {
701
- continue;
702
- }
703
-
704
- const existingBufferAndSlot =
705
- this.bufferAndSlotMap.get(accountIdString);
706
-
707
- if (!existingBufferAndSlot) {
708
- // Account not in our map yet, add it
709
- let newBuffer: Buffer | undefined = undefined;
710
- if (accountInfo.data) {
711
- if (Array.isArray(accountInfo.data)) {
712
- const [data, encoding] = accountInfo.data;
713
- newBuffer = Buffer.from(data, encoding);
714
- }
715
- }
716
-
717
- if (newBuffer) {
718
- this.updateBufferAndHandleChange(
719
- newBuffer,
720
- currentSlot,
721
- accountIdString
722
- );
723
- }
724
- continue;
725
- }
726
-
727
- // Check if we missed an update
728
- if (currentSlot > existingBufferAndSlot.slot) {
729
- let newBuffer: Buffer | undefined = undefined;
730
- if (accountInfo.data) {
731
- if (Array.isArray(accountInfo.data)) {
732
- const [data, encoding] = accountInfo.data;
733
- if (encoding === ('base58' as any)) {
734
- newBuffer = Buffer.from(bs58.decode(data));
735
- } else {
736
- newBuffer = Buffer.from(data, 'base64');
737
- }
738
- }
739
- }
740
-
741
- // Check if buffer has changed
742
- if (
743
- newBuffer &&
744
- (!existingBufferAndSlot.buffer ||
745
- !newBuffer.equals(existingBufferAndSlot.buffer))
746
- ) {
747
- if (this.resubOpts?.logResubMessages) {
748
- console.log(
749
- `[${this.subscriptionName}] Batch polling detected missed update for account ${accountIdString}, resubscribing`
750
- );
751
- }
752
- // We missed an update, return true to indicate resubscription is needed
753
- return true;
754
- }
755
- }
756
- }
757
-
758
- // No missed changes detected
759
- return false;
760
- } catch (error) {
761
- if (this.resubOpts?.logResubMessages) {
762
- console.log(
763
- `[${this.subscriptionName}] Error batch polling accounts:`,
764
- error
765
- );
766
- }
767
- return false;
768
- }
769
- }
770
-
771
- private async fetchAccountsBatch(accountIds: string[]): Promise<void> {
772
- try {
773
- // Chunk account IDs into groups of 100 (getMultipleAccounts limit)
774
- const chunkSize = 100;
775
- const chunks: string[][] = [];
776
- for (let i = 0; i < accountIds.length; i += chunkSize) {
777
- chunks.push(accountIds.slice(i, i + chunkSize));
778
- }
779
-
780
- // Process all chunks concurrently
781
- await Promise.all(
782
- chunks.map(async (chunk) => {
783
- const accountAddresses = chunk.map(
784
- (accountId) => accountId as Address
785
- );
786
- const rpcResponse = await this.rpc
787
- .getMultipleAccounts(accountAddresses, {
788
- commitment: this.options.commitment as GillCommitment,
789
- encoding: 'base64',
790
- })
791
- .send();
792
-
793
- const currentSlot = Number(rpcResponse.context.slot);
794
-
795
- // Process each account response in this chunk
796
- for (let i = 0; i < chunk.length; i++) {
797
- const accountIdString = chunk[i];
798
- const accountInfo = rpcResponse.value[i];
799
-
800
- if (!accountInfo) {
801
- continue;
802
- }
803
-
804
- const existingBufferAndSlot =
805
- this.bufferAndSlotMap.get(accountIdString);
806
-
807
- if (!existingBufferAndSlot) {
808
- // Account not in our map yet, add it
809
- let newBuffer: Buffer | undefined = undefined;
810
- if (accountInfo.data) {
811
- if (Array.isArray(accountInfo.data)) {
812
- const [data, encoding] = accountInfo.data;
813
- newBuffer = Buffer.from(data, encoding);
814
- }
815
- }
816
-
817
- if (newBuffer) {
818
- this.updateBufferAndHandleChange(
819
- newBuffer,
820
- currentSlot,
821
- accountIdString
822
- );
823
- }
824
- continue;
825
- }
826
-
827
- // Check if we missed an update
828
- if (currentSlot > existingBufferAndSlot.slot) {
829
- let newBuffer: Buffer | undefined = undefined;
830
- if (accountInfo.data) {
831
- if (Array.isArray(accountInfo.data)) {
832
- const [data, encoding] = accountInfo.data;
833
- if (encoding === ('base58' as any)) {
834
- newBuffer = Buffer.from(bs58.decode(data));
835
- } else {
836
- newBuffer = Buffer.from(data, 'base64');
837
- }
838
- }
839
- }
840
-
841
- // Check if buffer has changed
842
- if (
843
- newBuffer &&
844
- (!existingBufferAndSlot.buffer ||
845
- !newBuffer.equals(existingBufferAndSlot.buffer))
846
- ) {
847
- if (this.resubOpts?.logResubMessages) {
848
- console.log(
849
- `[${this.subscriptionName}] Batch polling detected missed update for account ${accountIdString}, signaling resubscription`
850
- );
851
- }
852
- // Signal missed change instead of immediately resubscribing
853
- this.signalMissedChange(accountIdString);
854
- return;
855
- }
856
- }
857
- }
858
- })
859
- );
860
- } catch (error) {
861
- if (this.resubOpts?.logResubMessages) {
862
- console.log(
863
- `[${this.subscriptionName}] Error fetching accounts batch:`,
864
- error
865
- );
866
- }
867
- }
868
- }
869
-
870
- private clearPollingTimeouts(): void {
871
- this.pollingTimeouts.forEach((timeoutId) => {
872
- clearTimeout(timeoutId);
873
- });
874
- this.pollingTimeouts.clear();
875
-
876
- // Clear batch polling timeout
877
- if (this.batchPollingTimeout) {
878
- clearTimeout(this.batchPollingTimeout);
879
- this.batchPollingTimeout = undefined;
880
- }
881
-
882
- // Clear initial fetch timeout
883
- // if (this.initialFetchTimeout) {
884
- // clearTimeout(this.initialFetchTimeout);
885
- // this.initialFetchTimeout = undefined;
886
- // }
887
-
888
- // Clear resubscription timeout
889
- if (this.resubscriptionTimeout) {
890
- clearTimeout(this.resubscriptionTimeout);
891
- this.resubscriptionTimeout = undefined;
892
- }
893
-
894
- // Clear accounts currently polling
895
- this.accountsCurrentlyPolling.clear();
896
-
897
- // Clear accounts pending initial monitor fetch
898
- // this.accountsPendingInitialMonitorFetch.clear();
899
-
900
- // Reset missed change flag and clear accounts with missed updates
901
- this.missedChangeDetected = false;
902
- this.accountsWithMissedUpdates.clear();
903
- }
904
-
905
- /**
906
- * Centralized resubscription handler that only resubscribes once after checking all accounts
907
- */
908
- private async handleResubscription(): Promise<void> {
909
- if (this.missedChangeDetected) {
910
- if (this.resubOpts?.logResubMessages) {
911
- console.log(
912
- `[${this.subscriptionName}] Missed change detected for ${
913
- this.accountsWithMissedUpdates.size
914
- } accounts: ${Array.from(this.accountsWithMissedUpdates).join(
915
- ', '
916
- )}, resubscribing`
917
- );
918
- }
919
- await this.unsubscribe(true);
920
- this.receivingData = false;
921
- await this.subscribe(this.onChange);
922
- this.missedChangeDetected = false;
923
- this.accountsWithMissedUpdates.clear();
924
- }
925
- }
926
-
927
- /**
928
- * Signal that a missed change was detected and schedule resubscription
929
- */
930
- private signalMissedChange(accountIdString: string): void {
931
- if (!this.missedChangeDetected) {
932
- this.missedChangeDetected = true;
933
- this.accountsWithMissedUpdates.add(accountIdString);
934
-
935
- // Clear any existing resubscription timeout
936
- if (this.resubscriptionTimeout) {
937
- clearTimeout(this.resubscriptionTimeout);
938
- }
939
-
940
- // Schedule resubscription after a short delay to allow for batch processing
941
- this.resubscriptionTimeout = setTimeout(async () => {
942
- await this.handleResubscription();
943
- }, 100); // 100ms delay to allow for batch processing
944
- } else {
945
- // If already detected, just add the account to the set
946
- this.accountsWithMissedUpdates.add(accountIdString);
947
- }
948
- }
949
-
950
- /**
951
- * Tears down all polling timeouts and aborts the WebSocket subscription.
952
- * @param onResub Internal flag set to `true` when called as part of an automatic resubscribe cycle, which preserves `resubOpts.resubTimeoutMs` instead of clearing it. Callers should omit this.
953
- */
954
- unsubscribe(onResub = false): Promise<void> {
955
- if (!onResub) {
956
- this.resubOpts.resubTimeoutMs = undefined;
957
- }
958
- this.isUnsubscribing = true;
959
- clearTimeout(this.timeoutId);
960
- this.timeoutId = undefined;
961
-
962
- // Clear polling timeouts
963
- this.clearPollingTimeouts();
964
-
965
- // Abort the WebSocket subscription
966
- if (this.abortController) {
967
- this.abortController.abort('unsubscribing');
968
- this.abortController = undefined;
969
- }
970
-
971
- this.listenerId = undefined;
972
- this.isUnsubscribing = false;
973
-
974
- return Promise.resolve();
975
- }
976
-
977
- // Method to add accounts to the polling list
978
- /**
979
- * Add an account to the monitored set.
980
- * - Monitored accounts are subject to initial fetch and periodic batch polls
981
- * if WS notifications are not observed within `pollingIntervalMs`.
982
- */
983
- addAccountToMonitor(accountId: PublicKey): void {
984
- const accountIdString = accountId.toBase58();
985
- this.accountsToMonitor.add(accountIdString);
986
-
987
- // If already subscribed, start monitoring for this account
988
- if (this.listenerId != null && !this.isUnsubscribing) {
989
- this.startMonitoringForAccount(accountIdString);
990
- }
991
- }
992
-
993
- // Method to remove accounts from the polling list
994
- /** Removes an account from the monitored set and clears any pending/active polling state for it. */
995
- removeAccountFromMonitor(accountId: PublicKey): void {
996
- const accountIdString = accountId.toBase58();
997
- this.accountsToMonitor.delete(accountIdString);
998
-
999
- // Clear monitoring timeout for this account
1000
- const timeoutId = this.pollingTimeouts.get(accountIdString);
1001
- if (timeoutId) {
1002
- clearTimeout(timeoutId);
1003
- this.pollingTimeouts.delete(accountIdString);
1004
- }
1005
-
1006
- // Remove from currently polling set if it was being polled
1007
- this.accountsCurrentlyPolling.delete(accountIdString);
1008
-
1009
- // If no more accounts are being polled, stop batch polling
1010
- if (this.accountsCurrentlyPolling.size === 0 && this.batchPollingTimeout) {
1011
- clearTimeout(this.batchPollingTimeout);
1012
- this.batchPollingTimeout = undefined;
1013
- }
1014
- }
1015
-
1016
- // Method to set polling interval
1017
- /**
1018
- * Set the monitoring/polling interval for monitored accounts.
1019
- * Shorter intervals detect missed updates sooner but increase RPC load.
1020
- */
1021
- setPollingInterval(intervalMs: number): void {
1022
- this.pollingIntervalMs = intervalMs;
1023
- // Restart monitoring with new interval if already subscribed
1024
- if (this.listenerId != null && !this.isUnsubscribing) {
1025
- this.startMonitoringForAccounts();
1026
- }
1027
- }
1028
-
1029
- private updateBufferAndHandleChange(
1030
- newBuffer: Buffer,
1031
- newSlot: number,
1032
- accountIdString: string
1033
- ) {
1034
- this.bufferAndSlotMap.set(accountIdString, {
1035
- buffer: newBuffer,
1036
- slot: newSlot,
1037
- });
1038
- const account = this.decodeBuffer(this.accountDiscriminator, newBuffer);
1039
- const accountIdPubkey = new PublicKey(accountIdString);
1040
- this.onChange(accountIdPubkey, account, { slot: newSlot }, newBuffer);
1041
- }
1042
- }