@deriverse/kit 1.0.39 → 1.0.42

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 (62) 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 +14 -0
  4. package/dist/constants.js +28 -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 +685 -0
  21. package/dist/engine/logs-decoder.d.ts +18 -0
  22. package/dist/engine/logs-decoder.js +525 -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 +497 -0
  27. package/dist/engine/perp-instructions.test.d.ts +1 -0
  28. package/dist/engine/perp-instructions.test.js +292 -0
  29. package/dist/engine/spot-instructions.d.ts +52 -0
  30. package/dist/engine/spot-instructions.js +399 -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 +332 -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 +10 -9
  40. package/dist/instruction_models.js +88 -69
  41. package/dist/logs_models.d.ts +147 -5
  42. package/dist/logs_models.js +243 -13
  43. package/dist/structure_models.d.ts +104 -99
  44. package/dist/structure_models.js +152 -129
  45. package/dist/types/engine-args.d.ts +32 -0
  46. package/dist/types/engine-args.js +2 -0
  47. package/dist/types/enums.d.ts +43 -0
  48. package/dist/{types.js → types/enums.js} +3 -5
  49. package/dist/types/index.d.ts +8 -0
  50. package/dist/types/index.js +38 -0
  51. package/dist/types/log-message.d.ts +2 -0
  52. package/dist/types/log-message.js +2 -0
  53. package/dist/types/responses.d.ts +248 -0
  54. package/dist/types/responses.js +2 -0
  55. package/dist/types/schemas.d.ts +168 -0
  56. package/dist/types/schemas.js +239 -0
  57. package/dist/types/schemas.test.d.ts +1 -0
  58. package/dist/types/schemas.test.js +93 -0
  59. package/dist/utils.d.ts +0 -0
  60. package/dist/utils.js +1 -0
  61. package/package.json +26 -6
  62. package/dist/types.d.ts +0 -565
