@triadxyz/triad-protocol 1.6.7-beta → 1.6.8-beta-dev

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/trade.js CHANGED
@@ -12,17 +12,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
+ const trade_1 = require("./types/trade");
15
16
  const bn_js_1 = __importDefault(require("bn.js"));
16
17
  const constants_1 = require("./utils/constants");
17
18
  const helpers_1 = require("./utils/helpers");
18
- const trade_1 = require("./utils/pda/trade");
19
+ const trade_2 = require("./utils/pda/trade");
19
20
  const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
20
21
  const sendTransactionWithOptions_1 = __importDefault(require("./utils/sendTransactionWithOptions"));
21
- const swap_1 = require("./utils/swap");
22
+ const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
22
23
  class Trade {
23
24
  constructor(program, provider) {
24
25
  this.program = program;
25
26
  this.provider = provider;
27
+ this.mint = constants_1.USDC_MINT_DEVNET;
28
+ this.decimals = 6;
26
29
  }
27
30
  /**
28
31
  * Get All Markets
@@ -64,6 +67,67 @@ class Trade {
64
67
  return orders;
65
68
  });
66
69
  }
70
+ /**
71
+ * Get Orders By Market ID
72
+ * @param marketId - The ID of the market
73
+ *
74
+ */
75
+ getLimitOrdersByMarketId(marketId) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const marketIdBytes = bytes_1.bs58.encode(new bn_js_1.default(marketId).toArrayLike(Buffer, 'le', 8));
78
+ const memcmpFilters = Array.from({ length: 10 }).map((_, index) => ({
79
+ memcmp: {
80
+ offset: 8 + // discriminator
81
+ 1 + // bump
82
+ 32 + // authority
83
+ 8 + // total_deposits
84
+ 8 + // total_withdraws
85
+ 8 + // opened_orders
86
+ // Per order: index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 28)
87
+ index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 1 + 27) +
88
+ // Then offset to market_id: 8 + 8 + 8
89
+ (8 + 8 + 8),
90
+ bytes: marketIdBytes
91
+ }
92
+ }));
93
+ const allResponses = yield Promise.all(memcmpFilters.map((filter) => this.program.account.userTrade.all([filter])));
94
+ const uniqueResponses = Array.from(new Map(allResponses.flat().map((item) => [item.publicKey.toString(), item])).values());
95
+ const userTrades = uniqueResponses.map(({ account, publicKey }) => (0, helpers_1.formatUserTrade)(account, publicKey));
96
+ const orders = userTrades.flatMap((userTrade) => userTrade.orders.map((order) => (Object.assign({}, order))));
97
+ const filteredOrders = orders.filter((order) => order.marketId === marketId.toString() &&
98
+ order.orderType === trade_1.OrderType.LIMIT &&
99
+ order.status === trade_1.OrderStatus.WAITING);
100
+ const orderBook = {
101
+ hype: {
102
+ bid: [],
103
+ ask: []
104
+ },
105
+ flop: {
106
+ bid: [],
107
+ ask: []
108
+ }
109
+ };
110
+ filteredOrders.forEach((order) => {
111
+ if (order.orderSide === trade_1.OrderSide.BID &&
112
+ order.direction === trade_1.OrderDirection.HYPE) {
113
+ orderBook.hype.bid.push(order);
114
+ return;
115
+ }
116
+ if (order.orderSide === trade_1.OrderSide.ASK &&
117
+ order.direction === trade_1.OrderDirection.HYPE) {
118
+ orderBook.hype.ask.push(order);
119
+ return;
120
+ }
121
+ if (order.orderSide === trade_1.OrderSide.BID &&
122
+ order.direction === trade_1.OrderDirection.FLOP) {
123
+ orderBook.flop.bid.push(order);
124
+ return;
125
+ }
126
+ orderBook.flop.ask.push(order);
127
+ });
128
+ return orderBook;
129
+ });
130
+ }
67
131
  /**
68
132
  * Get Market By ID
69
133
  * @param marketId - The ID of the market
@@ -71,7 +135,7 @@ class Trade {
71
135
  */
