@drift-labs/sdk 2.96.0-beta.9 → 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.
- package/README.md +3 -0
- package/VERSION +1 -1
- package/bun.lockb +0 -0
- package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +5 -3
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -1
- package/lib/accounts/types.d.ts +5 -0
- package/lib/accounts/types.js +7 -1
- package/lib/accounts/utils.d.ts +7 -0
- package/lib/accounts/utils.js +33 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +5 -4
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
- package/lib/config.d.ts +6 -1
- package/lib/config.js +10 -1
- package/lib/constants/perpMarkets.js +33 -1
- package/lib/constants/spotMarkets.js +10 -0
- package/lib/constants/txConstants.d.ts +1 -0
- package/lib/constants/txConstants.js +4 -0
- package/lib/driftClient.d.ts +36 -8
- package/lib/driftClient.js +166 -43
- package/lib/driftClientConfig.d.ts +3 -0
- package/lib/events/types.js +1 -5
- package/lib/idl/drift.json +170 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/orderParams.js +8 -8
- package/lib/orderSubscriber/OrderSubscriber.js +1 -6
- package/lib/tokenFaucet.js +2 -1
- package/lib/tx/baseTxSender.d.ts +0 -1
- package/lib/tx/baseTxSender.js +8 -26
- package/lib/tx/fastSingleTxSender.js +2 -2
- package/lib/tx/forwardOnlyTxSender.js +2 -2
- package/lib/tx/reportTransactionError.d.ts +20 -0
- package/lib/tx/reportTransactionError.js +103 -0
- package/lib/tx/retryTxSender.js +2 -2
- package/lib/tx/txHandler.js +10 -7
- package/lib/tx/whileValidTxSender.d.ts +4 -5
- package/lib/tx/whileValidTxSender.js +16 -17
- package/lib/types.d.ts +22 -1
- package/lib/types.js +6 -1
- package/lib/util/TransactionConfirmationManager.d.ts +16 -0
- package/lib/util/TransactionConfirmationManager.js +174 -0
- package/package.json +4 -3
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
- package/src/accounts/types.ts +6 -0
- package/src/accounts/utils.ts +42 -0
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +40 -5
- package/src/config.ts +17 -1
- package/src/constants/perpMarkets.ts +35 -1
- package/src/constants/spotMarkets.ts +11 -0
- package/src/constants/txConstants.ts +1 -0
- package/src/driftClient.ts +346 -53
- package/src/driftClientConfig.ts +3 -0
- package/src/events/types.ts +1 -5
- package/src/idl/drift.json +170 -2
- package/src/index.ts +1 -0
- package/src/orderParams.ts +20 -12
- package/src/orderSubscriber/OrderSubscriber.ts +2 -5
- package/src/tokenFaucet.ts +2 -2
- package/src/tx/baseTxSender.ts +10 -32
- package/src/tx/fastSingleTxSender.ts +2 -2
- package/src/tx/forwardOnlyTxSender.ts +2 -2
- package/src/tx/reportTransactionError.ts +159 -0
- package/src/tx/retryTxSender.ts +2 -2
- package/src/tx/txHandler.ts +8 -2
- package/src/tx/whileValidTxSender.ts +18 -27
- package/src/types.ts +31 -1
- package/src/util/TransactionConfirmationManager.ts +292 -0
- package/tests/ci/verifyConstants.ts +13 -0
- package/tests/tx/TransactionConfirmationManager.test.ts +305 -0
|
@@ -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
|
+
});
|