@deriverse/kit 1.0.32 → 1.0.39

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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Address, Base64EncodedDataResponse, Rpc, IAccountMeta, IInstruction, Commitment } from "@solana/kit";
2
- import { Instrument, GetClientSpotOrdersInfoResponse, GetClientSpotOrdersArgs, GetClientSpotOrdersResponse, SpotOrderCancelArgs, GetClientDataResponse, GetClientSpotOrdersInfoArgs, SpotLpArgs, getInstrAccountByTagArgs, GetInstrIdArgs, NewSpotOrderArgs, DepositArgs, WithdrawArgs, SpotQuotesReplaceArgs, SpotMassCancelArgs, InstrId, GetClientPerpOrdersInfoArgs, GetClientPerpOrdersInfoResponse, GetClientPerpOrdersArgs, GetClientPerpOrdersResponse, PerpDepositArgs, NewPerpOrderArgs, PerpQuotesReplaceArgs, PerpOrderCancelArgs, PerpMassCancelArgs, PerpForcedCloseArgs, CommunityData, LogMessage, PerpChangeLeverageArgs, PerpStatisticsResetArgs, EngineArgs, NewInstrumentArgs, PerpBuySeatArgs } from './types';
1
+ import { Address, Base64EncodedDataResponse, Rpc, IAccountMeta, Commitment } from "@solana/kit";
2
+ import { Instrument, GetClientSpotOrdersInfoResponse, GetClientSpotOrdersArgs, GetClientSpotOrdersResponse, SpotOrderCancelArgs, GetClientDataResponse, GetClientSpotOrdersInfoArgs, SpotLpArgs, getInstrAccountByTagArgs, GetInstrIdArgs, NewSpotOrderArgs, DepositArgs, WithdrawArgs, SpotQuotesReplaceArgs, SpotMassCancelArgs, InstrId, GetClientPerpOrdersInfoArgs, GetClientPerpOrdersInfoResponse, GetClientPerpOrdersArgs, GetClientPerpOrdersResponse, PerpDepositArgs, NewPerpOrderArgs, PerpQuotesReplaceArgs, PerpOrderCancelArgs, PerpMassCancelArgs, CommunityData, LogMessage, PerpChangeLeverageArgs, PerpStatisticsResetArgs, EngineArgs, NewInstrumentArgs, PerpBuySeatArgs, SwapArgs, PerpSellSeatArgs } from './types';
3
3
  import { InstrAccountHeaderModel, RootStateModel, TokenStateModel } from "./structure_models";
4
4
  export * from './types';
5
5
  export * from './logs_models';
@@ -38,6 +38,7 @@ export declare class Engine {
38
38
  clientLutAddress?: Address<any>;
39
39
  private refClientPrimaryAccount?;
40
40
  private refClientCommunityAccount?;
41
+ privateMode?: boolean;
41
42
  tokens: Map<number, TokenStateModel>;
42
43
  instruments: Map<number, Instrument>;
43
44
  version: number;
@@ -191,7 +192,7 @@ export declare class Engine {
191
192
  * @returns Transaction instruction
192
193
  */
193
194
  perpBuySeatInstruction(args: PerpBuySeatArgs): Promise<any>;
194
- perpSellSeatInstruction(args: PerpBuySeatArgs): Promise<any>;
195
+ perpSellSeatInstruction(args: PerpSellSeatArgs): Promise<any>;
195
196
  /**
196
197
  * Build instruction for new perp order in particular instrument
197
198
  * @param args Order data
@@ -216,12 +217,6 @@ export declare class Engine {
216
217
  * @returns Transaction instruction
217
218
  */
218
219
  perpMassCancelInstruction(args: PerpMassCancelArgs): Promise<any>;
219
- /**
220
- * Build instruction for perp forced close in particular instrument
221
- * @param args Order data
222
- * @returns Transaction instruction
223
- */
224
- perpForcedCloseInstruction(args: PerpForcedCloseArgs): Promise<IInstruction>;
225
220
  /**
226
221
  * Build instruction for new referral link
227
222
  * @returns Transaction instruction
@@ -245,4 +240,10 @@ export declare class Engine {
245
240
  * @returns Transaction instruction
246
241
  */
247
242
  newInstrumentInstructions(args: NewInstrumentArgs): Promise<any[]>;
243
+ /**
244
+ * Build instruction for durect swap
245
+ * @param args Order data
246
+ * @returns Transaction instruction
247
+ */
248
+ swapInstruction(args: SwapArgs): Promise<any>;
248
249
  }
package/dist/index.js CHANGED
@@ -281,6 +281,9 @@ function getPerpPriceStep(price) {
281
281
  }
282
282
  }
283
283
  exports.getPerpPriceStep = getPerpPriceStep;
