@triadxyz/triad-protocol 3.4.0-beta → 3.4.2-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
@@ -63,12 +63,12 @@ class TriadProtocol {
63
63
  this.claim = new claim_1.default(this.program, this.rpcOptions);
64
64
  }
65
65
  /**
66
- * Get My User Trades from a user authority
66
+ * Get User Orders
67
67
  * @param wallet - User wallet PublicKey
68
68
  */
69
- getMyUserTrades(wallet) {
69
+ getUserOrders(wallet) {
70
70
  return __awaiter(this, void 0, void 0, function* () {
71
- const response = yield this.program.account.userTrade.all([
71
+ const orders = yield this.program.account.orderV2.all([
72
72
  {
73
73
  memcmp: {
74
74
  offset: 8 + 1,
@@ -76,17 +76,7 @@ class TriadProtocol {
76
76
  }
77
77
  }
78
78
  ]);
79
- return response.map(({ account, publicKey }) => (0, helpers_2.formatUserTrade)(account, publicKey));
80
- });
81
- }
82
- /**
83
- * Get User Orders
84
- * @param wallet - User wallet PublicKey
85
- */
86
- getUserOrders(wallet) {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- const myUserTrades = yield this.getMyUserTrades(wallet);
89
- return myUserTrades.flatMap((userTrade) => userTrade.orders);
79
+ return orders.map(({ account }) => (0, helpers_1.formatOrder)(account));
90
80
  });
91
81
  }
92
82
  /**
@@ -96,8 +86,8 @@ class TriadProtocol {
96
86
  */
97
87
  getUserOrdersByMarketId(wallet, marketId) {
98
88
  return __awaiter(this, void 0, void 0, function* () {
99
- const userTrades = yield this.getMyUserTrades(wallet);
100
- return userTrades.flatMap((userTrade) => userTrade.orders.filter((order) => order.marketId === marketId.toString()));
89
+ const orders = yield this.getUserOrders(wallet);
90
+ return orders.filter((order) => order.marketId === marketId.toString());
101
91
  });
102
92
  }
103
93
  /**
@@ -157,30 +147,6 @@ class TriadProtocol {
157
147
  return (0, helpers_2.formatCustomer)(customer, customerPDA);
158
148
  });
159
149
  }
160
- /**
161
- * Get User Trade PDA
162
- * @param wallet - User wallet PublicKey
163
- * @param userNonce - The nonce of the user
164
- */
165
- getUserPDA(wallet, userNonce = 0) {
166
- let userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, wallet);
167
- if (userNonce !== 0) {
168
- const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, wallet, userNonce);
169
- userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
170
- }
171
- return userTradePDA;
172
- }
173
- /**
174
- * Get User Trade
175
- * @param wallet - User wallet PublicKey
176
- * @param userNonce - The nonce of the user
177
- */
178
- getUserTradeByNonce(wallet, userNonce = 0) {
179
- return __awaiter(this, void 0, void 0, function* () {
180
- const userTradePDA = this.getUserPDA(wallet, userNonce);
181
- return this.program.account.userTrade.fetch(userTradePDA);
182
- });
183
- }
184
150
  /**
185
151
  * Get All Pools
186
152
  */