72
136
  getMarketById(marketId) {
73
137
  return __awaiter(this, void 0, void 0, function* () {
74
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
138
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
75
139
  const response = yield this.program.account.marketV2.fetch(marketPDA);
76
140
  return (0, helpers_1.formatMarket)(response, marketPDA);
77
141
  });
@@ -95,16 +159,16 @@ class Trade {
95
159
  */
96
160
  getUserTrade(user, userNonce = 0) {
97
161
  return __awaiter(this, void 0, void 0, function* () {
98
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
162
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, user);
99
163
  if (userNonce !== 0) {
100
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userNonce);
101
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
164
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userNonce);
165
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
102
166
  }
103
167
  return this.program.account.userTrade.fetch(userTradePDA);
104
168
  });
105
169
  }
106
170
  /**
107
- * Initialize Market
171
+ * Create Market
108
172
  * @param args.marketId - new markert id - length + 1
109
173
  * @param args.startTime - start time
110
174
  * @param args.endTime - end time
@@ -113,21 +177,24 @@ class Trade {
113
177
  * @param options - RPC options
114
178
  *
115
179
  */
116
- initializeMarket({ marketId, startTime, endTime, question }, options) {
180
+ createMarket({ marketId, startTime, endTime, question, feeBps, customer }, options) {
117
181
  return __awaiter(this, void 0, void 0, function* () {
118
182
  if (question.length > 80) {
119
183
  throw new Error('Question must be less than 80 characters');
120
184
  }
121
185
  return (0, sendTransactionWithOptions_1.default)(this.program.methods
122
- .initializeMarket({
186
+ .createMarket({
123
187
  marketId: new bn_js_1.default(marketId),
124
188
  question: (0, helpers_1.encodeString)(question, 80),
125
189
  marketStart: new bn_js_1.default(startTime),
126
- marketEnd: new bn_js_1.default(endTime)
190
+ marketEnd: new bn_js_1.default(endTime),
191
+ feeBps
127
192
  })
128
193
  .accounts({
129
194
  signer: this.provider.publicKey,
130
- mint: constants_1.TRD_MINT
195
+ mint: this.mint,
196
+ tokenProgram: (0, helpers_1.getTokenProgram)(this.mint),
197
+ customer
131
198
  }), options);
132
199
  });
133
200
  }
@@ -143,44 +210,51 @@ class Trade {
143
210
  if (nonce !== null) {
144
211
  break;
145
212
  }
213
+ let freeSlots = 0;
146
214
  userTrade.orders.forEach((order) => {
147
215
  if (nonce !== null) {
148
216
  return;
149
217
  }
150
- let status = Object.keys(order.status)[0];
151
- if (status !== 'open') {
218
+ if (order.status !== trade_1.OrderStatus.OPEN &&
219
+ order.status !== trade_1.OrderStatus.WAITING &&
220
+ freeSlots >= 2) {
152
221
  nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
153
222
  }
223
+ if (order.status !== trade_1.OrderStatus.OPEN &&
224
+ order.status !== trade_1.OrderStatus.WAITING) {
225
+ freeSlots += 1;
226
+ }
154
227
  });
155
228
  }
156
229
  if (nonce === null) {
157
230
  throw new Error('No open orders found');
158
231
  }
159
232
  if (nonce === 0) {
160
- return (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
233
+ return (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
161
234
  }
162
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, nonce);
163
- const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
235
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, nonce);
236
+ const userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
164
237
  return userTradePDA;
165
238
  });
166
239
  }
167
240
  /**
168
- * Open Order
241
+ * Place Order
169
242
  * @param args.marketId - The ID of the Market
170
243
  * @param args.amount - The amount of the Order
171
244
  * @param args.direction - The direction of the Order
172
- * @param args.token - The token to use for the Order
245
+ * @param args.orderSide - The side of the Order
246
+ * @param args.mint - The mint of the Order
247
+ * @param args.price - The price of the Order
173
248
  *
174
249
  * @param options - RPC options
175
250
  *
176
251
  */
177
- openOrder({ marketId, amount, direction, token }, options) {
252
+ placeOrder({ marketId, amount, direction, orderSide, mint, price }, options) {
178
253
  return __awaiter(this, void 0, void 0, function* () {
179
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
180
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
254
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
255
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
181
256
  const ixs = [];
182
257
  const addressLookupTableAccounts = [];
183
- let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
184
258
  let myUserTrades = [];
185
259
  myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
186
260
  if (myUserTrades.length === 0) {
@@ -194,11 +268,31 @@ class Trade {
194
268
  if (myUserTrades.length > 0) {
195
269
  try {
196
270
  userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
271
+ console.log(userTradePDA);
272
+ // Check if we need to create a new sub user trade
273
+ // This happens when all existing slots have open orders
274
+ const allSlotsHaveOpenOrders = myUserTrades.every((trade) => trade.orders.some((order) => Object.keys(order.status)[0] === 'open' ||
275
+ Object.keys(order.status)[0] === 'waiting'));
276
+ if (allSlotsHaveOpenOrders) {
277
+ // Find the main user trade to get the current nonce
278
+ const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
279
+ if (!mainUserTrade) {
280
+ throw new Error('No main user trade found');
281
+ }
282
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
283
+ ixs.push(yield this.program.methods
284
+ .createSubUserTrade(subUserTradePDA)
285
+ .accounts({
286
+ signer: this.provider.publicKey
287
+ })
288
+ .instruction());
289
+ }
197
290
  }
198
- catch (_a) {
291
+ catch (error) {
292
+ console.log(error);
199
293
  const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
200
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
201
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
294
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
295
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
202
296
  ixs.push(yield this.program.methods
203
297
  .createSubUserTrade(subUserTradePDA)
204
298
  .accounts({
@@ -207,38 +301,41 @@ class Trade {
207
301
  .instruction());
208
302
  }
209
303
  }
210
- if (token !== constants_1.TRD_MINT.toBase58()) {
211
- const { setupInstructions, swapIxs, addressLookupTableAccounts, trdAmount } = yield (0, swap_1.swap)({
212
- connection: this.provider.connection,
213
- wallet: this.provider.publicKey.toBase58(),
214
- inToken: token,
215
- amount
216
- });
217
- amountInTRD = trdAmount;
218
- if (swapIxs.length === 0) {
219
- return;
220
- }
221
- ixs.push(...setupInstructions);
222
- ixs.push(...swapIxs);
223
- addressLookupTableAccounts.push(...addressLookupTableAccounts);
304
+ if (Object.keys(orderSide)[0] === 'bid') {
305
+ ixs.push(yield this.program.methods
306
+ .placeBidOrder({
307
+ amount: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
308
+ price: new bn_js_1.default(price * Math.pow(10, this.decimals)),
309
+ orderDirection: direction
310
+ })
311
+ .accounts({
312
+ signer: this.provider.publicKey,
313
+ market: marketPDA,
314
+ userTrade: userTradePDA,
315
+ mint,
316
+ tokenProgram: (0, helpers_1.getTokenProgram)(mint)
317
+ })
318
+ .instruction());
319
+ }
320
+ if (Object.keys(orderSide)[0] === 'ask') {
321
+ ixs.push(yield this.program.methods
322
+ .placeAskOrder({
323
+ shares: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
324
+ price: new bn_js_1.default(price * Math.pow(10, this.decimals)),
325
+ orderDirection: direction
326
+ })
327
+ .accounts({
328
+ signer: this.provider.publicKey,
329
+ market: marketPDA,
330
+ userTrade: userTradePDA
331
+ })
332
+ .instruction());
224
333
  }
225
- ixs.push(yield this.program.methods
226
- .openOrder({
227
- amount: new bn_js_1.default(amountInTRD),
228
- direction: direction
229
- })
230
- .accounts({
231
- signer: this.provider.publicKey,
232
- market: marketPDA,
233
- userTrade: userTradePDA,
234
- mint: constants_1.TRD_MINT
235
- })
236
- .instruction());
237
334
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
238
335
  });
239
336
  }
240
337
  /**
241
- * Close Order
338
+ * Cancel Order
242
339
  * @param args.marketId - The ID of the Market
243
340
  * @param args.orderId - The ID of the Order
244
341
  * @param args.userNonce - The nonce of the user
@@ -246,42 +343,23 @@ class Trade {
246
343
  * @param options - RPC options
247
344
  *
248
345
  */
249
- closeOrder({ marketId, orderId, userNonce }, options) {
346
+ cancelOrder({ marketId, orderId, userNonce }, options) {
250
347
  return __awaiter(this, void 0, void 0, function* () {
251
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
252
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
348
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
349
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
253
350
  if (userNonce !== 0) {
254
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
255
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
351
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
352
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
256
353
  }
257
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
354
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelOrder(new bn_js_1.default(orderId)).accounts({
258
355
  signer: this.provider.publicKey,
259
356
  market: marketPDA,
260
- mint: constants_1.TRD_MINT,
261
- userTrade: userTradePDA
357
+ mint: this.mint,
358
+ userTrade: userTradePDA,
359
+ tokenProgram: (0, helpers_1.getTokenProgram)(this.mint)
262
360
  }), options);
263
361
  });
264
362
  }
265
- /**
266
- * Resolve Market
267
- * @param args.marketId - The ID of the Market
268
- * @param args.winningDirection - The Winning Direction of the Market
269
- *
270
- * @param options - RPC options
271
- *
272
- */
273
- resolveMarket({ marketId, winningDirection }, options) {
274
- return __awaiter(this, void 0, void 0, function* () {
275
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
276
- const method = this.program.methods
277
- .resolveMarket(winningDirection)
278
- .accounts({
279
- signer: this.provider.publicKey,
280
- market: marketPDA
281
- });
282
- return (0, sendTransactionWithOptions_1.default)(method, options);
283
- });
284
- }
285
363
  /**
286
364
  * Collect Remaining Liquidity
287
365
  * @param marketId - The ID of the market
@@ -291,11 +369,11 @@ class Trade {
291
369
  */
292
370
  collectRemainingLiquidity(marketId, options) {
293
371
  return __awaiter(this, void 0, void 0, function* () {
294
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
372
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
295
373
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.collectRemainingLiquidity().accounts({
296
374
  signer: this.provider.publicKey,
297
375
  market: marketPDA,
298
- mint: constants_1.TRD_MINT
376
+ mint: this.mint
299
377
  }), options);
300
378
  });
301
379
  }
@@ -310,17 +388,17 @@ class Trade {
310
388
  */
311
389
  payoutOrder({ marketId, orderId, userNonce }, options) {
312
390
  return __awaiter(this, void 0, void 0, function* () {
313
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
314
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
391
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
392
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
315
393
  if (userNonce !== 0) {
316
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
317
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
394
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
395
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
318
396
  }
319
397
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.payoutOrder(new bn_js_1.default(orderId)).accounts({
320
398
  signer: this.provider.publicKey,
321
399
  userTrade: userTradePDA,
322
400
  market: marketPDA,
323
- mint: constants_1.TRD_MINT
401
+ mint: this.mint
324
402
  }), options);
325
403
  });
326
404
  }
@@ -333,7 +411,7 @@ class Trade {
333
411
  */
334
412
  allowMarketToPayout(marketId, options) {
335
413
  return __awaiter(this, void 0, void 0, function* () {
336
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
414
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
337
415
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.allowMarketToPayout().accounts({
338
416
  signer: this.provider.publicKey,
339
417
  market: marketPDA
@@ -350,7 +428,7 @@ class Trade {
350
428
  createSubUserTrade(user, options) {
351
429
  return __awaiter(this, void 0, void 0, function* () {
352
430
  const userTrade = yield this.getUserTrade(user);
353
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
431
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
354
432
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.createSubUserTrade(subUserTradePDA).accounts({
355
433
  signer: this.provider.publicKey
356
434
  }), options);
@@ -360,18 +438,125 @@ class Trade {
360
438
  * Update Market
361
439
  * @param marketId - The ID of the market
362
440
  * @param marketEnd - The end time of the market
441
+ * @param winningDirection - The winning direction of the market
363
442
  *
364
443
  * @param options - RPC options
365
444
  *
366
445
  */
367
- updateMarket(marketId, marketEnd, options) {
446
+ updateMarket({ marketId, marketEnd, winningDirection }, options) {
368
447
  return __awaiter(this, void 0, void 0, function* () {
369
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
370
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.updateMarket(new bn_js_1.default(marketEnd)).accounts({
448
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
449
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods
450
+ .updateMarket({
451
+ marketEnd: marketEnd ? new bn_js_1.default(marketEnd) : undefined,
452
+ winningDirection
453
+ })
454
+ .accounts({
371
455
  signer: this.provider.publicKey,
372
456
  market: marketPDA
373
457
  }), options);
374
458
  });
375
459
  }
460
+ /**
461
+ * Create Customer
462
+ * @param args.id - The ID of the customer
463
+ * @param args.name - The name of the customer
464
+ * @param args.authority - The authority of the customer
465
+ * @param args.shadowStorage - The shadow storage of the customer
466
+ *
467
+ * @param options - RPC options
468
+ *
469
+ */
470
+ createCustomer({ id, name, authority, shadowStorage }, options) {
471
+ return __awaiter(this, void 0, void 0, function* () {
472
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods
473
+ .createCustomer({ id, name, authority, shadowStorage })
474
+ .accounts({
475
+ signer: this.provider.publicKey
476
+ }), options);
477
+ });
478
+ }
479
+ /**
480
+ * Open Order
481
+ * @param args.marketId - The ID of the Market
482
+ * @param args.amount - The amount of the Order
483
+ * @param args.direction - The direction of the Order
484
+ *
485
+ * @param options - RPC options
486
+ *
487
+ */
488
+ openOrder({ marketId, amount, direction }, options) {
489
+ return __awaiter(this, void 0, void 0, function* () {
490
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
491
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
492
+ const ixs = [];
493
+ const addressLookupTableAccounts = [];
494
+ let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
495
+ let myUserTrades = [];
496
+ myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
497
+ if (myUserTrades.length === 0) {
498
+ ixs.push(yield this.program.methods
499
+ .createUserTrade()
500
+ .accounts({
501
+ signer: this.provider.publicKey
502
+ })
503
+ .instruction());
504
+ }
505
+ if (myUserTrades.length > 0) {
506
+ try {
507
+ userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
508
+ }
509
+ catch (_a) {
510
+ const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
511
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
512
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
513
+ ixs.push(yield this.program.methods
514
+ .createSubUserTrade(subUserTradePDA)
515
+ .accounts({
516
+ signer: this.provider.publicKey
517
+ })
518
+ .instruction());
519
+ }
520
+ }
521
+ ixs.push(yield this.program.methods
522
+ .openOrder({
523
+ amount: new bn_js_1.default(amountInTRD),
524
+ direction: direction
525
+ })
526
+ .accounts({
527
+ signer: this.provider.publicKey,
528
+ market: marketPDA,
529
+ userTrade: userTradePDA,
530
+ mint: this.mint
531
+ })
532
+ .instruction());
533
+ return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
534
+ });
535
+ }
536
+ /**
537
+ * Close Order
538
+ * @param args.marketId - The ID of the Market
539
+ * @param args.orderId - The ID of the Order
540
+ * @param args.userNonce - The nonce of the user
541
+ *
542
+ * @param options - RPC options
543
+ *
544
+ */
545
+ closeOrder({ marketId, orderId, userNonce }, options) {
546
+ return __awaiter(this, void 0, void 0, function* () {
547
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
548
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
549
+ if (userNonce !== 0) {
550
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
551
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
552
+ }
553
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
554
+ signer: this.provider.publicKey,
555
+ market: marketPDA,
556
+ mint: this.mint,
557
+ userTrade: userTradePDA
558
+ }), options);
559
+ });
560
+ }
376
561
  }
377
562
  exports.default = Trade;