@deriverse/kit 1.0.30 → 1.0.38

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.js CHANGED
@@ -23,7 +23,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
23
23
  });
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.Engine = exports.getPriceStep = void 0;
26
+ exports.Engine = exports.getPerpPriceStep = exports.getSpotPriceStep = void 0;
27
27
  const kit_1 = require("@solana/kit");
28
28
  const system_1 = require("@solana-program/system");
29
29
  const bs58_1 = require("bs58");
@@ -48,7 +48,7 @@ const nullOrder = 0xFFFF;
48
48
  * @param price Current market price
49
49
  * @returns Price step
50
50
  */
51
- function getPriceStep(price) {
51
+ function getSpotPriceStep(price) {
52
52
  if (price <= 0.00001) {
53
53
  return 0.000000001;
54
54
  }
@@ -161,7 +161,129 @@ function getPriceStep(price) {
161
161
  return 1000;
162
162
  }
163
163
  }
164
- exports.getPriceStep = getPriceStep;
164
+ exports.getSpotPriceStep = getSpotPriceStep;
165
+ /**
166
+ * Get price step between orderbook lines depending on curent price
167
+ * @param price Current market price
168
+ * @returns Price step
169
+ */
170
+ function getPerpPriceStep(price) {
171
+ if (price <= 0.00005) {
172
+ return 0.000000001;
173
+ }
174
+ else if (price <= 0.0001) {
175
+ return 0.000000002;
176
+ }
177
+ else if (price <= 0.0002) {
178
+ return 0.000000005;
179
+ }
180
+ else if (price <= 0.0005) {
181
+ return 0.00000001;
182
+ }
183
+ else if (price <= 0.001) {
184
+ return 0.00000002;
185
+ }
186
+ else if (price <= 0.002) {
187
+ return 0.00000005;
188
+ }
189
+ else if (price <= 0.005) {
190
+ return 0.0000001;
191
+ }
192
+ else if (price <= 0.01) {
193
+ return 0.0000002;
194
+ }
195
+ else if (price <= 0.02) {
196
+ return 0.0000005;
197
+ }
198
+ else if (price <= 0.05) {
199
+ return 0.000001;
200
+ }
201
+ else if (price <= 0.1) {
202
+ return 0.000002;
203
+ }
204
+ else if (price <= 0.2) {
205
+ return 0.000005;
206
+ }
207
+ else if (price <= 0.5) {
208
+ return 0.00001;
209
+ }
210
+ else if (price <= 1) {
211
+ return 0.00002;
212
+ }
213
+ else if (price <= 2) {
214
+ return 0.00005;
215
+ }
216
+ else if (price <= 5) {
217
+ return 0.0001;
218
+ }
219
+ else if (price <= 10) {
220
+ return 0.0002;
221
+ }
222
+ else if (price <= 20) {
223
+ return 0.0005;
224
+ }
225
+ else if (price <= 50) {
226
+ return 0.001;
227
+ }
228
+ else if (price <= 100) {
229
+ return 0.002;
230
+ }
231
+ else if (price <= 200) {
232
+ return 0.005;
233
+ }
234
+ else if (price <= 500) {
235
+ return 0.01;
236
+ }
237
+ else if (price <= 1000) {
238
+ return 0.02;
239
+ }
240
+ else if (price <= 2000) {
241
+ return 0.05;
242
+ }
243
+ else if (price <= 5000) {
244
+ return 0.1;
245
+ }
246
+ else if (price <= 10000) {
247
+ return 0.2;
248
+ }
249
+ else if (price <= 20000) {
250
+ return 0.5;
251
+ }
252
+ else if (price <= 50000) {
253
+ return 1;
254
+ }
255
+ else if (price <= 100000) {
256
+ return 2;
257
+ }
258
+ else if (price <= 200000) {
259
+ return 5;
260
+ }
261
+ else if (price <= 500000) {
262
+ return 10;
263
+ }
264
+ else if (price <= 1000000) {
265
+ return 20;
266
+ }
267
+ else if (price <= 2000000) {
268
+ return 50;
269
+ }
270
+ else if (price <= 5000000) {
271
+ return 100;
272
+ }
273
+ else if (price <= 10000000) {
274
+ return 200;
275
+ }
276
+ else if (price <= 20000000) {
277
+ return 500;
278
+ }
279
+ else {
280
+ return 1000;
281
+ }
282
+ }
283
+ exports.getPerpPriceStep = getPerpPriceStep;
284
+ function perpSeatReserve(activeUsers) {
285
+ return 250000 * activeUsers / (25000 - activeUsers);
286
+ }
165
287
  function findAssociatedTokenAddress(owner, tokenProgramId, mint) {
166
288
  return __awaiter(this, void 0, void 0, function* () {
167
289
  const address = (yield (0, kit_1.getProgramDerivedAddress)({
@@ -388,6 +510,22 @@ class Engine {
388
510
  }
389
511
  break;
390
512
  }
513
+ case logs_models_1.LogType.moveSpot: {
514
+ if (buffer.length == logs_models_1.MoveSpotAvailFundsReportModel.LENGTH) {
515
+ let report = logs_models_1.MoveSpotAvailFundsReportModel.fromBuffer(buffer);
516
+ if (this.uiNumbers) {
517
+ const instrInfo = this.instruments.get(report.instrId);
518
+ if (instrInfo) {
519
+ assetTokenDec = this.tokenDec(instrInfo.header.assetTokenId);
520
+ crncyTokenDec = this.tokenDec(instrInfo.header.crncyTokenId);
521
+ report.qty /= assetTokenDec;
522
+ report.crncy /= crncyTokenDec;
523
+ }
524
+ }
525
+ logs.push(report);
526
+ }
527
+ break;
528
+ }
391
529
  case logs_models_1.LogType.earnings: {
392
530
  if (buffer.length == logs_models_1.EarningsReportModel.LENGTH) {
393
531
  let report = logs_models_1.EarningsReportModel.fromBuffer(buffer);
@@ -680,6 +818,7 @@ class Engine {
680
818
  this.rootStateModel = structure_models_1.RootStateModel.fromBuffer(infos.value[0].data);
681
819
  this.tokens = new Map();
682
820
  this.instruments = new Map();
821
+ this.privateMode = (this.rootStateModel.mask & 1) != 0;
683
822
  const tokenAccounts = yield this.findAccountsByTag(types_1.AccountType.TOKEN);
684
823
  tokenAccounts.forEach((t) => {
685
824
  let tokenStateModel = structure_models_1.TokenStateModel.fromBuffer(t.account.data);
@@ -883,16 +1022,6 @@ class Engine {
883
1022
  }),
884
1023
  role: kit_1.AccountRole.WRITABLE
885
1024
  },
886
- /*
887
- {
888
- address: await this.getInstrAccountByTag({
889
- assetTokenId: instrAccountHeaderModel.assetTokenId,
890
- crncyTokenId: instrAccountHeaderModel.crncyTokenId,
891
- tag: AccountType.SPOT_CLIENT_ACCOUNTS
892
- }),
893
- role: AccountRole.WRITABLE
894
- },
895
- */
896
1025
  ];
897
1026
  });
898
1027
  }
@@ -991,16 +1120,6 @@ class Engine {
991
1120
  }),
992
1121
  role: kit_1.AccountRole.WRITABLE
993
1122
  },
994
- /*
995
- {
996
- address: await this.getInstrAccountByTag({
997
- assetTokenId: instrAccountHeaderModel.assetTokenId,
998
- crncyTokenId: instrAccountHeaderModel.crncyTokenId,
999
- tag: AccountType.PERP_CLIENT_ACCOUNTS
1000
- }),
1001
- role: AccountRole.WRITABLE
1002
- },
1003
- */
1004
1123
  {
1005
1124
  address: yield this.getInstrAccountByTag({
1006
1125
  assetTokenId: instrAccountHeaderModel.assetTokenId,
@@ -1025,16 +1144,6 @@ class Engine {
1025
1144
  }),
1026
1145
  role: kit_1.AccountRole.WRITABLE
1027
1146
  },
1028
- /*
1029
- {
1030
- address: await this.getInstrAccountByTag({
1031
- assetTokenId: instrAccountHeaderModel.assetTokenId,
1032
- crncyTokenId: instrAccountHeaderModel.crncyTokenId,
1033
- tag: AccountType.PERP_PRIORITY_TREE
1034
- }),
1035
- role: AccountRole.WRITABLE
1036
- },
1037
- */
1038
1147
  ];
1039
1148
  });
1040
1149
  }
