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

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,20 +12,17 @@ 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");
16
15
  const bn_js_1 = __importDefault(require("bn.js"));
17
16
  const constants_1 = require("./utils/constants");
18
17
  const helpers_1 = require("./utils/helpers");
19
- const trade_2 = require("./utils/pda/trade");
18
+ const trade_1 = require("./utils/pda/trade");
20
19
  const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
21
20
  const sendTransactionWithOptions_1 = __importDefault(require("./utils/sendTransactionWithOptions"));
22
- const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
21
+ const swap_1 = require("./utils/swap");
23
22
  class Trade {
24
23
  constructor(program, provider) {
25
24
  this.program = program;
26
25
  this.provider = provider;
27
- this.mint = constants_1.USDC_MINT_DEVNET;
28
- this.decimals = 6;
29
26
  }
30
27
  /**
31
28
  * Get All Markets
@@ -67,67 +64,6 @@ class Trade {
67
64
  return orders;
68
65
  });
69
66
  }
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
- }
131
67
  /**
132
68
  * Get Market By ID
133
69
  * @param marketId - The ID of the market
@@ -135,7 +71,7 @@ class Trade {
135
71
  */
136
72
  getMarketById(marketId) {
137
73
  return __awaiter(this, void 0, void 0, function* () {
138
- const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
74
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
139
75
  const response = yield this.program.account.marketV2.fetch(marketPDA);
140
76
  return (0, helpers_1.formatMarket)(response, marketPDA);
141
77
  });
@@ -159,16 +95,16 @@ class Trade {
159
95
  */
160
96
  getUserTrade(user, userNonce = 0) {
161
97
  return __awaiter(this, void 0, void 0, function* () {
162
- let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, user);
98
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
163
99
  if (userNonce !== 0) {
164
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userNonce);
165
- userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
100
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userNonce);
101
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
166
102
  }
167
103
  return this.program.account.userTrade.fetch(userTradePDA);
168
104
  });
169
105
  }
170
106
  /**
171
- * Create Market
107
+ * Initialize Market
172
108
  * @param args.marketId - new markert id - length + 1
173
109
  * @param args.startTime - start time
174
110
  * @param args.endTime - end time
@@ -177,24 +113,21 @@ class Trade {
177
113
  * @param options - RPC options
178
114
  *
179
115
  */
180
- createMarket({ marketId, startTime, endTime, question, feeBps, customer }, options) {
116
+ initializeMarket({ marketId, startTime, endTime, question }, options) {
181
117
  return __awaiter(this, void 0, void 0, function* () {
182
118
  if (question.length > 80) {
183
119
  throw new Error('Question must be less than 80 characters');
184
120
  }
185
121
  return (0, sendTransactionWithOptions_1.default)(this.program.methods
186
- .createMarket({
122
+ .initializeMarket({
187
123
  marketId: new bn_js_1.default(marketId),
188
124
  question: (0, helpers_1.encodeString)(question, 80),
189
125
  marketStart: new bn_js_1.default(startTime),
190
- marketEnd: new bn_js_1.default(endTime),
191
- feeBps
126
+ marketEnd: new bn_js_1.default(endTime)
192
127
  })
193
128
  .accounts({
194
129
  signer: this.provider.publicKey,
195
- mint: this.mint,
196
- tokenProgram: (0, helpers_1.getTokenProgram)(this.mint),
197
- customer
130
+ mint: constants_1.TRD_MINT
198
131
  }), options);
199
132
  });
200
133
  }
@@ -210,51 +143,44 @@ class Trade {
210
143
  if (nonce !== null) {
211
144
  break;
212
145
  }
213
- let freeSlots = 0;
214
146
  userTrade.orders.forEach((order) => {
215
147
  if (nonce !== null) {
216
148
  return;
217
149
  }
218
- if (order.status !== trade_1.OrderStatus.OPEN &&
219
- order.status !== trade_1.OrderStatus.WAITING &&
220
- freeSlots >= 2) {
150
+ let status = Object.keys(order.status)[0];
151
+ if (status !== 'open') {
221
152
  nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
222
153
  }
223
- if (order.status !== trade_1.OrderStatus.OPEN &&
224
- order.status !== trade_1.OrderStatus.WAITING) {
225
- freeSlots += 1;
226
- }
227
154
  });
228
155
  }
229
156
  if (nonce === null) {
230
157
  throw new Error('No open orders found');
231
158
  }
232
159
  if (nonce === 0) {
233
- return (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
160
+ return (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
234
161
  }
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);
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);
237
164
  return userTradePDA;
238
165
  });
239
166
  }
240
167
  /**
241
- * Place Order
168
+ * Open Order
242
169
  * @param args.marketId - The ID of the Market
243
170
  * @param args.amount - The amount of the Order
244
171
  * @param args.direction - The direction of 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
172
+ * @param args.token - The token to use for the Order
248
173
  *
249
174
  * @param options - RPC options
250
175
  *
251
176
  */
252
- placeOrder({ marketId, amount, direction, orderSide, mint, price }, options) {
177
+ openOrder({ marketId, amount, direction, token }, options) {
253
178
  return __awaiter(this, void 0, void 0, function* () {
254
- const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
255
- let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
179
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
180
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
256
181
  const ixs = [];
257
182
  const addressLookupTableAccounts = [];
183
+ let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
258
184
  let myUserTrades = [];
259
185
  myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
260
186
  if (myUserTrades.length === 0) {
@@ -268,31 +194,11 @@ class Trade {
268
194
  if (myUserTrades.length > 0) {
269
195
  try {
270
196
  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
- }
290
197
  }
291
- catch (error) {
292
- console.log(error);
198
+ catch (_a) {
293
199
  const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
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);
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);
296
202
  ixs.push(yield this.program.methods
297
203
  .createSubUserTrade(subUserTradePDA)
298
204
  .accounts({
@@ -301,41 +207,38 @@ class Trade {
301
207
  .instruction());
302
208
  }
303
209
  }
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());
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);
333
224
  }
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());
334
237
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
335
238
  });
336
239
  }
337
240
  /**
338
- * Cancel Order
241
+ * Close Order
339
242
  * @param args.marketId - The ID of the Market
340
243
  * @param args.orderId - The ID of the Order
341
244
  * @param args.userNonce - The nonce of the user
@@ -343,23 +246,42 @@ class Trade {
343
246
  * @param options - RPC options
344
247
  *
345
248
  */
346
- cancelOrder({ marketId, orderId, userNonce }, options) {
249
+ closeOrder({ marketId, orderId, userNonce }, options) {
347
250
  return __awaiter(this, void 0, void 0, function* () {
348
- const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
349
- let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
251
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
252
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
350
253
  if (userNonce !== 0) {
351
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
352
- userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
254
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
255
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
353
256
  }
354
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelOrder(new bn_js_1.default(orderId)).accounts({
257
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
355
258
  signer: this.provider.publicKey,
356
259
  market: marketPDA,
357
- mint: this.mint,
358
- userTrade: userTradePDA,
359
- tokenProgram: (0, helpers_1.getTokenProgram)(this.mint)
260
+ mint: constants_1.TRD_MINT,
261
+ userTrade: userTradePDA
360
262
  }), options);
361
263
  });
362
264
  }
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
+ }
363
285
  /**
364
286
  * Collect Remaining Liquidity
365
287
  * @param marketId - The ID of the market
@@ -369,11 +291,11 @@ class Trade {
369
291
  */