@@ -0,0 +1,68 @@
1
+ import { Address, SolanaRpcResponse, AccountInfoBase } from '@solana/kit';
2
+ import { Instrument, InstrId, PerpDepositArgs, PerpBuySeatArgs, PerpSellSeatArgs, NewPerpOrderArgs, PerpQuotesReplaceArgs, PerpOrderCancelArgs, PerpMassCancelArgs, PerpChangeLeverageArgs, PerpStatisticsResetArgs, NewInstrumentArgs, Instruction } from '../types';
3
+ import { TokenStateModel, RootStateModel } from '../structure_models';
4
+ import { AccountHelperContext } from './account-helpers';
5
+ /**
6
+ * Context needed for perp instruction builders
7
+ */
8
+ export interface PerpInstructionContext extends AccountHelperContext {
9
+ instruments: Map<number, Instrument>;
10
+ tokens: Map<number, TokenStateModel>;
11
+ rootStateModel: RootStateModel;
12
+ uiNumbers: boolean;
13
+ signer: Address;
14
+ rootAccount: Address;
15
+ clientPrimaryAccount: Address;
16
+ clientCommunityAccount: Address;
17
+ refClientPrimaryAccount: Address | null;
18
+ refClientCommunityAccount: Address | null;
19
+ }
20
+ /**
21
+ * Build upgrade to perp instructions
22
+ */
23
+ declare function buildUpgradeToPerpInstructions(ctx: PerpInstructionContext, args: InstrId, instr: Instrument, rpcGetMinBalance: (size: bigint) => Promise<bigint>): Promise<Instruction[]>;
24
+ /**
25
+ * Build perp deposit instruction
26
+ */
27
+ declare function buildPerpDepositInstruction(ctx: PerpInstructionContext, args: PerpDepositArgs, instr: Instrument): Promise<Instruction>;
28
+ /**
29
+ * Build perp buy seat instruction
30
+ */
31
+ declare function buildPerpBuySeatInstruction(ctx: PerpInstructionContext, args: PerpBuySeatArgs, instr: Instrument): Promise<Instruction>;
32
+ /**
33
+ * Build perp sell seat instruction
34
+ */
35
+ declare function buildPerpSellSeatInstruction(ctx: PerpInstructionContext, args: PerpSellSeatArgs, instr: Instrument): Promise<Instruction>;
36
+ /**
37
+ * Build new perp order instruction
38
+ */
39
+ declare function buildNewPerpOrderInstruction(ctx: PerpInstructionContext, args: NewPerpOrderArgs, instr: Instrument): Promise<Instruction>;
40
+ /**
41
+ * Build perp quotes replace instruction
42
+ */
43
+ declare function buildPerpQuotesReplaceInstruction(ctx: PerpInstructionContext, args: PerpQuotesReplaceArgs, instr: Instrument): Promise<Instruction>;
44
+ /**
45
+ * Build perp order cancel instruction
46
+ */
47
+ declare function buildPerpOrderCancelInstruction(ctx: PerpInstructionContext, args: PerpOrderCancelArgs, instr: Instrument): Promise<Instruction>;
48
+ /**
49
+ * Build perp mass cancel instruction
50
+ */
51
+ declare function buildPerpMassCancelInstruction(ctx: PerpInstructionContext, args: PerpMassCancelArgs, instr: Instrument): Promise<Instruction>;
52
+ /**
53
+ * Build perp change leverage instruction
54
+ */
55
+ declare function buildPerpChangeLeverageInstruction(ctx: PerpInstructionContext, args: PerpChangeLeverageArgs, instr: Instrument): Promise<Instruction>;
56
+ /**
57
+ * Build perp statistics reset instruction
58
+ */
59
+ declare function buildPerpStatisticsResetInstruction(ctx: PerpInstructionContext, args: PerpStatisticsResetArgs, instr: Instrument): Promise<Instruction>;
60
+ /**
61
+ * Build new ref link instruction
62
+ */
63
+ declare function buildNewRefLinkInstruction(ctx: PerpInstructionContext): Promise<Instruction>;
64
+ /**
65
+ * Build new instrument instructions
66
+ */
67
+ declare function buildNewInstrumentInstructions(ctx: PerpInstructionContext, args: NewInstrumentArgs, rpcGetSlot: () => Promise<bigint>, rpcGetAccountInfo: (address: Address) => Promise<SolanaRpcResponse<AccountInfoBase | null>>, rpcGetMinBalance: (size: bigint) => Promise<bigint>): Promise<Instruction[]>;
68
+ export { buildUpgradeToPerpInstructions, buildPerpDepositInstruction, buildPerpBuySeatInstruction, buildPerpSellSeatInstruction, buildNewPerpOrderInstruction, buildPerpQuotesReplaceInstruction, buildPerpOrderCancelInstruction, buildPerpMassCancelInstruction, buildPerpChangeLeverageInstruction, buildPerpStatisticsResetInstruction, buildNewRefLinkInstruction, buildNewInstrumentInstructions, };
@@ -0,0 +1,497 @@
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.buildUpgradeToPerpInstructions = buildUpgradeToPerpInstructions;
13
+ exports.buildPerpDepositInstruction = buildPerpDepositInstruction;
14
+ exports.buildPerpBuySeatInstruction = buildPerpBuySeatInstruction;
15
+ exports.buildPerpSellSeatInstruction = buildPerpSellSeatInstruction;
16
+ exports.buildNewPerpOrderInstruction = buildNewPerpOrderInstruction;
17
+ exports.buildPerpQuotesReplaceInstruction = buildPerpQuotesReplaceInstruction;
18
+ exports.buildPerpOrderCancelInstruction = buildPerpOrderCancelInstruction;
19
+ exports.buildPerpMassCancelInstruction = buildPerpMassCancelInstruction;
20
+ exports.buildPerpChangeLeverageInstruction = buildPerpChangeLeverageInstruction;
21
+ exports.buildPerpStatisticsResetInstruction = buildPerpStatisticsResetInstruction;
22
+ exports.buildNewRefLinkInstruction = buildNewRefLinkInstruction;
23
+ exports.buildNewInstrumentInstructions = buildNewInstrumentInstructions;
24
+ const kit_1 = require("@solana/kit");
25
+ const system_1 = require("@solana-program/system");
26
+ const enums_1 = require("../types/enums");
27
+ const constants_1 = require("../constants");
28
+ const utils_1 = require("./utils");
29
+ const structure_models_1 = require("../structure_models");
30
+ const instruction_models_1 = require("../instruction_models");
31
+ const account_helpers_1 = require("./account-helpers");
32
+ const context_builders_1 = require("./context-builders");
33
+ /**
34
+ * Build upgrade to perp instructions
35
+ */
36
+ function buildUpgradeToPerpInstructions(ctx, args, instr, rpcGetMinBalance) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ if ((instr.header.mask & enums_1.InstrMask.READY_TO_PERP_UPGRADE) == 0) {
39
+ throw new Error('Impossible to upgrade');
40
+ }
41
+ if ((instr.header.mask & enums_1.InstrMask.PERP) != 0) {
42
+ throw new Error('Instr already upgraded');
43
+ }
44
+ const perpMapsAccountSeed = ctx.version.toString() +
45
+ '_' +
46
+ enums_1.AccountType.PERP_MAPS.toString() +
47
+ '_' +
48
+ instr.header.assetTokenId.toString() +
49
+ '_' +
50
+ instr.header.crncyTokenId.toString();
51
+ const perpMapsAccount = yield (0, kit_1.createAddressWithSeed)({
52
+ baseAddress: ctx.signer,
53
+ programAddress: ctx.programId,
54
+ seed: perpMapsAccountSeed,
55
+ });
56
+ const perpMapsAccountSize = 168576;
57
+ const perpMapsAccountLamports = yield rpcGetMinBalance(BigInt(perpMapsAccountSize));
58
+ const createMapsAccountIx = (0, system_1.getCreateAccountWithSeedInstruction)({
59
+ payer: ctx.signer,
60
+ baseAccount: ctx.signer,
61
+ base: ctx.signer,
62
+ newAccount: perpMapsAccount,
63
+ seed: perpMapsAccountSeed,
64
+ space: perpMapsAccountSize,
65
+ programAddress: ctx.programId,
66
+ amount: perpMapsAccountLamports,
67
+ });
68
+ const tokenArgs = {
69
+ assetTokenId: instr.header.assetTokenId,
70
+ crncyTokenId: instr.header.crncyTokenId,
71
+ };
72
+ let keys = [
73
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
74
+ { address: ctx.rootAccount, role: kit_1.AccountRole.WRITABLE },
75
+ {
76
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.INSTR })),
77
+ role: kit_1.AccountRole.WRITABLE,
78
+ },
79
+ { address: instr.header.lutAddress, role: kit_1.AccountRole.WRITABLE },
80
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
81
+ { address: constants_1.ADDRESS_LOOKUP_TABLE_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
82
+ { address: ctx.drvsAuthority, role: kit_1.AccountRole.READONLY },
83
+ {
84
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_BIDS_TREE })),
85
+ role: kit_1.AccountRole.WRITABLE,
86
+ },
87
+ {
88
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_ASKS_TREE })),
89
+ role: kit_1.AccountRole.WRITABLE,
90
+ },
91
+ {
92
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_BID_ORDERS })),
93
+ role: kit_1.AccountRole.WRITABLE,
94
+ },
95
+ {
96
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_ASK_ORDERS })),
97
+ role: kit_1.AccountRole.WRITABLE,
98
+ },
99
+ {
100
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_LINES })),
101
+ role: kit_1.AccountRole.WRITABLE,
102
+ },
103
+ { address: perpMapsAccount, role: kit_1.AccountRole.WRITABLE },
104
+ {
105
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_CLIENT_INFOS })),
106
+ role: kit_1.AccountRole.WRITABLE,
107
+ },
108
+ {
109
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_CLIENT_INFOS2 })),
110
+ role: kit_1.AccountRole.WRITABLE,
111
+ },
112
+ {
113
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_CLIENT_INFOS3 })),
114
+ role: kit_1.AccountRole.WRITABLE,
115
+ },
116
+ {
117
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_CLIENT_INFOS4 })),
118
+ role: kit_1.AccountRole.WRITABLE,
119
+ },
120
+ {
121
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_CLIENT_INFOS5 })),
122
+ role: kit_1.AccountRole.WRITABLE,
123
+ },
124
+ {
125
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_LONG_PX_TREE })),
126
+ role: kit_1.AccountRole.WRITABLE,
127
+ },
128
+ {
129
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_SHORT_PX_TREE })),
130
+ role: kit_1.AccountRole.WRITABLE,
131
+ },
132
+ {
133
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.PERP_REBALANCE_TIME_TREE })),
134
+ role: kit_1.AccountRole.WRITABLE,
135
+ },
136
+ ];
137
+ const upgradeIx = {
138
+ accounts: keys,
139
+ programAddress: ctx.programId,
140
+ data: (0, instruction_models_1.upgradeToPerpData)(10, args.instrId),
141
+ };
142
+ return [createMapsAccountIx, upgradeIx];
143
+ });
144
+ }
145
+ /**
146
+ * Build perp deposit instruction
147
+ */
148
+ function buildPerpDepositInstruction(ctx, args, instr) {
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ let keys = [
151
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
152
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
153
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
154
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
155
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
156
+ ];
157
+ return {
158
+ accounts: keys,
159
+ programAddress: ctx.programId,
160
+ data: (0, instruction_models_1.perpDepositData)(11, args.instrId, args.amount * (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers)),
161
+ };
162
+ });
163
+ }
164
+ /**
165
+ * Build perp buy seat instruction
166
+ */
167
+ function buildPerpBuySeatInstruction(ctx, args, instr) {
168
+ return __awaiter(this, void 0, void 0, function* () {
169
+ var _a;
170
+ let keys = [
171
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
172
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
173
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
174
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
175
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
176
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
177
+ ];
178
+ const slippage = (_a = args.slippage) !== null && _a !== void 0 ? _a : 0;
179
+ const slippagePrice = ((0, utils_1.perpSeatReserve)(instr.header.perpClientsCount + 1) - (0, utils_1.perpSeatReserve)(instr.header.perpClientsCount)) *
180
+ (1 + slippage);
181
+ const crncyDec = (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers);
182
+ return {
183
+ accounts: keys,
184
+ programAddress: ctx.programId,
185
+ data: (0, instruction_models_1.buyMarketSeatData)(47, args.instrId, slippagePrice * crncyDec, args.amount * crncyDec),
186
+ };
187
+ });
188
+ }
189
+ /**
190
+ * Build perp sell seat instruction
191
+ */
192
+ function buildPerpSellSeatInstruction(ctx, args, instr) {
193
+ return __awaiter(this, void 0, void 0, function* () {
194
+ var _a;
195
+ let keys = [
196
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
197
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
198
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
199
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
200
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
201
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
202
+ ];
203
+ const slippage = (_a = args.slippage) !== null && _a !== void 0 ? _a : 0;
204
+ const slippagePrice = ((0, utils_1.perpSeatReserve)(instr.header.perpClientsCount) - (0, utils_1.perpSeatReserve)(instr.header.perpClientsCount - 1)) /
205
+ (1 + slippage);
206
+ const crncyDec = (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers);
207
+ return {
208
+ accounts: keys,
209
+ programAddress: ctx.programId,
210
+ data: (0, instruction_models_1.sellMarketSeatData)(48, args.instrId, slippagePrice * crncyDec),
211
+ };
212
+ });
213
+ }
214
+ /**
215
+ * Build new perp order instruction
216
+ */
217
+ function buildNewPerpOrderInstruction(ctx, args, instr) {
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ var _a, _b, _c, _d;
220
+ let keys = [
221
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
222
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
223
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
224
+ { address: ctx.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE },
225
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
226
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
227
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
228
+ ];
229
+ if (ctx.refClientPrimaryAccount != null) {
230
+ keys.push({ address: ctx.refClientPrimaryAccount, role: kit_1.AccountRole.WRITABLE });
231
+ }
232
+ if (ctx.refClientCommunityAccount != null) {
233
+ keys.push({ address: ctx.refClientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
234
+ }
235
+ return {
236
+ accounts: keys,
237
+ programAddress: ctx.programId,
238
+ data: (0, instruction_models_1.newPerpOrderData)(19, (_a = args.ioc) !== null && _a !== void 0 ? _a : 0, (_b = args.leverage) !== null && _b !== void 0 ? _b : 0, (_c = args.orderType) !== null && _c !== void 0 ? _c : 0, args.side, args.instrId, args.price * constants_1.DF, args.qty * (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers), ((_d = args.edgePrice) !== null && _d !== void 0 ? _d : 0) * constants_1.DF),
239
+ };
240
+ });
241
+ }
242
+ /**
243
+ * Build perp quotes replace instruction
244
+ */
245
+ function buildPerpQuotesReplaceInstruction(ctx, args, instr) {
246
+ return __awaiter(this, void 0, void 0, function* () {
247
+ let assetTokenDecFactor = (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers);
248
+ if (args.orders.length > 12) {
249
+ throw new Error('Exceeded orders limit of 12 for perp quotes replace instruction');
250
+ }
251
+ let mask = args.orders.length & 0b1111;
252
+ for (let i = 0; i < args.orders.length; i++) {
253
+ if (args.orders[i].side === 0) {
254
+ mask |= 1 << (4 + i);
255
+ }
256
+ }
257
+ let headerBuf = (0, instruction_models_1.perpQuotesReplaceData)(42, mask, args.instrId);
258
+ let ordersBuf = Buffer.alloc(args.orders.length * structure_models_1.QuoteOrderModel.LENGTH);
259
+ for (let i = 0; i < args.orders.length; i++) {
260
+ const offset = i * structure_models_1.QuoteOrderModel.LENGTH;
261
+ ordersBuf.writeBigInt64LE(BigInt(Math.round(args.orders[i].newPrice * constants_1.DF)), offset + structure_models_1.QuoteOrderModel.OFFSET_NEW_PRICE);
262
+ ordersBuf.writeBigInt64LE(BigInt(Math.round(args.orders[i].newQty * assetTokenDecFactor)), offset + structure_models_1.QuoteOrderModel.OFFSET_NEW_QTY);
263
+ ordersBuf.writeBigInt64LE(BigInt(Math.floor(args.orders[i].oldId)), offset + structure_models_1.QuoteOrderModel.OFFSET_OLD_ID);
264
+ }
265
+ let buf = Buffer.concat([headerBuf, ordersBuf]);
266
+ let keys = [
267
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
268
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
269
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
270
+ { address: ctx.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE },
271
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
272
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
273
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
274
+ ];
275
+ if (ctx.refClientPrimaryAccount != null) {
276
+ keys.push({ address: ctx.refClientPrimaryAccount, role: kit_1.AccountRole.WRITABLE });
277
+ }
278
+ if (ctx.refClientCommunityAccount != null) {
279
+ keys.push({ address: ctx.refClientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
280
+ }
281
+ return { accounts: keys, programAddress: ctx.programId, data: buf };
282
+ });
283
+ }
284
+ /**
285
+ * Build perp order cancel instruction
286
+ */
287
+ function buildPerpOrderCancelInstruction(ctx, args, instr) {
288
+ return __awaiter(this, void 0, void 0, function* () {
289
+ let keys = [
290
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
291
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
292
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
293
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
294
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
295
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
296
+ ];
297
+ return {
298
+ accounts: keys,
299
+ programAddress: ctx.programId,
300
+ data: (0, instruction_models_1.perpOrderCancelData)(30, args.side, args.instrId, args.orderId),
301
+ };
302
+ });
303
+ }
304
+ /**
305
+ * Build perp mass cancel instruction
306
+ */
307
+ function buildPerpMassCancelInstruction(ctx, args, instr) {
308
+ return __awaiter(this, void 0, void 0, function* () {
309
+ let keys = [
310
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
311
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
312
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
313
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
314
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
315
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
316
+ ];
317
+ return {
318
+ accounts: keys,
319
+ programAddress: ctx.programId,
320
+ data: (0, instruction_models_1.perpMassCancelData)(36, args.instrId),
321
+ };
322
+ });
323
+ }
324
+ /**
325
+ * Build perp change leverage instruction
326
+ */
327
+ function buildPerpChangeLeverageInstruction(ctx, args, instr) {
328
+ return __awaiter(this, void 0, void 0, function* () {
329
+ let keys = [
330
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
331
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
332
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
333
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
334
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
335
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
336
+ ];
337
+ return {
338
+ accounts: keys,
339
+ programAddress: ctx.programId,
340
+ data: (0, instruction_models_1.perpChangeLeverageData)(37, args.leverage, args.instrId),
341
+ };
342
+ });
343
+ }
344
+ /**
345
+ * Build perp statistics reset instruction
346
+ */
347
+ function buildPerpStatisticsResetInstruction(ctx, args, instr) {
348
+ return __awaiter(this, void 0, void 0, function* () {
349
+ let keys = [
350
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
351
+ { address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
352
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
353
+ ...(yield (0, context_builders_1.getPerpContext)(ctx, instr.header)),
354
+ { address: yield (0, account_helpers_1.getAccountByTag)(ctx, enums_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
355
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
356
+ ];
357
+ return {
358
+ accounts: keys,
359
+ programAddress: ctx.programId,
360
+ data: (0, instruction_models_1.perpStatisticsResetData)(46, args.instrId),
361
+ };
362
+ });
363
+ }
364
+ /**
365
+ * Build new ref link instruction
366
+ */
367
+ function buildNewRefLinkInstruction(ctx) {
368
+ return __awaiter(this, void 0, void 0, function* () {
369
+ let buf = Buffer.alloc(1);
370
+ buf.writeUInt8(45, 0);
371
+ let keys = [
372
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
373
+ { address: ctx.rootAccount, role: kit_1.AccountRole.WRITABLE },
374
+ { address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
375
+ ];
376
+ return { accounts: keys, programAddress: ctx.programId, data: buf };
377
+ });
378
+ }
379
+ /**
380
+ * Build new instrument instructions
381
+ */
382
+ function buildNewInstrumentInstructions(ctx, args, rpcGetSlot, rpcGetAccountInfo, rpcGetMinBalance) {
383
+ return __awaiter(this, void 0, void 0, function* () {
384
+ if (args.initialPrice <= 0) {
385
+ throw new Error('Invalid initial price');
386
+ }
387
+ const assetInfo = yield rpcGetAccountInfo(args.assetMint);
388
+ if (!assetInfo.value) {
389
+ throw new Error('Asset mint not found');
390
+ }
391
+ const tokenProgramId = assetInfo.value.owner == constants_1.TOKEN_2022_PROGRAM_ID ? constants_1.TOKEN_2022_PROGRAM_ID : constants_1.TOKEN_PROGRAM_ID;
392
+ const crncyTokenId = yield (0, account_helpers_1.getTokenId)(ctx, args.crncyMint);
393
+ const id = yield (0, account_helpers_1.getTokenId)(ctx, args.assetMint);
394
+ const newAssetToken = id == null;
395
+ const assetTokenId = newAssetToken ? ctx.rootStateModel.tokensCount : id;
396
+ if (!crncyTokenId) {
397
+ throw new Error('Currency mint not found');
398
+ }
399
+ const mapsAccountSeed = ctx.version.toString() +
400
+ '_' +
401
+ enums_1.AccountType.SPOT_MAPS.toString() +
402
+ '_' +
403
+ assetTokenId.toString() +
404
+ '_' +
405
+ crncyTokenId.toString();
406
+ const mapsAccount = yield (0, kit_1.createAddressWithSeed)({
407
+ baseAddress: ctx.signer,
408
+ programAddress: ctx.programId,
409
+ seed: mapsAccountSeed,
410
+ });
411
+ const mapsAccountSize = 42184;
412
+ const mapsAccountLamports = yield rpcGetMinBalance(BigInt(mapsAccountSize));
413
+ const createMapsAccountIx = (0, system_1.getCreateAccountWithSeedInstruction)({
414
+ payer: ctx.signer,
415
+ baseAccount: ctx.signer,
416
+ base: ctx.signer,
417
+ newAccount: mapsAccount,
418
+ seed: mapsAccountSeed,
419
+ space: mapsAccountSize,
420
+ programAddress: ctx.programId,
421
+ amount: mapsAccountLamports,
422
+ });
423
+ const slot = Number(yield rpcGetSlot()) - 1;
424
+ const lutAddress = yield (0, utils_1.getLookupTableAddress)(ctx.drvsAuthority, slot);
425
+ const tokenArgs = { assetTokenId: assetTokenId, crncyTokenId: crncyTokenId };
426
+ let keys = [
427
+ { address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
428
+ { address: ctx.rootAccount, role: kit_1.AccountRole.WRITABLE },
429
+ {
430
+ address: yield (0, account_helpers_1.getTokenAccount)(ctx, args.assetMint),
431
+ role: newAssetToken ? kit_1.AccountRole.WRITABLE : kit_1.AccountRole.READONLY,
432
+ },
433
+ { address: yield (0, account_helpers_1.getTokenAccount)(ctx, args.crncyMint), role: kit_1.AccountRole.READONLY },
434
+ {
435
+ address: newAssetToken ? args.newProgramAccountAddress : ctx.tokens.get(assetTokenId).programAddress,
436
+ role: newAssetToken ? kit_1.AccountRole.WRITABLE_SIGNER : kit_1.AccountRole.READONLY,
437
+ },
438
+ { address: args.assetMint, role: kit_1.AccountRole.READONLY },
439
+ { address: lutAddress, role: kit_1.AccountRole.WRITABLE },
440
+ { address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
441
+ { address: tokenProgramId, role: kit_1.AccountRole.READONLY },
442
+ { address: constants_1.ADDRESS_LOOKUP_TABLE_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
443
+ { address: ctx.drvsAuthority, role: kit_1.AccountRole.READONLY },
444
+ {
445
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.INSTR })),
446
+ role: kit_1.AccountRole.WRITABLE,
447
+ },
448
+ {
449
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_BIDS_TREE })),
450
+ role: kit_1.AccountRole.WRITABLE,
451
+ },
452
+ {
453
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_ASKS_TREE })),
454
+ role: kit_1.AccountRole.WRITABLE,
455
+ },
456
+ {
457
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_BID_ORDERS })),
458
+ role: kit_1.AccountRole.WRITABLE,
459
+ },
460
+ {
461
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_ASK_ORDERS })),
462
+ role: kit_1.AccountRole.WRITABLE,
463
+ },
464
+ {
465
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_LINES })),
466
+ role: kit_1.AccountRole.WRITABLE,
467
+ },
468
+ { address: mapsAccount, role: kit_1.AccountRole.WRITABLE },
469
+ {
470
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_CLIENT_INFOS })),
471
+ role: kit_1.AccountRole.WRITABLE,
472
+ },
473
+ {
474
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_CLIENT_INFOS2 })),
475
+ role: kit_1.AccountRole.WRITABLE,
476
+ },
477
+ {
478
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_1M_CANDLES })),
479
+ role: kit_1.AccountRole.WRITABLE,
480
+ },
481
+ {
482
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_15M_CANDLES })),
483
+ role: kit_1.AccountRole.WRITABLE,
484
+ },
485
+ {
486
+ address: yield (0, account_helpers_1.getInstrAccountByTag)(ctx, Object.assign(Object.assign({}, tokenArgs), { tag: enums_1.AccountType.SPOT_DAY_CANDLES })),
487
+ role: kit_1.AccountRole.WRITABLE,
488
+ },
489
+ ];
490
+ const newInstrIx = {
491
+ accounts: keys,
492
+ programAddress: ctx.programId,
493
+ data: (0, instruction_models_1.newInstrumentData)(9, crncyTokenId, slot, args.initialPrice * constants_1.DF),
494
+ };
495
+ return [createMapsAccountIx, newInstrIx];
496
+ });
497
+ }
@@ -0,0 +1 @@
1
+ export {};