@drift-labs/sdk 2.96.0-beta.8 → 2.97.0-beta.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 (76) hide show
  1. package/README.md +3 -0
  2. package/VERSION +1 -1
  3. package/bun.lockb +0 -0
  4. package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +5 -3
  5. package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -1
  6. package/lib/accounts/types.d.ts +5 -0
  7. package/lib/accounts/types.js +7 -1
  8. package/lib/accounts/utils.d.ts +7 -0
  9. package/lib/accounts/utils.js +33 -1
  10. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +5 -4
  11. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
  12. package/lib/config.d.ts +6 -1
  13. package/lib/config.js +10 -1
  14. package/lib/constants/perpMarkets.js +33 -1
  15. package/lib/constants/spotMarkets.js +10 -0
  16. package/lib/constants/txConstants.d.ts +1 -0
  17. package/lib/constants/txConstants.js +4 -0
  18. package/lib/driftClient.d.ts +45 -9
  19. package/lib/driftClient.js +191 -49
  20. package/lib/driftClientConfig.d.ts +3 -0
  21. package/lib/events/types.js +1 -5
  22. package/lib/idl/drift.json +170 -2
  23. package/lib/index.d.ts +1 -0
  24. package/lib/index.js +1 -0
  25. package/lib/math/margin.d.ts +16 -1
  26. package/lib/math/margin.js +67 -1
  27. package/lib/orderParams.js +8 -8
  28. package/lib/orderSubscriber/OrderSubscriber.js +1 -6
  29. package/lib/tokenFaucet.js +2 -1
  30. package/lib/tx/baseTxSender.d.ts +0 -1
  31. package/lib/tx/baseTxSender.js +8 -26
  32. package/lib/tx/fastSingleTxSender.js +2 -2
  33. package/lib/tx/forwardOnlyTxSender.js +2 -2
  34. package/lib/tx/reportTransactionError.d.ts +20 -0
  35. package/lib/tx/reportTransactionError.js +103 -0
  36. package/lib/tx/retryTxSender.js +2 -2
  37. package/lib/tx/txHandler.js +10 -7
  38. package/lib/tx/whileValidTxSender.d.ts +4 -5
  39. package/lib/tx/whileValidTxSender.js +16 -17
  40. package/lib/types.d.ts +22 -1
  41. package/lib/types.js +6 -1
  42. package/lib/user.d.ts +4 -1
  43. package/lib/user.js +9 -2
  44. package/lib/util/TransactionConfirmationManager.d.ts +16 -0
  45. package/lib/util/TransactionConfirmationManager.js +174 -0
  46. package/package.json +4 -3
  47. package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
  48. package/src/accounts/types.ts +6 -0
  49. package/src/accounts/utils.ts +42 -0
  50. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +40 -5
  51. package/src/config.ts +17 -1
  52. package/src/constants/perpMarkets.ts +35 -1
  53. package/src/constants/spotMarkets.ts +11 -0
  54. package/src/constants/txConstants.ts +1 -0
  55. package/src/driftClient.ts +426 -64
  56. package/src/driftClientConfig.ts +3 -0
  57. package/src/events/types.ts +1 -5
  58. package/src/idl/drift.json +170 -2
  59. package/src/index.ts +1 -0
  60. package/src/math/margin.ts +137 -1
  61. package/src/orderParams.ts +20 -12
  62. package/src/orderSubscriber/OrderSubscriber.ts +2 -5
  63. package/src/tokenFaucet.ts +2 -2
  64. package/src/tx/baseTxSender.ts +10 -32
  65. package/src/tx/fastSingleTxSender.ts +2 -2
  66. package/src/tx/forwardOnlyTxSender.ts +2 -2
  67. package/src/tx/reportTransactionError.ts +159 -0
  68. package/src/tx/retryTxSender.ts +2 -2
  69. package/src/tx/txHandler.ts +8 -2
  70. package/src/tx/whileValidTxSender.ts +18 -27
  71. package/src/types.ts +31 -1
  72. package/src/user.ts +35 -2
  73. package/src/util/TransactionConfirmationManager.ts +292 -0
  74. package/tests/ci/idl.ts +12 -3
  75. package/tests/ci/verifyConstants.ts +13 -0
  76. package/tests/tx/TransactionConfirmationManager.test.ts +305 -0