284
+ function perpSeatReserve(activeUsers) {
285
+ return 250000 * activeUsers / (25000 - activeUsers);
286
+ }
284
287
  function findAssociatedTokenAddress(owner, tokenProgramId, mint) {
285
288
  return __awaiter(this, void 0, void 0, function* () {
286
289
  const address = (yield (0, kit_1.getProgramDerivedAddress)({
@@ -815,6 +818,7 @@ class Engine {
815
818
  this.rootStateModel = structure_models_1.RootStateModel.fromBuffer(infos.value[0].data);
816
819
  this.tokens = new Map();
817
820
  this.instruments = new Map();
821
+ this.privateMode = (this.rootStateModel.mask & 1) != 0;
818
822
  const tokenAccounts = yield this.findAccountsByTag(types_1.AccountType.TOKEN);
819
823
  tokenAccounts.forEach((t) => {
820
824
  let tokenStateModel = structure_models_1.TokenStateModel.fromBuffer(t.account.data);
@@ -1018,16 +1022,6 @@ class Engine {
1018
1022
  }),
1019
1023
  role: kit_1.AccountRole.WRITABLE
1020
1024
  },
1021
- /*
1022
- {
1023
- address: await this.getInstrAccountByTag({
1024
- assetTokenId: instrAccountHeaderModel.assetTokenId,
1025
- crncyTokenId: instrAccountHeaderModel.crncyTokenId,
1026
- tag: AccountType.SPOT_CLIENT_ACCOUNTS
1027
- }),
1028
- role: AccountRole.WRITABLE
1029
- },
1030
- */
1031
1025
  ];
1032
1026
  });
1033
1027
  }
@@ -1126,16 +1120,6 @@ class Engine {
1126
1120
  }),
1127
1121
  role: kit_1.AccountRole.WRITABLE
1128
1122
  },
1129
- /*
1130
- {
1131
- address: await this.getInstrAccountByTag({
1132
- assetTokenId: instrAccountHeaderModel.assetTokenId,
1133
- crncyTokenId: instrAccountHeaderModel.crncyTokenId,
1134
- tag: AccountType.PERP_CLIENT_ACCOUNTS
1135
- }),
1136
- role: AccountRole.WRITABLE
1137
- },
1138
- */
1139
1123
  {
1140
1124
  address: yield this.getInstrAccountByTag({
1141
1125
  assetTokenId: instrAccountHeaderModel.assetTokenId,
@@ -1160,16 +1144,6 @@ class Engine {
1160
1144
  }),
1161
1145
  role: kit_1.AccountRole.WRITABLE
1162
1146
  },
1163
- /*
1164
- {
1165
- address: await this.getInstrAccountByTag({
1166
- assetTokenId: instrAccountHeaderModel.assetTokenId,
1167
- crncyTokenId: instrAccountHeaderModel.crncyTokenId,
1168
- tag: AccountType.PERP_PRIORITY_TREE
1169
- }),
1170
- role: AccountRole.WRITABLE
1171
- },
1172
- */
1173
1147
  ];
1174
1148
  });
1175
1149
  }
