@deriverse/kit 1.0.41 → 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.
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +3 -1
- package/dist/engine/index.test.js +34 -12
- package/dist/engine/logs-decoder.js +111 -0
- package/dist/engine/perp-instructions.js +24 -5
- package/dist/engine/perp-instructions.test.js +8 -12
- package/dist/engine/spot-instructions.js +28 -5
- package/dist/engine/utils.js +6 -3
- package/dist/instruction_models.d.ts +10 -7
- package/dist/instruction_models.js +49 -29
- package/dist/logs_models.d.ts +147 -3
- package/dist/logs_models.js +243 -13
- package/dist/structure_models.d.ts +104 -99
- package/dist/structure_models.js +152 -129
- package/dist/types/log-message.d.ts +2 -2
- package/dist/types/schemas.d.ts +15 -12
- package/dist/types/schemas.js +19 -34
- package/dist/types/schemas.test.js +11 -12
- package/package.json +1 -1
package/dist/constants.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export declare const SYSTEM_PROGRAM_ID: import("@solana/kit").Address<"111111111
|
|
|
6
6
|
export declare const TOKEN_PROGRAM_ID: import("@solana/kit").Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
|
|
7
7
|
export declare const TOKEN_2022_PROGRAM_ID: import("@solana/kit").Address<"TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb">;
|
|
8
8
|
export declare const ASSOCIATED_TOKEN_PROGRAM_ID: import("@solana/kit").Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">;
|
|
9
|
+
export declare const MAX_SWAP_FEE_RATE = 0.0002;
|
|
10
|
+
export declare const DF = 1000000000;
|
|
9
11
|
export declare let dec: number;
|
|
10
12
|
export declare let lpDec: number;
|
|
11
13
|
export declare const nullOrder = 65535;
|
package/dist/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.nullOrder = exports.lpDec = exports.dec = exports.ASSOCIATED_TOKEN_PROGRAM_ID = exports.TOKEN_2022_PROGRAM_ID = exports.TOKEN_PROGRAM_ID = exports.SYSTEM_PROGRAM_ID = exports.ADDRESS_LOOKUP_TABLE_PROGRAM_ID = exports.MARKET_DEPTH = exports.PROGRAM_ID = exports.VERSION = void 0;
|
|
3
|
+
exports.nullOrder = exports.lpDec = exports.dec = exports.DF = exports.MAX_SWAP_FEE_RATE = exports.ASSOCIATED_TOKEN_PROGRAM_ID = exports.TOKEN_2022_PROGRAM_ID = exports.TOKEN_PROGRAM_ID = exports.SYSTEM_PROGRAM_ID = exports.ADDRESS_LOOKUP_TABLE_PROGRAM_ID = exports.MARKET_DEPTH = exports.PROGRAM_ID = exports.VERSION = void 0;
|
|
4
4
|
exports.setDecimals = setDecimals;
|
|
5
5
|
const kit_1 = require("@solana/kit");
|
|
6
6
|
exports.VERSION = 1;
|
|
@@ -11,6 +11,8 @@ exports.SYSTEM_PROGRAM_ID = (0, kit_1.address)('11111111111111111111111111111111
|
|
|
11
11
|
exports.TOKEN_PROGRAM_ID = (0, kit_1.address)('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
|
|
12
12
|
exports.TOKEN_2022_PROGRAM_ID = (0, kit_1.address)('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
|
|
13
13
|
exports.ASSOCIATED_TOKEN_PROGRAM_ID = (0, kit_1.address)('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
|
|
14
|
+
exports.MAX_SWAP_FEE_RATE = 0.0002;
|
|
15
|
+
exports.DF = 1000000000;
|
|
14
16
|
exports.dec = 1000000000;
|
|
15
17
|
exports.lpDec = 10000;
|
|
16
18
|
exports.nullOrder = 0xffff;
|
|
@@ -407,12 +407,10 @@ function setupEngineWithClient() {
|
|
|
407
407
|
const { engine } = yield setupEngineWithClient();
|
|
408
408
|
yield engine.spotQuotesReplaceInstruction({
|
|
409
409
|
instrId: 1,
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
newAskQty: 10,
|
|
415
|
-
askOrderIdToCancel: 2,
|
|
410
|
+
orders: [
|
|
411
|
+
{ newPrice: 99, newQty: 10, oldId: 1, side: 0 },
|
|
412
|
+
{ newPrice: 101, newQty: 10, oldId: 2, side: 1 },
|
|
413
|
+
],
|
|
416
414
|
});
|
|
417
415
|
(0, vitest_1.expect)(spot_instructions_1.buildSpotQuotesReplaceInstruction).toHaveBeenCalled();
|
|
418
416
|
}));
|
|
@@ -440,9 +438,35 @@ function setupEngineWithClient() {
|
|
|
440
438
|
amount: 100,
|
|
441
439
|
limitPrice: 100,
|
|
442
440
|
crncyInput: true,
|
|
441
|
+
refFeeRate: 0,
|
|
442
|
+
minAmountOut: 0,
|
|
443
443
|
});
|
|
444
444
|
(0, vitest_1.expect)(spot_instructions_1.buildSwapInstruction).toHaveBeenCalled();
|
|
445
445
|
}));
|
|
446
|
+
(0, vitest_1.it)('should not call buildSwapInstruction because refFeeRate out of maximum', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
447
|
+
const { engine } = yield setupEngineWithClient();
|
|
448
|
+
yield (0, vitest_1.expect)(engine.swapInstruction({
|
|
449
|
+
assetMint: 'AssetMint1111111111111111111111111111',
|
|
450
|
+
crncyMint: 'CrncyMint1111111111111111111111111111',
|
|
451
|
+
amount: 100,
|
|
452
|
+
limitPrice: 100,
|
|
453
|
+
crncyInput: true,
|
|
454
|
+
refFeeRate: 0.0003,
|
|
455
|
+
minAmountOut: 0,
|
|
456
|
+
})).rejects.toThrow();
|
|
457
|
+
}));
|
|
458
|
+
(0, vitest_1.it)('should not call buildSwapInstruction because minAmountOut below 0', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
459
|
+
const { engine } = yield setupEngineWithClient();
|
|
460
|
+
yield (0, vitest_1.expect)(engine.swapInstruction({
|
|
461
|
+
assetMint: 'AssetMint1111111111111111111111111111',
|
|
462
|
+
crncyMint: 'CrncyMint1111111111111111111111111111',
|
|
463
|
+
amount: 100,
|
|
464
|
+
limitPrice: 100,
|
|
465
|
+
crncyInput: true,
|
|
466
|
+
refFeeRate: 0.0002,
|
|
467
|
+
minAmountOut: -0.005,
|
|
468
|
+
})).rejects.toThrow();
|
|
469
|
+
}));
|
|
446
470
|
});
|
|
447
471
|
// Perp instructions
|
|
448
472
|
(0, vitest_1.describe)('upgradeToPerpInstructions', () => {
|
|
@@ -501,12 +525,10 @@ function setupEngineWithClient() {
|
|
|
501
525
|
const { engine } = yield setupEngineWithClient();
|
|
502
526
|
yield engine.perpQuotesReplaceInstruction({
|
|
503
527
|
instrId: 1,
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
newAskQty: 10,
|
|
509
|
-
askOrderIdToCancel: 2,
|
|
528
|
+
orders: [
|
|
529
|
+
{ newPrice: 99, newQty: 10, oldId: 1, side: 0 },
|
|
530
|
+
{ newPrice: 101, newQty: 10, oldId: 2, side: 1 },
|
|
531
|
+
],
|
|
510
532
|
});
|
|
511
533
|
(0, vitest_1.expect)(perp_instructions_1.buildPerpQuotesReplaceInstruction).toHaveBeenCalled();
|
|
512
534
|
}));
|
|
@@ -408,6 +408,117 @@ function decodeTransactionLogs(data, ctx) {
|
|
|
408
408
|
}
|
|
409
409
|
break;
|
|
410
410
|
}
|
|
411
|
+
case logs_models_1.LogType.swapOrder: {
|
|
412
|
+
if (buffer.length == logs_models_1.PlaceSwapOrderReportModel.LENGTH) {
|
|
413
|
+
let report = logs_models_1.PlaceSwapOrderReportModel.fromBuffer(buffer);
|
|
414
|
+
if (uiNumbers) {
|
|
415
|
+
const instrInfo = instruments.get(report.instrId);
|
|
416
|
+
if (instrInfo) {
|
|
417
|
+
assetTokenDec = (0, utils_1.tokenDec)(tokens, instrInfo.header.assetTokenId, uiNumbers);
|
|
418
|
+
crncyTokenDec = (0, utils_1.tokenDec)(tokens, instrInfo.header.crncyTokenId, uiNumbers);
|
|
419
|
+
report.qty /= assetTokenDec;
|
|
420
|
+
}
|
|
421
|
+
report.price /= constants_1.dec;
|
|
422
|
+
}
|
|
423
|
+
logs.push(report);
|
|
424
|
+
}
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
case logs_models_1.LogType.changedPoints: {
|
|
428
|
+
if (buffer.length == logs_models_1.ChangePointsRecordModel.LENGTH) {
|
|
429
|
+
let report = logs_models_1.ChangePointsRecordModel.fromBuffer(buffer);
|
|
430
|
+
logs.push(report);
|
|
431
|
+
}
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
case logs_models_1.LogType.swapFees: {
|
|
435
|
+
if (buffer.length == logs_models_1.SwapRefFeesReportModel.LENGTH) {
|
|
436
|
+
let report = logs_models_1.SwapRefFeesReportModel.fromBuffer(buffer);
|
|
437
|
+
if (uiNumbers) {
|
|
438
|
+
report.fees /= crncyTokenDec;
|
|
439
|
+
}
|
|
440
|
+
logs.push(report);
|
|
441
|
+
}
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
case logs_models_1.LogType.vmInitActivate: {
|
|
445
|
+
if (buffer.length == logs_models_1.VmInitActivateReportModel.LENGTH) {
|
|
446
|
+
let report = logs_models_1.VmInitActivateReportModel.fromBuffer(buffer);
|
|
447
|
+
logs.push(report);
|
|
448
|
+
}
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
case logs_models_1.LogType.vmInitActivateCancel: {
|
|
452
|
+
if (buffer.length == logs_models_1.VmInitActivateCancelReportModel.LENGTH) {
|
|
453
|
+
let report = logs_models_1.VmInitActivateCancelReportModel.fromBuffer(buffer);
|
|
454
|
+
logs.push(report);
|
|
455
|
+
}
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case logs_models_1.LogType.vmFinalizeActivate: {
|
|
459
|
+
if (buffer.length == logs_models_1.VmFinalizeActivateReportModel.LENGTH) {
|
|
460
|
+
let report = logs_models_1.VmFinalizeActivateReportModel.fromBuffer(buffer);
|
|
461
|
+
logs.push(report);
|
|
462
|
+
}
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
case logs_models_1.LogType.vmInitDeactivate: {
|
|
466
|
+
if (buffer.length == logs_models_1.VmInitDeactivateReportModel.LENGTH) {
|
|
467
|
+
let report = logs_models_1.VmInitDeactivateReportModel.fromBuffer(buffer);
|
|
468
|
+
logs.push(report);
|
|
469
|
+
}
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
case logs_models_1.LogType.vmInitDeactivateCancel: {
|
|
473
|
+
if (buffer.length == logs_models_1.VmInitDeactivateCancelReportModel.LENGTH) {
|
|
474
|
+
let report = logs_models_1.VmInitDeactivateCancelReportModel.fromBuffer(buffer);
|
|
475
|
+
logs.push(report);
|
|
476
|
+
}
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
case logs_models_1.LogType.vmFinalizeDeactivate: {
|
|
480
|
+
if (buffer.length == logs_models_1.VmFinalizeDeactivateReportModel.LENGTH) {
|
|
481
|
+
let report = logs_models_1.VmFinalizeDeactivateReportModel.fromBuffer(buffer);
|
|
482
|
+
logs.push(report);
|
|
483
|
+
}
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
case logs_models_1.LogType.vmChangeList: {
|
|
487
|
+
if (buffer.length == logs_models_1.VmChangeListReportModel.LENGTH) {
|
|
488
|
+
let report = logs_models_1.VmChangeListReportModel.fromBuffer(buffer);
|
|
489
|
+
logs.push(report);
|
|
490
|
+
}
|
|
491
|
+
break;
|
|
492
|
+
}
|
|
493
|
+
case logs_models_1.LogType.vmInitWithdraw: {
|
|
494
|
+
if (buffer.length == logs_models_1.VmInitWithdrawReportModel.LENGTH) {
|
|
495
|
+
let report = logs_models_1.VmInitWithdrawReportModel.fromBuffer(buffer);
|
|
496
|
+
if (uiNumbers) {
|
|
497
|
+
crncyTokenDec = (0, utils_1.tokenDec)(tokens, report.tokenId, uiNumbers);
|
|
498
|
+
report.amount /= crncyTokenDec;
|
|
499
|
+
}
|
|
500
|
+
logs.push(report);
|
|
501
|
+
}
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
case logs_models_1.LogType.vmInitWithdrawCancel: {
|
|
505
|
+
if (buffer.length == logs_models_1.VmInitWithdrawCancelReportModel.LENGTH) {
|
|
506
|
+
let report = logs_models_1.VmInitWithdrawCancelReportModel.fromBuffer(buffer);
|
|
507
|
+
logs.push(report);
|
|
508
|
+
}
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
case logs_models_1.LogType.vmInitWithdrawFinalize: {
|
|
512
|
+
if (buffer.length == logs_models_1.VmInitWithdrawFinalizeReportModel.LENGTH) {
|
|
513
|
+
let report = logs_models_1.VmInitWithdrawFinalizeReportModel.fromBuffer(buffer);
|
|
514
|
+
if (uiNumbers) {
|
|
515
|
+
crncyTokenDec = (0, utils_1.tokenDec)(tokens, report.tokenId, uiNumbers);
|
|
516
|
+
report.amount /= crncyTokenDec;
|
|
517
|
+
}
|
|
518
|
+
logs.push(report);
|
|
519
|
+
}
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
411
522
|
}
|
|
412
523
|
}
|
|
413
524
|
return logs;
|
|
@@ -26,6 +26,7 @@ const system_1 = require("@solana-program/system");
|
|
|
26
26
|
const enums_1 = require("../types/enums");
|
|
27
27
|
const constants_1 = require("../constants");
|
|
28
28
|
const utils_1 = require("./utils");
|
|
29
|
+
const structure_models_1 = require("../structure_models");
|
|
29
30
|
const instruction_models_1 = require("../instruction_models");
|
|
30
31
|
const account_helpers_1 = require("./account-helpers");
|
|
31
32
|
const context_builders_1 = require("./context-builders");
|
|
@@ -171,6 +172,7 @@ function buildPerpBuySeatInstruction(ctx, args, instr) {
|
|
|
171
172
|
{ address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
|
|
172
173
|
{ address: ctx.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
|
|
173
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 },
|
|
174
176
|
{ address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
|
|
175
177
|
];
|
|
176
178
|
const slippage = (_a = args.slippage) !== null && _a !== void 0 ? _a : 0;
|
|
@@ -199,7 +201,7 @@ function buildPerpSellSeatInstruction(ctx, args, instr) {
|
|
|
199
201
|
{ address: constants_1.SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
|
|
200
202
|
];
|
|
201
203
|
const slippage = (_a = args.slippage) !== null && _a !== void 0 ? _a : 0;
|
|
202
|
-
const slippagePrice = ((0, utils_1.perpSeatReserve)(instr.header.perpClientsCount
|
|
204
|
+
const slippagePrice = ((0, utils_1.perpSeatReserve)(instr.header.perpClientsCount) - (0, utils_1.perpSeatReserve)(instr.header.perpClientsCount - 1)) /
|
|
203
205
|
(1 + slippage);
|
|
204
206
|
const crncyDec = (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers);
|
|
205
207
|
return {
|
|
@@ -233,7 +235,7 @@ function buildNewPerpOrderInstruction(ctx, args, instr) {
|
|
|
233
235
|
return {
|
|
234
236
|
accounts: keys,
|
|
235
237
|
programAddress: ctx.programId,
|
|
236
|
-
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 *
|
|
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),
|
|
237
239
|
};
|
|
238
240
|
});
|
|
239
241
|
}
|
|
@@ -243,7 +245,24 @@ function buildNewPerpOrderInstruction(ctx, args, instr) {
|
|
|
243
245
|
function buildPerpQuotesReplaceInstruction(ctx, args, instr) {
|
|
244
246
|
return __awaiter(this, void 0, void 0, function* () {
|
|
245
247
|
let assetTokenDecFactor = (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers);
|
|
246
|
-
|
|
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]);
|
|
247
266
|
let keys = [
|
|
248
267
|
{ address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
|
|
249
268
|
{ address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
|
|
@@ -318,7 +337,7 @@ function buildPerpChangeLeverageInstruction(ctx, args, instr) {
|
|
|
318
337
|
return {
|
|
319
338
|
accounts: keys,
|
|
320
339
|
programAddress: ctx.programId,
|
|
321
|
-
data: (0, instruction_models_1.perpChangeLeverageData)(37, args.
|
|
340
|
+
data: (0, instruction_models_1.perpChangeLeverageData)(37, args.leverage, args.instrId),
|
|
322
341
|
};
|
|
323
342
|
});
|
|
324
343
|
}
|
|
@@ -471,7 +490,7 @@ function buildNewInstrumentInstructions(ctx, args, rpcGetSlot, rpcGetAccountInfo
|
|
|
471
490
|
const newInstrIx = {
|
|
472
491
|
accounts: keys,
|
|
473
492
|
programAddress: ctx.programId,
|
|
474
|
-
data: (0, instruction_models_1.newInstrumentData)(9, crncyTokenId, slot, args.initialPrice *
|
|
493
|
+
data: (0, instruction_models_1.newInstrumentData)(9, crncyTokenId, slot, args.initialPrice * constants_1.DF),
|
|
475
494
|
};
|
|
476
495
|
return [createMapsAccountIx, newInstrIx];
|
|
477
496
|
});
|
|
@@ -251,12 +251,10 @@ function createMockPerpContext(overrides = {}) {
|
|
|
251
251
|
const instr = ctx.instruments.get(1);
|
|
252
252
|
const args = {
|
|
253
253
|
instrId: 1,
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
newAskQty: 10,
|
|
259
|
-
askOrderIdToCancel: 67890,
|
|
254
|
+
orders: [
|
|
255
|
+
{ newPrice: 99, newQty: 10, oldId: 12345, side: 0 },
|
|
256
|
+
{ newPrice: 101, newQty: 10, oldId: 67890, side: 1 },
|
|
257
|
+
],
|
|
260
258
|
};
|
|
261
259
|
const instruction = yield (0, perp_instructions_1.buildPerpQuotesReplaceInstruction)(ctx, args, instr);
|
|
262
260
|
(0, vitest_1.expect)(instruction).toBeDefined();
|
|
@@ -271,12 +269,10 @@ function createMockPerpContext(overrides = {}) {
|
|
|
271
269
|
const instr = ctx.instruments.get(1);
|
|
272
270
|
const args = {
|
|
273
271
|
instrId: 1,
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
newAskQty: 10,
|
|
279
|
-
askOrderIdToCancel: 67890,
|
|
272
|
+
orders: [
|
|
273
|
+
{ newPrice: 99, newQty: 10, oldId: 12345, side: 0 },
|
|
274
|
+
{ newPrice: 101, newQty: 10, oldId: 67890, side: 1 },
|
|
275
|
+
],
|
|
280
276
|
};
|
|
281
277
|
const instruction = yield (0, perp_instructions_1.buildPerpQuotesReplaceInstruction)(ctx, args, instr);
|
|
282
278
|
(0, vitest_1.expect)(instruction.accounts.length).toBeGreaterThan(18);
|
|
@@ -21,6 +21,7 @@ const kit_1 = require("@solana/kit");
|
|
|
21
21
|
const enums_1 = require("../types/enums");
|
|
22
22
|
const constants_1 = require("../constants");
|
|
23
23
|
const utils_1 = require("./utils");
|
|
24
|
+
const structure_models_1 = require("../structure_models");
|
|
24
25
|
const instruction_models_1 = require("../instruction_models");
|
|
25
26
|
const account_helpers_1 = require("./account-helpers");
|
|
26
27
|
const context_builders_1 = require("./context-builders");
|
|
@@ -199,7 +200,7 @@ function buildSpotLpInstruction(ctx, args, instr) {
|
|
|
199
200
|
return {
|
|
200
201
|
accounts: keys,
|
|
201
202
|
programAddress: ctx.programId,
|
|
202
|
-
data: (0, instruction_models_1.spotLpData)(14, args.side, args.instrId, Math.round(args.amount * constants_1.lpDec), minPrice *
|
|
203
|
+
data: (0, instruction_models_1.spotLpData)(14, args.side, args.instrId, Math.round(args.amount * constants_1.lpDec), minPrice * constants_1.DF, maxPrice * constants_1.DF),
|
|
203
204
|
};
|
|
204
205
|
});
|
|
205
206
|
}
|
|
@@ -209,7 +210,7 @@ function buildSpotLpInstruction(ctx, args, instr) {
|
|
|
209
210
|
function buildNewSpotOrderInstruction(ctx, args, instr) {
|
|
210
211
|
return __awaiter(this, void 0, void 0, function* () {
|
|
211
212
|
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 *
|
|
213
|
+
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 * constants_1.DF), Math.round(args.qty * (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers)), ((_c = args.edgePrice) !== null && _c !== void 0 ? _c : 0) * constants_1.DF);
|
|
213
214
|
let keys = [
|
|
214
215
|
{ address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
|
|
215
216
|
{ address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
|
|
@@ -238,7 +239,24 @@ function buildNewSpotOrderInstruction(ctx, args, instr) {
|
|
|
238
239
|
function buildSpotQuotesReplaceInstruction(ctx, args, instr) {
|
|
239
240
|
return __awaiter(this, void 0, void 0, function* () {
|
|
240
241
|
let assetTokenDecFactor = (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers);
|
|
241
|
-
|
|
242
|
+
if (args.orders.length > 12) {
|
|
243
|
+
throw new Error('Exceeded orders limit of 12 for spot quotes replace instruction');
|
|
244
|
+
}
|
|
245
|
+
let mask = args.orders.length & 0b1111;
|
|
246
|
+
for (let i = 0; i < args.orders.length; i++) {
|
|
247
|
+
if (args.orders[i].side === 0) {
|
|
248
|
+
mask |= 1 << (4 + i);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
let headerBuf = (0, instruction_models_1.spotQuotesReplaceData)(34, mask, args.instrId);
|
|
252
|
+
let ordersBuf = Buffer.alloc(args.orders.length * structure_models_1.QuoteOrderModel.LENGTH);
|
|
253
|
+
for (let i = 0; i < args.orders.length; i++) {
|
|
254
|
+
const offset = i * structure_models_1.QuoteOrderModel.LENGTH;
|
|
255
|
+
ordersBuf.writeBigInt64LE(BigInt(Math.round(args.orders[i].newPrice * constants_1.DF)), offset + structure_models_1.QuoteOrderModel.OFFSET_NEW_PRICE);
|
|
256
|
+
ordersBuf.writeBigInt64LE(BigInt(Math.round(args.orders[i].newQty * assetTokenDecFactor)), offset + structure_models_1.QuoteOrderModel.OFFSET_NEW_QTY);
|
|
257
|
+
ordersBuf.writeBigInt64LE(BigInt(Math.floor(args.orders[i].oldId)), offset + structure_models_1.QuoteOrderModel.OFFSET_OLD_ID);
|
|
258
|
+
}
|
|
259
|
+
let buf = Buffer.concat([headerBuf, ordersBuf]);
|
|
242
260
|
let keys = [
|
|
243
261
|
{ address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
|
|
244
262
|
{ address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
|
|
@@ -347,10 +365,10 @@ function buildSwapInstruction(ctx, args, instr) {
|
|
|
347
365
|
}
|
|
348
366
|
const clientAssetTokenAccount = yield (0, utils_1.findAssociatedTokenAddress)(ctx.signer, assetTokenProgramId, args.assetMint);
|
|
349
367
|
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 *
|
|
368
|
+
let buf = (0, instruction_models_1.swapData)(26, args.crncyInput ? 1 : 0, instrId, Math.round(args.limitPrice * constants_1.DF), Math.round(args.amount *
|
|
351
369
|
(args.crncyInput
|
|
352
370
|
? (0, utils_1.tokenDec)(ctx.tokens, instr.header.crncyTokenId, ctx.uiNumbers)
|
|
353
|
-
: (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers))));
|
|
371
|
+
: (0, utils_1.tokenDec)(ctx.tokens, instr.header.assetTokenId, ctx.uiNumbers))), args.refFeeRate, args.minAmountOut);
|
|
354
372
|
let keys = [
|
|
355
373
|
{ address: ctx.signer, role: kit_1.AccountRole.READONLY_SIGNER },
|
|
356
374
|
{ address: ctx.rootAccount, role: kit_1.AccountRole.READONLY },
|
|
@@ -371,6 +389,11 @@ function buildSwapInstruction(ctx, args, instr) {
|
|
|
371
389
|
{ address: crncyTokenProgramId, role: kit_1.AccountRole.READONLY },
|
|
372
390
|
{ address: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
|
|
373
391
|
];
|
|
392
|
+
if (args.feeTakerWallet && args.refFeeRate > 0) {
|
|
393
|
+
const feeTakerTokenAccount = yield (0, utils_1.findAssociatedTokenAddress)(args.feeTakerWallet, crncyTokenProgramId, args.crncyMint);
|
|
394
|
+
keys.push({ address: feeTakerTokenAccount, role: kit_1.AccountRole.WRITABLE });
|
|
395
|
+
keys.push({ address: args.feeTakerWallet, role: kit_1.AccountRole.WRITABLE });
|
|
396
|
+
}
|
|
374
397
|
return { accounts: keys, programAddress: ctx.programId, data: buf };
|
|
375
398
|
});
|
|
376
399
|
}
|
package/dist/engine/utils.js
CHANGED
|
@@ -269,11 +269,14 @@ function getPerpPriceStep(price) {
|
|
|
269
269
|
return 1000;
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
|
+
const MAX_SUPPLY = 262200;
|
|
273
|
+
const INIT_SEAT_PRICE = 1.0;
|
|
272
274
|
function perpSeatReserve(activeUsers) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
+
const differenceToMax = MAX_SUPPLY - activeUsers;
|
|
276
|
+
if (differenceToMax <= 0) {
|
|
277
|
+
throw new Error('Active users cannot exceed MAX_SUPPLY');
|
|
275
278
|
}
|
|
276
|
-
return (
|
|
279
|
+
return (MAX_SUPPLY * INIT_SEAT_PRICE * activeUsers) / differenceToMax;
|
|
277
280
|
}
|
|
278
281
|
function getMultipleSpotOrders(data, firstEntry, clientId) {
|
|
279
282
|
let orders = [];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export declare function newOperatorData(tag: number): Buffer;
|
|
2
|
-
export declare function newRootAccountData(tag: number, privateMode: number, lutSlot: number): Buffer;
|
|
1
|
+
export declare function newOperatorData(tag: number, version: number): Buffer;
|
|
2
|
+
export declare function newRootAccountData(tag: number, privateMode: number, version: number, lutSlot: number): Buffer;
|
|
3
3
|
export declare function newSpotOrderData(tag: number, ioc: number, orderType: number, side: number, instrId: number, price: number, amount: number, edgePrice: number): Buffer;
|
|
4
4
|
export declare function newPerpOrderData(tag: number, ioc: number, leverage: number, orderType: number, side: number, instrId: number, price: number, amount: number, edgePrice: number): Buffer;
|
|
5
5
|
export declare function perpChangeLeverageData(tag: number, leverage: number, instrId: number): Buffer;
|
|
@@ -15,13 +15,13 @@ export declare function perpDepositData(tag: number, instrId: number, amount: nu
|
|
|
15
15
|
export declare function moveSpotAvailFundsData(tag: number, instrId: number): Buffer;
|
|
16
16
|
export declare function perpWithdrawData(tag: number, instrId: number, amount: number): Buffer;
|
|
17
17
|
export declare function withdrawData(tag: number, tokenId: number, amount: number): Buffer;
|
|
18
|
-
export declare function swapData(tag: number, inputCrncy: number, instrId: number, price: number, amount: number): Buffer;
|
|
19
|
-
export declare function spotQuotesReplaceData(tag: number,
|
|
20
|
-
export declare function perpQuotesReplaceData(tag: number,
|
|
18
|
+
export declare function swapData(tag: number, inputCrncy: number, instrId: number, price: number, amount: number, refFeeRate: number, minAmountOut: number): Buffer;
|
|
19
|
+
export declare function spotQuotesReplaceData(tag: number, mask: number, instrId: number): Buffer;
|
|
20
|
+
export declare function perpQuotesReplaceData(tag: number, mask: number, instrId: number): Buffer;
|
|
21
21
|
export declare function votingData(tag: number, choice: number, votingCounter: number): Buffer;
|
|
22
22
|
export declare function airdropData(tag: number, ratio: number): Buffer;
|
|
23
23
|
export declare function upgradeToPerpData(tag: number, instrId: number): Buffer;
|
|
24
|
-
export declare function setInstrReadyForPerpUpgradeData(tag: number, instrId: number
|
|
24
|
+
export declare function setInstrReadyForPerpUpgradeData(tag: number, instrId: number): Buffer;
|
|
25
25
|
export declare function perpOrderCancelData(tag: number, side: number, instrId: number, orderId: number): Buffer;
|
|
26
26
|
export declare function perpMassCancelData(tag: number, instrId: number): Buffer;
|
|
27
27
|
export declare function changeRefProgramData(tag: number, refProgramDuration: number, refLinkDuration: number, refDiscount: number, refRatio: number): Buffer;
|
|
@@ -29,7 +29,7 @@ export declare function buyMarketSeatData(tag: number, instrId: number, edgePric
|
|
|
29
29
|
export declare function sellMarketSeatData(tag: number, instrId: number, edgePrice: number): Buffer;
|
|
30
30
|
export declare function newPrivateClient(tag: number, expirationTime: number): Buffer;
|
|
31
31
|
export declare function pointsProgramExpiration(tag: number, newExpirationTime: number): Buffer;
|
|
32
|
-
export declare function setVarianceData(tag: number, variance: number): Buffer;
|
|
32
|
+
export declare function setVarianceData(tag: number, instrId: number, variance: number): Buffer;
|
|
33
33
|
export declare function changeDenominatorData(tag: number, baseCrncyId: number, denominator: number): Buffer;
|
|
34
34
|
export declare function newBaseCrncyData(tag: number, denominator: number): Buffer;
|
|
35
35
|
export declare function perpClientsProcessingData(tag: number, instrId: number): Buffer;
|
|
@@ -37,3 +37,6 @@ export declare function setSeatPurchasingFeeData(tag: number, fee: number): Buff
|
|
|
37
37
|
export declare function changeVotingData(tag: number, newChoice: number, votingCounter: number): Buffer;
|
|
38
38
|
export declare function garbageCollectorData(tag: number, instrId: number): Buffer;
|
|
39
39
|
export declare function activateClientRefProgramData(tag: number, refId: number): Buffer;
|
|
40
|
+
export declare function cleanCandlesData(tag: number, instrId: number): Buffer;
|
|
41
|
+
export declare function vmInitWithdrawData(tag: number, tokenId: number, amount: number): Buffer;
|
|
42
|
+
export declare function vmChangeWhitelistData(tag: number, mask: number): Buffer;
|
|
@@ -39,19 +39,24 @@ exports.setSeatPurchasingFeeData = setSeatPurchasingFeeData;
|
|
|
39
39
|
exports.changeVotingData = changeVotingData;
|
|
40
40
|
exports.garbageCollectorData = garbageCollectorData;
|
|
41
41
|
exports.activateClientRefProgramData = activateClientRefProgramData;
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
exports.cleanCandlesData = cleanCandlesData;
|
|
43
|
+
exports.vmInitWithdrawData = vmInitWithdrawData;
|
|
44
|
+
exports.vmChangeWhitelistData = vmChangeWhitelistData;
|
|
45
|
+
function newOperatorData(tag, version) {
|
|
46
|
+
let buf = Buffer.alloc(8);
|
|
44
47
|
buf.writeUint8(tag, 0);
|
|
45
48
|
buf.writeUint8(0, 1);
|
|
46
49
|
buf.writeUint16LE(0, 2);
|
|
50
|
+
buf.writeUint32LE(version, 4);
|
|
47
51
|
return buf;
|
|
48
52
|
}
|
|
49
|
-
function newRootAccountData(tag, privateMode, lutSlot) {
|
|
50
|
-
let buf = Buffer.alloc(
|
|
53
|
+
function newRootAccountData(tag, privateMode, version, lutSlot) {
|
|
54
|
+
let buf = Buffer.alloc(12);
|
|
51
55
|
buf.writeUint8(tag, 0);
|
|
52
56
|
buf.writeUint8(privateMode, 1);
|
|
53
57
|
buf.writeUint16LE(0, 2);
|
|
54
|
-
buf.writeUint32LE(
|
|
58
|
+
buf.writeUint32LE(version, 4);
|
|
59
|
+
buf.writeUint32LE(lutSlot, 8);
|
|
55
60
|
return buf;
|
|
56
61
|
}
|
|
57
62
|
function newSpotOrderData(tag, ioc, orderType, side, instrId, price, amount, edgePrice) {
|
|
@@ -202,42 +207,31 @@ function withdrawData(tag, tokenId, amount) {
|
|
|
202
207
|
buf.writeBigInt64LE(BigInt(Math.floor(amount)), 8);
|
|
203
208
|
return buf;
|
|
204
209
|
}
|
|
205
|
-
function swapData(tag, inputCrncy, instrId, price, amount) {
|
|
206
|
-
let buf = Buffer.alloc(
|
|
210
|
+
function swapData(tag, inputCrncy, instrId, price, amount, refFeeRate, minAmountOut) {
|
|
211
|
+
let buf = Buffer.alloc(40);
|
|
207
212
|
buf.writeUint8(tag, 0);
|
|
208
213
|
buf.writeUint8(inputCrncy, 1);
|
|
209
214
|
buf.writeUint16LE(0, 2);
|
|
210
215
|
buf.writeUint32LE(instrId, 4);
|
|
211
216
|
buf.writeBigInt64LE(BigInt(Math.floor(price)), 8);
|
|
212
217
|
buf.writeBigInt64LE(BigInt(Math.floor(amount)), 16);
|
|
218
|
+
buf.writeBigInt64LE(BigInt(Math.floor(minAmountOut)), 24);
|
|
213
219
|
return buf;
|
|
214
220
|
}
|
|
215
|
-
function spotQuotesReplaceData(tag,
|
|
216
|
-
let buf = Buffer.alloc(
|
|
221
|
+
function spotQuotesReplaceData(tag, mask, instrId) {
|
|
222
|
+
let buf = Buffer.alloc(8);
|
|
217
223
|
buf.writeUint8(tag, 0);
|
|
218
224
|
buf.writeUint8(0, 1);
|
|
219
|
-
buf.writeUint16LE(
|
|
225
|
+
buf.writeUint16LE(mask, 2);
|
|
220
226
|
buf.writeUint32LE(instrId, 4);
|
|
221
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newBidPrice)), 8);
|
|
222
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newBidQty)), 16);
|
|
223
|
-
buf.writeBigInt64LE(BigInt(Math.floor(oldBidOrderId)), 24);
|
|
224
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newAskPrice)), 32);
|
|
225
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newAskQty)), 40);
|
|
226
|
-
buf.writeBigInt64LE(BigInt(Math.floor(oldAskOrderId)), 48);
|
|
227
227
|
return buf;
|
|
228
228
|
}
|
|
229
|
-
function perpQuotesReplaceData(tag,
|
|
230
|
-
let buf = Buffer.alloc(
|
|
229
|
+
function perpQuotesReplaceData(tag, mask, instrId) {
|
|
230
|
+
let buf = Buffer.alloc(8);
|
|
231
231
|
buf.writeUint8(tag, 0);
|
|
232
232
|
buf.writeUint8(0, 1);
|
|
233
|
-
buf.writeUint16LE(
|
|
233
|
+
buf.writeUint16LE(mask, 2);
|
|
234
234
|
buf.writeUint32LE(instrId, 4);
|
|
235
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newBidPrice)), 8);
|
|
236
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newBidQty)), 16);
|
|
237
|
-
buf.writeBigInt64LE(BigInt(Math.floor(oldBidOrderId)), 24);
|
|
238
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newAskPrice)), 32);
|
|
239
|
-
buf.writeBigInt64LE(BigInt(Math.floor(newAskQty)), 40);
|
|
240
|
-
buf.writeBigInt64LE(BigInt(Math.floor(oldAskOrderId)), 48);
|
|
241
235
|
return buf;
|
|
242
236
|
}
|
|
243
237
|
function votingData(tag, choice, votingCounter) {
|
|
@@ -264,8 +258,8 @@ function upgradeToPerpData(tag, instrId) {
|
|
|
264
258
|
buf.writeUint32LE(instrId, 4);
|
|
265
259
|
return buf;
|
|
266
260
|
}
|
|
267
|
-
function setInstrReadyForPerpUpgradeData(tag, instrId
|
|
268
|
-
let buf = Buffer.alloc(
|
|
261
|
+
function setInstrReadyForPerpUpgradeData(tag, instrId) {
|
|
262
|
+
let buf = Buffer.alloc(8);
|
|
269
263
|
buf.writeUint8(tag, 0);
|
|
270
264
|
buf.writeUint8(0, 1);
|
|
271
265
|
buf.writeUint16LE(0, 2);
|
|
@@ -334,11 +328,12 @@ function pointsProgramExpiration(tag, newExpirationTime) {
|
|
|
334
328
|
buf.writeUint32LE(newExpirationTime, 4);
|
|
335
329
|
return buf;
|
|
336
330
|
}
|
|
337
|
-
function setVarianceData(tag, variance) {
|
|
338
|
-
let buf = Buffer.alloc(
|
|
331
|
+
function setVarianceData(tag, instrId, variance) {
|
|
332
|
+
let buf = Buffer.alloc(16);
|
|
339
333
|
buf.writeUint8(tag, 0);
|
|
340
334
|
buf.writeUint8(0, 1);
|
|
341
335
|
buf.writeUint16LE(0, 2);
|
|
336
|
+
buf.writeUint32LE(instrId, 4);
|
|
342
337
|
return buf;
|
|
343
338
|
}
|
|
344
339
|
function changeDenominatorData(tag, baseCrncyId, denominator) {
|
|
@@ -397,3 +392,28 @@ function activateClientRefProgramData(tag, refId) {
|
|
|
397
392
|
buf.writeUint32LE(refId, 4);
|
|
398
393
|
return buf;
|
|
399
394
|
}
|
|
395
|
+
function cleanCandlesData(tag, instrId) {
|
|
396
|
+
let buf = Buffer.alloc(8);
|
|
397
|
+
buf.writeUint8(tag, 0);
|
|
398
|
+
buf.writeUint8(0, 1);
|
|
399
|
+
buf.writeUint16LE(0, 2);
|
|
400
|
+
buf.writeUint32LE(instrId, 4);
|
|
401
|
+
return buf;
|
|
402
|
+
}
|
|
403
|
+
function vmInitWithdrawData(tag, tokenId, amount) {
|
|
404
|
+
let buf = Buffer.alloc(16);
|
|
405
|
+
buf.writeUint8(tag, 0);
|
|
406
|
+
buf.writeUint8(0, 1);
|
|
407
|
+
buf.writeUint16LE(0, 2);
|
|
408
|
+
buf.writeUint32LE(tokenId, 4);
|
|
409
|
+
buf.writeBigInt64LE(BigInt(Math.floor(amount)), 8);
|
|
410
|
+
return buf;
|
|
411
|
+
}
|
|
412
|
+
function vmChangeWhitelistData(tag, mask) {
|
|
413
|
+
let buf = Buffer.alloc(8);
|
|
414
|
+
buf.writeUint8(tag, 0);
|
|
415
|
+
buf.writeUint8(0, 1);
|
|
416
|
+
buf.writeUint16LE(0, 2);
|
|
417
|
+
buf.writeUint32LE(mask, 4);
|
|
418
|
+
return buf;
|
|
419
|
+
}
|