@triadxyz/triad-protocol 1.7.3-beta → 1.7.4-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,18 +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;
26
27
  this.mint = constants_1.TRD_MINT;
28
+ this.decimals = constants_1.TRD_DECIMALS; // We're using only TRD or USDC so we can use the same decimals 6
27
29
  }
28
30
  /**
29
31
  * Get All Markets
@@ -72,7 +74,7 @@ class Trade {
72
74
  */
73
75
  getMarketById(marketId) {
74
76
  return __awaiter(this, void 0, void 0, function* () {
75
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
77
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
76
78
  const response = yield this.program.account.marketV2.fetch(marketPDA);
77
79
  return (0, helpers_1.formatMarket)(response, marketPDA);
78
80
  });
@@ -96,10 +98,10 @@ class Trade {
96
98
  */
97
99
  getUserTrade(user, userNonce = 0) {
98
100
  return __awaiter(this, void 0, void 0, function* () {
99
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
101
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, user);
100
102
  if (userNonce !== 0) {
101
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userNonce);
102
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
103
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userNonce);
104
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
103
105
  }
104
106
  return this.program.account.userTrade.fetch(userTradePDA);
105
107
  });
@@ -110,11 +112,13 @@ class Trade {
110
112
  * @param args.startTime - start time
111
113
  * @param args.endTime - end time
112
114
  * @param args.question - question (max 80 characters)
115
+ * @param args.liquidityAtStart - liquidity at start
116
+ * @param args.payoutFee - payout fee (to add affiliate system)
113
117
  *
114
118
  * @param options - RPC options
115
119
  *
116
120
  */
117
- createMarket({ marketId, startTime, endTime, question, feeBps, customer, mint }, options) {
121
+ createMarket({ marketId, startTime, endTime, question, feeBps, customer, payoutFee, mint }, options) {
118
122
  return __awaiter(this, void 0, void 0, function* () {
119
123
  if (question.length > 80) {
120
124
  throw new Error('Question must be less than 80 characters');
@@ -125,7 +129,8 @@ class Trade {
125
129
  question: (0, helpers_1.encodeString)(question, 80),
126
130
  marketStart: new bn_js_1.default(startTime),
127
131
  marketEnd: new bn_js_1.default(endTime),
128
- feeBps
132
+ feeBps,
133
+ payoutFee
129
134
  })
130
135
  .accounts({
131
136
  signer: this.provider.publicKey,
@@ -135,53 +140,20 @@ class Trade {
135
140
  }), options);
136
141
  });
137
142
  }
138
- /**
139
- * Get User Trade Nonce With Slots
140
- * @param userTrades - User Trades
141
- *
142
- */
143
- getUserTradeNonceWithSlots(userTrades) {
144
- return __awaiter(this, void 0, void 0, function* () {
145
- let nonce = null;
146
- for (const userTrade of userTrades.reverse()) {
147
- if (nonce !== null) {
148
- break;
149
- }
150
- userTrade.orders.forEach((order) => {
151
- if (nonce !== null) {
152
- return;
153
- }
154
- let status = Object.keys(order.status)[0];
155
- if (status !== 'open') {
156
- nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
157
- }
158
- });
159
- }
160
- if (nonce === null) {
161
- throw new Error('No open orders found');
162
- }
163
- if (nonce === 0) {
164
- return (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
165
- }
166
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, nonce);
167
- const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
168
- return userTradePDA;
169
- });
170
- }
171
143
  /**
172
144
  * Open Order
173
145
  * @param args.marketId - The ID of the Market
174
146
  * @param args.amount - The amount of the Order
175
147
  * @param args.direction - The direction of the Order
176
- * @param args.token - The token of the Order
148
+ * @param args.mint - The mint of the Order
177
149
  *
178
150
  * @param options - RPC options
179
151
  *
180
152
  */
181
- openOrder({ marketId, amount, direction, token }, options) {
153
+ openOrder({ marketId, amount, direction, mint }, options) {
182
154
  return __awaiter(this, void 0, void 0, function* () {
183
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
184
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
155
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
156
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
185
157
  const ixs = [];
186
158
  const addressLookupTableAccounts = [];
187
159
  let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
@@ -201,8 +173,8 @@ class Trade {
201
173
  }
202
174
  catch (_a) {
203
175
  const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
204
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
205
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
176
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
177
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
206
178
  ixs.push(yield this.program.methods
207
179
  .createSubUserTrade(subUserTradePDA)
208
180
  .accounts({
@@ -211,21 +183,6 @@ class Trade {
211
183
  .instruction());
212
184
  }
213
185
  }
214
- if (token !== constants_1.TRD_MINT.toBase58()) {
215
- const { setupInstructions, swapIxs, addressLookupTableAccounts, trdAmount } = yield (0, swap_1.swap)({
216
- connection: this.provider.connection,
217
- wallet: this.provider.publicKey.toBase58(),
218
- inToken: token,
219
- amount
220
- });
221
- amountInTRD = trdAmount;
222
- if (swapIxs.length === 0) {
223
- return;
224
- }
225
- ixs.push(...setupInstructions);
226
- ixs.push(...swapIxs);
227
- addressLookupTableAccounts.push(...addressLookupTableAccounts);
228
- }
229
186
  ixs.push(yield this.program.methods
230
187
  .openOrder({
231
188
  amount: new bn_js_1.default(amountInTRD),
@@ -235,7 +192,7 @@ class Trade {
235
192
  signer: this.provider.publicKey,
236
193
  market: marketPDA,
237
194
  userTrade: userTradePDA,
238
- mint: this.mint
195
+ mint
239
196
  })
240
197
  .instruction());
241
198
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
@@ -252,11 +209,11 @@ class Trade {
252
209
  */
253
210
  closeOrder({ marketId, orderId, userNonce }, options) {
254
211
  return __awaiter(this, void 0, void 0, function* () {
255
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
256
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
212
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
213
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
257
214
  if (userNonce !== 0) {
258
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
259
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
215
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
216
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
260
217
  }
261
218
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
262
219
  signer: this.provider.publicKey,
@@ -276,7 +233,7 @@ class Trade {
276
233
  */
277
234
  resolveMarket({ marketId, winningDirection }, options) {
278
235
  return __awaiter(this, void 0, void 0, function* () {
279
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
236
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
280
237
  const method = this.program.methods
281
238
  .resolveMarket(winningDirection)
282
239
  .accounts({
@@ -295,7 +252,7 @@ class Trade {
295
252
  */
296
253
  collectRemainingLiquidity(marketId, options) {
297
254
  return __awaiter(this, void 0, void 0, function* () {
298
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
255
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
299
256
  const market = yield this.getMarketByAddress(marketPDA);
300
257
  if (market.isAllowedToPayout) {
301
258
  return;
@@ -318,11 +275,11 @@ class Trade {
318
275
  */
319
276
  payoutOrder({ marketId, orderId, userNonce, mint }, options) {
320
277
  return __awaiter(this, void 0, void 0, function* () {
321
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
322
- let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
278
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
279
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
323
280
  if (userNonce !== 0) {
324
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
325
- userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
281
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
282
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
326
283
  }
327
284
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.payoutOrder(new bn_js_1.default(orderId)).accounts({
328
285
  signer: this.provider.publicKey,
@@ -341,7 +298,7 @@ class Trade {
341
298
  */
342
299
  allowMarketToPayout(marketId, options) {
343
300
  return __awaiter(this, void 0, void 0, function* () {
344
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
301
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
345
302
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.allowMarketToPayout().accounts({
346
303
  signer: this.provider.publicKey,
347
304
  market: marketPDA
@@ -358,7 +315,7 @@ class Trade {
358
315
  createSubUserTrade(user, options) {
359
316
  return __awaiter(this, void 0, void 0, function* () {
360
317
  const userTrade = yield this.getUserTrade(user);
361
- const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
318
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
362
319
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.createSubUserTrade(subUserTradePDA).accounts({
363
320
  signer: this.provider.publicKey
364
321
  }), options);
@@ -374,7 +331,7 @@ class Trade {
374
331
  */
375
332
  updateMarket(marketId, marketEnd, options) {
376
333
  return __awaiter(this, void 0, void 0, function* () {
377
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
334
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
378
335
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.updateMarket(new bn_js_1.default(marketEnd)).accounts({
379
336
  signer: this.provider.publicKey,
380
337
  market: marketPDA
@@ -390,12 +347,305 @@ class Trade {
390
347
  * @param options - RPC options
391
348
  *
392
349
  */
393
- createCustomer({ id, name, authority }, options) {
350
+ createCustomer({ id, name, authority, feeRecipient }, options) {
351
+ return __awaiter(this, void 0, void 0, function* () {
352
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods
353
+ .createCustomer({ id, name, authority, feeRecipient })
354
+ .accounts({
355
+ signer: this.provider.publicKey
356
+ }), options);
357
+ });
358
+ }
359
+ /**
360
+ * Get User Trade Nonce With Slots
361
+ * @param userTrades - User Trades
362
+ *
363
+ */
364
+ getUserTradeNonceWithSlots(userTrades) {
365
+ return __awaiter(this, void 0, void 0, function* () {
366
+ let nonce = null;
367
+ for (const userTrade of userTrades.reverse()) {
368
+ if (nonce !== null) {
369
+ break;
370
+ }
371
+ let freeSlots = 0;
372
+ userTrade.orders.forEach((order) => {
373
+ if (nonce !== null) {
374
+ return;
375
+ }
376
+ if (order.orderStatus !== trade_1.OrderStatus.OPEN &&
377
+ order.orderStatus !== trade_1.OrderStatus.WAITING &&
378
+ freeSlots >= 2) {
379
+ nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
380
+ }
381
+ if (order.orderStatus !== trade_1.OrderStatus.OPEN &&
382
+ order.orderStatus !== trade_1.OrderStatus.WAITING) {
383
+ freeSlots += 1;
384
+ }
385
+ });
386
+ }
387
+ if (nonce === null) {
388
+ throw new Error('No open orders found');
389
+ }
390
+ if (nonce === 0) {
391
+ return (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
392
+ }
393
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, nonce);
394
+ const userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
395
+ return userTradePDA;
396
+ });
397
+ }
398
+ getUserTradeIxs() {
399
+ return __awaiter(this, void 0, void 0, function* () {
400
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
401
+ const ixs = [];
402
+ let myUserTrades = [];
403
+ myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
404
+ if (myUserTrades.length === 0) {
405
+ ixs.push(yield this.program.methods
406
+ .createUserTrade()
407
+ .accounts({
408
+ signer: this.provider.publicKey
409
+ })
410
+ .instruction());
411
+ }
412
+ if (myUserTrades.length > 0) {
413
+ try {
414
+ userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
415
+ // Check if we need to create a new sub user trade
416
+ // This happens when all existing slots have open orders
417
+ const allSlotsHaveOpenOrders = myUserTrades.every((trade) => trade.orders.some((order) => order.orderStatus === trade_1.OrderStatus.OPEN ||
418
+ order.orderStatus === trade_1.OrderStatus.WAITING));
419
+ if (allSlotsHaveOpenOrders) {
420
+ // Find the main user trade to get the current nonce
421
+ const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
422
+ if (!mainUserTrade) {
423
+ throw new Error('No main user trade found');
424
+ }
425
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
426
+ ixs.push(yield this.program.methods
427
+ .createSubUserTrade(subUserTradePDA)
428
+ .accounts({
429
+ signer: this.provider.publicKey
430
+ })
431
+ .instruction());
432
+ }
433
+ }
434
+ catch (error) {
435
+ const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
436
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
437
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
438
+ ixs.push(yield this.program.methods
439
+ .createSubUserTrade(subUserTradePDA)
440
+ .accounts({
441
+ signer: this.provider.publicKey
442
+ })
443
+ .instruction());
444
+ }
445
+ }
446
+ return {
447
+ userTradePDA,
448
+ ixs
449
+ };
450
+ });
451
+ }
452
+ /**
453
+ * Place Order
454
+ * @param args.marketId - The ID of the Market
455
+ * @param args.amount - The amount of the Order
456
+ * @param args.direction - The direction of the Order
457
+ * @param args.orderSide - The side of the Order
458
+ * @param args.mint - The mint of the Order
459
+ * @param args.price - The price of the Order
460
+ *
461
+ * @param options - RPC options
462
+ *
463
+ */
464
+ placeOrder({ marketId, amount, direction, orderSide, mint, price, bidOrderId }, options) {
394
465
  return __awaiter(this, void 0, void 0, function* () {
395
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.createCustomer({ id, name, authority }).accounts({
466
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
467
+ const ixs = [];
468
+ const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
469
+ ixs.push(...userTradeIxs);
470
+ if (Object.keys(orderSide)[0] === 'bid') {
471
+ ixs.push(yield this.program.methods
472
+ .placeBidOrder({
473
+ amount: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
474
+ price: new bn_js_1.default(price * Math.pow(10, this.decimals)),
475
+ orderDirection: direction
476
+ })
477
+ .accounts({
478
+ signer: this.provider.publicKey,
479
+ market: marketPDA,
480
+ userTrade: userTradePDA,
481
+ mint,
482
+ tokenProgram: (0, helpers_1.getTokenProgram)(mint)
483
+ })
484
+ .instruction());
485
+ }
486
+ if (Object.keys(orderSide)[0] === 'ask' && bidOrderId) {
487
+ ixs.push(yield this.program.methods
488
+ .placeAskOrder({
489
+ shares: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
490
+ price: new bn_js_1.default(price * Math.pow(10, this.decimals)),
491
+ bidOrderId: new bn_js_1.default(bidOrderId),
492
+ orderDirection: direction
493
+ })
494
+ .accounts({
495
+ signer: this.provider.publicKey,
496
+ market: marketPDA,
497
+ userTrade: userTradePDA
498
+ })
499
+ .instruction());
500
+ }
501
+ return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
502
+ });
503
+ }
504
+ /**
505
+ * Cancel Order
506
+ * @param args.marketId - The ID of the Market
507
+ * @param args.orderId - The ID of the Order
508
+ * @param args.userNonce - The nonce of the user
509
+ * @param args.orderSide - The side of the Order
510
+ *
511
+ * @param options - RPC options
512
+ */
513
+ cancelOrder({ marketId, orderId, userNonce, orderSide, mint, userNonceBidOrder }, options) {
514
+ return __awaiter(this, void 0, void 0, function* () {
515
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
516
+ let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
517
+ let bidUserTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
518
+ if (userNonce !== 0) {
519
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
520
+ userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
521
+ }
522
+ if (userNonceBidOrder !== null && userNonceBidOrder !== 0) {
523
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonceBidOrder);
524
+ bidUserTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
525
+ }
526
+ if (orderSide === trade_1.OrderSide.BID) {
527
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelBidOrder(new bn_js_1.default(orderId)).accounts({
528
+ signer: this.provider.publicKey,
529
+ market: marketPDA,
530
+ userTrade: userTradePDA,
531
+ mint
532
+ }), options);
533
+ }
534
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelAskOrder(new bn_js_1.default(orderId)).accounts({
535
+ signer: this.provider.publicKey,
536
+ market: marketPDA,
537
+ userTrade: userTradePDA,
538
+ bidUserTrade: bidUserTradePDA
539
+ }), options);
540
+ });
541
+ }
542
+ /**
543
+ * Update Customer Fee
544
+ * @param args.id - The ID of the Customer
545
+ * @param args.feeRecipient - The fee account
546
+ *
547
+ * @param options - RPC options
548
+ */
549
+ updateCustomerFee({ id, feeRecipient }, options) {
550
+ return __awaiter(this, void 0, void 0, function* () {
551
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.updateCustomerFee({ id, feeRecipient }).accounts({
396
552
  signer: this.provider.publicKey
397
553
  }), options);
398
554
  });
399
555
  }
556
+ /**
557
+ * Market Bid Order
558
+ * @param args.marketId - The ID of the Market
559
+ * @param args.amount - The amount of the Order
560
+ * @param args.direction - The direction of the Order
561
+ * @param args.mint - The mint of the Order
562
+ *
563
+ * @param options - RPC options
564
+ */
565
+ marketBidOrder({ marketId, amount, direction, mint }, options) {
566
+ return __awaiter(this, void 0, void 0, function* () {
567
+ const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
568
+ const ixs = [];
569
+ const { userTradePDA: buyerTrade, ixs: userTradeIxs } = yield this.getUserTradeIxs();
570
+ // TODO: Get dynamic until fill all order
571
+ const sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
572
+ ixs.push(...userTradeIxs);
573
+ ixs.push(yield this.program.methods
574
+ .marketBidOrder({
575
+ amount: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
576
+ direction
577
+ })
578
+ .accounts({
579
+ signer: this.provider.publicKey,
580
+ market: marketPDA,
581
+ buyerTrade,
582
+ sellerTrade: sellerTradePDA,
583
+ mint
584
+ })
585
+ .instruction());
586
+ return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
587
+ });
588
+ }
589
+ /**
590
+ * Get Orders By Market ID
591
+ * @param marketId - The ID of the market
592
+ *
593
+ */
594
+ getOrderBook(marketId) {
595
+ return __awaiter(this, void 0, void 0, function* () {
596
+ const marketIdBytes = bytes_1.bs58.encode(new bn_js_1.default(marketId).toArrayLike(Buffer, 'le', 8));
597
+ const memcmpFilters = Array.from({ length: 10 }).map((_, index) => ({
598
+ memcmp: {
599
+ offset: 8 + // discriminator
600
+ 1 + // bump
601
+ 32 + // authority
602
+ 8 + // total_deposits
603
+ 8 + // total_withdraws
604
+ 8 + // opened_orders
605
+ // Per order: index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 28)
606
+ index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 1 + 27) +
607
+ // Then offset to market_id: 8 + 8 + 8
608
+ (8 + 8 + 8),
609
+ bytes: marketIdBytes
610
+ }
611
+ }));
612
+ const allResponses = yield Promise.all(memcmpFilters.map((filter) => this.program.account.userTrade.all([filter])));
613
+ const uniqueResponses = Array.from(new Map(allResponses.flat().map((item) => [item.publicKey.toString(), item])).values());
614
+ const userTrades = uniqueResponses.map(({ account, publicKey }) => (0, helpers_1.formatUserTrade)(account, publicKey));
615
+ const orders = userTrades.flatMap((userTrade) => userTrade.orders.map((order) => (Object.assign({}, order))));
616
+ const filteredOrders = orders.filter((order) => order.marketId === marketId.toString() &&
617
+ order.orderType === trade_1.OrderType.LIMIT &&
618
+ order.orderStatus === trade_1.OrderStatus.WAITING);
619
+ const orderBook = {
620
+ hype: {
621
+ bid: [],
622
+ ask: []
623
+ },
624
+ flop: {
625
+ bid: [],
626
+ ask: []
627
+ }
628
+ };
629
+ filteredOrders.forEach((order) => {
630
+ if (order.orderSide === trade_1.OrderSide.BID &&
631
+ order.orderDirection === trade_1.OrderDirection.HYPE) {
632
+ orderBook.hype.bid.push(order);
633
+ return;
634
+ }
635
+ if (order.orderSide === trade_1.OrderSide.ASK &&
636
+ order.orderDirection === trade_1.OrderDirection.HYPE) {
637
+ orderBook.hype.ask.push(order);
638
+ return;
639
+ }
640
+ if (order.orderSide === trade_1.OrderSide.BID &&
641
+ order.orderDirection === trade_1.OrderDirection.FLOP) {
642
+ orderBook.flop.bid.push(order);
643
+ return;
644
+ }
645
+ orderBook.flop.ask.push(order);
646
+ });
647
+ return orderBook;
648
+ });
649
+ }
400
650
  }
401
651
  exports.default = Trade;