@@ -517,18 +483,14 @@ class TriadProtocol {
517
483
  * Open Order
518
484
  * @param args.marketId - The ID of the Market
519
485
  * @param args.amount - The amount of the Order
520
- * @param args.direction - The direction of the Order
486
+ * @param args.orderDirection - The direction of the Order
521
487
  * @param args.mint - The mint of the Order
522
488
  * @param args.token - The token to use for the Order
523
489
  */
524
- openOrder({ marketId, amount, direction, mint, token }) {
490
+ openOrder({ marketId, amount, orderDirection, mint, token }) {
525
491
  return __awaiter(this, void 0, void 0, function* () {
526
492
  const ixs = [];
527
493
  const addressLookupTableAccounts = [];
528
- const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
529
- if (userTradeIxs.length > 0) {
530
- ixs.push(...userTradeIxs);
531
- }
532
494
  let amountInTRD = amount * Math.pow(10, constants_1.BASE_DECIMALS);
533
495
  if (token !== constants_1.TRD_MINT.toBase58()) {
534
496
  const { setupInstructions, swapIxs, addressLookupTableAccounts, outAmount } = yield (0, swap_1.swap)({
@@ -550,13 +512,12 @@ class TriadProtocol {
550
512
  ixs.push(yield this.program.methods
551
513
  .openOrder({
552
514
  amount: new bn_js_1.default(amountInTRD),
553
- direction: direction
515
+ orderDirection
554
516
  })
555
517
  .accounts({
556
518
  signer: this.program.provider.publicKey,
557
519
  payer: this.rpcOptions.payer,
558
520
  market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
559
- userTrade: userTradePDA,
560
521
  mint
561
522
  })
562
523
  .instruction());
@@ -566,21 +527,18 @@ class TriadProtocol {
566
527
  /**
567
528
  * Close Order
568
529
  * @param args.marketId - The ID of the Market
569
- * @param args.orderId - The ID of the Order
570
- * @param args.userNonce - The nonce of the user
530
+ * @param args.orderDirection - The direction of the Order
571
531
  */
572
- closeOrder({ marketId, orderId, userNonce }) {
532
+ closeOrder({ marketId, orderDirection }) {
573
533
  return __awaiter(this, void 0, void 0, function* () {
574
- const userTrade = this.getUserPDA(this.program.provider.publicKey, userNonce);
575
534
  const ixs = [
576
535
  yield this.program.methods
577
- .closeOrder(new bn_js_1.default(orderId))
536
+ .closeOrder(orderDirection)
578
537
  .accounts({
579
538
  signer: this.program.provider.publicKey,
580
539
  payer: this.rpcOptions.payer,
581
540
  market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
582
- mint: constants_1.TRD_MINT,
583
- userTrade
541
+ mint: constants_1.TRD_MINT
584
542
  })
585
543
  .instruction()
586
544
  ];
@@ -590,8 +548,7 @@ class TriadProtocol {
590
548
  /**
591
549
  * Payout Order
592
550
  * @param args.marketId - The ID of the Market
593
- * @param args.orderId - The ID of the Order to Payout
594
- * @param args.userNonce - The nonce of the user
551
+ * @param args.orderDirection - The direction of the Order to Payout
595
552
  * @param args.mint - The mint of the market
596
553
  */
597
554
  payoutOrder(orders) {
@@ -602,11 +559,10 @@ class TriadProtocol {
602
559
  }
603
560
  for (const order of orders) {
604
561
  ixs.push(yield this.program.methods
605
- .payoutOrder(new bn_js_1.default(order.orderId))
562
+ .payoutOrder(order.orderDirection)
606
563
  .accounts({
607
564
  signer: this.program.provider.publicKey,
608
565
  payer: this.rpcOptions.payer,
609
- userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce),
610
566
  market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
611
567
  mint: order.mint,
612
568
  tokenProgram: (0, helpers_2.getTokenProgram)(order.mint)
@@ -616,25 +572,6 @@ class TriadProtocol {
616
572
  return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
617
573
  });
618
574
  }
619
- /**
620
- * Create Sub User Trade
621
- * @param user - User PublicKey the main user
622
- */
623
- createSubUserTrade(user) {
624
- return __awaiter(this, void 0, void 0, function* () {
625
- const ixs = [];
626
- const userTrade = yield this.getUserTradeByNonce(user);
627
- const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
628
- ixs.push(yield this.program.methods
629
- .createSubUserTrade(subUserTradePDA)
630
- .accounts({
631
- signer: this.program.provider.publicKey,
632
- payer: this.rpcOptions.payer
633
- })
634
- .instruction());
635
- return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
636
- });
637
- }
638
575
  /**
639
576
  * Create Customer
640
577
  * @param args.id - The ID of the customer
@@ -655,90 +592,6 @@ class TriadProtocol {
655
592
  return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
656
593
  });
657
594
  }
658
- /**
659
- * Get User Trade Ixs
660
- */
661
- getUserTradeIxs() {
662
- return __awaiter(this, void 0, void 0, function* () {
663
- const ixs = [];
664
- let myUserTrades = [];
665
- myUserTrades = yield this.getMyUserTrades(this.program.provider.publicKey);
666
- if (myUserTrades.length === 0) {
667
- ixs.push(yield this.program.methods
668
- .createUserTrade()
669
- .accounts({
670
- signer: this.program.provider.publicKey,
671
- payer: this.rpcOptions.payer
672
- })
673
- .instruction());
674
- return {
675
- userTradePDA: this.getUserPDA(this.program.provider.publicKey),
676
- ixs,
677
- nonce: 0
678
- };
679
- }
680
- try {
681
- let nonce = null;
682
- for (const userTrade of myUserTrades) {
683
- if (nonce !== null) {
684
- break;
685
- }
686
- userTrade.orders.forEach((order) => {
687
- if (order.orderStatus !== types_1.OrderStatus.OPEN) {
688
- nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
689
- }
690
- });
691
- }
692
- if (nonce === null) {
693
- throw new Error('No open orders found');
694
- }
695
- return {
696
- userTradePDA: this.getUserPDA(this.program.provider.publicKey, nonce),
697
- ixs
698
- };
699
- }
700
- catch (_a) {
701
- const mainUserTrade = myUserTrades.find((trade) => !trade.isSubUser);
702
- const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, Number(mainUserTrade.nonce) + 1);
703
- ixs.push(yield this.program.methods
704
- .createSubUserTrade(subUserTradePDA)
705
- .accounts({
706
- signer: this.program.provider.publicKey,
707
- payer: this.rpcOptions.payer
708
- })
709
- .instruction());
710
- return {
711
- userTradePDA: (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA),
712
- ixs
713
- };
714
- }
715
- });
716
- }
717
- /**
718
- * Get User Trade Nonce
719
- * @param marketId - The ID of the Market
720
- * @param orderDirection - The direction of the Order
721
- */
722
- getUserTradeNonce(marketId, orderDirection) {
723
- var _a;
724
- return __awaiter(this, void 0, void 0, function* () {
725
- const userOrders = yield this.getUserOrdersByMarketId(this.program.provider.publicKey, marketId);
726
- const userNonce = (_a = userOrders.find((order) => order.orderDirection === orderDirection &&
727
- order.orderStatus === types_1.OrderStatus.OPEN &&
728
- Number(order.marketId) === marketId)) === null || _a === void 0 ? void 0 : _a.userNonce;
729
- let userTradePDA = null;
730
- let userTradeIxs = [];
731
- if (userNonce) {
732
- userTradePDA = this.getUserPDA(this.program.provider.publicKey, Number(userNonce));
733
- }
734
- if (!userNonce) {
735
- const { userTradePDA: user, ixs: ixsUser } = yield this.getUserTradeIxs();
736
- userTradePDA = user;
737
- userTradeIxs = ixsUser;
738
- }
739
- return { userTradePDA, userTradeIxs };
740
- });
741
- }
742
595
  /**
743
596
  * Place Bid Order
744
597
  * @param args.orders - Array of orders to execute
@@ -746,15 +599,14 @@ class TriadProtocol {
746
599
  * @param args.orders.amount - The amount of the Order
747
600
  * @param args.orders.price - The price of the Order
748
601
  * @param args.orders.orderDirection - The direction of the Order
749
- * @param args.mint - The mint of the Order
750
602
  * @param args.isTrdPayout - Whether to payout in TRD or not
751
603
  */
752
604
  placeBidOrder({ orders, isTrdPayout }) {
753
605
  return __awaiter(this, void 0, void 0, function* () {
754
606
  const ixs = [];
755
607
  const addressLookupTableAccounts = [];
756
- if (orders.length > 4) {
757
- throw new Error('You can only place up to 4 orders at a time');
608
+ if (orders.length > 5) {
609
+ throw new Error('Max 5 orders per transaction');
758
610
  }
759
611
  let amountInUSDC = new bn_js_1.default(0);
760
612
  let totalAmount = 0;
@@ -783,10 +635,6 @@ class TriadProtocol {
783
635
  return;
784
636
  }
785
637
  }
786
- const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(orders[0].marketId, Object.keys(orders[0].orderDirection)[0]);
787
- if (userTradeIxs.length > 0) {
788
- ixs.push(...userTradeIxs);
789
- }
790
638
  for (const order of orders) {
791
639
  ixs.push(yield this.program.methods
792
640
  .placeBidOrder({
@@ -797,8 +645,7 @@ class TriadProtocol {
797
645
  .accounts({
798
646
  signer: this.program.provider.publicKey,
799
647
  market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
800
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
801
- userTrade: userTradePDA
648
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId)
802
649
  })
803
650
  .instruction());
804
651
  }
@@ -818,20 +665,19 @@ class TriadProtocol {
818
665
  return __awaiter(this, void 0, void 0, function* () {
819
666
  const ixs = [];
820
667
  if (orders.length > 5) {
821
- throw new Error('You can only place up to 5 orders at a time');
668
+ throw new Error('Max 5 orders per transaction');
822
669
  }
823
670
  for (const order of orders) {
824
671
  ixs.push(yield this.program.methods
825
672
  .placeAskOrder({
826
673
  shares: new bn_js_1.default(order.amount * Math.pow(10, constants_1.BASE_DECIMALS)),
827
674
  price: new bn_js_1.default(order.price * Math.pow(10, constants_1.BASE_DECIMALS)),
828
- bidOrderId: new bn_js_1.default(order.bidOrderId)
675
+ orderDirection: order.orderDirection
829
676
  })
830
677
  .accounts({
831
678
  signer: this.program.provider.publicKey,
832
679
  market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
833
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
834
- userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce)
680
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId)
835
681
  })
836
682
  .instruction());
837
683
  }
@@ -840,28 +686,27 @@ class TriadProtocol {
840
686
  }
841
687
  /**
842
688
  * Cancel Bid Order
843
- * @param args.orders.orderId - The ID of the Order
844
- * @param args.orders.userNonce - The nonce of the user
689
+ * @param args.orders.bookOrderId - The ID of the Book Order
690
+ * @param args.orders.marketId - The ID of the Market
845
691
  * @param args.orders.orderDirection - The direction of the Order
846
692
  */
847
693
  cancelBidOrder({ orders }) {
848
694
  return __awaiter(this, void 0, void 0, function* () {
849
695
  const ixs = [];
850
- if (orders.length > 5) {
851
- throw new Error('You can only cancel up to 5 orders at a time');
696
+ if (orders.length > 6) {
697
+ throw new Error('You can only cancel up to 6 orders at a time');
852
698
  }
853
699
  for (const order of orders) {
854
700
  ixs.push(yield this.program.methods
855
701
  .cancelBidOrder({
856
- orderId: new bn_js_1.default(order.orderId),
702
+ bookOrderId: new bn_js_1.default(order.bookOrderId),
857
703
  orderDirection: order.orderDirection
858
704
  })
859
705
  .accounts({
860
706
  signer: this.program.provider.publicKey,
861
707
  payer: this.rpcOptions.payer,
862
708
  market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
863
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
864
- userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce)
709
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId)
865
710
  })
866
711
  .instruction());
867
712
  }
@@ -870,28 +715,26 @@ class TriadProtocol {
870
715
  }
871
716
  /**
872
717
  * Cancel Ask Order
873
- * @param args.marketId - The ID of the Market
874
- * @param args.orders.orderId - The ID of the Order
875
- * @param args.orders.userNonce - The nonce of the user
718
+ * @param args.orders.marketId - The ID of the Market
719
+ * @param args.orders.bookOrderId - The ID of the Book Order
876
720
  * @param args.orders.orderDirection - The direction of the Order
877
721
  */
878
- cancelAskOrder({ marketId, orders }) {
722
+ cancelAskOrder({ orders }) {
879
723
  return __awaiter(this, void 0, void 0, function* () {
880
724
  const ixs = [];
881
- if (orders.length > 4) {
882
- throw new Error('You can only cancel up to 4 orders at a time');
725
+ if (orders.length > 5) {
726
+ throw new Error('Max 5 orders per transaction');
883
727
  }
884
728
  for (const order of orders) {
885
729
  ixs.push(yield this.program.methods
886
730
  .cancelAskOrder({
887
- orderId: new bn_js_1.default(order.orderId),
731
+ bookOrderId: new bn_js_1.default(order.bookOrderId),
888
732
  orderDirection: order.orderDirection
889
733
  })
890
734
  .accounts({
891
735
  signer: this.program.provider.publicKey,
892
- market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
893
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
894
- userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce)
736
+ market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
737
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId)
895
738
  })
896
739
  .instruction());
897
740
  }
@@ -910,7 +753,6 @@ class TriadProtocol {
910
753
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
911
754
  const ixs = [];
912
755
  const addressLookupTableAccounts = [];
913
- const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(orderDirection)[0]);
914
756
  const orderBook = yield this.getOrderBook(marketId);
915
757
  let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, constants_1.BASE_DECIMALS));
916
758
  if (isTrdPayout) {
@@ -992,25 +834,19 @@ class TriadProtocol {
992
834
  .marketBidOrder({
993
835
  amount: new bn_js_1.default(usdcAmount),
994
836
  orderDirection,
995
- askOrderId: new bn_js_1.default(order.id),
996
- isTrdPayout
837
+ bookOrderAskId: new bn_js_1.default(order.id),
838
+ oppositeOrderDirection: (0, helpers_1.getOppositeOrderDirectionEncoded)(orderDirection)
997
839
  })
998
840
  .accounts({
999
841
  signer: this.program.provider.publicKey,
1000
842
  market: marketPDA,
1001
- buyerTrade: userTradePDA,
1002
843
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
1003
- sellerAuthority: new web3_js_1.PublicKey(order.authority),
1004
- sellerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
1005
- marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
1006
- buyerAta: (0, pda_1.getTokenATA)(this.program.provider.publicKey, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID)
844
+ bookOrderAskAuthority: new web3_js_1.PublicKey(order.authority),
845
+ oppositeOrder: (0, pda_1.getOrderPDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), marketId, (0, helpers_1.getOppositeOrderDirection)(order.orderDirection))
1007
846
  })
1008
847
  .instruction());
1009
848
  remainingUSDC = remainingUSDC.sub(usdcAmount);
1010
849
  }
1011
- if (userTradeIxs.length > 0) {
1012
- ixs.unshift(...userTradeIxs);
1013
- }
1014
850
  if (ixs.length === 0) {
1015
851
  throw new Error('No matching orders found to fill the requested amount');
1016
852
  }
@@ -1020,13 +856,11 @@ class TriadProtocol {
1020
856
  /**
1021
857
  * Market Ask Order
1022
858
  * @param args.marketId - The ID of the Market
1023
- * @param args.orders - Array of orders to execute
1024
- * @param args.orders.shares - The amount of shares to sell
1025
- * @param args.orders.bidOrderId - The ID of the Bid Order
1026
- * @param args.orders.userNonce - The nonce of the user
859
+ * @param args.shares - The amount of shares to sell
860
+ * @param args.bookOrderBidId - The ID of the Bid Order
1027
861
  * @param args.orderDirection - The direction of the Order
1028
862
  */
1029
- marketAskOrder({ marketId, orders, orderDirection }) {
863
+ marketAskOrder({ marketId, shares, orderDirection }) {
1030
864
  return __awaiter(this, void 0, void 0, function* () {
1031
865
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
1032
866
  const ixs = [];
@@ -1037,43 +871,38 @@ class TriadProtocol {
1037
871
  : orderBook.flop.bid;
1038
872
  const sortedOrders = bidOrders.sort((a, b) => Number(b.price) - Number(a.price));
1039
873
  let amountOfUSDC = new bn_js_1.default(0);
1040
- for (const inputOrder of orders) {
1041
- let remainingShares = new bn_js_1.default(inputOrder.shares * Math.pow(10, constants_1.BASE_DECIMALS));
1042
- for (const order of sortedOrders) {
1043
- if (remainingShares.lte(new bn_js_1.default(0)))
1044
- break;
1045
- if (order.authority === this.program.provider.publicKey.toBase58() ||
1046
- order.linkedBookOrderId !== constants_1.BOOK_ORDER_NULL.toString()) {
1047
- continue;
1048
- }
1049
- const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
1050
- const sharesToSell = bn_js_1.default.min(remainingShares, availableShares);
1051
- if (sharesToSell.lt(new bn_js_1.default(0)))
1052
- continue;
1053
- remainingShares = remainingShares.sub(sharesToSell);
1054
- const orderPrice = new bn_js_1.default(order.price);
1055
- const effectivePriceDecimal = (0, feeCalculator_1.applySellFee)(orderPrice.toNumber() / 1000000);
1056
- const adjustedPrice = new bn_js_1.default(Math.floor(effectivePriceDecimal * 1000000));
1057
- amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS))));
1058
- ixs.push(yield this.program.methods
1059
- .marketAskOrder({
1060
- shares: new bn_js_1.default(sharesToSell),
1061
- orderDirection,
1062
- bidOrderId: new bn_js_1.default(inputOrder.bidOrderId),
1063
- bookOrderBidId: new bn_js_1.default(order.id)
1064
- })
1065
- .accounts({
1066
- signer: this.program.provider.publicKey,
1067
- payer: this.rpcOptions.payer,
1068
- market: marketPDA,
1069
- buyerAuthority: new web3_js_1.PublicKey(order.authority),
1070
- buyerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
1071
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
1072
- marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
1073
- sellerTrade: this.getUserPDA(this.program.provider.publicKey, inputOrder.userNonce)
1074
- })
1075
- .instruction());
874
+ let remainingShares = new bn_js_1.default(shares * Math.pow(10, constants_1.BASE_DECIMALS));
875
+ for (const order of sortedOrders) {
876
+ if (remainingShares.lte(new bn_js_1.default(0)))
877
+ break;
878
+ if (order.authority === this.program.provider.publicKey.toBase58() ||
879
+ order.linkedBookOrderId !== constants_1.BOOK_ORDER_NULL.toString()) {
880
+ continue;
1076
881
  }
882
+ const availableShares = new bn_js_1.default(order.totalShares).sub(new bn_js_1.default(order.filledShares));
883
+ const sharesToSell = bn_js_1.default.min(remainingShares, availableShares);
884
+ if (sharesToSell.lt(new bn_js_1.default(0)))
885
+ continue;
886
+ remainingShares = remainingShares.sub(sharesToSell);
887
+ const orderPrice = new bn_js_1.default(order.price);
888
+ const effectivePriceDecimal = (0, feeCalculator_1.applySellFee)(orderPrice.toNumber() / 1000000);
889
+ const adjustedPrice = new bn_js_1.default(Math.floor(effectivePriceDecimal * 1000000));
890
+ amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS))));
891
+ ixs.push(yield this.program.methods
892
+ .marketAskOrder({
893
+ shares: new bn_js_1.default(sharesToSell),
894
+ orderDirection,
895
+ bookOrderBidId: new bn_js_1.default(order.id)
896
+ })
897
+ .accounts({
898
+ signer: this.program.provider.publicKey,
899
+ payer: this.rpcOptions.payer,
900
+ market: marketPDA,
901
+ buyerAuthority: new web3_js_1.PublicKey(order.authority),
902
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
903
+ marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID)
904
+ })
905
+ .instruction());
1077
906
  }
1078
907
  if (ixs.length === 0) {
1079
908
  throw new Error('No matching orders found to fill the requested amount');
@@ -1135,5 +964,31 @@ class TriadProtocol {
1135
964
  return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
1136
965
  });
1137
966
  }
967
+ closeUser(userTrade) {
968
+ return __awaiter(this, void 0, void 0, function* () {
969
+ const ixs = [
970
+ yield this.program.methods
971
+ .closeUser()
972
+ .accounts({ userTrade })
973
+ .instruction()
974
+ ];
975
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
976
+ });
977
+ }
978
+ migrateUserTradeToOrder(userTrade, marketId, orderId, orderDirection) {
979
+ return __awaiter(this, void 0, void 0, function* () {
980
+ const ixs = [
981
+ yield this.program.methods
982
+ .migrateUserTradeToOrder({
983
+ marketId: new bn_js_1.default(marketId),
984
+ orderId: new bn_js_1.default(orderId),
985
+ orderDirection
986
+ })
987
+ .accounts({ userTrade })
988
+ .instruction()
989
+ ];
990
+ return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);
991
+ });
992
+ }
1138
993
  }
1139
994
  exports.default = TriadProtocol;
package/dist/poseidon.js CHANGED
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const constants_1 = require("./utils/constants");
16
16
  const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
17
17
  const pda_1 = require("./utils/pda");
18
- const pda_2 = require("./utils/pda");
19
18
  class Poseidon {
20
19
  constructor(program, rpcOptions) {
21
20
  this.program = program;
@@ -100,7 +99,6 @@ class Poseidon {
100
99
  .addTraderPoseidon()
101
100
  .accounts({
102
101
  poseidonAsset,
103
- userTrade: (0, pda_2.getUserTradePDA)(this.program.programId, user),
104
102
  corePoseidonCollection: constants_1.POSEIDON_CORE_COLLECTION,
105
103
  poseidonCollection: (0, pda_1.getCollectionPDA)(this.program.programId, constants_1.POSEIDON_COLLECTION_SYMBOL)
106
104
  })
@@ -120,7 +118,6 @@ class Poseidon {
120
118
  yield this.program.methods
121
119
  .removeTraderPoseidon()
122
120
  .accounts({
123
- userTrade: (0, pda_2.getUserTradePDA)(this.program.programId, user),
124
121
  poseidonAsset,
125
122
  corePoseidonCollection: constants_1.POSEIDON_CORE_COLLECTION,
126
123
  poseidonCollection: (0, pda_1.getCollectionPDA)(this.program.programId, constants_1.POSEIDON_COLLECTION_SYMBOL)
package/dist/stake.js CHANGED
@@ -81,6 +81,7 @@ class Stake {
81
81
  amount: new anchor_1.BN(amount * Math.pow(10, 6))
82
82
  })
83
83
  .accounts({
84
+ signer: this.program.provider.publicKey,
84
85
  mint: constants_1.TRD_MINT
85
86
  })
86
87
  .instruction()
@@ -98,6 +99,7 @@ class Stake {
98
99
  yield this.program.methods
99
100
  .addStakeVaultRewards(new anchor_1.BN(amount * Math.pow(10, 6)))
100
101
  .accounts({
102
+ signer: this.program.provider.publicKey,
101
103
  mint: constants_1.TRD_MINT
102
104
  })
103
105
  .instruction()
@@ -114,6 +116,9 @@ class Stake {
114
116
  const ixs = [
115
117
  yield this.program.methods
116
118
  .updateStakeVaultIsLocked(isLocked)
119
+ .accounts({
120
+ signer: this.program.provider.publicKey
121
+ })
117
122
  .instruction()
118
123
  ];
119
124
  return (0, sendVersionedTransaction_1.default)(this.program, ixs, this.rpcOptions);