@deriverse/kit 1.0.39 → 1.0.41

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 (59) hide show
  1. package/dist/auto_buffer.d.ts +0 -2
  2. package/dist/auto_data.d.ts +0 -2
  3. package/dist/constants.d.ts +12 -0
  4. package/dist/constants.js +26 -0
  5. package/dist/engine/account-helpers.d.ts +59 -0
  6. package/dist/engine/account-helpers.js +177 -0
  7. package/dist/engine/account-helpers.test.d.ts +1 -0
  8. package/dist/engine/account-helpers.test.js +199 -0
  9. package/dist/engine/client-queries.d.ts +36 -0
  10. package/dist/engine/client-queries.js +498 -0
  11. package/dist/engine/client-queries.test.d.ts +1 -0
  12. package/dist/engine/client-queries.test.js +341 -0
  13. package/dist/engine/context-builders.d.ts +16 -0
  14. package/dist/engine/context-builders.js +158 -0
  15. package/dist/engine/context-builders.test.d.ts +1 -0
  16. package/dist/engine/context-builders.test.js +156 -0
  17. package/dist/engine/index.d.ts +101 -0
  18. package/dist/engine/index.js +745 -0
  19. package/dist/engine/index.test.d.ts +1 -0
  20. package/dist/engine/index.test.js +663 -0
  21. package/dist/engine/logs-decoder.d.ts +18 -0
  22. package/dist/engine/logs-decoder.js +414 -0
  23. package/dist/engine/logs-decoder.test.d.ts +1 -0
  24. package/dist/engine/logs-decoder.test.js +836 -0
  25. package/dist/engine/perp-instructions.d.ts +68 -0
  26. package/dist/engine/perp-instructions.js +478 -0
  27. package/dist/engine/perp-instructions.test.d.ts +1 -0
  28. package/dist/engine/perp-instructions.test.js +296 -0
  29. package/dist/engine/spot-instructions.d.ts +52 -0
  30. package/dist/engine/spot-instructions.js +376 -0
  31. package/dist/engine/spot-instructions.test.d.ts +1 -0
  32. package/dist/engine/spot-instructions.test.js +221 -0
  33. package/dist/engine/utils.d.ts +23 -0
  34. package/dist/engine/utils.js +329 -0
  35. package/dist/engine/utils.test.d.ts +1 -0
  36. package/dist/engine/utils.test.js +120 -0
  37. package/dist/index.d.ts +6 -247
  38. package/dist/index.js +14 -2923
  39. package/dist/instruction_models.d.ts +0 -2
  40. package/dist/instruction_models.js +39 -40
  41. package/dist/logs_models.d.ts +0 -2
  42. package/dist/types/engine-args.d.ts +32 -0
  43. package/dist/types/engine-args.js +2 -0
  44. package/dist/types/enums.d.ts +43 -0
  45. package/dist/{types.js → types/enums.js} +3 -5
  46. package/dist/types/index.d.ts +8 -0
  47. package/dist/types/index.js +38 -0
  48. package/dist/types/log-message.d.ts +2 -0
  49. package/dist/types/log-message.js +2 -0
  50. package/dist/types/responses.d.ts +248 -0
  51. package/dist/types/responses.js +2 -0
  52. package/dist/types/schemas.d.ts +165 -0
  53. package/dist/types/schemas.js +254 -0
  54. package/dist/types/schemas.test.d.ts +1 -0
  55. package/dist/types/schemas.test.js +94 -0
  56. package/dist/utils.d.ts +0 -0
  57. package/dist/utils.js +1 -0
  58. package/package.json +26 -6
  59. package/dist/types.d.ts +0 -565
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,663 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const vitest_1 = require("vitest");
13
+ const index_1 = require("./index");
14
+ const constants_1 = require("../constants");
15
+ const structure_models_1 = require("../structure_models");
16
+ // Mock account-helpers
17
+ vitest_1.vi.mock('./account-helpers', () => ({
18
+ getAccountByTag: vitest_1.vi.fn().mockResolvedValue('MockAccountByTag1111111111111111'),
19
+ getInstrAccountByTag: vitest_1.vi.fn().mockResolvedValue('MockInstrAccount111111111111111'),
20
+ getTokenAccount: vitest_1.vi.fn().mockResolvedValue('MockTokenAccount11111111111111111'),
21
+ getTokenId: vitest_1.vi.fn().mockResolvedValue(1),
22
+ getInstrId: vitest_1.vi.fn().mockResolvedValue(1),
23
+ findClientPrimaryAccount: vitest_1.vi.fn().mockResolvedValue('MockClientPrimary1111111111111111'),
24
+ findClientCommunityAccount: vitest_1.vi.fn().mockResolvedValue('MockClientCommunity11111111111111'),
25
+ findAccountsByTag: vitest_1.vi.fn().mockResolvedValue([]),
26
+ AccountHelperContext: {},
27
+ }));
28
+ // Mock spot-instructions
29
+ vitest_1.vi.mock('./spot-instructions', () => ({
30
+ buildDepositInstruction: vitest_1.vi.fn().mockResolvedValue({
31
+ programAddress: 'MockProgram1111111111111111111111111',
32
+ accounts: [],
33
+ data: new Uint8Array([7]),
34
+ }),
35
+ buildWithdrawInstruction: vitest_1.vi.fn().mockResolvedValue({
36
+ programAddress: 'MockProgram1111111111111111111111111',
37
+ accounts: [],
38
+ data: new Uint8Array([8]),
39
+ }),
40
+ buildSpotLpInstruction: vitest_1.vi.fn().mockResolvedValue({
41
+ programAddress: 'MockProgram1111111111111111111111111',
42
+ accounts: [],
43
+ data: new Uint8Array([9]),
44
+ }),
45
+ buildNewSpotOrderInstruction: vitest_1.vi.fn().mockResolvedValue({
46
+ programAddress: 'MockProgram1111111111111111111111111',
47
+ accounts: [],
48
+ data: new Uint8Array([10]),
49
+ }),
50
+ buildSpotQuotesReplaceInstruction: vitest_1.vi.fn().mockResolvedValue({
51
+ programAddress: 'MockProgram1111111111111111111111111',
52
+ accounts: [],
53
+ data: new Uint8Array([11]),
54
+ }),
55
+ buildSpotOrderCancelInstruction: vitest_1.vi.fn().mockResolvedValue({
56
+ programAddress: 'MockProgram1111111111111111111111111',
57
+ accounts: [],
58
+ data: new Uint8Array([12]),
59
+ }),
60
+ buildSpotMassCancelInstruction: vitest_1.vi.fn().mockResolvedValue({
61
+ programAddress: 'MockProgram1111111111111111111111111',
62
+ accounts: [],
63
+ data: new Uint8Array([13]),
64
+ }),
65
+ buildSwapInstruction: vitest_1.vi.fn().mockResolvedValue({
66
+ programAddress: 'MockProgram1111111111111111111111111',
67
+ accounts: [],
68
+ data: new Uint8Array([14]),
69
+ }),
70
+ SpotInstructionContext: {},
71
+ }));
72
+ // Mock perp-instructions
73
+ vitest_1.vi.mock('./perp-instructions', () => ({
74
+ buildUpgradeToPerpInstructions: vitest_1.vi.fn().mockResolvedValue([
75
+ {
76
+ programAddress: 'MockProgram1111111111111111111111111',
77
+ accounts: [],
78
+ data: new Uint8Array([20]),
79
+ },
80
+ ]),
81
+ buildPerpDepositInstruction: vitest_1.vi.fn().mockResolvedValue({
82
+ programAddress: 'MockProgram1111111111111111111111111',
83
+ accounts: [],
84
+ data: new Uint8Array([21]),
85
+ }),
86
+ buildPerpBuySeatInstruction: vitest_1.vi.fn().mockResolvedValue({
87
+ programAddress: 'MockProgram1111111111111111111111111',
88
+ accounts: [],
89
+ data: new Uint8Array([22]),
90
+ }),
91
+ buildPerpSellSeatInstruction: vitest_1.vi.fn().mockResolvedValue({
92
+ programAddress: 'MockProgram1111111111111111111111111',
93
+ accounts: [],
94
+ data: new Uint8Array([23]),
95
+ }),
96
+ buildNewPerpOrderInstruction: vitest_1.vi.fn().mockResolvedValue({
97
+ programAddress: 'MockProgram1111111111111111111111111',
98
+ accounts: [],
99
+ data: new Uint8Array([24]),
100
+ }),
101
+ buildPerpQuotesReplaceInstruction: vitest_1.vi.fn().mockResolvedValue({
102
+ programAddress: 'MockProgram1111111111111111111111111',
103
+ accounts: [],
104
+ data: new Uint8Array([25]),
105
+ }),
106
+ buildPerpOrderCancelInstruction: vitest_1.vi.fn().mockResolvedValue({
107
+ programAddress: 'MockProgram1111111111111111111111111',
108
+ accounts: [],
109
+ data: new Uint8Array([26]),
110
+ }),
111
+ buildPerpMassCancelInstruction: vitest_1.vi.fn().mockResolvedValue({
112
+ programAddress: 'MockProgram1111111111111111111111111',
113
+ accounts: [],
114
+ data: new Uint8Array([27]),
115
+ }),
116
+ buildPerpChangeLeverageInstruction: vitest_1.vi.fn().mockResolvedValue({
117
+ programAddress: 'MockProgram1111111111111111111111111',
118
+ accounts: [],
119
+ data: new Uint8Array([28]),
120
+ }),
121
+ buildPerpStatisticsResetInstruction: vitest_1.vi.fn().mockResolvedValue({
122
+ programAddress: 'MockProgram1111111111111111111111111',
123
+ accounts: [],
124
+ data: new Uint8Array([29]),
125
+ }),
126
+ buildNewRefLinkInstruction: vitest_1.vi.fn().mockResolvedValue({
127
+ programAddress: 'MockProgram1111111111111111111111111',
128
+ accounts: [],
129
+ data: new Uint8Array([30]),
130
+ }),
131
+ buildNewInstrumentInstructions: vitest_1.vi.fn().mockResolvedValue([
132
+ {
133
+ programAddress: 'MockProgram1111111111111111111111111',
134
+ accounts: [],
135
+ data: new Uint8Array([31]),
136
+ },
137
+ ]),
138
+ PerpInstructionContext: {},
139
+ }));
140
+ // Mock context-builders
141
+ vitest_1.vi.mock('./context-builders', () => ({
142
+ getSpotContext: vitest_1.vi.fn().mockResolvedValue([]),
143
+ getPerpContext: vitest_1.vi.fn().mockResolvedValue([]),
144
+ getSpotCandles: vitest_1.vi.fn().mockResolvedValue([]),
145
+ }));
146
+ // Mock logs-decoder
147
+ vitest_1.vi.mock('./logs-decoder', () => ({
148
+ decodeTransactionLogs: vitest_1.vi.fn().mockReturnValue([]),
149
+ }));
150
+ // Import mocked modules for assertions
151
+ const spot_instructions_1 = require("./spot-instructions");
152
+ const perp_instructions_1 = require("./perp-instructions");
153
+ // Mock RPC for testing
154
+ const createMockRpc = () => ({
155
+ getAccountInfo: vitest_1.vi.fn().mockReturnValue({ send: vitest_1.vi.fn() }),
156
+ getMultipleAccounts: vitest_1.vi.fn().mockReturnValue({ send: vitest_1.vi.fn() }),
157
+ getProgramAccounts: vitest_1.vi.fn().mockReturnValue({ send: vitest_1.vi.fn() }),
158
+ getSlot: vitest_1.vi.fn().mockReturnValue({ send: vitest_1.vi.fn().mockResolvedValue(BigInt(12345)) }),
159
+ getMinimumBalanceForRentExemption: vitest_1.vi.fn().mockReturnValue({ send: vitest_1.vi.fn().mockResolvedValue(BigInt(1000000)) }),
160
+ });
161
+ // Helper to create mock token
162
+ function createMockToken(id, decimals = 9) {
163
+ const token = new structure_models_1.TokenStateModel();
164
+ token.id = id;
165
+ token.mask = decimals;
166
+ return token;
167
+ }
168
+ // Helper to create mock instrument header
169
+ function createMockInstrHeader(instrId) {
170
+ const header = new structure_models_1.InstrAccountHeaderModel();
171
+ header.instrId = instrId;
172
+ header.assetTokenId = 1;
173
+ header.crncyTokenId = 2;
174
+ header.mask = 0;
175
+ header.mapsAddress = 'MockMapsAddress111111111111111111111';
176
+ header.perpMapsAddress = 'MockPerpMapsAddress1111111111111111';
177
+ header.lutAddress = 'MockLutAddress1111111111111111111111';
178
+ header.perpClientsCount = 10;
179
+ return header;
180
+ }
181
+ // Helper to create mock instruments map
182
+ function createMockInstrumentsMap() {
183
+ const map = new Map();
184
+ map.set(1, {
185
+ address: 'MockInstrAddress111111111111111111111',
186
+ header: createMockInstrHeader(1),
187
+ spotBids: [],
188
+ spotAsks: [],
189
+ perpBids: [],
190
+ perpAsks: [],
191
+ });
192
+ return map;
193
+ }
194
+ // Helper to create mock tokens map
195
+ function createMockTokensMap() {
196
+ const map = new Map();
197
+ map.set(0, createMockToken(0, 9)); // DRVS token
198
+ map.set(1, createMockToken(1, 9));
199
+ map.set(2, createMockToken(2, 6));
200
+ return map;
201
+ }
202
+ // Helper to setup engine with client connected
203
+ function setupEngineWithClient() {
204
+ return __awaiter(this, void 0, void 0, function* () {
205
+ const mockRpc = createMockRpc();
206
+ const engine = new index_1.Engine(mockRpc);
207
+ // Setup required state
208
+ engine.tokens = createMockTokensMap();
209
+ engine.instruments = createMockInstrumentsMap();
210
+ engine.rootAccount = 'MockRootAccount11111111111111111111';
211
+ engine.communityAccount = 'MockCommunityAccount111111111111111';
212
+ engine.drvsAuthority = 'MockDrvsAuthority11111111111111111';
213
+ engine.rootStateModel = new structure_models_1.RootStateModel();
214
+ engine.rootStateModel.tokensCount = 3;
215
+ engine.rootStateModel.instrCount = 1;
216
+ // Setup client state directly (bypass setSigner complexity)
217
+ engine.signer = 'MockSigner1111111111111111111111111111';
218
+ engine.clientPrimaryAccount = 'MockClientPrimary1111111111111111';
219
+ engine.clientCommunityAccount = 'MockClientCommunity11111111111111';
220
+ engine.originalClientId = 12345;
221
+ // Mock updateInstrData to avoid RPC calls
222
+ vitest_1.vi.spyOn(engine, 'updateInstrData').mockResolvedValue(undefined);
223
+ return { engine, mockRpc };
224
+ });
225
+ }
226
+ (0, vitest_1.describe)('Engine class', () => {
227
+ (0, vitest_1.describe)('constructor', () => {
228
+ (0, vitest_1.it)('uses default PROGRAM_ID when not provided', () => {
229
+ const mockRpc = createMockRpc();
230
+ const engine = new index_1.Engine(mockRpc);
231
+ (0, vitest_1.expect)(engine.programId).toBe(constants_1.PROGRAM_ID);
232
+ });
233
+ (0, vitest_1.it)('uses default VERSION when not provided', () => {
234
+ const mockRpc = createMockRpc();
235
+ const engine = new index_1.Engine(mockRpc);
236
+ (0, vitest_1.expect)(engine.version).toBe(constants_1.VERSION);
237
+ });
238
+ (0, vitest_1.it)('uses default commitment (confirmed) when not provided', () => {
239
+ const mockRpc = createMockRpc();
240
+ const engine = new index_1.Engine(mockRpc);
241
+ (0, vitest_1.expect)(engine.commitment).toBe('confirmed');
242
+ });
243
+ (0, vitest_1.it)('accepts custom programId', () => {
244
+ const mockRpc = createMockRpc();
245
+ const customProgramId = 'CustomProgramId11111111111111111111111111111';
246
+ const engine = new index_1.Engine(mockRpc, { programId: customProgramId });
247
+ (0, vitest_1.expect)(engine.programId).toBe(customProgramId);
248
+ });
249
+ (0, vitest_1.it)('accepts custom version', () => {
250
+ const mockRpc = createMockRpc();
251
+ const engine = new index_1.Engine(mockRpc, { version: 2 });
252
+ (0, vitest_1.expect)(engine.version).toBe(2);
253
+ });
254
+ (0, vitest_1.it)('accepts custom commitment', () => {
255
+ const mockRpc = createMockRpc();
256
+ const engine = new index_1.Engine(mockRpc, { commitment: 'finalized' });
257
+ (0, vitest_1.expect)(engine.commitment).toBe('finalized');
258
+ });
259
+ (0, vitest_1.it)('accepts uiNumbers option', () => {
260
+ const mockRpc = createMockRpc();
261
+ // Default is true, test with false
262
+ const engine = new index_1.Engine(mockRpc, { uiNumbers: false });
263
+ // uiNumbers is private, so we can't directly test it
264
+ // But it should not throw
265
+ (0, vitest_1.expect)(engine).toBeDefined();
266
+ });
267
+ });
268
+ (0, vitest_1.describe)('logsDecode', () => {
269
+ (0, vitest_1.it)('returns empty array for error logs', () => {
270
+ const mockRpc = createMockRpc();
271
+ const engine = new index_1.Engine(mockRpc);
272
+ // Initialize empty maps for the test
273
+ engine.instruments = new Map();
274
+ engine.tokens = new Map();
275
+ const errorLogs = ['Program returned error: insufficient funds'];
276
+ const result = engine.logsDecode(errorLogs);
277
+ (0, vitest_1.expect)(result).toEqual([]);
278
+ });
279
+ (0, vitest_1.it)('returns empty array for logs starting with Error', () => {
280
+ const mockRpc = createMockRpc();
281
+ const engine = new index_1.Engine(mockRpc);
282
+ engine.instruments = new Map();
283
+ engine.tokens = new Map();
284
+ const errorLogs = ['Program logged: "Error: some error"'];
285
+ const result = engine.logsDecode(errorLogs);
286
+ (0, vitest_1.expect)(result).toEqual([]);
287
+ });
288
+ (0, vitest_1.it)('returns empty array for non-program data logs', () => {
289
+ const mockRpc = createMockRpc();
290
+ const engine = new index_1.Engine(mockRpc);
291
+ engine.instruments = new Map();
292
+ engine.tokens = new Map();
293
+ const otherLogs = ['Program invoke', 'Some other log'];
294
+ const result = engine.logsDecode(otherLogs);
295
+ (0, vitest_1.expect)(result).toEqual([]);
296
+ });
297
+ (0, vitest_1.it)('returns empty array for empty log array', () => {
298
+ const mockRpc = createMockRpc();
299
+ const engine = new index_1.Engine(mockRpc);
300
+ engine.instruments = new Map();
301
+ engine.tokens = new Map();
302
+ const result = engine.logsDecode([]);
303
+ (0, vitest_1.expect)(result).toEqual([]);
304
+ });
305
+ });
306
+ (0, vitest_1.describe)('initial state', () => {
307
+ (0, vitest_1.it)('has undefined rootAccount before initialization', () => {
308
+ const mockRpc = createMockRpc();
309
+ const engine = new index_1.Engine(mockRpc);
310
+ (0, vitest_1.expect)(engine.rootAccount).toBeUndefined();
311
+ });
312
+ (0, vitest_1.it)('has undefined tokens and instruments before initialization', () => {
313
+ const mockRpc = createMockRpc();
314
+ const engine = new index_1.Engine(mockRpc);
315
+ (0, vitest_1.expect)(engine.tokens).toBeUndefined();
316
+ (0, vitest_1.expect)(engine.instruments).toBeUndefined();
317
+ });
318
+ });
319
+ });
320
+ // ============================================
321
+ // INSTRUCTION METHOD BEHAVIOR TESTS
322
+ // ============================================
323
+ (0, vitest_1.describe)('Engine instruction methods', () => {
324
+ (0, vitest_1.beforeEach)(() => {
325
+ vitest_1.vi.clearAllMocks();
326
+ });
327
+ (0, vitest_1.describe)('depositInstruction', () => {
328
+ (0, vitest_1.it)('throws when wallet not connected', () => __awaiter(void 0, void 0, void 0, function* () {
329
+ const mockRpc = createMockRpc();
330
+ const engine = new index_1.Engine(mockRpc);
331
+ yield (0, vitest_1.expect)(engine.depositInstruction({ tokenId: 1, amount: 100 })).rejects.toThrow('Wallet');
332
+ }));
333
+ (0, vitest_1.it)('calls buildDepositInstruction with correct args', () => __awaiter(void 0, void 0, void 0, function* () {
334
+ const { engine } = yield setupEngineWithClient();
335
+ yield engine.depositInstruction({ tokenId: 1, amount: 100 });
336
+ (0, vitest_1.expect)(spot_instructions_1.buildDepositInstruction).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
337
+ programId: engine.programId,
338
+ tokens: engine.tokens,
339
+ instruments: engine.instruments,
340
+ }), { tokenId: 1, amount: 100 }, vitest_1.expect.any(Boolean), vitest_1.expect.any(Function));
341
+ }));
342
+ (0, vitest_1.it)('returns instruction from builder', () => __awaiter(void 0, void 0, void 0, function* () {
343
+ const { engine } = yield setupEngineWithClient();
344
+ const result = yield engine.depositInstruction({ tokenId: 1, amount: 100 });
345
+ (0, vitest_1.expect)(result).toBeDefined();
346
+ (0, vitest_1.expect)(result.data[0]).toBe(7); // deposit tag
347
+ }));
348
+ });
349
+ (0, vitest_1.describe)('withdrawInstruction', () => {
350
+ (0, vitest_1.it)('throws when client account not found', () => __awaiter(void 0, void 0, void 0, function* () {
351
+ const mockRpc = createMockRpc();
352
+ const engine = new index_1.Engine(mockRpc);
353
+ engine.tokens = createMockTokensMap();
354
+ engine.instruments = createMockInstrumentsMap();
355
+ // Set signer but no client account
356
+ engine.signer = 'MockSigner1111111111111111111111111111';
357
+ engine.clientPrimaryAccount = null;
358
+ yield (0, vitest_1.expect)(engine.withdrawInstruction({ tokenId: 1, amount: 50 })).rejects.toThrow('Client account not found');
359
+ }));
360
+ (0, vitest_1.it)('calls buildWithdrawInstruction with correct args', () => __awaiter(void 0, void 0, void 0, function* () {
361
+ const { engine } = yield setupEngineWithClient();
362
+ yield engine.withdrawInstruction({ tokenId: 1, amount: 50 });
363
+ (0, vitest_1.expect)(spot_instructions_1.buildWithdrawInstruction).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
364
+ programId: engine.programId,
365
+ tokens: engine.tokens,
366
+ }), { tokenId: 1, amount: 50 });
367
+ }));
368
+ });
369
+ (0, vitest_1.describe)('spotLpInstruction', () => {
370
+ (0, vitest_1.it)('throws when client account not found', () => __awaiter(void 0, void 0, void 0, function* () {
371
+ const mockRpc = createMockRpc();
372
+ const engine = new index_1.Engine(mockRpc);
373
+ engine.tokens = createMockTokensMap();
374
+ engine.instruments = createMockInstrumentsMap();
375
+ // Set signer but no client account
376
+ engine.signer = 'MockSigner1111111111111111111111111111';
377
+ engine.clientPrimaryAccount = null;
378
+ yield (0, vitest_1.expect)(engine.spotLpInstruction({ instrId: 1, side: 0, amount: 100 })).rejects.toThrow('Client account not found');
379
+ }));
380
+ (0, vitest_1.it)('calls buildSpotLpInstruction with correct args', () => __awaiter(void 0, void 0, void 0, function* () {
381
+ const { engine } = yield setupEngineWithClient();
382
+ yield engine.spotLpInstruction({ instrId: 1, side: 0, amount: 100 });
383
+ (0, vitest_1.expect)(spot_instructions_1.buildSpotLpInstruction).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
384
+ programId: engine.programId,
385
+ }), { instrId: 1, side: 0, amount: 100 }, vitest_1.expect.any(Object));
386
+ }));
387
+ });
388
+ (0, vitest_1.describe)('newSpotOrderInstruction', () => {
389
+ (0, vitest_1.it)('throws when client account not found', () => __awaiter(void 0, void 0, void 0, function* () {
390
+ const mockRpc = createMockRpc();
391
+ const engine = new index_1.Engine(mockRpc);
392
+ engine.tokens = createMockTokensMap();
393
+ engine.instruments = createMockInstrumentsMap();
394
+ // Set signer but no client account
395
+ engine.signer = 'MockSigner1111111111111111111111111111';
396
+ engine.clientPrimaryAccount = null;
397
+ yield (0, vitest_1.expect)(engine.newSpotOrderInstruction({ instrId: 1, side: 0, price: 100, qty: 10 })).rejects.toThrow('Client account not found');
398
+ }));
399
+ (0, vitest_1.it)('calls buildNewSpotOrderInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
400
+ const { engine } = yield setupEngineWithClient();
401
+ yield engine.newSpotOrderInstruction({ instrId: 1, side: 0, price: 100, qty: 10 });
402
+ (0, vitest_1.expect)(spot_instructions_1.buildNewSpotOrderInstruction).toHaveBeenCalled();
403
+ }));
404
+ });
405
+ (0, vitest_1.describe)('spotQuotesReplaceInstruction', () => {
406
+ (0, vitest_1.it)('calls buildSpotQuotesReplaceInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
407
+ const { engine } = yield setupEngineWithClient();
408
+ yield engine.spotQuotesReplaceInstruction({
409
+ instrId: 1,
410
+ newBidPrice: 99,
411
+ newBidQty: 10,
412
+ bidOrderIdToCancel: 1,
413
+ newAskPrice: 101,
414
+ newAskQty: 10,
415
+ askOrderIdToCancel: 2,
416
+ });
417
+ (0, vitest_1.expect)(spot_instructions_1.buildSpotQuotesReplaceInstruction).toHaveBeenCalled();
418
+ }));
419
+ });
420
+ (0, vitest_1.describe)('spotOrderCancelInstruction', () => {
421
+ (0, vitest_1.it)('calls buildSpotOrderCancelInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
422
+ const { engine } = yield setupEngineWithClient();
423
+ yield engine.spotOrderCancelInstruction({ instrId: 1, orderId: 12345, side: 0 });
424
+ (0, vitest_1.expect)(spot_instructions_1.buildSpotOrderCancelInstruction).toHaveBeenCalled();
425
+ }));
426
+ });
427
+ (0, vitest_1.describe)('spotMassCancelInstruction', () => {
428
+ (0, vitest_1.it)('calls buildSpotMassCancelInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
429
+ const { engine } = yield setupEngineWithClient();
430
+ yield engine.spotMassCancelInstruction({ instrId: 1 });
431
+ (0, vitest_1.expect)(spot_instructions_1.buildSpotMassCancelInstruction).toHaveBeenCalled();
432
+ }));
433
+ });
434
+ (0, vitest_1.describe)('swapInstruction', () => {
435
+ (0, vitest_1.it)('calls buildSwapInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
436
+ const { engine } = yield setupEngineWithClient();
437
+ yield engine.swapInstruction({
438
+ assetMint: 'AssetMint1111111111111111111111111111',
439
+ crncyMint: 'CrncyMint1111111111111111111111111111',
440
+ amount: 100,
441
+ limitPrice: 100,
442
+ crncyInput: true,
443
+ });
444
+ (0, vitest_1.expect)(spot_instructions_1.buildSwapInstruction).toHaveBeenCalled();
445
+ }));
446
+ });
447
+ // Perp instructions
448
+ (0, vitest_1.describe)('upgradeToPerpInstructions', () => {
449
+ (0, vitest_1.it)('throws when wallet not connected', () => __awaiter(void 0, void 0, void 0, function* () {
450
+ const mockRpc = createMockRpc();
451
+ const engine = new index_1.Engine(mockRpc);
452
+ engine.instruments = createMockInstrumentsMap();
453
+ yield (0, vitest_1.expect)(engine.upgradeToPerpInstructions({ instrId: 1 })).rejects.toThrow('Wallet is not connected');
454
+ }));
455
+ (0, vitest_1.it)('calls buildUpgradeToPerpInstructions', () => __awaiter(void 0, void 0, void 0, function* () {
456
+ const { engine } = yield setupEngineWithClient();
457
+ yield engine.upgradeToPerpInstructions({ instrId: 1 });
458
+ (0, vitest_1.expect)(perp_instructions_1.buildUpgradeToPerpInstructions).toHaveBeenCalled();
459
+ }));
460
+ });
461
+ (0, vitest_1.describe)('perpDepositInstruction', () => {
462
+ (0, vitest_1.it)('throws when client account not found', () => __awaiter(void 0, void 0, void 0, function* () {
463
+ const mockRpc = createMockRpc();
464
+ const engine = new index_1.Engine(mockRpc);
465
+ engine.tokens = createMockTokensMap();
466
+ engine.instruments = createMockInstrumentsMap();
467
+ // Set signer but no client account
468
+ engine.signer = 'MockSigner1111111111111111111111111111';
469
+ engine.clientPrimaryAccount = null;
470
+ yield (0, vitest_1.expect)(engine.perpDepositInstruction({ instrId: 1, amount: 100 })).rejects.toThrow('Client account not found');
471
+ }));
472
+ (0, vitest_1.it)('calls buildPerpDepositInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
473
+ const { engine } = yield setupEngineWithClient();
474
+ yield engine.perpDepositInstruction({ instrId: 1, amount: 100 });
475
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpDepositInstruction).toHaveBeenCalled();
476
+ }));
477
+ });
478
+ (0, vitest_1.describe)('perpBuySeatInstruction', () => {
479
+ (0, vitest_1.it)('calls buildPerpBuySeatInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
480
+ const { engine } = yield setupEngineWithClient();
481
+ yield engine.perpBuySeatInstruction({ instrId: 1, amount: 50 });
482
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpBuySeatInstruction).toHaveBeenCalled();
483
+ }));
484
+ });
485
+ (0, vitest_1.describe)('perpSellSeatInstruction', () => {
486
+ (0, vitest_1.it)('calls buildPerpSellSeatInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
487
+ const { engine } = yield setupEngineWithClient();
488
+ yield engine.perpSellSeatInstruction({ instrId: 1 });
489
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpSellSeatInstruction).toHaveBeenCalled();
490
+ }));
491
+ });
492
+ (0, vitest_1.describe)('newPerpOrderInstruction', () => {
493
+ (0, vitest_1.it)('calls buildNewPerpOrderInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
494
+ const { engine } = yield setupEngineWithClient();
495
+ yield engine.newPerpOrderInstruction({ instrId: 1, side: 0, price: 100, qty: 10 });
496
+ (0, vitest_1.expect)(perp_instructions_1.buildNewPerpOrderInstruction).toHaveBeenCalled();
497
+ }));
498
+ });
499
+ (0, vitest_1.describe)('perpQuotesReplaceInstruction', () => {
500
+ (0, vitest_1.it)('calls buildPerpQuotesReplaceInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
501
+ const { engine } = yield setupEngineWithClient();
502
+ yield engine.perpQuotesReplaceInstruction({
503
+ instrId: 1,
504
+ newBidPrice: 99,
505
+ newBidQty: 10,
506
+ bidOrderIdToCancel: 1,
507
+ newAskPrice: 101,
508
+ newAskQty: 10,
509
+ askOrderIdToCancel: 2,
510
+ });
511
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpQuotesReplaceInstruction).toHaveBeenCalled();
512
+ }));
513
+ });
514
+ (0, vitest_1.describe)('perpOrderCancelInstruction', () => {
515
+ (0, vitest_1.it)('calls buildPerpOrderCancelInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
516
+ const { engine } = yield setupEngineWithClient();
517
+ yield engine.perpOrderCancelInstruction({ instrId: 1, orderId: 12345, side: 0 });
518
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpOrderCancelInstruction).toHaveBeenCalled();
519
+ }));
520
+ });
521
+ (0, vitest_1.describe)('perpMassCancelInstruction', () => {
522
+ (0, vitest_1.it)('calls buildPerpMassCancelInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
523
+ const { engine } = yield setupEngineWithClient();
524
+ yield engine.perpMassCancelInstruction({ instrId: 1 });
525
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpMassCancelInstruction).toHaveBeenCalled();
526
+ }));
527
+ });
528
+ (0, vitest_1.describe)('perpChangeLeverageInstruction', () => {
529
+ (0, vitest_1.it)('calls buildPerpChangeLeverageInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
530
+ const { engine } = yield setupEngineWithClient();
531
+ yield engine.perpChangeLeverageInstruction({ instrId: 1, leverage: 5 });
532
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpChangeLeverageInstruction).toHaveBeenCalled();
533
+ }));
534
+ });
535
+ (0, vitest_1.describe)('perpStatisticsResetInstruction', () => {
536
+ (0, vitest_1.it)('calls buildPerpStatisticsResetInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
537
+ const { engine } = yield setupEngineWithClient();
538
+ yield engine.perpStatisticsResetInstruction({ instrId: 1 });
539
+ (0, vitest_1.expect)(perp_instructions_1.buildPerpStatisticsResetInstruction).toHaveBeenCalled();
540
+ }));
541
+ });
542
+ (0, vitest_1.describe)('newRefLinkInstruction', () => {
543
+ (0, vitest_1.it)('calls buildNewRefLinkInstruction', () => __awaiter(void 0, void 0, void 0, function* () {
544
+ const { engine } = yield setupEngineWithClient();
545
+ yield engine.newRefLinkInstruction();
546
+ (0, vitest_1.expect)(perp_instructions_1.buildNewRefLinkInstruction).toHaveBeenCalled();
547
+ }));
548
+ });
549
+ (0, vitest_1.describe)('newInstrumentInstructions', () => {
550
+ (0, vitest_1.it)('throws when wallet not connected', () => __awaiter(void 0, void 0, void 0, function* () {
551
+ const mockRpc = createMockRpc();
552
+ const engine = new index_1.Engine(mockRpc);
553
+ yield (0, vitest_1.expect)(engine.newInstrumentInstructions({
554
+ assetMint: 'AssetMint1111111111111111111111111111',
555
+ crncyMint: 'CrncyMint1111111111111111111111111111',
556
+ initialPrice: 100,
557
+ })).rejects.toThrow('Wallet is not connected');
558
+ }));
559
+ (0, vitest_1.it)('calls buildNewInstrumentInstructions', () => __awaiter(void 0, void 0, void 0, function* () {
560
+ const { engine } = yield setupEngineWithClient();
561
+ yield engine.newInstrumentInstructions({
562
+ assetMint: 'AssetMint1111111111111111111111111111',
563
+ crncyMint: 'CrncyMint1111111111111111111111111111',
564
+ initialPrice: 100,
565
+ });
566
+ (0, vitest_1.expect)(perp_instructions_1.buildNewInstrumentInstructions).toHaveBeenCalled();
567
+ }));
568
+ });
569
+ (0, vitest_1.describe)('getClientData', () => {
570
+ (0, vitest_1.it)('throws when client not found', () => __awaiter(void 0, void 0, void 0, function* () {
571
+ const mockRpc = createMockRpc();
572
+ const engine = new index_1.Engine(mockRpc);
573
+ engine.signer = 'MockSigner1111111111111111111111111111';
574
+ engine.clientPrimaryAccount = null;
575
+ yield (0, vitest_1.expect)(engine.getClientData()).rejects.toThrow('Client account not found');
576
+ }));
577
+ });
578
+ (0, vitest_1.describe)('getClientSpotOrdersInfo', () => {
579
+ (0, vitest_1.it)('throws for invalid instrument', () => __awaiter(void 0, void 0, void 0, function* () {
580
+ const { engine } = yield setupEngineWithClient();
581
+ yield (0, vitest_1.expect)(engine.getClientSpotOrdersInfo({ instrId: 999, clientId: 1 })).rejects.toThrow('Invalid Instrument ID');
582
+ }));
583
+ });
584
+ (0, vitest_1.describe)('getClientPerpOrdersInfo', () => {
585
+ (0, vitest_1.it)('throws for invalid instrument', () => __awaiter(void 0, void 0, void 0, function* () {
586
+ const { engine } = yield setupEngineWithClient();
587
+ yield (0, vitest_1.expect)(engine.getClientPerpOrdersInfo({ instrId: 999, clientId: 1 })).rejects.toThrow('Invalid Instrument ID');
588
+ }));
589
+ });
590
+ (0, vitest_1.describe)('getClientSpotOrders', () => {
591
+ (0, vitest_1.it)('throws when originalClientId is null', () => __awaiter(void 0, void 0, void 0, function* () {
592
+ const { engine } = yield setupEngineWithClient();
593
+ engine.originalClientId = null;
594
+ yield (0, vitest_1.expect)(engine.getClientSpotOrders({ instrId: 1, bidsEntry: 0, bidsCount: 0, asksEntry: 0, asksCount: 0 })).rejects.toThrow('Original client ID not found');
595
+ }));
596
+ });
597
+ (0, vitest_1.describe)('getClientPerpOrders', () => {
598
+ (0, vitest_1.it)('throws when originalClientId is null', () => __awaiter(void 0, void 0, void 0, function* () {
599
+ const { engine } = yield setupEngineWithClient();
600
+ engine.originalClientId = null;
601
+ yield (0, vitest_1.expect)(engine.getClientPerpOrders({ instrId: 1, bidsEntry: 0, bidsCount: 0, asksEntry: 0, asksCount: 0 })).rejects.toThrow('Original client ID not found');
602
+ }));
603
+ });
604
+ });
605
+ // ============================================
606
+ // DATA UPDATE METHOD TESTS
607
+ // ============================================
608
+ (0, vitest_1.describe)('Engine data update methods', () => {
609
+ (0, vitest_1.beforeEach)(() => {
610
+ vitest_1.vi.clearAllMocks();
611
+ });
612
+ (0, vitest_1.describe)('addToken', () => {
613
+ (0, vitest_1.it)('throws when getAccountInfo returns null', () => __awaiter(void 0, void 0, void 0, function* () {
614
+ const mockRpc = createMockRpc();
615
+ mockRpc.getAccountInfo = vitest_1.vi.fn().mockReturnValue({
616
+ send: vitest_1.vi.fn().mockResolvedValue({ value: null }),
617
+ });
618
+ const engine = new index_1.Engine(mockRpc);
619
+ engine.rootStateModel = { tokensCount: 10 };
620
+ yield (0, vitest_1.expect)(engine.addToken('TokenAccount111111111111111111111')).rejects.toThrow('Add Token Failed: getAccountInfo');
621
+ }));
622
+ });
623
+ (0, vitest_1.describe)('addInstr', () => {
624
+ (0, vitest_1.it)('throws when getAccountInfo returns null', () => __awaiter(void 0, void 0, void 0, function* () {
625
+ const mockRpc = createMockRpc();
626
+ mockRpc.getAccountInfo = vitest_1.vi.fn().mockReturnValue({
627
+ send: vitest_1.vi.fn().mockResolvedValue({ value: null }),
628
+ });
629
+ const engine = new index_1.Engine(mockRpc);
630
+ engine.rootStateModel = { instrCount: 10 };
631
+ yield (0, vitest_1.expect)(engine.addInstr('InstrAccount111111111111111111111')).rejects.toThrow('Add Instrument Failed: getAccountInfo');
632
+ }));
633
+ });
634
+ (0, vitest_1.describe)('updateCommunity', () => {
635
+ (0, vitest_1.it)('throws when getAccountInfo returns null', () => __awaiter(void 0, void 0, void 0, function* () {
636
+ const mockRpc = createMockRpc();
637
+ mockRpc.getAccountInfo = vitest_1.vi.fn().mockReturnValue({
638
+ send: vitest_1.vi.fn().mockResolvedValue({ value: null }),
639
+ });
640
+ const engine = new index_1.Engine(mockRpc);
641
+ engine.communityAccount = 'CommunityAccount1111111111111111111';
642
+ yield (0, vitest_1.expect)(engine.updateCommunity()).rejects.toThrow('Community Account: GetAccountInfo Failed');
643
+ }));
644
+ });
645
+ (0, vitest_1.describe)('updateRoot', () => {
646
+ (0, vitest_1.it)('throws when getAccountInfo returns null', () => __awaiter(void 0, void 0, void 0, function* () {
647
+ const mockRpc = createMockRpc();
648
+ mockRpc.getAccountInfo = vitest_1.vi.fn().mockReturnValue({
649
+ send: vitest_1.vi.fn().mockResolvedValue({ value: null }),
650
+ });
651
+ const engine = new index_1.Engine(mockRpc);
652
+ engine.rootAccount = 'RootAccount11111111111111111111111';
653
+ yield (0, vitest_1.expect)(engine.updateRoot()).rejects.toThrow('Root Account: GetAccountInfo Failed');
654
+ }));
655
+ });
656
+ (0, vitest_1.describe)('updateInstrData', () => {
657
+ (0, vitest_1.it)('throws when instrument not found', () => __awaiter(void 0, void 0, void 0, function* () {
658
+ const { engine } = yield setupEngineWithClient();
659
+ vitest_1.vi.mocked(engine.updateInstrData).mockRestore();
660
+ yield (0, vitest_1.expect)(engine.updateInstrData({ instrId: 999 })).rejects.toThrow('Instrument not found');
661
+ }));
662
+ });
663
+ });