@deriverse/kit 1.0.38 → 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 -2925
  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,376 @@
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
+ exports.buildDepositInstruction = buildDepositInstruction;
13
+ exports.buildWithdrawInstruction = buildWithdrawInstruction;
14
+ exports.buildSpotLpInstruction = buildSpotLpInstruction;
15
+ exports.buildNewSpotOrderInstruction = buildNewSpotOrderInstruction;
16
+ exports.buildSpotQuotesReplaceInstruction = buildSpotQuotesReplaceInstruction;
17
+ exports.buildSpotOrderCancelInstruction = buildSpotOrderCancelInstruction;
18
+ exports.buildSpotMassCancelInstruction = buildSpotMassCancelInstruction;
19
+ exports.buildSwapInstruction = buildSwapInstruction;
20
+ const kit_1 = require("@solana/kit");
21
+ const enums_1 = require("../types/enums");
22
+ const constants_1 = require("../constants");
23
+ const utils_1 = require("./utils");
24
+ const instruction_models_1 = require("../instruction_models");
25
+ const account_helpers_1 = require("./account-helpers");
26
+ const context_builders_1 = require("./context-builders");
27
+ /**
28
+ * Build deposit instruction
29
+ */
30
+ function buildDepositInstruction(ctx, args, exists, rpcGetSlot) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ var _a;
33
+ const amount = (_a = args.amount) !== null && _a !== void 0 ? _a : 0;
34
+ const allFunds = args.all_funds ? 1 : 0;
35
+ const token = ctx.tokens.get(args.tokenId);
36
+ if (!token) {
37
+ throw new Error(`Token ${args.tokenId} not found`);
38
+ }
39
+ const tokenProgramId = (token.mask & 0x80000000) != 0 ? constants_1.TOKEN_2022_PROGRAM_ID : constants_1.TOKEN_PROGRAM_ID;
40
+ const clientTokenAccount = yield (0, utils_1.findAssociatedTokenAddress)(ctx.signer, tokenProgramId, token.address);
41
+ const clientPrimaryAccount = yield (0, account_helpers_1.findClientPrimaryAccount)(ctx, ctx.signer);
42
+ let keys = [
43
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
44
+ { address: clientTokenAccount, role: kit_1.AccountRole.WRITABLE },
45
+ { address: token.programAddress, role: kit_1.AccountRole.WRITABLE },
46
+ { address: token.address, role: kit_1.AccountRole.READONLY },
47
+ { address: ctx.rootAccount, role: exists ? kit_1.AccountRole.READONLY : kit_1.AccountRole.WRITABLE },
48
+ { address: yield (0, account_helpers_1.getTokenAccount)(ctx, token.address), role: kit_1.AccountRole.READONLY },
49
+ { address: clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
50
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
51
+ { address: tokenProgramId, role: kit_1.AccountRole.READONLY },
52
+ ];
53
+ if (ctx.privateMode) {
54
+ keys.push({
55
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.PRIVATE_CLIENTS),
56
+ role: kit_1.AccountRole.WRITABLE,
57
+ });
58
+ }
59
+ if (exists) {
60
+ if (args.tokenId == 0) {
61
+ keys.push({
62
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
63
+ role: kit_1.AccountRole.WRITABLE,
64
+ });
65
+ keys.push({ address: ctx.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
66
+ }
67
+ return {
68
+ accounts: keys,
69
+ programAddress: ctx.programId,
70
+ data: (0, instruction_models_1.depositData)(7, 0, allFunds, args.tokenId, amount * (0, utils_1.tokenDec)(ctx.tokens, args.tokenId, ctx.uiNumbers), 0, 0),
71
+ };
72
+ }
73
+ else {
74
+ const slot = Number(yield rpcGetSlot()) - 1;
75
+ const lutAddress = yield (0, utils_1.getLookupTableAddress)(ctx.signer, slot);
76
+ const clientCommunityAccount = yield (0, account_helpers_1.findClientCommunityAccount)(ctx, ctx.signer);
77
+ keys.push({ address: clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
78
+ keys.push({ address: lutAddress, role: kit_1.AccountRole.WRITABLE });
79
+ keys.push({ address: constants_1.ADDRESS_LOOKUP_TABLE_PROGRAM_ID, role: kit_1.AccountRole.WRITABLE });
80
+ if (args.tokenId == 0) {
81
+ keys.push({
82
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
83
+ role: kit_1.AccountRole.WRITABLE,
84
+ });
85
+ keys.push({ address: clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
86
+ }
87
+ let refId;
88
+ if (args.refId != null && args.refId != undefined) {
89
+ refId = args.refId;
90
+ if (args.refWallet == null) {
91
+ throw new Error('Ref Wallet Not Found');
92
+ }
93
+ keys.push({
94
+ address: yield (0, account_helpers_1.findClientPrimaryAccount)(ctx, args.refWallet),
95
+ role: kit_1.AccountRole.WRITABLE,
96
+ });
97
+ keys.push({
98
+ address: yield (0, account_helpers_1.findClientCommunityAccount)(ctx, args.refWallet),
99
+ role: kit_1.AccountRole.WRITABLE,
100
+ });
101
+ }
102
+ else {
103
+ refId = 0;
104
+ }
105
+ return {
106
+ accounts: keys,
107
+ programAddress: ctx.programId,
108
+ data: (0, instruction_models_1.depositData)(7, 0, allFunds, args.tokenId, amount * (0, utils_1.tokenDec)(ctx.tokens, args.tokenId, ctx.uiNumbers), slot, refId),
109
+ };
110
+ }
111
+ });
112
+ }
113
+ /**
114
+ * Build withdraw instruction
115
+ */
116
+ function buildWithdrawInstruction(ctx, args) {
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ const token = ctx.tokens.get(args.tokenId);
119
+ if (!token) {
120
+ throw new Error(`Token ${args.tokenId} not found`);
121
+ }
122
+ const tokenProgramId = (token.mask & 0x80000000) != 0 ? constants_1.TOKEN_2022_PROGRAM_ID : constants_1.TOKEN_PROGRAM_ID;
123
+ const clientTokenAccount = yield (0, utils_1.findAssociatedTokenAddress)(ctx.signer, tokenProgramId, token.address);
124
+ let keys = [
125
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
126
+ { address: clientTokenAccount, role: kit_1.AccountRole.WRITABLE },
127
+ { address: token.programAddress, role: kit_1.AccountRole.WRITABLE },
128
+ { address: token.address, role: kit_1.AccountRole.READONLY },
129
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
130
+ { address: yield (0, account_helpers_1.getTokenAccount)(ctx, token.address), role: kit_1.AccountRole.READONLY },
131
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
132
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
133
+ { address: tokenProgramId, role: kit_1.AccountRole.READONLY },
134
+ { address: ctx.drvsAuthority, role: kit_1.AccountRole.READONLY },
135
+ { address: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, role: kit_1.AccountRole.WRITABLE },
136
+ ];
137
+ if (args.spot != undefined) {
138
+ for (let i = 0; i < args.spot.length; ++i) {
139
+ const instr = ctx.instruments.get(args.spot[i].instrId);
140
+ if (!instr) {
141
+ throw new Error(`Instrument ${args.spot[i].instrId} not found`);
142
+ }
143
+ if (instr.header.assetTokenId == args.tokenId || instr.header.crncyTokenId == args.tokenId) {
144
+ keys.push({ address: instr.header.mapsAddress, role: kit_1.AccountRole.READONLY });
145
+ keys.push({
146
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
147
+ assetTokenId: instr.header.assetTokenId,
148
+ crncyTokenId: instr.header.crncyTokenId,
149
+ tag: enums_1.AccountType.SPOT_CLIENT_INFOS,
150
+ }),
151
+ role: kit_1.AccountRole.READONLY,
152
+ });
153
+ }
154
+ }
155
+ }
156
+ if (args.tokenId == 0) {
157
+ keys.push({
158
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
159
+ role: kit_1.AccountRole.WRITABLE,
160
+ });
161
+ keys.push({ address: ctx.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
162
+ }
163
+ return {
164
+ accounts: keys,
165
+ programAddress: ctx.programId,
166
+ data: (0, instruction_models_1.withdrawData)(8, args.tokenId, args.amount * (0, utils_1.tokenDec)(ctx.tokens, args.tokenId, ctx.uiNumbers)),
167
+ };
168
+ });
169
+ }
170
+ /**
171
+ * Build spot LP instruction
172
+ */
173
+ function buildSpotLpInstruction(ctx, args, instr) {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ var _a, _b;
176
+ let keys = [
177
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
178
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
179
+ {
180
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, {
181
+ assetTokenId: instr.header.assetTokenId,
182
+ crncyTokenId: instr.header.crncyTokenId,
183
+ tag: enums_1.AccountType.INSTR,
184
+ }),
185
+ role: kit_1.AccountRole.WRITABLE,
186
+ },
187
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
188
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
189
+ ];
190
+ if (instr.header.assetTokenId == 0) {
191
+ keys.push({
192
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
193
+ role: kit_1.AccountRole.WRITABLE,
194
+ });
195
+ keys.push({ address: ctx.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
196
+ }
197
+ const minPrice = (_a = args.minPrice) !== null && _a !== void 0 ? _a : 0;
198
+ const maxPrice = (_b = args.maxPrice) !== null && _b !== void 0 ? _b : 0;
199
+ return {
200
+ accounts: keys,
201
+ programAddress: ctx.programId,
202
+ data: (0, instruction_models_1.spotLpData)(14, args.side, args.instrId, Math.round(args.amount * constants_1.lpDec), minPrice * 1000000000, maxPrice * 1000000000),
203
+ };
204
+ });
205
+ }
206
+ /**
207
+ * Build new spot order instruction
208
+ */
209
+ function buildNewSpotOrderInstruction(ctx, args, instr) {
210
+ return __awaiter(this, void 0, void 0, function* () {
211
+ var _a, _b, _c;
212
+ let buf = (0, instruction_models_1.newSpotOrderData)(12, (_a = args.ioc) !== null && _a !== void 0 ? _a : 0, (_b = args.orderType) !== null && _b !== void 0 ? _b : 0, args.side, args.instrId, Math.round(args.price * 1000000000), Math.round(args.qty * (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers)), ((_c = args.edgePrice) !== null && _c !== void 0 ? _c : 0) * 1000000000);
213
+ let keys = [
214
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
215
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
216
+ { address: yield (0, account_helpers_1.findClientPrimaryAccount)(ctx, ctx.signer), role: kit_1.AccountRole.WRITABLE },
217
+ { address: yield (0, account_helpers_1.findClientCommunityAccount)(ctx, ctx.signer), role: kit_1.AccountRole.WRITABLE },
218
+ ...(yield (0, context_builders_1.getSpotContext)(ctx, instr.header)),
219
+ ...(yield (0, context_builders_1.getSpotCandles)(ctx, instr.header)),
220
+ {
221
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
222
+ role: instr.header.assetTokenId == 0 ? kit_1.AccountRole.WRITABLE : kit_1.AccountRole.READONLY,
223
+ },
224
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
225
+ ];
226
+ if (ctx.refClientPrimaryAccount != null) {
227
+ keys.push({ address: ctx.refClientPrimaryAccount, role: kit_1.AccountRole.WRITABLE });
228
+ }
229
+ if (ctx.refClientCommunityAccount != null) {
230
+ keys.push({ address: ctx.refClientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
231
+ }
232
+ return { accounts: keys, programAddress: ctx.programId, data: buf };
233
+ });
234
+ }
235
+ /**
236
+ * Build spot quotes replace instruction
237
+ */
238
+ function buildSpotQuotesReplaceInstruction(ctx, args, instr) {
239
+ return __awaiter(this, void 0, void 0, function* () {
240
+ let assetTokenDecFactor = (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers);
241
+ let buf = (0, instruction_models_1.spotQuotesReplaceData)(34, args.instrId, Math.round(args.newBidPrice * 1000000000), Math.round(args.newBidQty * assetTokenDecFactor), args.bidOrderIdToCancel, Math.round(args.newAskPrice * 1000000000), Math.round(args.newAskQty * assetTokenDecFactor), args.askOrderIdToCancel);
242
+ let keys = [
243
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
244
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
245
+ { address: yield (0, account_helpers_1.findClientPrimaryAccount)(ctx, ctx.signer), role: kit_1.AccountRole.WRITABLE },
246
+ { address: yield (0, account_helpers_1.findClientCommunityAccount)(ctx, ctx.signer), role: kit_1.AccountRole.WRITABLE },
247
+ ...(yield (0, context_builders_1.getSpotContext)(ctx, instr.header)),
248
+ ...(yield (0, context_builders_1.getSpotCandles)(ctx, instr.header)),
249
+ {
250
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
251
+ role: instr.header.assetTokenId == 0 ? kit_1.AccountRole.WRITABLE : kit_1.AccountRole.READONLY,
252
+ },
253
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
254
+ ];
255
+ if (ctx.refClientPrimaryAccount != null) {
256
+ keys.push({ address: ctx.refClientPrimaryAccount, role: kit_1.AccountRole.WRITABLE });
257
+ }
258
+ if (ctx.refClientCommunityAccount != null) {
259
+ keys.push({ address: ctx.refClientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
260
+ }
261
+ return { accounts: keys, programAddress: ctx.programId, data: buf };
262
+ });
263
+ }
264
+ /**
265
+ * Build spot order cancel instruction
266
+ */
267
+ function buildSpotOrderCancelInstruction(ctx, args, instr) {
268
+ return __awaiter(this, void 0, void 0, function* () {
269
+ const drvs = instr.header.assetTokenId == 0;
270
+ let keys = [
271
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
272
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
273
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
274
+ ...(yield (0, context_builders_1.getSpotContext)(ctx, instr.header)),
275
+ {
276
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
277
+ role: drvs ? kit_1.AccountRole.WRITABLE : kit_1.AccountRole.READONLY,
278
+ },
279
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
280
+ ];
281
+ if (drvs) {
282
+ keys.push({
283
+ address: yield (0, account_helpers_1.findClientCommunityAccount)(ctx, ctx.signer),
284
+ role: kit_1.AccountRole.WRITABLE,
285
+ });
286
+ }
287
+ return {
288
+ accounts: keys,
289
+ programAddress: ctx.programId,
290
+ data: (0, instruction_models_1.spotOrderCancelData)(13, args.side, args.instrId, args.orderId),
291
+ };
292
+ });
293
+ }
294
+ /**
295
+ * Build spot mass cancel instruction
296
+ */
297
+ function buildSpotMassCancelInstruction(ctx, args, instr) {
298
+ return __awaiter(this, void 0, void 0, function* () {
299
+ const drvs = instr.header.assetTokenId == 0;
300
+ let keys = [
301
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
302
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
303
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
304
+ ...(yield (0, context_builders_1.getSpotContext)(ctx, instr.header)),
305
+ {
306
+ address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY),
307
+ role: drvs ? kit_1.AccountRole.WRITABLE : kit_1.AccountRole.READONLY,
308
+ },
309
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
310
+ ];
311
+ if (drvs) {
312
+ keys.push({
313
+ address: yield (0, account_helpers_1.findClientCommunityAccount)(ctx, ctx.signer),
314
+ role: kit_1.AccountRole.WRITABLE,
315
+ });
316
+ }
317
+ return {
318
+ accounts: keys,
319
+ programAddress: ctx.programId,
320
+ data: (0, instruction_models_1.spotMassCancelData)(15, args.instrId),
321
+ };
322
+ });
323
+ }
324
+ /**
325
+ * Build swap instruction
326
+ */
327
+ function buildSwapInstruction(ctx, args, instr) {
328
+ return __awaiter(this, void 0, void 0, function* () {
329
+ const assetTokenId = yield (0, account_helpers_1.getTokenId)(ctx, args.assetMint);
330
+ const crncyTokenId = yield (0, account_helpers_1.getTokenId)(ctx, args.crncyMint);
331
+ if (assetTokenId == null) {
332
+ throw new Error(`Asset token not found for mint ${args.assetMint}`);
333
+ }
334
+ if (crncyTokenId == null) {
335
+ throw new Error(`Currency token not found for mint ${args.crncyMint}`);
336
+ }
337
+ const assetTokenAccount = ctx.tokens.get(assetTokenId);
338
+ const crncyTokenAccount = ctx.tokens.get(crncyTokenId);
339
+ if (!assetTokenAccount || !crncyTokenAccount) {
340
+ throw new Error('Token account not found');
341
+ }
342
+ const assetTokenProgramId = (assetTokenAccount.mask & 0x80000000) == 0 ? constants_1.TOKEN_PROGRAM_ID : constants_1.TOKEN_2022_PROGRAM_ID;
343
+ const crncyTokenProgramId = (crncyTokenAccount.mask & 0x80000000) == 0 ? constants_1.TOKEN_PROGRAM_ID : constants_1.TOKEN_2022_PROGRAM_ID;
344
+ let instrId = yield (0, account_helpers_1.getInstrId)(ctx, { assetTokenId: assetTokenId, crncyTokenId: crncyTokenId });
345
+ if (instrId === null) {
346
+ throw new Error('No instruction ID');
347
+ }
348
+ const clientAssetTokenAccount = yield (0, utils_1.findAssociatedTokenAddress)(ctx.signer, assetTokenProgramId, args.assetMint);
349
+ const clientCrncyTokenAccount = yield (0, utils_1.findAssociatedTokenAddress)(ctx.signer, crncyTokenProgramId, args.crncyMint);
350
+ let buf = (0, instruction_models_1.swapData)(26, args.crncyInput ? 1 : 0, instrId, Math.round(args.limitPrice * 1000000000), Math.round(args.amount *
351
+ (args.crncyInput
352
+ ? (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers)
353
+ : (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers))));
354
+ let keys = [
355
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
356
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
357
+ ...(yield (0, context_builders_1.getSpotContext)(ctx, instr.header)),
358
+ ...(yield (0, context_builders_1.getSpotCandles)(ctx, instr.header)),
359
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
360
+ { address: assetTokenAccount.programAddress, role: kit_1.AccountRole.WRITABLE },
361
+ { address: crncyTokenAccount.programAddress, role: kit_1.AccountRole.WRITABLE },
362
+ { address: args.assetMint, role: kit_1.AccountRole.READONLY },
363
+ { address: args.crncyMint, role: kit_1.AccountRole.READONLY },
364
+ { address: yield (0, account_helpers_1.getTokenAccount)(ctx, args.assetMint), role: kit_1.AccountRole.READONLY },
365
+ { address: yield (0, account_helpers_1.getTokenAccount)(ctx, args.crncyMint), role: kit_1.AccountRole.READONLY },
366
+ { address: clientAssetTokenAccount, role: kit_1.AccountRole.WRITABLE },
367
+ { address: clientCrncyTokenAccount, role: kit_1.AccountRole.WRITABLE },
368
+ { address: ctx.drvsAuthority, role: kit_1.AccountRole.READONLY },
369
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
370
+ { address: assetTokenProgramId, role: kit_1.AccountRole.READONLY },
371
+ { address: crncyTokenProgramId, role: kit_1.AccountRole.READONLY },
372
+ { address: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
373
+ ];
374
+ return { accounts: keys, programAddress: ctx.programId, data: buf };
375
+ });
376
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,221 @@
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 kit_1 = require("@solana/kit");
14
+ const spot_instructions_1 = require("./spot-instructions");
15
+ const structure_models_1 = require("../structure_models");
16
+ vitest_1.vi.mock('./account-helpers', () => ({
17
+ getAccountByTag: vitest_1.vi.fn().mockResolvedValue('MockAccountByTag11111111111111'),
18
+ getInstrAccountByTag: vitest_1.vi.fn().mockResolvedValue('MockInstrAccount1111111111111'),
19
+ getTokenAccount: vitest_1.vi.fn().mockResolvedValue('MockTokenAccount111111111111111'),
20
+ getTokenId: vitest_1.vi.fn().mockResolvedValue(1),
21
+ getInstrId: vitest_1.vi.fn().mockResolvedValue(1),
22
+ findClientPrimaryAccount: vitest_1.vi.fn().mockResolvedValue('MockClientPrimary11111111111'),
23
+ findClientCommunityAccount: vitest_1.vi.fn().mockResolvedValue('MockClientCommunity111111111'),
24
+ }));
25
+ vitest_1.vi.mock('./context-builders', () => ({
26
+ getSpotContext: vitest_1.vi.fn().mockResolvedValue([
27
+ { address: 'SpotCtx1111111111111111111111111', role: kit_1.AccountRole.WRITABLE },
28
+ { address: 'SpotCtx2222222222222222222222222', role: kit_1.AccountRole.WRITABLE },
29
+ { address: 'SpotCtx3333333333333333333333333', role: kit_1.AccountRole.WRITABLE },
30
+ { address: 'SpotCtx4444444444444444444444444', role: kit_1.AccountRole.WRITABLE },
31
+ { address: 'SpotCtx5555555555555555555555555', role: kit_1.AccountRole.WRITABLE },
32
+ { address: 'SpotCtx6666666666666666666666666', role: kit_1.AccountRole.WRITABLE },
33
+ { address: 'SpotCtx7777777777777777777777777', role: kit_1.AccountRole.WRITABLE },
34
+ { address: 'SpotCtx8888888888888888888888888', role: kit_1.AccountRole.WRITABLE },
35
+ { address: 'SpotCtx9999999999999999999999999', role: kit_1.AccountRole.WRITABLE },
36
+ ]),
37
+ getSpotCandles: vitest_1.vi.fn().mockResolvedValue([
38
+ { address: 'SpotCandles1111111111111111111', role: kit_1.AccountRole.WRITABLE },
39
+ { address: 'SpotCandles2222222222222222222', role: kit_1.AccountRole.WRITABLE },
40
+ { address: 'SpotCandles3333333333333333333', role: kit_1.AccountRole.WRITABLE },
41
+ ]),
42
+ getPerpContext: vitest_1.vi.fn().mockResolvedValue([]),
43
+ }));
44
+ vitest_1.vi.mock('./utils', () => ({
45
+ findAssociatedTokenAddress: vitest_1.vi.fn().mockResolvedValue('MockATA1111111111111111111111111'),
46
+ getLookupTableAddress: vitest_1.vi.fn().mockResolvedValue('MockLUT1111111111111111111111111'),
47
+ perpSeatReserve: vitest_1.vi.fn().mockReturnValue(0),
48
+ tokenDec: vitest_1.vi.fn().mockReturnValue(1000000000), // 10^9
49
+ }));
50
+ // Helper to create a mock token
51
+ function createMockToken(id, decimals = 9) {
52
+ const token = new structure_models_1.TokenStateModel();
53
+ token.id = id;
54
+ token.mask = decimals; // decimals stored in lower byte
55
+ token.address = `MockToken${id}${'1'.repeat(32 - 10 - id.toString().length)}`;
56
+ token.programAddress = `MockProgramAddress${id}${'1'.repeat(32 - 19 - id.toString().length)}`;
57
+ return token;
58
+ }
59
+ // Helper to create a mock instrument
60
+ function createMockInstrument(instrId, assetTokenId = 1, crncyTokenId = 0) {
61
+ const header = new structure_models_1.InstrAccountHeaderModel();
62
+ header.instrId = instrId;
63
+ header.assetTokenId = assetTokenId;
64
+ header.crncyTokenId = crncyTokenId;
65
+ header.mapsAddress = '11111111111111111111111111111111';
66
+ header.perpMapsAddress = '22222222222222222222222222222222';
67
+ return {
68
+ address: `MockInstr${instrId}${'1'.repeat(32 - 9 - instrId.toString().length)}`,
69
+ header,
70
+ spotBids: [],
71
+ spotAsks: [],
72
+ perpBids: [],
73
+ perpAsks: [],
74
+ };
75
+ }
76
+ // Helper to create a mock context
77
+ function createMockSpotContext(overrides = {}) {
78
+ const tokens = new Map();
79
+ tokens.set(0, createMockToken(0, 9)); // DRVS token
80
+ tokens.set(1, createMockToken(1, 9)); // SOL-like token
81
+ const instruments = new Map();
82
+ instruments.set(1, createMockInstrument(1, 1, 0));
83
+ return Object.assign({ rpc: {}, programId: 'DRVSvY68xD69Zwwgj9N8hRBu2eXvEzy8eejiVLNYGLti', version: 1, commitment: 'confirmed', drvsAuthority: 'DRVStQsAKhF8gpz1xLzWyk2Q1HKKF3g21tknXtHpVLab', instruments,
84
+ tokens, uiNumbers: true, signer: 'SignerAddress111111111111111111', rootAccount: 'RootAccount1111111111111111111111', clientPrimaryAccount: 'ClientPrimary11111111111111111', clientCommunityAccount: 'ClientCommunity111111111111111', refClientPrimaryAccount: null, refClientCommunityAccount: null, privateMode: false }, overrides);
85
+ }
86
+ (0, vitest_1.describe)('spot instruction builders', () => {
87
+ (0, vitest_1.beforeEach)(() => {
88
+ vitest_1.vi.clearAllMocks();
89
+ });
90
+ (0, vitest_1.describe)('buildDepositInstruction', () => {
91
+ (0, vitest_1.it)('builds deposit instruction for existing account', () => __awaiter(void 0, void 0, void 0, function* () {
92
+ const ctx = createMockSpotContext();
93
+ const args = { tokenId: 1, amount: 100 };
94
+ const rpcGetSlot = vitest_1.vi.fn().mockResolvedValue(BigInt(12345));
95
+ const instruction = yield (0, spot_instructions_1.buildDepositInstruction)(ctx, args, true, rpcGetSlot);
96
+ (0, vitest_1.expect)(instruction).toBeDefined();
97
+ (0, vitest_1.expect)(instruction.programAddress).toBe(ctx.programId);
98
+ (0, vitest_1.expect)(instruction.accounts).toBeDefined();
99
+ (0, vitest_1.expect)(instruction.accounts.length).toBeGreaterThan(0);
100
+ (0, vitest_1.expect)(instruction.data).toBeInstanceOf(Uint8Array);
101
+ (0, vitest_1.expect)(instruction.data[0]).toBe(7); // deposit instruction tag
102
+ }));
103
+ (0, vitest_1.it)('builds deposit instruction for new account', () => __awaiter(void 0, void 0, void 0, function* () {
104
+ const ctx = createMockSpotContext();
105
+ const args = { tokenId: 1, amount: 50 };
106
+ const rpcGetSlot = vitest_1.vi.fn().mockResolvedValue(BigInt(12345));
107
+ const instruction = yield (0, spot_instructions_1.buildDepositInstruction)(ctx, args, false, rpcGetSlot);
108
+ (0, vitest_1.expect)(instruction).toBeDefined();
109
+ (0, vitest_1.expect)(instruction.programAddress).toBe(ctx.programId);
110
+ (0, vitest_1.expect)(instruction.accounts.length).toBeGreaterThan(0);
111
+ (0, vitest_1.expect)(rpcGetSlot).toHaveBeenCalled();
112
+ }));
113
+ (0, vitest_1.it)('includes private mode account when enabled', () => __awaiter(void 0, void 0, void 0, function* () {
114
+ const ctx = createMockSpotContext({ privateMode: true });
115
+ const args = { tokenId: 1, amount: 100 };
116
+ const rpcGetSlot = vitest_1.vi.fn().mockResolvedValue(BigInt(12345));
117
+ const instruction = yield (0, spot_instructions_1.buildDepositInstruction)(ctx, args, true, rpcGetSlot);
118
+ (0, vitest_1.expect)(instruction.accounts.length).toBeGreaterThan(9);
119
+ }));
120
+ (0, vitest_1.it)('handles all_funds flag', () => __awaiter(void 0, void 0, void 0, function* () {
121
+ const ctx = createMockSpotContext();
122
+ const args = { tokenId: 1, amount: 0, all_funds: true };
123
+ const rpcGetSlot = vitest_1.vi.fn().mockResolvedValue(BigInt(12345));
124
+ const instruction = yield (0, spot_instructions_1.buildDepositInstruction)(ctx, args, true, rpcGetSlot);
125
+ (0, vitest_1.expect)(instruction).toBeDefined();
126
+ (0, vitest_1.expect)(instruction.data[2]).toBe(1); // all_funds flag position
127
+ }));
128
+ });
129
+ (0, vitest_1.describe)('buildWithdrawInstruction', () => {
130
+ (0, vitest_1.it)('builds withdraw instruction', () => __awaiter(void 0, void 0, void 0, function* () {
131
+ const ctx = createMockSpotContext();
132
+ const args = { tokenId: 1, amount: 50 };
133
+ const instruction = yield (0, spot_instructions_1.buildWithdrawInstruction)(ctx, args);
134
+ (0, vitest_1.expect)(instruction).toBeDefined();
135
+ (0, vitest_1.expect)(instruction.programAddress).toBe(ctx.programId);
136
+ (0, vitest_1.expect)(instruction.accounts).toBeDefined();
137
+ (0, vitest_1.expect)(instruction.accounts.length).toBeGreaterThan(0);
138
+ (0, vitest_1.expect)(instruction.data).toBeInstanceOf(Uint8Array);
139
+ (0, vitest_1.expect)(instruction.data[0]).toBe(8); // withdraw instruction tag
140
+ }));
141
+ (0, vitest_1.it)('includes spot instruments when provided', () => __awaiter(void 0, void 0, void 0, function* () {
142
+ const ctx = createMockSpotContext();
143
+ const args = {
144
+ tokenId: 1,
145
+ amount: 50,
146
+ spot: [{ instrId: 1 }],
147
+ };
148
+ const instruction = yield (0, spot_instructions_1.buildWithdrawInstruction)(ctx, args);
149
+ (0, vitest_1.expect)(instruction.accounts.length).toBeGreaterThan(11);
150
+ }));
151
+ });
152
+ (0, vitest_1.describe)('buildSpotLpInstruction', () => {
153
+ (0, vitest_1.it)('builds spot LP instruction', () => __awaiter(void 0, void 0, void 0, function* () {
154
+ const ctx = createMockSpotContext();
155
+ const instr = ctx.instruments.get(1);
156
+ const args = { instrId: 1, side: 0, amount: 100 };
157
+ const instruction = yield (0, spot_instructions_1.buildSpotLpInstruction)(ctx, args, instr);
158
+ (0, vitest_1.expect)(instruction).toBeDefined();
159
+ (0, vitest_1.expect)(instruction.programAddress).toBe(ctx.programId);
160
+ (0, vitest_1.expect)(instruction.data[0]).toBe(14); // spotLp instruction tag
161
+ }));
162
+ (0, vitest_1.it)('includes price bounds when provided', () => __awaiter(void 0, void 0, void 0, function* () {
163
+ const ctx = createMockSpotContext();
164
+ const instr = ctx.instruments.get(1);
165
+ const args = { instrId: 1, side: 1, amount: 100, minPrice: 0.9, maxPrice: 1.1 };
166
+ const instruction = yield (0, spot_instructions_1.buildSpotLpInstruction)(ctx, args, instr);
167
+ (0, vitest_1.expect)(instruction).toBeDefined();
168
+ }));
169
+ });
170
+ (0, vitest_1.describe)('buildNewSpotOrderInstruction', () => {
171
+ (0, vitest_1.it)('builds new spot order instruction', () => __awaiter(void 0, void 0, void 0, function* () {
172
+ const ctx = createMockSpotContext();
173
+ const instr = ctx.instruments.get(1);
174
+ const args = { instrId: 1, side: 0, price: 100, qty: 10 };
175
+ const instruction = yield (0, spot_instructions_1.buildNewSpotOrderInstruction)(ctx, args, instr);
176
+ (0, vitest_1.expect)(instruction).toBeDefined();
177
+ (0, vitest_1.expect)(instruction.programAddress).toBe(ctx.programId);
178
+ (0, vitest_1.expect)(instruction.data[0]).toBe(12); // newSpotOrder instruction tag
179
+ }));
180
+ (0, vitest_1.it)('includes IOC flag when provided', () => __awaiter(void 0, void 0, void 0, function* () {
181
+ const ctx = createMockSpotContext();
182
+ const instr = ctx.instruments.get(1);
183
+ const args = { instrId: 1, side: 1, price: 100, qty: 10, ioc: 1 };
184
+ const instruction = yield (0, spot_instructions_1.buildNewSpotOrderInstruction)(ctx, args, instr);
185
+ (0, vitest_1.expect)(instruction).toBeDefined();
186
+ (0, vitest_1.expect)(instruction.data[1]).toBe(1); // ioc flag
187
+ }));
188
+ (0, vitest_1.it)('includes ref accounts when provided', () => __awaiter(void 0, void 0, void 0, function* () {
189
+ const ctx = createMockSpotContext({
190
+ refClientPrimaryAccount: 'RefPrimary1111111111111111111111',
191
+ refClientCommunityAccount: 'RefCommunity11111111111111111111',
192
+ });
193
+ const instr = ctx.instruments.get(1);
194
+ const args = { instrId: 1, side: 0, price: 100, qty: 10 };
195
+ const instruction = yield (0, spot_instructions_1.buildNewSpotOrderInstruction)(ctx, args, instr);
196
+ (0, vitest_1.expect)(instruction.accounts.length).toBeGreaterThan(15);
197
+ }));
198
+ });
199
+ (0, vitest_1.describe)('buildSpotOrderCancelInstruction', () => {
200
+ (0, vitest_1.it)('builds spot order cancel instruction', () => __awaiter(void 0, void 0, void 0, function* () {
201
+ const ctx = createMockSpotContext();
202
+ const instr = ctx.instruments.get(1);
203
+ const args = { instrId: 1, side: 0, orderId: 12345 };
204
+ const instruction = yield (0, spot_instructions_1.buildSpotOrderCancelInstruction)(ctx, args, instr);
205
+ (0, vitest_1.expect)(instruction).toBeDefined();
206
+ (0, vitest_1.expect)(instruction.programAddress).toBe(ctx.programId);
207
+ (0, vitest_1.expect)(instruction.data[0]).toBe(13); // spotOrderCancel instruction tag
208
+ }));
209
+ });
210
+ (0, vitest_1.describe)('buildSpotMassCancelInstruction', () => {
211
+ (0, vitest_1.it)('builds spot mass cancel instruction', () => __awaiter(void 0, void 0, void 0, function* () {
212
+ const ctx = createMockSpotContext();
213
+ const instr = ctx.instruments.get(1);
214
+ const args = { instrId: 1 };
215
+ const instruction = yield (0, spot_instructions_1.buildSpotMassCancelInstruction)(ctx, args, instr);
216
+ (0, vitest_1.expect)(instruction).toBeDefined();
217
+ (0, vitest_1.expect)(instruction.programAddress).toBe(ctx.programId);
218
+ (0, vitest_1.expect)(instruction.data[0]).toBe(15); // spotMassCancel instruction tag
219
+ }));
220
+ });
221
+ });
@@ -0,0 +1,23 @@
1
+ import { Address, Base64EncodedDataResponse } from '@solana/kit';
2
+ import { OrderModel, TokenStateModel } from '../structure_models';
3
+ /**
4
+ * Get token decimal factor for UI number conversion
5
+ */
6
+ export declare function tokenDec(tokens: Map<number, TokenStateModel>, tokenId: number, uiNumbers: boolean): number;
7
+ /**
8
+ * Get price step between orderbook lines depending on curent price
9
+ * @param price Current market price
10
+ * @returns Price step
11
+ */
12
+ export declare function getSpotPriceStep(price: number): number;
13
+ /**
14
+ * Get price step between orderbook lines depending on curent price
15
+ * @param price Current market price
16
+ * @returns Price step
17
+ */
18
+ export declare function getPerpPriceStep(price: number): number;
19
+ export declare function perpSeatReserve(activeUsers: number): number;
20
+ export declare function getMultipleSpotOrders(data: Base64EncodedDataResponse, firstEntry: number, clientId: number): Array<OrderModel>;
21
+ export declare function getMultiplePerpOrders(data: Base64EncodedDataResponse, firstEntry: number, clientId: number): Array<OrderModel>;
22
+ export declare function findAssociatedTokenAddress(owner: Address, tokenProgramId: Address, mint: Address): Promise<Address>;
23
+ export declare function getLookupTableAddress(authority: Address, slot: number): Promise<Address>;