@@ -0,0 +1,292 @@
1
+ import {
2
+ ClientSubscriptionId,
3
+ Connection,
4
+ Context,
5
+ RpcResponseAndContext,
6
+ SignatureResult,
7
+ SignatureStatus,
8
+ TransactionConfirmationStatus,
9
+ } from '@solana/web3.js';
10
+ import { DEFAULT_CONFIRMATION_OPTS } from '../config';
11
+ import { TxSendError } from '..';
12
+ import { NOT_CONFIRMED_ERROR_CODE } from '../constants/txConstants';
13
+ import {
14
+ getTransactionErrorFromTxSig,
15
+ throwTransactionError,
16
+ } from '../tx/reportTransactionError';
17
+ import { promiseTimeout } from './promiseTimeout';
18
+
19
+ type ResolveReference = {
20
+ resolve?: () => void;
21
+ };
22
+
23
+ const confirmationStatusValues: Record<TransactionConfirmationStatus, number> =
24
+ {
25
+ processed: 0,
26
+ confirmed: 1,
27
+ finalized: 2,
28
+ };
29
+
30
+ interface TransactionConfirmationRequest {
31
+ txSig: string;
32
+ desiredConfirmationStatus: TransactionConfirmationStatus;
33
+ timeout: number;
34
+ pollInterval: number;
35
+ searchTransactionHistory: boolean;
36
+ startTime: number;
37
+ resolve: (status: SignatureStatus) => void;
38
+ reject: (error: Error) => void;
39
+ }
40
+
41
+ /**
42
+ * Class to await for transaction confirmations in an optimised manner. It tracks a shared list of all pending transactions and fetches them in bulk in a shared RPC request whenever they have an "overlapping" polling interval. E.g. tx1 with an interval of 200ms and tx2 with an interval of 300ms (if sent at the same time) will be fetched together at at 600ms, 1200ms, 1800ms, etc.
43
+ */
44
+ export class TransactionConfirmationManager {
45
+ private connection: Connection;
46
+ private pendingConfirmations: Map<string, TransactionConfirmationRequest> =
47
+ new Map();
48
+ private intervalId: NodeJS.Timeout | null = null;
49
+
50
+ constructor(connection: Connection) {
51
+ this.connection = connection;
52
+ }
53
+
54
+ async confirmTransactionWebSocket(
55
+ txSig: string,
56
+ timeout = 30000,
57
+ desiredConfirmationStatus = DEFAULT_CONFIRMATION_OPTS.commitment as TransactionConfirmationStatus
58
+ ): Promise<RpcResponseAndContext<SignatureResult>> {
59
+ const start = Date.now();
60
+ const subscriptionCommitment =
61
+ desiredConfirmationStatus || DEFAULT_CONFIRMATION_OPTS.commitment;
62
+
63
+ let response: RpcResponseAndContext<SignatureResult> | null = null;
64
+
65
+ let subscriptionId: ClientSubscriptionId;
66
+
67
+ const confirmationPromise = new Promise((resolve, reject) => {
68
+ try {
69
+ subscriptionId = this.connection.onSignature(
70
+ txSig,
71
+ (result: SignatureResult, context: Context) => {
72
+ response = {
73
+ context,
74
+ value: result,
75
+ };
76
+ resolve(null);
77
+ },
78
+ subscriptionCommitment
79
+ );
80
+ } catch (err) {
81
+ reject(err);
82
+ }
83
+ });
84
+
85
+ // We do a one-shot confirmation check just in case the transaction is ALREADY confirmed when we create the websocket confirmation .. We want to run this concurrently with the onSignature subscription. If this returns true then we can return early as the transaction has already been confirmed.
86
+ const oneShotConfirmationPromise = this.connection.getSignatureStatuses([
87
+ txSig,
88
+ ]);
89
+
90
+ const resolveReference: ResolveReference = {};
91
+
92
+ // This is the promise we are waiting on to resolve the overall confirmation. It will resolve the faster of a positive oneShot confirmation, or the websocket confirmation, or the timeout.
93
+ const overallWaitingForConfirmationPromise = new Promise<void>(
94
+ (resolve) => {
95
+ resolveReference.resolve = resolve;
96
+ }
97
+ );
98
+
99
+ // Await for the one shot confirmation and resolve the waiting promise if we get a positive confirmation result
100
+ oneShotConfirmationPromise.then(
101
+ async (oneShotResponse) => {
102
+ if (!oneShotResponse || !oneShotResponse?.value?.[0]) return;
103
+
104
+ const resultValue = oneShotResponse.value[0];
105
+
106
+ if (resultValue.err) {
107
+ await throwTransactionError(txSig, this.connection);
108
+ }
109
+
110
+ if (
111
+ this.checkStatusMatchesDesiredConfirmationStatus(
112
+ resultValue,
113
+ desiredConfirmationStatus
114
+ )
115
+ ) {
116
+ response = {
117
+ context: oneShotResponse.context,
118
+ value: oneShotResponse.value[0],
119
+ };
120
+ resolveReference.resolve?.();
121
+ }
122
+ },
123
+ (onRejected) => {
124
+ throw onRejected;
125
+ }
126
+ );
127
+
128
+ // Await for the websocket confirmation with the configured timeout
129
+ promiseTimeout(confirmationPromise, timeout).then(
130
+ () => {
131
+ resolveReference.resolve?.();
132
+ },
133
+ (onRejected) => {
134
+ throw onRejected;
135
+ }
136
+ );
137
+
138
+ try {
139
+ await overallWaitingForConfirmationPromise;
140
+ } finally {
141
+ if (subscriptionId !== undefined) {
142
+ this.connection.removeSignatureListener(subscriptionId);
143
+ }
144
+ }
145
+
146
+ const duration = (Date.now() - start) / 1000;
147
+
148
+ if (response === null) {
149
+ throw new TxSendError(
150
+ `Transaction was not confirmed in ${duration.toFixed(
151
+ 2
152
+ )} seconds. It is unknown if it succeeded or failed. Check signature ${txSig} using the Solana Explorer or CLI tools.`,
153
+ NOT_CONFIRMED_ERROR_CODE
154
+ );
155
+ }
156
+
157
+ return response;
158
+ }
159
+
160
+ async confirmTransactionPolling(
161
+ txSig: string,
162
+ desiredConfirmationStatus = DEFAULT_CONFIRMATION_OPTS.commitment as TransactionConfirmationStatus,
163
+ timeout = 30000,
164
+ pollInterval = 1000,
165
+ searchTransactionHistory = false
166
+ ): Promise<SignatureStatus> {
167
+ // Interval must be > 400ms and a multiple of 100ms
168
+ if (pollInterval < 400 || pollInterval % 100 !== 0) {
169
+ throw new Error(
170
+ 'Transaction confirmation polling interval must be at least 400ms and a multiple of 100ms'
171
+ );
172
+ }
173
+
174
+ return new Promise((resolve, reject) => {
175
+ this.pendingConfirmations.set(txSig, {
176
+ txSig,
177
+ desiredConfirmationStatus,
178
+ timeout,
179
+ pollInterval,
180
+ searchTransactionHistory,
181
+ startTime: Date.now(),
182
+ resolve,
183
+ reject,
184
+ });
185
+
186
+ if (!this.intervalId) {
187
+ this.startConfirmationLoop();
188
+ }
189
+ });
190
+ }
191
+
192
+ private startConfirmationLoop() {
193
+ this.intervalId = setInterval(() => this.checkPendingConfirmations(), 100);
194
+ }
195
+
196
+ private async checkPendingConfirmations() {
197
+ const now = Date.now();
198
+ const transactionsToCheck: TransactionConfirmationRequest[] = [];
199
+
200
+ for (const [txSig, request] of this.pendingConfirmations.entries()) {
201
+ if (now - request.startTime >= request.timeout) {
202
+ request.reject(
203
+ new Error(
204
+ `Transaction confirmation timeout after ${request.timeout}ms`
205
+ )
206
+ );
207
+ this.pendingConfirmations.delete(txSig);
208
+ } else if ((now - request.startTime) % request.pollInterval < 100) {
209
+ transactionsToCheck.push(request);
210
+ }
211
+ }
212
+
213
+ if (transactionsToCheck.length > 0) {
214
+ await this.checkTransactionStatuses(transactionsToCheck);
215
+ }
216
+
217
+ if (this.pendingConfirmations.size === 0 && this.intervalId) {
218
+ clearInterval(this.intervalId);
219
+ this.intervalId = null;
220
+ }
221
+ }
222
+
223
+ private checkStatusMatchesDesiredConfirmationStatus(
224
+ status: SignatureStatus,
225
+ desiredConfirmationStatus: TransactionConfirmationStatus
226
+ ): boolean {
227
+ if (
228
+ status.confirmationStatus &&
229
+ confirmationStatusValues[status.confirmationStatus] >=
230
+ confirmationStatusValues[desiredConfirmationStatus]
231
+ ) {
232
+ return true;
233
+ }
234
+
235
+ return false;
236
+ }
237
+
238
+ private async checkTransactionStatuses(
239
+ requests: TransactionConfirmationRequest[]
240
+ ) {
241
+ const txSigs = requests.map((request) => request.txSig);
242
+ const { value: statuses } = await this.connection.getSignatureStatuses(
243
+ txSigs,
244
+ {
245
+ searchTransactionHistory: requests.some(
246
+ (req) => req.searchTransactionHistory
247
+ ),
248
+ }
249
+ );
250
+
251
+ if (!statuses || statuses.length !== txSigs.length) {
252
+ throw new Error('Failed to get signature statuses');
253
+ }
254
+
255
+ for (let i = 0; i < statuses.length; i++) {
256
+ const status = statuses[i];
257
+ const request = requests[i];
258
+
259
+ if (status === null) {
260
+ continue;
261
+ }
262
+
263
+ if (status.err) {
264
+ this.pendingConfirmations.delete(request.txSig);
265
+ request.reject(
266
+ await getTransactionErrorFromTxSig(request.txSig, this.connection)
267
+ );
268
+ continue;
269
+ }
270
+
271
+ if (
272
+ confirmationStatusValues[status.confirmationStatus] === undefined ||
273
+ confirmationStatusValues[request.desiredConfirmationStatus] ===
274
+ undefined
275
+ ) {
276
+ throw new Error(
277
+ `Invalid confirmation status when awaiting confirmation: ${status.confirmationStatus}`
278
+ );
279
+ }
280
+
281
+ if (
282
+ this.checkStatusMatchesDesiredConfirmationStatus(
283
+ status,
284
+ request.desiredConfirmationStatus
285
+ )
286
+ ) {
287
+ request.resolve(status);
288
+ this.pendingConfirmations.delete(request.txSig);
289
+ }
290
+ }
291
+ }
292
+ }
package/tests/ci/idl.ts CHANGED
@@ -51,7 +51,9 @@ describe('Verify IDL', function () {
51
51
  );
52
52
 
53
53
  if (onChainIdl === null) {
54
- throw new Error(`onChainIdl for ${mainnetDriftClient.program.programId.toBase58()} null`);
54
+ throw new Error(
55
+ `onChainIdl for ${mainnetDriftClient.program.programId.toBase58()} null`
56
+ );
55
57
  }
56
58
 
57
59
  // anchor idl init seems to strip the metadata
@@ -65,13 +67,20 @@ describe('Verify IDL', function () {
65
67
  const encodedSdkIdl = JSON.stringify(sdkIdl);
66
68
 
67
69
  try {
68
- assert(encodedSdkIdl === encodedMainnetIdl, 'on-chain IDL does not match SDK IDL');
70
+ assert(
71
+ encodedSdkIdl === encodedMainnetIdl,
72
+ 'on-chain IDL does not match SDK IDL'
73
+ );
69
74
  } catch (error) {
70
75
  const diff = {};
71
76
  for (const key of IDL_KEYS_TO_CHECK) {
72
77
  const onChainItems = onChainIdl[key];
73
78
  const sdkItems = sdkIdl[key];
74
- for (let i = 0; i < Math.max(onChainItems.length, sdkItems.length); i++) {
79
+ for (
80
+ let i = 0;
81
+ i < Math.max(onChainItems.length, sdkItems.length);
82
+ i++
83
+ ) {
75
84
  let onChainItem = null;
76
85
  let sdkItem = null;
77
86
  if (i < onChainItems.length) {
@@ -6,6 +6,7 @@ import {
6
6
  MainnetPerpMarkets,
7
7
  BulkAccountLoader,
8
8
  getVariant,
9
+ isOneOfVariant,
9
10
  } from '../../src';
10
11
  import { Connection, Keypair } from '@solana/web3.js';
11
12
  import { Wallet } from '@coral-xyz/anchor';
@@ -128,6 +129,12 @@ describe('Verify Constants', function () {
128
129
  market.marketIndex
129
130
  } oracle ${market.oracle.toBase58()}`
130
131
  );
132
+
133
+ if (isOneOfVariant(market.oracleSource, ['pythPull', 'pyth1KPull', 'pyth1MPull', 'pythStableCoinPull'])) {
134
+ if (!correspondingConfigMarket.pythFeedId) {
135
+ assert(false, `spot market ${market.marketIndex} missing feed id`);
136
+ }
137
+ }
131
138
  }
132
139
 
133
140
  const perpMarkets = mainnetDriftClient.getPerpMarketAccounts();
@@ -177,6 +184,12 @@ describe('Verify Constants', function () {
177
184
  market.marketIndex
178
185
  } oracle ${market.amm.oracle.toBase58()}`
179
186
  );
187
+
188
+ if (isOneOfVariant(market.amm.oracleSource, ['pythPull', 'pyth1KPull', 'pyth1MPull', 'pythStableCoinPull'])) {
189
+ if (!correspondingConfigMarket.pythFeedId) {
190
+ assert(false, `perp market ${market.marketIndex} missing feed id`);
191
+ }
192
+ }
180
193
  }
181
194
  });
182
195
 
@@ -0,0 +1,305 @@
1
+ import { expect } from 'chai';
2
+ import sinon from 'sinon';
3
+ import {
4
+ Connection,
5
+ SignatureStatus,
6
+ VersionedTransactionResponse,
7
+ } from '@solana/web3.js';
8
+ import { TransactionConfirmationManager } from '../../src/util/TransactionConfirmationManager';
9
+ import assert from 'assert';
10
+
11
+ describe('TransactionConfirmationManager_Polling_Tests', () => {
12
+ let manager: TransactionConfirmationManager;
13
+ let mockConnection: sinon.SinonStubbedInstance<Connection>;
14
+
15
+ beforeEach(() => {
16
+ mockConnection = sinon.createStubInstance(Connection);
17
+ manager = new TransactionConfirmationManager(
18
+ mockConnection as unknown as Connection
19
+ );
20
+ });
21
+
22
+ afterEach(() => {
23
+ sinon.restore();
24
+ });
25
+
26
+ it('should throw error for invalid poll interval', async () => {
27
+ try {
28
+ await manager.confirmTransactionPolling(
29
+ 'fakeTxSig',
30
+ 'confirmed',
31
+ 30000,
32
+ 300
33
+ );
34
+ assert.fail('Expected an error to be thrown');
35
+ } catch (error) {
36
+ assert(error instanceof Error);
37
+ assert.strictEqual(
38
+ error.message,
39
+ 'Transaction confirmation polling interval must be at least 400ms and a multiple of 100ms'
40
+ );
41
+ }
42
+ });
43
+
44
+ it('should resolve when transaction is confirmed', async () => {
45
+ const fakeTxSig = 'fakeTxSig';
46
+ const fakeStatus: SignatureStatus = {
47
+ slot: 100,
48
+ confirmations: 1,
49
+ err: null,
50
+ confirmationStatus: 'confirmed',
51
+ };
52
+
53
+ mockConnection.getSignatureStatuses.resolves({
54
+ context: { slot: 100 },
55
+ value: [fakeStatus],
56
+ });
57
+
58
+ const result = await manager.confirmTransactionPolling(
59
+ fakeTxSig,
60
+ 'confirmed',
61
+ 30000,
62
+ 400
63
+ );
64
+
65
+ expect(result).to.deep.equal(fakeStatus);
66
+ expect(
67
+ mockConnection.getSignatureStatuses.calledWith([fakeTxSig], {
68
+ searchTransactionHistory: false,
69
+ })
70
+ ).to.be.true;
71
+ });
72
+
73
+ it('should reject when transaction fails', async function () {
74
+ const fakeTxSig = 'fakeTxSig';
75
+ const fakeStatus: SignatureStatus = {
76
+ slot: 100,
77
+ confirmations: 1,
78
+ err: { InstructionError: [0, 'Custom'] },
79
+ confirmationStatus: 'confirmed',
80
+ };
81
+
82
+ mockConnection.getSignatureStatuses.resolves({
83
+ context: { slot: 100 },
84
+ value: [fakeStatus],
85
+ });
86
+
87
+ // The transaction manager falls into getTransaction when it detects a transaction failure so we need to mock that as well
88
+ // @ts-ignore
89
+ mockConnection.getTransaction.resolves({
90
+ meta: {
91
+ logMessages: ['Transaction failed: Custom'],
92
+ err: { InstructionError: [0, 'Custom'] },
93
+ },
94
+ } as VersionedTransactionResponse);
95
+
96
+ try {
97
+ await manager.confirmTransactionPolling(
98
+ fakeTxSig,
99
+ 'confirmed',
100
+ 30000,
101
+ 400
102
+ );
103
+ assert.fail('Expected an error to be thrown');
104
+ } catch (error) {
105
+ return;
106
+ }
107
+ });
108
+
109
+ it('should reject on timeout', async () => {
110
+ const clock = sinon.useFakeTimers();
111
+
112
+ const fakeTxSig = 'fakeTxSig';
113
+ mockConnection.getSignatureStatuses.resolves({
114
+ context: { slot: 100 },
115
+ value: [null],
116
+ });
117
+
118
+ const promise = manager.confirmTransactionPolling(
119
+ fakeTxSig,
120
+ 'confirmed',
121
+ 5000,
122
+ 1000
123
+ );
124
+
125
+ clock.tick(6000);
126
+
127
+ try {
128
+ await promise;
129
+ assert.fail('Expected an error to be thrown');
130
+ } catch (error) {
131
+ assert(error instanceof Error);
132
+ assert.strictEqual(
133
+ error.message,
134
+ 'Transaction confirmation timeout after 5000ms'
135
+ );
136
+ }
137
+
138
+ clock.restore();
139
+ });
140
+
141
+ it('should check multiple transactions together', async () => {
142
+ const fakeTxSig1 = 'fakeTxSig1';
143
+ const fakeTxSig2 = 'fakeTxSig2';
144
+ const fakeStatus1: SignatureStatus = {
145
+ slot: 100,
146
+ confirmations: 1,
147
+ err: null,
148
+ confirmationStatus: 'confirmed',
149
+ };
150
+ const fakeStatus2: SignatureStatus = {
151
+ slot: 100,
152
+ confirmations: 1,
153
+ err: null,
154
+ confirmationStatus: 'confirmed',
155
+ };
156
+
157
+ mockConnection.getSignatureStatuses.resolves({
158
+ context: { slot: 100 },
159
+ value: [fakeStatus1, fakeStatus2],
160
+ });
161
+
162
+ const promise1 = manager.confirmTransactionPolling(
163
+ fakeTxSig1,
164
+ 'confirmed',
165
+ 30000,
166
+ 400
167
+ );
168
+ const promise2 = manager.confirmTransactionPolling(
169
+ fakeTxSig2,
170
+ 'confirmed',
171
+ 30000,
172
+ 400
173
+ );
174
+
175
+ const clock = sinon.useFakeTimers();
176
+ clock.tick(400);
177
+ clock.restore();
178
+
179
+ const [result1, result2] = await Promise.all([promise1, promise2]);
180
+
181
+ expect(result1).to.deep.equal(fakeStatus1);
182
+ expect(result2).to.deep.equal(fakeStatus2);
183
+ expect(
184
+ mockConnection.getSignatureStatuses.calledWith([fakeTxSig1, fakeTxSig2], {
185
+ searchTransactionHistory: false,
186
+ })
187
+ ).to.be.true;
188
+ });
189
+
190
+ it('should have overlapping request for transactions with 400ms and 1200ms intervals on the third 400ms interval', async function () {
191
+ this.timeout(5000); // Increase timeout for this test to 5 seconds
192
+
193
+ const fakeTxSig1 = 'fakeTxSig1'; // 400ms interval
194
+ const fakeTxSig2 = 'fakeTxSig2'; // 1200ms interval
195
+ const fakeStatus: SignatureStatus = {
196
+ slot: 100,
197
+ confirmations: 1,
198
+ err: null,
199
+ confirmationStatus: 'confirmed',
200
+ };
201
+
202
+ let callCount = 0;
203
+ const callTimes: number[] = [];
204
+ const callSignatures: string[][] = [];
205
+
206
+ mockConnection.getSignatureStatuses = async (signatures) => {
207
+ callCount++;
208
+ const currentTime = Date.now();
209
+ callTimes.push(currentTime);
210
+ callSignatures.push([...signatures]);
211
+
212
+ if (callCount < 3) {
213
+ return {
214
+ context: { slot: 100 },
215
+ value: signatures.map(() => null),
216
+ };
217
+ } else {
218
+ return {
219
+ context: { slot: 100 },
220
+ value: signatures.map(() => fakeStatus),
221
+ };
222
+ }
223
+ };
224
+
225
+ const startTime = Date.now();
226
+
227
+ // Start both confirmation processes
228
+ const promise1 = manager.confirmTransactionPolling(
229
+ fakeTxSig1,
230
+ 'confirmed',
231
+ 5000,
232
+ 400
233
+ );
234
+ const promise2 = manager.confirmTransactionPolling(
235
+ fakeTxSig2,
236
+ 'confirmed',
237
+ 5000,
238
+ 1200
239
+ );
240
+
241
+ // Wait for 1250ms to ensure we've hit the third 400ms interval and first 1200ms interval
242
+ await new Promise((resolve) => setTimeout(resolve, 1250));
243
+
244
+ // Resolve both promises
245
+ await Promise.all([promise1, promise2]);
246
+
247
+ // Check the call times and signatures
248
+ assert.strictEqual(callTimes.length, 3, 'Should have exactly 3 calls');
249
+
250
+ // Check if the third call is close to 1200ms and includes both signatures
251
+ const overlapCall = 2; // The third call should be the overlapping one
252
+ const overlapTime = callTimes[overlapCall] - startTime;
253
+
254
+ assert(
255
+ Math.abs(overlapTime - 1200) < 100,
256
+ `Overlapping call should be around 1200ms, but was at ${overlapTime}ms`
257
+ );
258
+
259
+ // Verify the call pattern
260
+ assert(
261
+ callSignatures[0].includes(fakeTxSig1) &&
262
+ !callSignatures[0].includes(fakeTxSig2),
263
+ 'First call should only include 400ms interval transaction'
264
+ );
265
+ assert(
266
+ callSignatures[1].includes(fakeTxSig1) &&
267
+ !callSignatures[1].includes(fakeTxSig2),
268
+ 'Second call should only include 400ms interval transaction'
269
+ );
270
+ assert(
271
+ callSignatures[2].includes(fakeTxSig1) &&
272
+ callSignatures[2].includes(fakeTxSig2),
273
+ 'Third call should include both transactions'
274
+ );
275
+
276
+ // Wait for 1000ms to check that we haven't made any more calls now that all transactions are confirmed
277
+ await new Promise((resolve) => setTimeout(resolve, 1000));
278
+
279
+ // Verify that no more calls were made
280
+ assert.strictEqual(
281
+ callTimes.length,
282
+ 3,
283
+ 'Should not have made any more calls after all transactions are confirmed'
284
+ );
285
+
286
+ // Verify that only the third call returns non-null results
287
+ callCount = 0;
288
+ const results = await Promise.all(
289
+ callSignatures.map((sigs) => mockConnection.getSignatureStatuses!(sigs))
290
+ );
291
+
292
+ assert(
293
+ results[0].value.every((v) => v === null),
294
+ 'First call should return null results'
295
+ );
296
+ assert(
297
+ results[1].value.every((v) => v === null),
298
+ 'Second call should return null results'
299
+ );
300
+ assert(
301
+ results[2].value.every((v) => v !== null),
302
+ 'Third call should return non-null results'
303
+ );
304
+ });
305
+ });