@@ -1389,8 +1363,8 @@ class Engine {
1389
1363
  for (var i = 0; i < clientPrimaryAccountHeaderModel.assetsCount; ++i) {
1390
1364
  const offset = structure_models_1.ClientPrimaryAccountHeaderModel.LENGTH + i * 16;
1391
1365
  const assetInfo = primaryData.readUint32LE(offset);
1392
- const tag = assetInfo >> 24;
1393
- const id = assetInfo & 0xFFFFFF;
1366
+ const tag = assetInfo >> 28;
1367
+ const id = assetInfo & 0xFFFFFFF;
1394
1368
  if (tag == 1) {
1395
1369
  tokens.set(id, {
1396
1370
  tokenId: id,
@@ -1862,8 +1836,8 @@ class Engine {
1862
1836
  header.dayCrncyTokens /= crncyTokenDec;
1863
1837
  header.prevDayAssetTokens /= assetTokenDec;
1864
1838
  header.prevDayCrncyTokens /= crncyTokenDec;
1865
- header.perpLastAssetTokens /= assetTokenDec;
1866
- header.perpLastCrncyTokens /= crncyTokenDec;
1839
+ header.perpLastTradeAssetTokens /= assetTokenDec;
1840
+ header.perpLastTradeCrncyTokens /= crncyTokenDec;
1867
1841
  header.perpDayAssetTokens /= assetTokenDec;
1868
1842
  header.perpDayCrncyTokens /= crncyTokenDec;
1869
1843
  header.fixingAssetTokens /= assetTokenDec;
@@ -1888,8 +1862,8 @@ class Engine {
1888
1862
  header.fixingPx /= dec;
1889
1863
  header.perpLastClose /= dec;
1890
1864
  header.perpLastPx /= dec;
1891
- //header.lastHourPx /= dec;
1892
- header.perpSpotPriceForWithdrowal /= dec;
1865
+ header.perpLongSpotPriceForWithdrowal /= dec;
1866
+ header.perpShortSpotPriceForWithdrowal /= dec;
1893
1867
  header.poolFees /= crncyTokenDec;
1894
1868
  let spotBids = [];
1895
1869
  let spotAsks = [];
@@ -2006,6 +1980,9 @@ class Engine {
2006
1980
  { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2007
1981
  { address: tokenProgramId, role: kit_1.AccountRole.READONLY },
2008
1982
  ];
1983
+ if (this.privateMode) {
1984
+ keys.push({ address: yield this.getAccountByTag(types_1.AccountType.PRIVATE_CLIENTS), role: kit_1.AccountRole.WRITABLE });
1985
+ }
2009
1986
  if (exists) {
2010
1987
  if (args.tokenId == 0) {
2011
1988
  keys.push({ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.WRITABLE });
@@ -2021,11 +1998,6 @@ class Engine {
2021
1998
  const slot = Number((yield this.rpc.getSlot().send())) - 1;
2022
1999
  const lutAddress = yield getLookupTableAddress(this.signer, slot);
2023
2000
  const clientCommunityAccount = yield this.findClientCommunityAccount();
2024
- /*
2025
- keys.push(
2026
- { address: await this.findClientDrvAccount(), role: AccountRole.WRITABLE }
2027
- );
2028
- */
2029
2001
  keys.push({ address: clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
2030
2002
  keys.push({ address: lutAddress, role: kit_1.AccountRole.WRITABLE });
2031
2003
  keys.push({ address: ADDRESS_LOOKUP_TABLE_PROGRAM_ID, role: kit_1.AccountRole.WRITABLE });
@@ -2080,7 +2052,6 @@ class Engine {
2080
2052
  { address: ASSOCIATED_TOKEN_PROGRAM_ID, role: kit_1.AccountRole.WRITABLE },
2081
2053
  ];
2082
2054
  if (args.spot != undefined) {
2083
- //keys.push({ address: await this.findClientDrvAccount(), role: AccountRole.READONLY });
2084
2055
  for (var i = 0; i < args.spot.length; ++i) {
2085
2056
  const instr = this.instruments.get(args.spot[i].instrId);
2086
2057
  if (instr.header.assetTokenId == args.tokenId || instr.header.crncyTokenId == args.tokenId) {
@@ -2097,8 +2068,8 @@ class Engine {
2097
2068
  }
2098
2069
  }
2099
2070
  if (args.tokenId == 0) {
2100
- keys.push({ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY });
2101
- keys.push({ address: yield this.clientCommunityAccount, role: kit_1.AccountRole.READONLY });
2071
+ keys.push({ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.WRITABLE });
2072
+ keys.push({ address: yield this.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
2102
2073
  }
2103
2074
  return {
2104
2075
  accounts: keys,
@@ -2117,7 +2088,8 @@ class Engine {
2117
2088
  if (!(yield this.checkClient())) {
2118
2089
  throw new Error("Client account not found");
2119
2090
  }
2120
- const instr = this.instruments.get(args.instrId);
2091
+ yield this.updateInstrData({ instrId: args.instrId });
2092
+ let instr = this.instruments.get(args.instrId);
2121
2093
  let keys = [
2122
2094
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2123
2095
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2136,10 +2108,12 @@ class Engine {
2136
2108
  keys.push({ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.WRITABLE });
2137
2109
  keys.push({ address: this.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
2138
2110
  }
2111
+ const minPrice = (args.minPrice == undefined || args.minPrice == null) ? 0 : args.minPrice;
2112
+ const maxPrice = (args.maxPrice == undefined || args.maxPrice == null) ? 0 : args.maxPrice;
2139
2113
  return {
2140
2114
  accounts: keys,
2141
2115
  programAddress: this.programId,
2142
- data: (0, instruction_models_1.spotLpData)(14, args.side, args.instrId, Math.round(args.amount * lpDec)),
2116
+ data: (0, instruction_models_1.spotLpData)(14, args.side, args.instrId, Math.round(args.amount * lpDec), minPrice * 1000000000, maxPrice * 1000000000),
2143
2117
  };
2144
2118
  });
2145
2119
  }
@@ -2154,7 +2128,11 @@ class Engine {
2154
2128
  throw new Error("Client account not found");
2155
2129
  }
2156
2130
  let instr = this.instruments.get(args.instrId);
2157
- let buf = (0, instruction_models_1.newSpotOrderData)(12, args.ioc == null || args.ioc == undefined ? 0 : args.ioc, args.orderType == null || args.orderType == undefined ? 0 : args.orderType, args.side, args.instrId, Math.round(args.price * 1000000000), Math.round(args.qty * this.tokenDec(instr.header.assetTokenId)));
2131
+ if (instr.header.mapsAddress == undefined) {
2132
+ yield this.updateInstrData({ instrId: args.instrId });
2133
+ instr = this.instruments.get(args.instrId);
2134
+ }
2135
+ let buf = (0, instruction_models_1.newSpotOrderData)(12, args.ioc == null || args.ioc == undefined ? 0 : args.ioc, args.orderType == null || args.orderType == undefined ? 0 : args.orderType, args.side, args.instrId, Math.round(args.price * 1000000000), Math.round(args.qty * this.tokenDec(instr.header.assetTokenId)), args.edgePrice == null || args.edgePrice == undefined ? 0 : args.edgePrice * 1000000000);
2158
2136
  let keys = [
2159
2137
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2160
2138
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2188,6 +2166,10 @@ class Engine {
2188
2166
  throw new Error("Client account not found");
2189
2167
  }
2190
2168
  let instr = this.instruments.get(args.instrId);
2169
+ if (instr.header.mapsAddress == undefined) {
2170
+ yield this.updateInstrData({ instrId: args.instrId });
2171
+ instr = this.instruments.get(args.instrId);
2172
+ }
2191
2173
  let assetTokenDecFactor = this.tokenDec(instr.header.assetTokenId);
2192
2174
  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);
2193
2175
  let keys = [
@@ -2223,6 +2205,10 @@ class Engine {
2223
2205
  throw new Error("Client account not found");
2224
2206
  }
2225
2207
  let instr = this.instruments.get(args.instrId);
2208
+ if (instr.header.mapsAddress == undefined) {
2209
+ yield this.updateInstrData({ instrId: args.instrId });
2210
+ instr = this.instruments.get(args.instrId);
2211
+ }
2226
2212
  const drvs = instr.header.assetTokenId == 0;
2227
2213
  let keys = [
2228
2214
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
@@ -2255,7 +2241,11 @@ class Engine {
2255
2241
  if (!(yield this.checkClient())) {
2256
2242
  throw new Error("Client account not found");
2257
2243
  }
2258
- const instr = this.instruments.get(args.instrId);
2244
+ let instr = this.instruments.get(args.instrId);
2245
+ if (instr.header.mapsAddress == undefined) {
2246
+ yield this.updateInstrData({ instrId: args.instrId });
2247
+ instr = this.instruments.get(args.instrId);
2248
+ }
2259
2249
  const drvs = instr.header.assetTokenId == 0;
2260
2250
  let keys = [
2261
2251
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
@@ -2410,16 +2400,6 @@ class Engine {
2410
2400
  tag: types_1.AccountType.PERP_CLIENT_INFOS5
2411
2401
  }), role: kit_1.AccountRole.WRITABLE
2412
2402
  },
2413
- /*
2414
- {
2415
- address: await this.getInstrAccountByTag(
2416
- {
2417
- assetTokenId: instr.header.assetTokenId,
2418
- crncyTokenId: instr.header.crncyTokenId,
2419
- tag: AccountType.PERP_CLIENT_ACCOUNTS
2420
- }), role: AccountRole.WRITABLE
2421
- },
2422
- */
2423
2403
  {
2424
2404
  address: yield this.getInstrAccountByTag({
2425
2405
  assetTokenId: instr.header.assetTokenId,
@@ -2441,16 +2421,6 @@ class Engine {
2441
2421
  tag: types_1.AccountType.PERP_REBALANCE_TIME_TREE
2442
2422
  }), role: kit_1.AccountRole.WRITABLE
2443
2423
  },
2444
- /*
2445
- {
2446
- address: await this.getInstrAccountByTag(
2447
- {
2448
- assetTokenId: instr.header.assetTokenId,
2449
- crncyTokenId: instr.header.crncyTokenId,
2450
- tag: AccountType.PERP_PRIORITY_TREE
2451
- }), role: AccountRole.WRITABLE
2452
- },
2453
- */
2454
2424
  ];
2455
2425
  const upgradeIx = {
2456
2426
  accounts: keys,
@@ -2470,7 +2440,11 @@ class Engine {
2470
2440
  if (!(yield this.checkClient())) {
2471
2441
  throw new Error("Client account not found");
2472
2442
  }
2473
- const instr = this.instruments.get(args.instrId);
2443
+ let instr = this.instruments.get(args.instrId);
2444
+ if (instr.header.perpMapsAddress == undefined) {
2445
+ yield this.updateInstrData({ instrId: args.instrId });
2446
+ instr = this.instruments.get(args.instrId);
2447
+ }
2474
2448
  let keys = [
2475
2449
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2476
2450
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2495,7 +2469,8 @@ class Engine {
2495
2469
  if (!(yield this.checkClient())) {
2496
2470
  throw new Error("Client account not found");
2497
2471
  }
2498
- const instr = this.instruments.get(args.instrId);
2472
+ yield this.updateInstrData({ instrId: args.instrId });
2473
+ let instr = this.instruments.get(args.instrId);
2499
2474
  let keys = [
2500
2475
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2501
2476
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2503,10 +2478,14 @@ class Engine {
2503
2478
  ...yield this.getPerpContext(instr.header),
2504
2479
  { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2505
2480
  ];
2481
+ const splippage = (args.slippage == undefined || args.slippage == null) ? 0 : args.slippage;
2482
+ const slippagePrice = (perpSeatReserve(instr.header.perpClientsCount + 1) -
2483
+ perpSeatReserve(instr.header.perpClientsCount)) * (1 + splippage);
2484
+ const crncyDec = this.tokenDec(instr.header.crncyTokenId);
2506
2485
  return {
2507
2486
  accounts: keys,
2508
2487
  programAddress: this.programId,
2509
- data: (0, instruction_models_1.buyMarketSeatData)(47, args.instrId, args.amount * this.tokenDec(instr.header.crncyTokenId)),
2488
+ data: (0, instruction_models_1.buyMarketSeatData)(47, args.instrId, slippagePrice * crncyDec, args.amount * crncyDec),
2510
2489
  };
2511
2490
  });
2512
2491
  }
@@ -2515,7 +2494,8 @@ class Engine {
2515
2494
  if (!(yield this.checkClient())) {
2516
2495
  throw new Error("Client account not found");
2517
2496
  }
2518
- const instr = this.instruments.get(args.instrId);
2497
+ yield this.updateInstrData({ instrId: args.instrId });
2498
+ let instr = this.instruments.get(args.instrId);
2519
2499
  let keys = [
2520
2500
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2521
2501
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2524,10 +2504,14 @@ class Engine {
2524
2504
  { address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
2525
2505
  { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2526
2506
  ];
2507
+ const splippage = (args.slippage == undefined || args.slippage == null) ? 0 : args.slippage;
2508
+ const slippagePrice = (perpSeatReserve(instr.header.perpClientsCount + 1) -
2509
+ perpSeatReserve(instr.header.perpClientsCount)) / (1 + splippage);
2510
+ const crncyDec = this.tokenDec(instr.header.crncyTokenId);
2527
2511
  return {
2528
2512
  accounts: keys,
2529
2513
  programAddress: this.programId,
2530
- data: (0, instruction_models_1.sellMarketSeatData)(48, args.instrId),
2514
+ data: (0, instruction_models_1.sellMarketSeatData)(48, slippagePrice * crncyDec, args.instrId),
2531
2515
  };
2532
2516
  });
2533
2517
  }
@@ -2541,7 +2525,11 @@ class Engine {
2541
2525
  if (!(yield this.checkClient())) {
2542
2526
  throw new Error("Client account not found");
2543
2527
  }
2544
- const instr = this.instruments.get(args.instrId);
2528
+ let instr = this.instruments.get(args.instrId);
2529
+ if (instr.header.perpMapsAddress == undefined) {
2530
+ yield this.updateInstrData({ instrId: args.instrId });
2531
+ instr = this.instruments.get(args.instrId);
2532
+ }
2545
2533
  let keys = [
2546
2534
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2547
2535
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2558,7 +2546,7 @@ class Engine {
2558
2546
  return {
2559
2547
  accounts: keys,
2560
2548
  programAddress: this.programId,
2561
- data: (0, instruction_models_1.newPerpOrderData)(19, args.ioc == null || args.ioc == undefined ? 0 : args.ioc, args.leverage == null || args.leverage == undefined ? 0 : args.leverage, args.orderType == null || args.orderType == undefined ? 0 : args.orderType, args.side, args.instrId, args.price * 1000000000, args.qty * this.tokenDec(instr.header.assetTokenId)),
2549
+ data: (0, instruction_models_1.newPerpOrderData)(19, args.ioc == null || args.ioc == undefined ? 0 : args.ioc, args.leverage == null || args.leverage == undefined ? 0 : args.leverage, args.orderType == null || args.orderType == undefined ? 0 : args.orderType, args.side, args.instrId, args.price * 1000000000, args.qty * this.tokenDec(instr.header.assetTokenId), args.edgePrice == null || args.edgePrice == undefined ? 0 : args.edgePrice * 1000000000),
2562
2550
  };
2563
2551
  });
2564
2552
  }
@@ -2572,7 +2560,11 @@ class Engine {
2572
2560
  if (!(yield this.checkClient())) {
2573
2561
  throw new Error("Client account not found");
2574
2562
  }
2575
- const instr = this.instruments.get(args.instrId);
2563
+ let instr = this.instruments.get(args.instrId);
2564
+ if (instr.header.perpMapsAddress == undefined) {
2565
+ yield this.updateInstrData({ instrId: args.instrId });
2566
+ instr = this.instruments.get(args.instrId);
2567
+ }
2576
2568
  let assetTokenDecFactor = this.tokenDec(instr.header.assetTokenId);
2577
2569
  let buf = (0, instruction_models_1.perpQuotesReplaceData)(42, 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);
2578
2570
  let keys = [
@@ -2605,7 +2597,11 @@ class Engine {
2605
2597
  if (!(yield this.checkClient())) {
2606
2598
  throw new Error("Client account not found");
2607
2599
  }
2608
- const instr = this.instruments.get(args.instrId);
2600
+ let instr = this.instruments.get(args.instrId);
2601
+ if (instr.header.perpMapsAddress == undefined) {
2602
+ yield this.updateInstrData({ instrId: args.instrId });
2603
+ instr = this.instruments.get(args.instrId);
2604
+ }
2609
2605
  let keys = [
2610
2606
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2611
2607
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2631,7 +2627,11 @@ class Engine {
2631
2627
  if (!(yield this.checkClient())) {
2632
2628
  throw new Error("Client account not found");
2633
2629
  }
2634
- const instr = this.instruments.get(args.instrId);
2630
+ let instr = this.instruments.get(args.instrId);
2631
+ if (instr.header.perpMapsAddress == undefined) {
2632
+ yield this.updateInstrData({ instrId: args.instrId });
2633
+ instr = this.instruments.get(args.instrId);
2634
+ }
2635
2635
  let keys = [
2636
2636
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2637
2637
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2647,32 +2647,6 @@ class Engine {
2647
2647
  };
2648
2648
  });
2649
2649
  }
2650
- /**
2651
- * Build instruction for perp forced close in particular instrument
2652
- * @param args Order data
2653
- * @returns Transaction instruction
2654
- */
2655
- perpForcedCloseInstruction(args) {
2656
- return __awaiter(this, void 0, void 0, function* () {
2657
- if (!(yield this.checkClient())) {
2658
- throw new Error("Client account not found");
2659
- }
2660
- const instr = this.instruments.get(args.instrId);
2661
- let keys = [
2662
- { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2663
- { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
2664
- { address: args.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
2665
- ...yield this.getPerpContext(instr.header),
2666
- { address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
2667
- { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2668
- ];
2669
- return {
2670
- accounts: keys,
2671
- programAddress: this.programId,
2672
- data: (0, instruction_models_1.perpForcedCloseData)(38, args.instrId),
2673
- };
2674
- });
2675
- }
2676
2650
  /**
2677
2651
  * Build instruction for new referral link
2678
2652
  * @returns Transaction instruction
@@ -2706,7 +2680,11 @@ class Engine {
2706
2680
  if (!(yield this.checkClient())) {
2707
2681
  throw new Error("Client account not found");
2708
2682
  }
2709
- const instr = this.instruments.get(args.instrId);
2683
+ let instr = this.instruments.get(args.instrId);
2684
+ if (instr.header.perpMapsAddress == undefined) {
2685
+ yield this.updateInstrData({ instrId: args.instrId });
2686
+ instr = this.instruments.get(args.instrId);
2687
+ }
2710
2688
  let keys = [
2711
2689
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2712
2690
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2732,7 +2710,11 @@ class Engine {
2732
2710
  if (!(yield this.checkClient())) {
2733
2711
  throw new Error("Client account not found");
2734
2712
  }
2735
- const instr = this.instruments.get(args.instrId);
2713
+ let instr = this.instruments.get(args.instrId);
2714
+ if (instr.header.perpMapsAddress == undefined) {
2715
+ yield this.updateInstrData({ instrId: args.instrId });
2716
+ instr = this.instruments.get(args.instrId);
2717
+ }
2736
2718
  let keys = [
2737
2719
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2738
2720
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2867,16 +2849,6 @@ class Engine {
2867
2849
  tag: types_1.AccountType.SPOT_CLIENT_INFOS2
2868
2850
  }), role: kit_1.AccountRole.WRITABLE
2869
2851
  },
2870
- /*
2871
- {
2872
- address: await this.getInstrAccountByTag(
2873
- {
2874
- assetTokenId: assetTokenId,
2875
- crncyTokenId: crncyTokenId,
2876
- tag: AccountType.SPOT_CLIENT_ACCOUNTS
2877
- }), role: AccountRole.WRITABLE
2878
- },
2879
- */
2880
2852
  {
2881
2853
  address: yield this.getInstrAccountByTag({
2882
2854
  assetTokenId: assetTokenId,
@@ -2898,16 +2870,6 @@ class Engine {
2898
2870
  tag: types_1.AccountType.SPOT_DAY_CANDLES
2899
2871
  }), role: kit_1.AccountRole.WRITABLE
2900
2872
  },
2901
- /*
2902
- {
2903
- address: await this.getInstrAccountByTag(
2904
- {
2905
- assetTokenId: assetTokenId,
2906
- crncyTokenId: crncyTokenId,
2907
- tag: AccountType.INSTR_TRACE
2908
- }), role: AccountRole.WRITABLE
2909
- },
2910
- */
2911
2873
  ];
2912
2874
  const newInstrIx = {
2913
2875
  accounts: keys,
@@ -2917,5 +2879,63 @@ class Engine {
2917
2879
  return [createMapsAccountIx, newInstrIx];
2918
2880
  });
2919
2881
  }
2882
+ /**
2883
+ * Build instruction for durect swap
2884
+ * @param args Order data
2885
+ * @returns Transaction instruction
2886
+ */
2887
+ swapInstruction(args) {
2888
+ return __awaiter(this, void 0, void 0, function* () {
2889
+ const assetTokenId = yield this.getTokenId(args.assetMint);
2890
+ const crncyTokenId = yield this.getTokenId(args.crncyMint);
2891
+ const assetTokenAccount = this.tokens.get(assetTokenId);
2892
+ const crncyTokenAccount = this.tokens.get(crncyTokenId);
2893
+ const assetTokenProgramId = (assetTokenAccount.mask & 0x80000000) == 0 ?
2894
+ TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
2895
+ const crncyTokenProgramId = (crncyTokenAccount.mask & 0x80000000) == 0 ?
2896
+ TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
2897
+ let instrId = yield this.getInstrId({
2898
+ assetTokenId: assetTokenId,
2899
+ crncyTokenId: crncyTokenId
2900
+ });
2901
+ const clientAssetTokenAccount = yield findAssociatedTokenAddress(this.signer, assetTokenProgramId, args.assetMint);
2902
+ const clientCrncyTokenAccount = yield findAssociatedTokenAddress(this.signer, crncyTokenProgramId, args.crncyMint);
2903
+ let instr = this.instruments.get(instrId);
2904
+ if (instr.header.mapsAddress == undefined) {
2905
+ yield this.updateInstrData({ instrId: instrId });
2906
+ instr = this.instruments.get(instrId);
2907
+ }
2908
+ let buf = (0, instruction_models_1.swapData)(26, args.crncyInput ? 1 : 0, instrId, Math.round(args.limitPrice * 1000000000), Math.round(args.amount *
2909
+ (args.crncyInput ?
2910
+ this.tokenDec(instr.header.crncyTokenId) :
2911
+ this.tokenDec(instr.header.assetTokenId))));
2912
+ let keys = [
2913
+ { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2914
+ { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
2915
+ ...yield this.getSpotContext(instr.header),
2916
+ ...yield this.getSpotCandles(instr.header),
2917
+ {
2918
+ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY),
2919
+ role: kit_1.AccountRole.READONLY
2920
+ },
2921
+ { address: assetTokenAccount.programAddress, role: kit_1.AccountRole.WRITABLE },
2922
+ { address: crncyTokenAccount.programAddress, role: kit_1.AccountRole.WRITABLE },
2923
+ { address: args.assetMint, role: kit_1.AccountRole.READONLY },
2924
+ { address: args.crncyMint, role: kit_1.AccountRole.READONLY },
2925
+ { address: yield this.getTokenAccount(args.assetMint), role: kit_1.AccountRole.READONLY },
2926
+ { address: yield this.getTokenAccount(args.crncyMint), role: kit_1.AccountRole.READONLY },
2927
+ { address: clientAssetTokenAccount, role: kit_1.AccountRole.WRITABLE },
2928
+ { address: clientCrncyTokenAccount, role: kit_1.AccountRole.WRITABLE },
2929
+ { address: this.drvsAuthority, role: kit_1.AccountRole.READONLY },
2930
+ { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2931
+ { address: assetTokenProgramId, role: kit_1.AccountRole.READONLY },
2932
+ { address: crncyTokenProgramId, role: kit_1.AccountRole.READONLY },
2933
+ { address: ASSOCIATED_TOKEN_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2934
+ ];
2935
+ return {
2936
+ accounts: keys, programAddress: this.programId, data: buf
2937
+ };
2938
+ });
2939
+ }
2920
2940
  }
2921
2941
  exports.Engine = Engine;
@@ -2,13 +2,13 @@
2
2
  /// <reference types="node" />
3
3
  export declare function newOperatorData(tag: number): Buffer;
4
4
  export declare function newRootAccountData(tag: number, privateMode: number, lutSlot: number): Buffer;
5
- export declare function newSpotOrderData(tag: number, ioc: number, orderType: number, side: number, instrId: number, price: number, amount: number): Buffer;
6
- export declare function newPerpOrderData(tag: number, ioc: number, leverage: number, orderType: number, side: number, instrId: number, price: number, amount: number): Buffer;
5
+ export declare function newSpotOrderData(tag: number, ioc: number, orderType: number, side: number, instrId: number, price: number, amount: number, edgePrice: number): Buffer;
6
+ export declare function newPerpOrderData(tag: number, ioc: number, leverage: number, orderType: number, side: number, instrId: number, price: number, amount: number, edgePrice: number): Buffer;
7
7
  export declare function perpChangeLeverageData(tag: number, leverage: number, instrId: number): Buffer;
8
8
  export declare function perpStatisticsResetData(tag: number, instrId: number): Buffer;
9
9
  export declare function spotOrderCancelData(tag: number, side: number, instrId: number, orderId: number): Buffer;
10
10
  export declare function spotMassCancelData(tag: number, instrId: number): Buffer;
11
- export declare function spotLpData(tag: number, side: number, instrId: number, amount: number): Buffer;
11
+ export declare function spotLpData(tag: number, side: number, instrId: number, amount: number, minPrice: number, maxPrice: number): Buffer;
12
12
  export declare function newInstrumentData(tag: number, crncyTokenId: number, lutSlot: number, price: number): Buffer;
13
13
  export declare function depositData(tag: number, competitionId: number, depositAll: number, tokenId: number, amount: number, lutSlot: number, refId: number): Buffer;
14
14
  export declare function feesDepositData(tag: number, tokenId: number, amount: number): Buffer;
@@ -17,19 +17,25 @@ export declare function perpDepositData(tag: number, instrId: number, amount: nu
17
17
  export declare function moveSpotAvailFundsData(tag: number, instrId: number): Buffer;
18
18
  export declare function perpWithdrawData(tag: number, instrId: number, amount: number): Buffer;
19
19
  export declare function withdrawData(tag: number, tokenId: number, amount: number): Buffer;
20
- export declare function swapData(tag: number, side: number, instrId: number, price: number, amount: number): Buffer;
20
+ export declare function swapData(tag: number, inputCrncy: number, instrId: number, price: number, amount: number): Buffer;
21
21
  export declare function spotQuotesReplaceData(tag: number, instrId: number, newBidPrice: number, newBidQty: number, oldBidOrderId: number, newAskPrice: number, newAskQty: number, oldAskOrderId: number): Buffer;
22
22
  export declare function perpQuotesReplaceData(tag: number, instrId: number, newBidPrice: number, newBidQty: number, oldBidOrderId: number, newAskPrice: number, newAskQty: number, oldAskOrderId: number): Buffer;
23
23
  export declare function votingData(tag: number, choice: number, votingCounter: number): Buffer;
24
24
  export declare function airdropData(tag: number, ratio: number): Buffer;
25
25
  export declare function upgradeToPerpData(tag: number, instrId: number): Buffer;
26
- export declare function setInstrOracleFeedData(tag: number, instrId: number): Buffer;
27
26
  export declare function setInstrReadyForPerpUpgradeData(tag: number, instrId: number, variance: number): Buffer;
28
- export declare function newTokenData(tag: number, crncy: number, needInitialization: number): Buffer;
29
27
  export declare function perpOrderCancelData(tag: number, side: number, instrId: number, orderId: number): Buffer;
30
28
  export declare function perpMassCancelData(tag: number, instrId: number): Buffer;
31
- export declare function perpForcedCloseData(tag: number, instrId: number): Buffer;
32
29
  export declare function changeRefProgramData(tag: number, refProgramDuration: number, refLinkDuration: number, refDiscount: number, refRatio: number): Buffer;
33
- export declare function buyMarketSeatData(tag: number, instrId: number, amount: number): Buffer;
34
- export declare function sellMarketSeatData(tag: number, instrId: number): Buffer;
30
+ export declare function buyMarketSeatData(tag: number, instrId: number, edgePrice: number, amount: number): Buffer;
31
+ export declare function sellMarketSeatData(tag: number, instrId: number, edgePrice: number): Buffer;
35
32
  export declare function newPrivateClient(tag: number, expirationTime: number): Buffer;
33
+ export declare function pointsProgramExpiration(tag: number, newExpirationTime: number): Buffer;
34
+ export declare function setVarianceData(tag: number, variance: number): Buffer;
35
+ export declare function changeDenominatorData(tag: number, baseCrncyId: number, denominator: number): Buffer;
36
+ export declare function newBaseCrncyData(tag: number, denominator: number): Buffer;
37
+ export declare function perpClientsProcessingData(tag: number, instrId: number): Buffer;
38
+ export declare function setSeatPurchasingFeeData(tag: number, fee: number): Buffer;
39
+ export declare function changeVotingData(tag: number, newChoice: number, votingCounter: number): Buffer;
40
+ export declare function garbageCollectorData(tag: number, instrId: number): Buffer;
41
+ export declare function activateClientRefProgramData(tag: number, refId: number): Buffer;