@@ -1254,8 +1363,8 @@ class Engine {
1254
1363
  for (var i = 0; i < clientPrimaryAccountHeaderModel.assetsCount; ++i) {
1255
1364
  const offset = structure_models_1.ClientPrimaryAccountHeaderModel.LENGTH + i * 16;
1256
1365
  const assetInfo = primaryData.readUint32LE(offset);
1257
- const tag = assetInfo >> 24;
1258
- const id = assetInfo & 0xFFFFFF;
1366
+ const tag = assetInfo >> 28;
1367
+ const id = assetInfo & 0xFFFFFFF;
1259
1368
  if (tag == 1) {
1260
1369
  tokens.set(id, {
1261
1370
  tokenId: id,
@@ -1727,8 +1836,8 @@ class Engine {
1727
1836
  header.dayCrncyTokens /= crncyTokenDec;
1728
1837
  header.prevDayAssetTokens /= assetTokenDec;
1729
1838
  header.prevDayCrncyTokens /= crncyTokenDec;
1730
- header.perpLastAssetTokens /= assetTokenDec;
1731
- header.perpLastCrncyTokens /= crncyTokenDec;
1839
+ header.perpLastTradeAssetTokens /= assetTokenDec;
1840
+ header.perpLastTradeCrncyTokens /= crncyTokenDec;
1732
1841
  header.perpDayAssetTokens /= assetTokenDec;
1733
1842
  header.perpDayCrncyTokens /= crncyTokenDec;
1734
1843
  header.fixingAssetTokens /= assetTokenDec;
@@ -1753,8 +1862,8 @@ class Engine {
1753
1862
  header.fixingPx /= dec;
1754
1863
  header.perpLastClose /= dec;
1755
1864
  header.perpLastPx /= dec;
1756
- //header.lastHourPx /= dec;
1757
- header.perpSpotPriceForWithdrowal /= dec;
1865
+ header.perpLongSpotPriceForWithdrowal /= dec;
1866
+ header.perpShortSpotPriceForWithdrowal /= dec;
1758
1867
  header.poolFees /= crncyTokenDec;
1759
1868
  let spotBids = [];
1760
1869
  let spotAsks = [];
@@ -1871,6 +1980,9 @@ class Engine {
1871
1980
  { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
1872
1981
  { address: tokenProgramId, role: kit_1.AccountRole.READONLY },
1873
1982
  ];
1983
+ if (this.privateMode) {
1984
+ keys.push({ address: yield this.getAccountByTag(types_1.AccountType.PRIVATE_CLIENTS), role: kit_1.AccountRole.WRITABLE });
1985
+ }
1874
1986
  if (exists) {
1875
1987
  if (args.tokenId == 0) {
1876
1988
  keys.push({ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.WRITABLE });
@@ -1886,11 +1998,6 @@ class Engine {
1886
1998
  const slot = Number((yield this.rpc.getSlot().send())) - 1;
1887
1999
  const lutAddress = yield getLookupTableAddress(this.signer, slot);
1888
2000
  const clientCommunityAccount = yield this.findClientCommunityAccount();
1889
- /*
1890
- keys.push(
1891
- { address: await this.findClientDrvAccount(), role: AccountRole.WRITABLE }
1892
- );
1893
- */
1894
2001
  keys.push({ address: clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
1895
2002
  keys.push({ address: lutAddress, role: kit_1.AccountRole.WRITABLE });
1896
2003
  keys.push({ address: ADDRESS_LOOKUP_TABLE_PROGRAM_ID, role: kit_1.AccountRole.WRITABLE });
@@ -1945,7 +2052,6 @@ class Engine {
1945
2052
  { address: ASSOCIATED_TOKEN_PROGRAM_ID, role: kit_1.AccountRole.WRITABLE },
1946
2053
  ];
1947
2054
  if (args.spot != undefined) {
1948
- //keys.push({ address: await this.findClientDrvAccount(), role: AccountRole.READONLY });
1949
2055
  for (var i = 0; i < args.spot.length; ++i) {
1950
2056
  const instr = this.instruments.get(args.spot[i].instrId);
1951
2057
  if (instr.header.assetTokenId == args.tokenId || instr.header.crncyTokenId == args.tokenId) {
@@ -1962,8 +2068,8 @@ class Engine {
1962
2068
  }
1963
2069
  }
1964
2070
  if (args.tokenId == 0) {
1965
- keys.push({ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY });
1966
- 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 });
1967
2073
  }
1968
2074
  return {
1969
2075
  accounts: keys,
@@ -1982,7 +2088,8 @@ class Engine {
1982
2088
  if (!(yield this.checkClient())) {
1983
2089
  throw new Error("Client account not found");
1984
2090
  }
1985
- const instr = this.instruments.get(args.instrId);
2091
+ yield this.updateInstrData({ instrId: args.instrId });
2092
+ let instr = this.instruments.get(args.instrId);
1986
2093
  let keys = [
1987
2094
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
1988
2095
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2001,10 +2108,12 @@ class Engine {
2001
2108
  keys.push({ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.WRITABLE });
2002
2109
  keys.push({ address: this.clientCommunityAccount, role: kit_1.AccountRole.WRITABLE });
2003
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;
2004
2113
  return {
2005
2114
  accounts: keys,
2006
2115
  programAddress: this.programId,
2007
- 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),
2008
2117
  };
2009
2118
  });
2010
2119
  }
@@ -2019,7 +2128,11 @@ class Engine {
2019
2128
  throw new Error("Client account not found");
2020
2129
  }
2021
2130
  let instr = this.instruments.get(args.instrId);
2022
- 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);
2023
2136
  let keys = [
2024
2137
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2025
2138
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2053,6 +2166,10 @@ class Engine {
2053
2166
  throw new Error("Client account not found");
2054
2167
  }
2055
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
+ }
2056
2173
  let assetTokenDecFactor = this.tokenDec(instr.header.assetTokenId);
2057
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);
2058
2175
  let keys = [
@@ -2088,6 +2205,10 @@ class Engine {
2088
2205
  throw new Error("Client account not found");
2089
2206
  }
2090
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
+ }
2091
2212
  const drvs = instr.header.assetTokenId == 0;
2092
2213
  let keys = [
2093
2214
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
@@ -2120,7 +2241,11 @@ class Engine {
2120
2241
  if (!(yield this.checkClient())) {
2121
2242
  throw new Error("Client account not found");
2122
2243
  }
2123
- 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
+ }
2124
2249
  const drvs = instr.header.assetTokenId == 0;
2125
2250
  let keys = [
2126
2251
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
@@ -2275,16 +2400,6 @@ class Engine {
2275
2400
  tag: types_1.AccountType.PERP_CLIENT_INFOS5
2276
2401
  }), role: kit_1.AccountRole.WRITABLE
2277
2402
  },
2278
- /*
2279
- {
2280
- address: await this.getInstrAccountByTag(
2281
- {
2282
- assetTokenId: instr.header.assetTokenId,
2283
- crncyTokenId: instr.header.crncyTokenId,
2284
- tag: AccountType.PERP_CLIENT_ACCOUNTS
2285
- }), role: AccountRole.WRITABLE
2286
- },
2287
- */
2288
2403
  {
2289
2404
  address: yield this.getInstrAccountByTag({
2290
2405
  assetTokenId: instr.header.assetTokenId,
@@ -2306,16 +2421,6 @@ class Engine {
2306
2421
  tag: types_1.AccountType.PERP_REBALANCE_TIME_TREE
2307
2422
  }), role: kit_1.AccountRole.WRITABLE
2308
2423
  },
2309
- /*
2310
- {
2311
- address: await this.getInstrAccountByTag(
2312
- {
2313
- assetTokenId: instr.header.assetTokenId,
2314
- crncyTokenId: instr.header.crncyTokenId,
2315
- tag: AccountType.PERP_PRIORITY_TREE
2316
- }), role: AccountRole.WRITABLE
2317
- },
2318
- */
2319
2424
  ];
2320
2425
  const upgradeIx = {
2321
2426
  accounts: keys,
@@ -2335,7 +2440,11 @@ class Engine {
2335
2440
  if (!(yield this.checkClient())) {
2336
2441
  throw new Error("Client account not found");
2337
2442
  }
2338
- 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
+ }
2339
2448
  let keys = [
2340
2449
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2341
2450
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2360,7 +2469,8 @@ class Engine {
2360
2469
  if (!(yield this.checkClient())) {
2361
2470
  throw new Error("Client account not found");
2362
2471
  }
2363
- const instr = this.instruments.get(args.instrId);
2472
+ yield this.updateInstrData({ instrId: args.instrId });
2473
+ let instr = this.instruments.get(args.instrId);
2364
2474
  let keys = [
2365
2475
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2366
2476
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2368,10 +2478,14 @@ class Engine {
2368
2478
  ...yield this.getPerpContext(instr.header),
2369
2479
  { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2370
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);
2371
2485
  return {
2372
2486
  accounts: keys,
2373
2487
  programAddress: this.programId,
2374
- 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),
2375
2489
  };
2376
2490
  });
2377
2491
  }
@@ -2380,7 +2494,8 @@ class Engine {
2380
2494
  if (!(yield this.checkClient())) {
2381
2495
  throw new Error("Client account not found");
2382
2496
  }
2383
- const instr = this.instruments.get(args.instrId);
2497
+ yield this.updateInstrData({ instrId: args.instrId });
2498
+ let instr = this.instruments.get(args.instrId);
2384
2499
  let keys = [
2385
2500
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2386
2501
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2389,10 +2504,14 @@ class Engine {
2389
2504
  { address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
2390
2505
  { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2391
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);
2392
2511
  return {
2393
2512
  accounts: keys,
2394
2513
  programAddress: this.programId,
2395
- data: (0, instruction_models_1.sellMarketSeatData)(48, args.instrId),
2514
+ data: (0, instruction_models_1.sellMarketSeatData)(48, slippagePrice * crncyDec, args.instrId),
2396
2515
  };
2397
2516
  });
2398
2517
  }
@@ -2406,7 +2525,11 @@ class Engine {
2406
2525
  if (!(yield this.checkClient())) {
2407
2526
  throw new Error("Client account not found");
2408
2527
  }
2409
- 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
+ }
2410
2533
  let keys = [
2411
2534
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2412
2535
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2423,7 +2546,7 @@ class Engine {
2423
2546
  return {
2424
2547
  accounts: keys,
2425
2548
  programAddress: this.programId,
2426
- 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),
2427
2550
  };
2428
2551
  });
2429
2552
  }
@@ -2437,7 +2560,11 @@ class Engine {
2437
2560
  if (!(yield this.checkClient())) {
2438
2561
  throw new Error("Client account not found");
2439
2562
  }
2440
- 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
+ }
2441
2568
  let assetTokenDecFactor = this.tokenDec(instr.header.assetTokenId);
2442
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);
2443
2570
  let keys = [
@@ -2470,7 +2597,11 @@ class Engine {
2470
2597
  if (!(yield this.checkClient())) {
2471
2598
  throw new Error("Client account not found");
2472
2599
  }
2473
- 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
+ }
2474
2605
  let keys = [
2475
2606
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2476
2607
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2496,7 +2627,11 @@ class Engine {
2496
2627
  if (!(yield this.checkClient())) {
2497
2628
  throw new Error("Client account not found");
2498
2629
  }
2499
- 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
+ }
2500
2635
  let keys = [
2501
2636
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2502
2637
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2512,32 +2647,6 @@ class Engine {
2512
2647
  };
2513
2648
  });
2514
2649
  }
2515
- /**
2516
- * Build instruction for perp forced close in particular instrument
2517
- * @param args Order data
2518
- * @returns Transaction instruction
2519
- */
2520
- perpForcedCloseInstruction(args) {
2521
- return __awaiter(this, void 0, void 0, function* () {
2522
- if (!(yield this.checkClient())) {
2523
- throw new Error("Client account not found");
2524
- }
2525
- const instr = this.instruments.get(args.instrId);
2526
- let keys = [
2527
- { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2528
- { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
2529
- { address: args.clientPrimaryAccount, role: kit_1.AccountRole.WRITABLE },
2530
- ...yield this.getPerpContext(instr.header),
2531
- { address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY), role: kit_1.AccountRole.READONLY },
2532
- { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2533
- ];
2534
- return {
2535
- accounts: keys,
2536
- programAddress: this.programId,
2537
- data: (0, instruction_models_1.perpForcedCloseData)(38, args.instrId),
2538
- };
2539
- });
2540
- }
2541
2650
  /**
2542
2651
  * Build instruction for new referral link
2543
2652
  * @returns Transaction instruction
@@ -2571,7 +2680,11 @@ class Engine {
2571
2680
  if (!(yield this.checkClient())) {
2572
2681
  throw new Error("Client account not found");
2573
2682
  }
2574
- 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
+ }
2575
2688
  let keys = [
2576
2689
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2577
2690
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2597,7 +2710,11 @@ class Engine {
2597
2710
  if (!(yield this.checkClient())) {
2598
2711
  throw new Error("Client account not found");
2599
2712
  }
2600
- 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
+ }
2601
2718
  let keys = [
2602
2719
  { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2603
2720
  { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
@@ -2732,16 +2849,6 @@ class Engine {
2732
2849
  tag: types_1.AccountType.SPOT_CLIENT_INFOS2
2733
2850
  }), role: kit_1.AccountRole.WRITABLE
2734
2851
  },
2735
- /*
2736
- {
2737
- address: await this.getInstrAccountByTag(
2738
- {
2739
- assetTokenId: assetTokenId,
2740
- crncyTokenId: crncyTokenId,
2741
- tag: AccountType.SPOT_CLIENT_ACCOUNTS
2742
- }), role: AccountRole.WRITABLE
2743
- },
2744
- */
2745
2852
  {
2746
2853
  address: yield this.getInstrAccountByTag({
2747
2854
  assetTokenId: assetTokenId,
@@ -2763,16 +2870,6 @@ class Engine {
2763
2870
  tag: types_1.AccountType.SPOT_DAY_CANDLES
2764
2871
  }), role: kit_1.AccountRole.WRITABLE
2765
2872
  },
2766
- /*
2767
- {
2768
- address: await this.getInstrAccountByTag(
2769
- {
2770
- assetTokenId: assetTokenId,
2771
- crncyTokenId: crncyTokenId,
2772
- tag: AccountType.INSTR_TRACE
2773
- }), role: AccountRole.WRITABLE
2774
- },
2775
- */
2776
2873
  ];
2777
2874
  const newInstrIx = {
2778
2875
  accounts: keys,
@@ -2782,5 +2879,65 @@ class Engine {
2782
2879
  return [createMapsAccountIx, newInstrIx];
2783
2880
  });
2784
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
+ if (!(yield this.checkClient())) {
2890
+ throw new Error("Client account not found");
2891
+ }
2892
+ const assetTokenId = yield this.getTokenId(args.assetMint);
2893
+ const crncyTokenId = yield this.getTokenId(args.crncyMint);
2894
+ const assetTokenAccount = this.tokens.get(assetTokenId);
2895
+ const crncyTokenAccount = this.tokens.get(crncyTokenId);
2896
+ const assetTokenProgramId = (assetTokenAccount.mask & 0x80000000) == 0 ?
2897
+ TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
2898
+ const crncyTokenProgramId = (crncyTokenAccount.mask & 0x80000000) == 0 ?
2899
+ TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
2900
+ let instrId = yield this.getInstrId({
2901
+ assetTokenId: assetTokenId,
2902
+ crncyTokenId: crncyTokenId
2903
+ });
2904
+ const clientAssetTokenAccount = yield findAssociatedTokenAddress(this.signer, assetTokenProgramId, args.assetMint);
2905
+ const clientCrncyTokenAccount = yield findAssociatedTokenAddress(this.signer, crncyTokenProgramId, args.crncyMint);
2906
+ let instr = this.instruments.get(instrId);
2907
+ if (instr.header.mapsAddress == undefined) {
2908
+ yield this.updateInstrData({ instrId: instrId });
2909
+ instr = this.instruments.get(instrId);
2910
+ }
2911
+ let buf = (0, instruction_models_1.swapData)(26, args.crncyInput ? 1 : 0, instrId, Math.round(args.limitPrice * 1000000000), Math.round(args.amount *
2912
+ (args.crncyInput ?
2913
+ this.tokenDec(instr.header.crncyTokenId) :
2914
+ this.tokenDec(instr.header.assetTokenId))));
2915
+ let keys = [
2916
+ { address: this.signer, role: kit_1.AccountRole.READONLY_SIGNER },
2917
+ { address: this.rootAccount, role: kit_1.AccountRole.READONLY },
2918
+ ...yield this.getSpotContext(instr.header),
2919
+ ...yield this.getSpotCandles(instr.header),
2920
+ {
2921
+ address: yield this.getAccountByTag(types_1.AccountType.COMMUNITY),
2922
+ role: kit_1.AccountRole.READONLY
2923
+ },
2924
+ { address: SYSTEM_PROGRAM_ID, role: kit_1.AccountRole.READONLY },
2925
+ { address: assetTokenProgramId, role: kit_1.AccountRole.READONLY },
2926
+ { address: crncyTokenProgramId, role: kit_1.AccountRole.READONLY },
2927
+ { address: assetTokenAccount.programAddress, role: kit_1.AccountRole.WRITABLE },
2928
+ { address: crncyTokenAccount.programAddress, role: kit_1.AccountRole.WRITABLE },
2929
+ { address: args.assetMint, role: kit_1.AccountRole.READONLY },
2930
+ { address: args.crncyMint, role: kit_1.AccountRole.READONLY },
2931
+ { address: yield this.getTokenAccount(args.assetMint), role: kit_1.AccountRole.READONLY },
2932
+ { address: yield this.getTokenAccount(args.crncyMint), role: kit_1.AccountRole.READONLY },
2933
+ { address: clientAssetTokenAccount, role: kit_1.AccountRole.WRITABLE },
2934
+ { address: clientCrncyTokenAccount, role: kit_1.AccountRole.WRITABLE },
2935
+ { address: this.drvsAuthority, role: kit_1.AccountRole.READONLY },
2936
+ ];
2937
+ return {
2938
+ accounts: keys, programAddress: this.programId, data: buf
2939
+ };
2940
+ });
2941
+ }
2785
2942
  }
2786
2943
  exports.Engine = Engine;