@triadxyz/triad-protocol 3.1.1-beta → 3.1.3-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/index.js CHANGED
@@ -38,23 +38,23 @@ const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersione
38
38
  const swap_1 = require("./utils/swap");
39
39
  const stake_1 = __importDefault(require("./stake"));
40
40
  const poseidon_1 = __importDefault(require("./poseidon"));
41
+ const spl_token_1 = require("@solana/spl-token");
41
42
  __exportStar(require("./types"), exports);
42
- __exportStar(require("./types/stake"), exports);
43
- __exportStar(require("./types/poseidon"), exports);
44
43
  __exportStar(require("./utils/helpers"), exports);
45
44
  class TriadProtocolClient {
46
- constructor(connection, wallet, commitment = 'confirmed') {
47
- this.decimals = constants_1.TRD_DECIMALS; // We're using only TRD or USDC so we can use the same decimals 6
48
- this.provider = new anchor_1.AnchorProvider(connection, wallet, {
49
- commitment
45
+ constructor(connection, wallet, rpcOptions) {
46
+ this.connection = connection;
47
+ this.wallet = wallet;
48
+ this.rpcOptions = rpcOptions;
49
+ this.provider = new anchor_1.AnchorProvider(this.connection, this.wallet, {
50
+ commitment: this.rpcOptions.commitment || 'confirmed'
50
51
  });
51
52
  this.program = new anchor_1.Program(idl_triad_protocol_json_1.default, this.provider);
52
- this.stake = new stake_1.default(this.program);
53
- this.poseidon = new poseidon_1.default(this.program);
53
+ this.stake = new stake_1.default(this.program, this.rpcOptions);
54
+ this.poseidon = new poseidon_1.default(this.program, this.rpcOptions);
54
55
  }
55
56
  /**
56
57
  * Get All Pools
57
- *
58
58
  */
59
59
  getAllPools() {
60
60
  return __awaiter(this, void 0, void 0, function* () {
@@ -64,7 +64,6 @@ class TriadProtocolClient {
64
64
  }
65
65
  /**
66
66
  * Get All Markets
67
- *
68
67
  */
69
68
  getAllMarkets() {
70
69
  return __awaiter(this, void 0, void 0, function* () {
@@ -75,7 +74,6 @@ class TriadProtocolClient {
75
74
  /**
76
75
  * Get My User Trades from a user authority
77
76
  * @param wallet - User wallet PublicKey
78
- *
79
77
  */
80
78
  getMyUserTrades(wallet) {
81
79
  return __awaiter(this, void 0, void 0, function* () {
@@ -93,7 +91,6 @@ class TriadProtocolClient {
93
91
  /**
94
92
  * Get User Orders
95
93
  * @param wallet - User wallet PublicKey
96
- *
97
94
  */
98
95
  getUserOrders(wallet) {
99
96
  return __awaiter(this, void 0, void 0, function* () {
@@ -105,7 +102,6 @@ class TriadProtocolClient {
105
102
  * Get User Orders By Market ID
106
103
  * @param wallet - User wallet PublicKey
107
104
  * @param marketId - The ID of the market
108
- *
109
105
  */
110
106
  getUserOrdersByMarketId(wallet, marketId) {
111
107
  return __awaiter(this, void 0, void 0, function* () {
@@ -115,7 +111,6 @@ class TriadProtocolClient {
115
111
  }
116
112
  /**
117
113
  * Get User Book Orders
118
- *
119
114
  */
120
115
  getUserBookOrders(wallet, marketId) {
121
116
  return __awaiter(this, void 0, void 0, function* () {
@@ -130,7 +125,6 @@ class TriadProtocolClient {
130
125
  /**
131
126
  * Get Pool By ID
132
127
  * @param poolId - The ID of the pool
133
- *
134
128
  */
135
129
  getPoolById(poolId) {
136
130
  return __awaiter(this, void 0, void 0, function* () {
@@ -142,7 +136,6 @@ class TriadProtocolClient {
142
136
  /**
143
137
  * Get Market By ID
144
138
  * @param marketId - The ID of the market
145
- *
146
139
  */
147
140
  getMarketById(marketId) {
148
141
  return __awaiter(this, void 0, void 0, function* () {
@@ -154,7 +147,6 @@ class TriadProtocolClient {
154
147
  /**
155
148
  * Get Market By Address
156
149
  * @param marketAddress - The address of the market
157
- *
158
150
  */
159
151
  getMarketByAddress(marketAddress) {
160
152
  return __awaiter(this, void 0, void 0, function* () {
@@ -165,7 +157,6 @@ class TriadProtocolClient {
165
157
  /**
166
158
  * Get Costumer By Wallet Address
167
159
  * @param wallet - The wallet address of the customer
168
- *
169
160
  */
170
161
  getCustomerByWallet(wallet) {
171
162
  return __awaiter(this, void 0, void 0, function* () {
@@ -183,7 +174,6 @@ class TriadProtocolClient {
183
174
  /**
184
175
  * Get Customer By ID
185
176
  * @param customerId - The ID of the customer
186
- *
187
177
  */
188
178
  getCustomerById(customerId) {
189
179
  return __awaiter(this, void 0, void 0, function* () {
@@ -194,18 +184,15 @@ class TriadProtocolClient {
194
184
  }
195
185
  /**
196
186
  * Get Current Market ID
197
- *
198
187
  */
199
188
  nextMarketId() {
200
189
  return __awaiter(this, void 0, void 0, function* () {
201
190
  const markets = yield this.program.account.marketV2.all();
202
- // 10 is the initial market id
203
191
  return markets.length + 10;
204
192
  });
205
193
  }
206
194
  /**
207
195
  * Get Next Customer ID
208
- *
209
196
  */
210
197
  nextCustomerId() {
211
198
  return __awaiter(this, void 0, void 0, function* () {
@@ -215,7 +202,6 @@ class TriadProtocolClient {
215
202
  }
216
203
  /**
217
204
  * Get Next Pool ID
218
- *
219
205
  */
220
206
  nextPoolId() {
221
207
  return __awaiter(this, void 0, void 0, function* () {
@@ -227,7 +213,6 @@ class TriadProtocolClient {
227
213
  * Get User Trade PDA
228
214
  * @param wallet - User wallet PublicKey
229
215
  * @param userNonce - The nonce of the user
230
- *
231
216
  */
232
217
  getUserPDA(wallet, userNonce = 0) {
233
218
  let userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, wallet);
@@ -241,7 +226,6 @@ class TriadProtocolClient {
241
226
  * Get User Trade
242
227
  * @param wallet - User wallet PublicKey
243
228
  * @param userNonce - The nonce of the user
244
- *
245
229
  */
246
230
  getUserTrade(wallet, userNonce = 0) {
247
231
  return __awaiter(this, void 0, void 0, function* () {
@@ -251,17 +235,17 @@ class TriadProtocolClient {
251
235
  }
252
236
  /**
253
237
  * Create Market
254
- * @param args.marketId - new markert id - length + 1
255
- * @param args.startTime - start time
256
- * @param args.endTime - end time
257
- * @param args.question - question (max 80 characters)
258
- * @param args.liquidityAtStart - liquidity at start
259
- * @param args.payoutFee - payout fee (to add affiliate system)
260
- *
261
- * @param options - RPC options
262
- *
238
+ * @param args.markets - Array of markets to create
239
+ * @param args.markets.marketId - Market ID
240
+ * @param args.markets.startTime - start time
241
+ * @param args.markets.endTime - end time
242
+ * @param args.markets.question - question (max 80 characters)
243
+ * @param args.markets.liquidityAtStart - liquidity at start
244
+ * @param args.markets.payoutFee - payout fee (to add affiliate system)
245
+ * @param args.customer - The customer of the market
246
+ * @param args.poolId - The ID of the pool
263
247
  */
264
- createMarket({ markets, customer, mint, poolId }, options) {
248
+ createMarket({ markets, customer, poolId }) {
265
249
  return __awaiter(this, void 0, void 0, function* () {
266
250
  const ixs = [];
267
251
  let poolPDA = null;
@@ -283,7 +267,7 @@ class TriadProtocolClient {
283
267
  })
284
268
  .accounts({
285
269
  signer: this.program.provider.publicKey,
286
- tokenProgram: (0, helpers_1.getTokenProgram)(mint),
270
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
287
271
  pool: poolPDA,
288
272
  customer
289
273
  })
@@ -296,7 +280,7 @@ class TriadProtocolClient {
296
280
  })
297
281
  .instruction());
298
282
  }
299
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
283
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
300
284
  });
301
285
  }
302
286
  /**
@@ -304,10 +288,8 @@ class TriadProtocolClient {
304
288
  * @param poolId - The ID of the pool
305
289
  * @param question - The question of the pool
306
290
  * @param markets - The markets of the pool
307
- *
308
- * @param options - RPC options
309
291
  */
310
- createPool({ poolId, question, markets, mint, customer, startTime, endTime, feeBps, payoutFee, isFast }, options) {
292
+ createPool({ poolId, question, markets, customer, startTime, endTime, feeBps, payoutFee, isFast }) {
311
293
  return __awaiter(this, void 0, void 0, function* () {
312
294
  if (question.length > 80) {
313
295
  throw new Error('Pool question must be less than 80 characters');
@@ -340,7 +322,7 @@ class TriadProtocolClient {
340
322
  })
341
323
  .accounts({
342
324
  signer: this.program.provider.publicKey,
343
- tokenProgram: (0, helpers_1.getTokenProgram)(mint),
325
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
344
326
  pool: poolPDA,
345
327
  customer
346
328
  })
@@ -353,7 +335,7 @@ class TriadProtocolClient {
353
335
  })
354
336
  .instruction());
355
337
  }
356
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
338
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
357
339
  });
358
340
  }
359
341
  /**
@@ -363,11 +345,8 @@ class TriadProtocolClient {
363
345
  * @param args.direction - The direction of the Order
364
346
  * @param args.mint - The mint of the Order
365
347
  * @param args.token - The token to use for the Order
366
- *
367
- * @param options - RPC options
368
- *
369
348
  */
370
- openOrder({ marketId, amount, direction, mint, token }, options) {
349
+ openOrder({ marketId, amount, direction, mint, token }) {
371
350
  return __awaiter(this, void 0, void 0, function* () {
372
351
  const ixs = [];
373
352
  const addressLookupTableAccounts = [];
@@ -375,7 +354,7 @@ class TriadProtocolClient {
375
354
  if (userTradeIxs.length > 0) {
376
355
  ixs.push(...userTradeIxs);
377
356
  }
378
- let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
357
+ let amountInTRD = amount * Math.pow(10, constants_1.BASE_DECIMALS);
379
358
  if (token !== constants_1.TRD_MINT.toBase58()) {
380
359
  const { setupInstructions, swapIxs, addressLookupTableAccounts, outAmount } = yield (0, swap_1.swap)({
381
360
  connection: this.program.provider.connection,
@@ -404,7 +383,7 @@ class TriadProtocolClient {
404
383
  mint
405
384
  })
406
385
  .instruction());
407
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
386
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions, addressLookupTableAccounts);
408
387
  });
409
388
  }
410
389
  /**
@@ -412,11 +391,8 @@ class TriadProtocolClient {
412
391
  * @param args.marketId - The ID of the Market
413
392
  * @param args.orderId - The ID of the Order
414
393
  * @param args.userNonce - The nonce of the user
415
- *
416
- * @param options - RPC options
417
- *
418
394
  */
419
- closeOrder({ marketId, orderId, userNonce }, options) {
395
+ closeOrder({ marketId, orderId, userNonce }) {
420
396
  return __awaiter(this, void 0, void 0, function* () {
421
397
  const ixs = [];
422
398
  const userTrade = this.getUserPDA(this.program.provider.publicKey, userNonce);
@@ -429,18 +405,16 @@ class TriadProtocolClient {
429
405
  userTrade
430
406
  })
431
407
  .instruction());
432
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
408
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
433
409
  });
434
410
  }
435
411
  /**
436
412
  * Resolve Market
437
413
  * @param args.marketId - The ID of the Market
414
+ * @param args.poolId - The ID of the Pool
438
415
  * @param args.winningDirection - The Winning Direction of the Market
439
- *
440
- * @param options - RPC options
441
- *
442
416
  */
443
- resolveMarket({ marketId, poolId, winningDirection }, options) {
417
+ updateMarketWinningDirection({ marketId, poolId, winningDirection }) {
444
418
  return __awaiter(this, void 0, void 0, function* () {
445
419
  const ixs = [];
446
420
  let poolPDA = null;
@@ -448,41 +422,14 @@ class TriadProtocolClient {
448
422
  poolPDA = (0, pda_1.getPoolPDA)(this.program.programId, poolId);
449
423
  }
450
424
  ixs.push(yield this.program.methods
451
- .updateMarket({
452
- winningDirection,
453
- allowPayout: null,
454
- marketEnd: null
455
- })
425
+ .updateMarketWinningDirection(winningDirection)
456
426
  .accounts({
457
427
  signer: this.program.provider.publicKey,
458
428
  market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
459
429
  pool: poolPDA
460
430
  })
461
431
  .instruction());
462
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
463
- });
464
- }
465
- /**
466
- * Collect Remaining Liquidity
467
- * @param marketId - The ID of the market
468
- *
469
- * @param options - RPC options
470
- *
471
- */
472
- collectRemainingLiquidity(marketId, options) {
473
- return __awaiter(this, void 0, void 0, function* () {
474
- const ixs = [];
475
- const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
476
- const market = yield this.getMarketByAddress(marketPDA);
477
- ixs.push(yield this.program.methods
478
- .collectRemainingLiquidity()
479
- .accounts({
480
- signer: this.program.provider.publicKey,
481
- market: marketPDA,
482
- mint: market.mint
483
- })
484
- .instruction());
485
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
432
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
486
433
  });
487
434
  }
488
435
  /**
@@ -491,12 +438,8 @@ class TriadProtocolClient {
491
438
  * @param args.orderId - The ID of the Order to Payout
492
439
  * @param args.userNonce - The nonce of the user
493
440
  * @param args.mint - The mint of the market
494
- * @param args.shares - The shares of the order
495
- *
496
- * @param options - RPC options
497
- *
498
441
  */
499
- payoutOrder(orders, options) {
442
+ payoutOrder(orders) {
500
443
  return __awaiter(this, void 0, void 0, function* () {
501
444
  const ixs = [];
502
445
  if (orders.length > 6) {
@@ -514,18 +457,16 @@ class TriadProtocolClient {
514
457
  })
515
458
  .instruction());
516
459
  }
517
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
460
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
518
461
  });
519
462
  }
520
463
  /**
521
- * Allow Market to Payout
464
+ * Update Market Payout
522
465
  * @param marketId - The ID of the market
523
466
  * @param poolId - The ID of the pool
524
- *
525
- * @param options - RPC options
526
- *
467
+ * @param allowPayout - Whether to allow the market to payout
527
468
  */
528
- allowMarketToPayout({ marketId, poolId }, options) {
469
+ updateMarketPayout({ marketId, poolId, allowPayout }) {
529
470
  return __awaiter(this, void 0, void 0, function* () {
530
471
  const ixs = [];
531
472
  let poolPDA = null;
@@ -533,28 +474,20 @@ class TriadProtocolClient {
533
474
  poolPDA = (0, pda_1.getPoolPDA)(this.program.programId, poolId);
534
475
  }
535
476
  ixs.push(yield this.program.methods
536
- .updateMarket({
537
- winningDirection: null,
538
- allowPayout: true,
539
- marketEnd: null
540
- })
477
+ .updateMarketPayout(allowPayout)
541
478
  .accounts({
542
479
  signer: this.program.provider.publicKey,
543
- market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
544
- pool: poolPDA
480
+ market: (0, pda_1.getMarketPDA)(this.program.programId, marketId)
545
481
  })
546
482
  .instruction());
547
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
483
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
548
484
  });
549
485
  }
550
486
  /**
551
487
  * Create Sub User Trade
552
488
  * @param user - User PublicKey the main user
553
- *
554
- * @param options - RPC options
555
- *
556
489
  */
557
- createSubUserTrade(user, options) {
490
+ createSubUserTrade(user) {
558
491
  return __awaiter(this, void 0, void 0, function* () {
559
492
  const ixs = [];
560
493
  const userTrade = yield this.getUserTrade(user);
@@ -565,37 +498,43 @@ class TriadProtocolClient {
565
498
  signer: this.program.provider.publicKey
566
499
  })
567
500
  .instruction());
568
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
501
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
569
502
  });
570
503
  }
571
504
  /**
572
- * Update Market
505
+ * Update Market End
573
506
  * @param marketId - The ID of the market
574
507
  * @param marketEnd - The end time of the market
575
- *
576
- * @param options - RPC options
577
- *
578
508
  */
579
- updateMarket({ marketId, marketEnd, poolId }, options) {
509
+ updateMarketEnd({ marketId, marketEnd }) {
580
510
  return __awaiter(this, void 0, void 0, function* () {
581
511
  const ixs = [];
582
- let poolPDA = null;
583
- if (poolId) {
584
- poolPDA = (0, pda_1.getPoolPDA)(this.program.programId, poolId);
585
- }
586
512
  ixs.push(yield this.program.methods
587
- .updateMarket({
588
- marketEnd: new bn_js_1.default(marketEnd),
589
- winningDirection: null,
590
- allowPayout: null
513
+ .updateMarketEnd(new bn_js_1.default(marketEnd))
514
+ .accounts({
515
+ signer: this.program.provider.publicKey,
516
+ market: (0, pda_1.getMarketPDA)(this.program.programId, marketId)
591
517
  })
518
+ .instruction());
519
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
520
+ });
521
+ }
522
+ /**
523
+ * Update Market Question
524
+ * @param marketId - The ID of the market
525
+ * @param question - The question of the market
526
+ */
527
+ updateMarketQuestion({ marketId, question }) {
528
+ return __awaiter(this, void 0, void 0, function* () {
529
+ const ixs = [];
530
+ ixs.push(yield this.program.methods
531
+ .updateMarketQuestion((0, helpers_1.encodeString)(question, 80))
592
532
  .accounts({
593
533
  signer: this.program.provider.publicKey,
594
- market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
595
- pool: poolPDA
534
+ market: (0, pda_1.getMarketPDA)(this.program.programId, marketId)
596
535
  })
597
536
  .instruction());
598
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
537
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
599
538
  });
600
539
  }
601
540
  /**
@@ -604,26 +543,23 @@ class TriadProtocolClient {
604
543
  * @param args.name - The name of the customer
605
544
  * @param args.authority - The authority of the customer
606
545
  * @param args.feeRecipient - The fee recipient of the customer
607
- *
608
- * @param options - RPC options
609
- *
610
546
  */
611
- createCustomer({ id, name, authority, feeRecipient }, options) {
547
+ createCustomer({ id, name, authority, feeRecipient }) {
612
548
  return __awaiter(this, void 0, void 0, function* () {
613
- const ixs = [];
614
- ixs.push(yield this.program.methods
615
- .createCustomer({ id, name, authority, feeRecipient })
616
- .accounts({
617
- signer: this.program.provider.publicKey
618
- })
619
- .instruction());
620
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
549
+ const ixs = [
550
+ yield this.program.methods
551
+ .createCustomer({ id, name, authority, feeRecipient })
552
+ .accounts({
553
+ signer: this.program.provider.publicKey
554
+ })
555
+ .instruction()
556
+ ];
557
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
621
558
  });
622
559
  }
623
560
  /**
624
561
  * Get User Trade Nonce With Slots
625
562
  * @param userTrades - User Trades
626
- *
627
563
  */
628
564
  getUserTradeNonceWithSlots(userTrades) {
629
565
  let nonce = null;
@@ -645,7 +581,6 @@ class TriadProtocolClient {
645
581
  }
646
582
  /**
647
583
  * Get User Trade Ixs
648
- *
649
584
  */
650
585
  getUserTradeIxs() {
651
586
  return __awaiter(this, void 0, void 0, function* () {
@@ -688,14 +623,13 @@ class TriadProtocolClient {
688
623
  /**
689
624
  * Get User Trade Nonce
690
625
  * @param marketId - The ID of the Market
691
- * @param direction - The direction of the Order
692
- *
626
+ * @param orderDirection - The direction of the Order
693
627
  */
694
- getUserTradeNonce(marketId, direction) {
628
+ getUserTradeNonce(marketId, orderDirection) {
695
629
  var _a;
696
630
  return __awaiter(this, void 0, void 0, function* () {
697
631
  const userOrders = yield this.getUserOrdersByMarketId(this.program.provider.publicKey, marketId);
698
- const userNonce = (_a = userOrders.find((order) => order.orderDirection === direction &&
632
+ const userNonce = (_a = userOrders.find((order) => order.orderDirection === orderDirection &&
699
633
  order.orderStatus === types_1.OrderStatus.OPEN)) === null || _a === void 0 ? void 0 : _a.userNonce;
700
634
  let userTradePDA = null;
701
635
  let userTradeIxs = [];
@@ -713,15 +647,14 @@ class TriadProtocolClient {
713
647
  /**
714
648
  * Place Bid Order
715
649
  * @param args.marketId - The ID of the Market
716
- * @param args.amount - The amount of the Order
717
- * @param args.direction - The direction of the Order
650
+ * @param args.orders - Array of orders to execute
651
+ * @param args.orders.amount - The amount of the Order
652
+ * @param args.orders.price - The price of the Order
653
+ * @param args.orders.orderDirection - The direction of the Order
718
654
  * @param args.mint - The mint of the Order
719
- * @param args.price - The price of the Order
720
655
  * @param args.isTrdPayout - Whether to payout in TRD or not
721
- *
722
- * @param options - RPC options
723
656
  */
724
- placeBidOrder({ marketId, orders, mint, isTrdPayout }, options) {
657
+ placeBidOrder({ marketId, orders, isTrdPayout }) {
725
658
  return __awaiter(this, void 0, void 0, function* () {
726
659
  const ixs = [];
727
660
  const addressLookupTableAccounts = [];
@@ -750,47 +683,42 @@ class TriadProtocolClient {
750
683
  ixs.push(...setupInstructions);
751
684
  ixs.push(...swapIxs);
752
685
  addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
753
- if (amountInUSDC.lt(new bn_js_1.default(totalAmount * Math.pow(10, this.decimals)))) {
686
+ if (amountInUSDC.lt(new bn_js_1.default(totalAmount * Math.pow(10, constants_1.BASE_DECIMALS)))) {
754
687
  return;
755
688
  }
756
689
  }
757
- const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(orders[0].direction)[0]);
690
+ const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(orders[0].orderDirection)[0]);
758
691
  if (userTradeIxs.length > 0) {
759
692
  ixs.push(...userTradeIxs);
760
693
  }
761
694
  for (const order of orders) {
762
695
  ixs.push(yield this.program.methods
763
696
  .placeBidOrder({
764
- amount: new bn_js_1.default(order.amount * Math.pow(10, this.decimals)),
765
- price: new bn_js_1.default(order.price * Math.pow(10, this.decimals)),
766
- orderDirection: order.direction
697
+ amount: new bn_js_1.default(order.amount * Math.pow(10, constants_1.BASE_DECIMALS)),
698
+ price: new bn_js_1.default(order.price * Math.pow(10, constants_1.BASE_DECIMALS)),
699
+ orderDirection: order.orderDirection
767
700
  })
768
701
  .accounts({
769
702
  signer: this.program.provider.publicKey,
770
703
  market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
771
704
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
772
- userTrade: userTradePDA,
773
- mint,
774
- tokenProgram: (0, helpers_1.getTokenProgram)(mint)
705
+ userTrade: userTradePDA
775
706
  })
776
707
  .instruction());
777
708
  }
778
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
709
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
779
710
  });
780
711
  }
781
712
  /**
782
713
  * Place Ask Order
783
714
  * @param args.marketId - The ID of the Market
784
- * @param args.amount - The amount of the Order
785
- * @param args.direction - The direction of the Order
786
- * @param args.price - The price of the Order
787
- * @param args.bidOrderId - The ID of the Bid Order
788
- * @param args.bidNonce - The nonce of the Bid Order
789
- *
790
- * @param options - RPC options
791
- *
715
+ * @param args.orders - Array of orders to execute
716
+ * @param args.orders.amount - The amount of the Order
717
+ * @param args.orders.price - The price of the Order
718
+ * @param args.orders.bidOrderId - The ID of the Bid Order
719
+ * @param args.orders.bidNonce - The nonce of the Bid Order
792
720
  */
793
- placeAskOrder({ marketId, orders }, options) {
721
+ placeAskOrder({ marketId, orders }) {
794
722
  return __awaiter(this, void 0, void 0, function* () {
795
723
  const ixs = [];
796
724
  if (orders.length > 4) {
@@ -799,8 +727,8 @@ class TriadProtocolClient {
799
727
  for (const order of orders) {
800
728
  ixs.push(yield this.program.methods
801
729
  .placeAskOrder({
802
- shares: new bn_js_1.default(order.amount * Math.pow(10, this.decimals)),
803
- price: new bn_js_1.default(order.price * Math.pow(10, this.decimals)),
730
+ shares: new bn_js_1.default(order.amount * Math.pow(10, constants_1.BASE_DECIMALS)),
731
+ price: new bn_js_1.default(order.price * Math.pow(10, constants_1.BASE_DECIMALS)),
804
732
  bidOrderId: new bn_js_1.default(order.bidOrderId)
805
733
  })
806
734
  .accounts({
@@ -811,19 +739,17 @@ class TriadProtocolClient {
811
739
  })
812
740
  .instruction());
813
741
  }
814
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
742
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
815
743
  });
816
744
  }
817
745
  /**
818
746
  * Cancel Bid Order
819
747
  * @param args.marketId - The ID of the Market
820
- * @param args.orderId - The ID of the Order
821
- * @param args.userNonce - The nonce of the user
822
- * @param args.direction - The direction of the Order
823
- *
824
- * @param options - RPC options
748
+ * @param args.orders.orderId - The ID of the Order
749
+ * @param args.orders.userNonce - The nonce of the user
750
+ * @param args.orders.orderDirection - The direction of the Order
825
751
  */
826
- cancelBidOrder({ marketId, orders, mint }, options) {
752
+ cancelBidOrder({ marketId, orders }) {
827
753
  return __awaiter(this, void 0, void 0, function* () {
828
754
  const ixs = [];
829
755
  if (orders.length > 4) {
@@ -833,32 +759,27 @@ class TriadProtocolClient {
833
759
  ixs.push(yield this.program.methods
834
760
  .cancelBidOrder({
835
761
  orderId: new bn_js_1.default(order.orderId),
836
- orderDirection: order.direction
762
+ orderDirection: order.orderDirection
837
763
  })
838
764
  .accounts({
839
765
  signer: this.program.provider.publicKey,
840
766
  market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
841
767
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
842
- userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce),
843
- mint,
844
- tokenProgram: (0, helpers_1.getTokenProgram)(mint)
768
+ userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce)
845
769
  })
846
770
  .instruction());
847
771
  }
848
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
772
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
849
773
  });
850
774
  }
851
775
  /**
852
776
  * Cancel Ask Order
853
777
  * @param args.marketId - The ID of the Market
854
- * @param args.orderId - The ID of the Order
855
- * @param args.userNonce - The nonce of the user
856
- * @param args.userNonceBidOrder - The nonce of the bid user
857
- * @param args.direction - The direction of the Order
858
- *
859
- * @param options - RPC options
778
+ * @param args.orders.orderId - The ID of the Order
779
+ * @param args.orders.userNonce - The nonce of the user
780
+ * @param args.orders.orderDirection - The direction of the Order
860
781
  */
861
- cancelAskOrder({ marketId, orders }, options) {
782
+ cancelAskOrder({ marketId, orders }) {
862
783
  return __awaiter(this, void 0, void 0, function* () {
863
784
  const ixs = [];
864
785
  if (orders.length > 4) {
@@ -868,7 +789,7 @@ class TriadProtocolClient {
868
789
  ixs.push(yield this.program.methods
869
790
  .cancelAskOrder({
870
791
  orderId: new bn_js_1.default(order.orderId),
871
- orderDirection: order.direction
792
+ orderDirection: order.orderDirection
872
793
  })
873
794
  .accounts({
874
795
  signer: this.program.provider.publicKey,
@@ -878,38 +799,34 @@ class TriadProtocolClient {
878
799
  })
879
800
  .instruction());
880
801
  }
881
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
802
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
882
803
  });
883
804
  }
884
805
  /**
885
806
  * Market Bid Order
886
807
  * @param args.marketId - The ID of the Market
887
808
  * @param args.amount - The amount of the Order
888
- * @param args.direction - The direction of the Order
889
- * @param args.mint - The mint of the Order
809
+ * @param args.orderDirection - The direction of the Order
890
810
  * @param args.feeBps - The fee in basis points
891
811
  * @param args.isTrdPayout - Whether to payout in TRD or not
892
- *
893
- * @param options - RPC options
894
812
  */
895
- marketBidOrder({ marketId, amount, direction, mint, feeBps = 0, isTrdPayout }, options) {
813
+ marketBidOrder({ marketId, amount, orderDirection, feeBps = 0, isTrdPayout }) {
896
814
  return __awaiter(this, void 0, void 0, function* () {
897
815
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
898
816
  const ixs = [];
899
817
  const addressLookupTableAccounts = [];
900
- const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(direction)[0]);
818
+ const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(orderDirection)[0]);
901
819
  const orderBook = yield this.getOrderBook(marketId);
902
- let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, this.decimals));
820
+ let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, constants_1.BASE_DECIMALS));
903
821
  if (isTrdPayout) {
904
822
  const price = yield (0, swap_1.getPrice)(constants_1.TRD_MINT.toString());
905
823
  const trdToUsdc = amount * price;
906
- remainingUSDC = new bn_js_1.default(trdToUsdc * Math.pow(10, this.decimals));
824
+ remainingUSDC = new bn_js_1.default(trdToUsdc * Math.pow(10, constants_1.BASE_DECIMALS));
907
825
  }
908
- const orders = Object.keys(direction)[0] === 'hype'
826
+ const orders = Object.keys(orderDirection)[0] === 'hype'
909
827
  ? orderBook.hype.ask
910
828
  : orderBook.flop.ask;
911
829
  const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
912
- const tokenProgram = (0, helpers_1.getTokenProgram)(mint);
913
830
  let totalUSDCNeeded = new bn_js_1.default(0);
914
831
  let tempRemainingUSDC = remainingUSDC.clone();
915
832
  if (isTrdPayout) {
@@ -932,21 +849,21 @@ class TriadProtocolClient {
932
849
  adjustedPrice = orderPrice.add(fee);
933
850
  }
934
851
  const maxSharesForPrice = tempRemainingUSDC
935
- .mul(new bn_js_1.default(Math.pow(10, this.decimals)))
852
+ .mul(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)))
936
853
  .div(adjustedPrice);
937
854
  const sharesToBuy = bn_js_1.default.min(maxSharesForPrice, availableShares);
938
855
  if (sharesToBuy.lte(new bn_js_1.default(0)))
939
856
  continue;
940
857
  const usdcAmount = sharesToBuy
941
858
  .mul(adjustedPrice)
942
- .div(new bn_js_1.default(Math.pow(10, this.decimals)));
859
+ .div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)));
943
860
  if (usdcAmount.lte(new bn_js_1.default(0)))
944
861
  continue;
945
862
  totalUSDCNeeded = totalUSDCNeeded.add(usdcAmount);
946
863
  tempRemainingUSDC = tempRemainingUSDC.sub(usdcAmount);
947
864
  }
948
865
  const price = yield (0, swap_1.getPrice)(constants_1.TRD_MINT.toString());
949
- const amountInTRD = totalUSDCNeeded.toNumber() / (price * Math.pow(10, this.decimals));
866
+ const amountInTRD = totalUSDCNeeded.toNumber() / (price * Math.pow(10, constants_1.BASE_DECIMALS));
950
867
  const { swapIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts, outAmount, setupInstructions } = yield (0, swap_1.swap)({
951
868
  connection: this.program.provider.connection,
952
869
  wallet: this.program.provider.publicKey.toBase58(),
@@ -981,20 +898,20 @@ class TriadProtocolClient {
981
898
  adjustedPrice = orderPrice.add(fee);
982
899
  }
983
900
  const maxSharesForPrice = remainingUSDC
984
- .mul(new bn_js_1.default(Math.pow(10, this.decimals)))
901
+ .mul(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)))
985
902
  .div(adjustedPrice);
986
903
  const sharesToBuy = bn_js_1.default.min(maxSharesForPrice, availableShares);
987
904
  if (sharesToBuy.lte(new bn_js_1.default(0)))
988
905
  continue;
989
906
  const usdcAmount = sharesToBuy
990
907
  .mul(adjustedPrice)
991
- .div(new bn_js_1.default(Math.pow(10, this.decimals)));
908
+ .div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)));
992
909
  if (usdcAmount.lte(new bn_js_1.default(0)))
993
910
  continue;
994
911
  ixs.push(yield this.program.methods
995
912
  .marketBidOrder({
996
913
  amount: new bn_js_1.default(usdcAmount),
997
- orderDirection: direction,
914
+ orderDirection,
998
915
  askOrderId: new bn_js_1.default(order.id),
999
916
  isTrdPayout
1000
917
  })
@@ -1003,12 +920,12 @@ class TriadProtocolClient {
1003
920
  market: marketPDA,
1004
921
  buyerTrade: userTradePDA,
1005
922
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
1006
- mint,
1007
- tokenProgram,
1008
923
  sellerAuthority: new web3_js_1.PublicKey(order.authority),
1009
924
  sellerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
1010
- marketAta: (0, pda_1.getTokenATA)(marketPDA, mint, tokenProgram),
1011
- buyerAta: (0, pda_1.getTokenATA)(this.program.provider.publicKey, mint, tokenProgram)
925
+ marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
926
+ buyerAta: (0, pda_1.getTokenATA)(this.program.provider.publicKey, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
927
+ mint: constants_1.USDC_MINT,
928
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
1012
929
  })
1013
930
  .instruction());
1014
931
  remainingUSDC = remainingUSDC.sub(usdcAmount);
@@ -1019,32 +936,33 @@ class TriadProtocolClient {
1019
936
  if (ixs.length === 0) {
1020
937
  throw new Error('No matching orders found to fill the requested amount');
1021
938
  }
1022
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
939
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions, addressLookupTableAccounts);
1023
940
  });
1024
941
  }
1025
942
  /**
1026
943
  * Market Ask Order
1027
944
  * @param args.marketId - The ID of the Market
1028
945
  * @param args.orders - Array of orders to execute
1029
- * @param args.direction - The direction of the Order
946
+ * @param args.orders.shares - The amount of shares to sell
947
+ * @param args.orders.bidOrderId - The ID of the Bid Order
948
+ * @param args.orders.userNonce - The nonce of the user
949
+ * @param args.orderDirection - The direction of the Order
1030
950
  * @param args.isTrdPayout - Whether to payout in TRD or not
1031
- *
1032
- * @param options - RPC options
951
+ * @param args.feeBps - The fee in basis points
1033
952
  */
1034
- marketAskOrder({ marketId, orders, direction, isTrdPayout, feeBps }, options) {
953
+ marketAskOrder({ marketId, orders, orderDirection, isTrdPayout, feeBps = 0 }) {
1035
954
  return __awaiter(this, void 0, void 0, function* () {
1036
955
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
1037
956
  const ixs = [];
1038
957
  const addressLookupTableAccounts = [];
1039
958
  const orderBook = yield this.getOrderBook(marketId);
1040
- const bidOrders = Object.keys(direction)[0] === 'hype'
959
+ const bidOrders = Object.keys(orderDirection)[0] === 'hype'
1041
960
  ? orderBook.hype.bid
1042
961
  : orderBook.flop.bid;
1043
962
  const sortedOrders = bidOrders.sort((a, b) => Number(b.price) - Number(a.price));
1044
- const tokenProgram = (0, helpers_1.getTokenProgram)(constants_1.USDC_MINT);
1045
963
  let amountOfUSDC = new bn_js_1.default(0);
1046
964
  for (const inputOrder of orders) {
1047
- let remainingShares = new bn_js_1.default(inputOrder.shares * Math.pow(10, this.decimals));
965
+ let remainingShares = new bn_js_1.default(inputOrder.shares * Math.pow(10, constants_1.BASE_DECIMALS));
1048
966
  for (const order of sortedOrders) {
1049
967
  if (remainingShares.lte(new bn_js_1.default(0)))
1050
968
  break;
@@ -1068,11 +986,11 @@ class TriadProtocolClient {
1068
986
  .div(new bn_js_1.default(1000000));
1069
987
  adjustedPrice = orderPrice.sub(fee);
1070
988
  }
1071
- amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, this.decimals))));
989
+ amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS))));
1072
990
  ixs.push(yield this.program.methods
1073
991
  .marketAskOrder({
1074
992
  shares: new bn_js_1.default(sharesToSell),
1075
- orderDirection: direction,
993
+ orderDirection,
1076
994
  bidOrderId: new bn_js_1.default(inputOrder.bidOrderId),
1077
995
  bookOrderBidId: new bn_js_1.default(order.id)
1078
996
  })
@@ -1082,10 +1000,10 @@ class TriadProtocolClient {
1082
1000
  buyerAuthority: new web3_js_1.PublicKey(order.authority),
1083
1001
  buyerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
1084
1002
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
1003
+ marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
1004
+ sellerTrade: this.getUserPDA(this.program.provider.publicKey, inputOrder.userNonce),
1085
1005
  mint: constants_1.USDC_MINT,
1086
- tokenProgram,
1087
- marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, tokenProgram),
1088
- sellerTrade: this.getUserPDA(this.program.provider.publicKey, inputOrder.userNonce)
1006
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
1089
1007
  })
1090
1008
  .instruction());
1091
1009
  }
@@ -1107,13 +1025,12 @@ class TriadProtocolClient {
1107
1025
  if (ixs.length === 0) {
1108
1026
  throw new Error('No matching orders found to fill the requested amount');
1109
1027
  }
1110
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
1028
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions, addressLookupTableAccounts);
1111
1029
  });
1112
1030
  }
1113
1031
  /**
1114
1032
  * Get Orders By Market ID
1115
- * @param marketId - The ID of the market
1116
- *
1033
+ * @param marketId - Market ID
1117
1034
  */
1118
1035
  getOrderBook(marketId) {
1119
1036
  return __awaiter(this, void 0, void 0, function* () {
@@ -1150,14 +1067,11 @@ class TriadProtocolClient {
1150
1067
  /**
1151
1068
  * Collect Market Fee
1152
1069
  * @param args.markets - The markets to collect the fee from
1153
- * @param args.markets.mint - The mint of the market
1154
1070
  * @param args.markets.marketAddress - The address of the market
1155
1071
  * @param args.markets.customerId - The ID of the customer
1156
1072
  * @param args.markets.customerFeeRecipient - The address of the customer fee recipient
1157
- *
1158
- * @param options - RPC options
1159
1073
  */
1160
- collectMarketFee(markets, options) {
1074
+ collectMarketFee({ markets }) {
1161
1075
  return __awaiter(this, void 0, void 0, function* () {
1162
1076
  const ixs = [];
1163
1077
  for (const market of markets) {
@@ -1166,23 +1080,21 @@ class TriadProtocolClient {
1166
1080
  .accounts({
1167
1081
  signer: this.program.provider.publicKey,
1168
1082
  market: market.marketAddress,
1169
- mint: market.mint,
1083
+ mint: constants_1.USDC_MINT,
1170
1084
  customer: (0, pda_1.getCustomerPDA)(this.program.programId, market.customerId),
1171
1085
  customerFeeRecipient: market.customerFeeRecipient,
1172
- tokenProgram: (0, helpers_1.getTokenProgram)(market.mint)
1086
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
1173
1087
  })
1174
1088
  .instruction());
1175
1089
  }
1176
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
1090
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
1177
1091
  });
1178
1092
  }
1179
1093
  /**
1180
1094
  * Close Order Book
1181
- * @param marketId - The ID of the market
1182
- *
1183
- * @param options - RPC options
1095
+ * @param marketIds - Market IDs
1184
1096
  */
1185
- closeOrderBook(marketIds, options) {
1097
+ closeOrderBook(marketIds) {
1186
1098
  return __awaiter(this, void 0, void 0, function* () {
1187
1099
  const ixs = [];
1188
1100
  for (const marketId of marketIds) {
@@ -1195,28 +1107,7 @@ class TriadProtocolClient {
1195
1107
  })
1196
1108
  .instruction());
1197
1109
  }
1198
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
1199
- });
1200
- }
1201
- /**
1202
- * Update Customer Fee
1203
- * @param feeBps - The fee in basis points
1204
- * @param wallet - The wallet of the customer to update the fee for
1205
- *
1206
- * @param options - RPC options
1207
- */
1208
- updateCustomerFee({ feeBps, wallet }, options) {
1209
- return __awaiter(this, void 0, void 0, function* () {
1210
- const ixs = [];
1211
- const customer = yield this.getCustomerByWallet(wallet);
1212
- ixs.push(yield this.program.methods
1213
- .updateCustomerFee(feeBps)
1214
- .accounts({
1215
- signer: this.program.provider.publicKey,
1216
- customer: customer.address
1217
- })
1218
- .instruction());
1219
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
1110
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
1220
1111
  });
1221
1112
  }
1222
1113
  }