370
292
  collectRemainingLiquidity(marketId, options) {
371
293
  return __awaiter(this, void 0, void 0, function* () {
372
- const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
294
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
373
295
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.collectRemainingLiquidity().accounts({
374
296
  signer: this.provider.publicKey,
375
297
  market: marketPDA,
376
- mint: this.mint
298
+ mint: constants_1.TRD_MINT
377
299
  }), options);
378
300
  });
379
301
  }
@@ -388,17 +310,17 @@ class Trade {
388
310
  */
389
311
  payoutOrder({ marketId, orderId, userNonce }, options) {
390
312
  return __awaiter(this, void 0, void 0, function* () {
391
- const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
392
- let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
313
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
314
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
393
315
  if (userNonce !== 0) {
394
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
395
- userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
316
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
317
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
396
318
  }
397
319
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.payoutOrder(new bn_js_1.default(orderId)).accounts({
398
320
  signer: this.provider.publicKey,
399
321
  userTrade: userTradePDA,
400
322
  market: marketPDA,
401
- mint: this.mint
323
+ mint: constants_1.TRD_MINT
402
324
  }), options);
403
325
  });
404
326
  }
@@ -411,7 +333,7 @@ class Trade {
411
333
  */
412
334
  allowMarketToPayout(marketId, options) {
413
335
  return __awaiter(this, void 0, void 0, function* () {
414
- const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
336
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
415
337
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.allowMarketToPayout().accounts({
416
338
  signer: this.provider.publicKey,
417
339
  market: marketPDA
@@ -428,7 +350,7 @@ class Trade {
428
350
  createSubUserTrade(user, options) {
429
351
  return __awaiter(this, void 0, void 0, function* () {
430
352
  const userTrade = yield this.getUserTrade(user);
431
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
353
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
432
354
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.createSubUserTrade(subUserTradePDA).accounts({
433
355
  signer: this.provider.publicKey
434
356
  }), options);
@@ -438,122 +360,42 @@ class Trade {
438
360
  * Update Market
439
361
  * @param marketId - The ID of the market
440
362
  * @param marketEnd - The end time of the market
441
- * @param winningDirection - The winning direction of the market
442
363
  *
443
364
  * @param options - RPC options
444
365
  *
445
366
  */
446
- updateMarket({ marketId, marketEnd, winningDirection }, options) {
367
+ updateMarket(marketId, marketEnd, options) {
447
368
  return __awaiter(this, void 0, void 0, function* () {
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({
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({
455
371
  signer: this.provider.publicKey,
456
372
  market: marketPDA
457
373
  }), options);
458
374
  });
459
375
  }
460
376
  /**
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
377
+ * Force Cancel Order
538
378
  * @param args.marketId - The ID of the Market
379
+ * @param args.user - The user to force cancel the order for
539
380
  * @param args.orderId - The ID of the Order
540
381
  * @param args.userNonce - The nonce of the user
541
382
  *
542
383
  * @param options - RPC options
543
384
  *
544
385
  */
545
- closeOrder({ marketId, orderId, userNonce }, options) {
386
+ forceCancelOrder({ marketId, user, orderId, userNonce }, options) {
546
387
  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);
388
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
389
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
549
390
  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);
391
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userNonce);
392
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
552
393
  }
553
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
394
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.forceCancelOrder(new bn_js_1.default(orderId)).accounts({
554
395
  signer: this.provider.publicKey,
396
+ user,
555
397
  market: marketPDA,
556
- mint: this.mint,
398
+ mint: constants_1.TRD_MINT,
557
399
  userTrade: userTradePDA
558
400
  }), options);
559
401
  });