@triadxyz/triad-protocol 3.3.8-beta → 3.4.0-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.d.ts CHANGED
@@ -11,8 +11,6 @@ export * from './types';
11
11
  export * from './utils/helpers';
12
12
  export * from './utils/merkle';
13
13
  export * from './utils/feeCalculator';
14
- export * from './utils/constants';
15
- export * from './utils/pda';
16
14
  export default class TriadProtocol {
17
15
  private connection;
18
16
  private wallet;
@@ -330,7 +328,7 @@ export default class TriadProtocol {
330
328
  /**
331
329
  * Get User Trade Ixs
332
330
  */
333
- getUserTradeIxs(wallet: PublicKey): Promise<{
331
+ getUserTradeIxs(): Promise<{
334
332
  userTradePDA: PublicKey;
335
333
  ixs: TransactionInstruction[];
336
334
  nonce: number;
@@ -344,7 +342,7 @@ export default class TriadProtocol {
344
342
  * @param marketId - The ID of the Market
345
343
  * @param orderDirection - The direction of the Order
346
344
  */
347
- getUserTradeNonce(wallet: PublicKey, marketId: number, orderDirection: OrderDirection): Promise<{
345
+ getUserTradeNonce(marketId: number, orderDirection: OrderDirection): Promise<{
348
346
  userTradePDA: PublicKey;
349
347
  userTradeIxs: TransactionInstruction[];
350
348
  }>;
@@ -361,8 +359,8 @@ export default class TriadProtocol {
361
359
  placeBidOrder({ orders, isTrdPayout }: PlaceBidOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
362
360
  /**
363
361
  * Place Ask Order
362
+ * @param args.marketId - The ID of the Market
364
363
  * @param args.orders - Array of orders to execute
365
- * @param args.orders.marketId - The ID of the Market
366
364
  * @param args.orders.amount - The amount of the Order
367
365
  * @param args.orders.price - The price of the Order
368
366
  * @param args.orders.bidOrderId - The ID of the Bid Order
@@ -400,9 +398,8 @@ export default class TriadProtocol {
400
398
  * @param args.orders.bidOrderId - The ID of the Bid Order
401
399
  * @param args.orders.userNonce - The nonce of the user
402
400
  * @param args.orderDirection - The direction of the Order
403
- * @param args.isTrdPayout - Whether to payout in TRD or not
404
401
  */
405
- marketAskOrder({ marketId, orders, orderDirection, isTrdPayout }: MarketAskOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
402
+ marketAskOrder({ marketId, orders, orderDirection }: MarketAskOrderArgs): Promise<string | import("@solana/web3.js").VersionedTransaction>;
406
403
  /**
407
404
  * Get Orders By Market ID
408
405
  * @param marketId - Market ID
package/dist/index.js CHANGED
@@ -46,8 +46,6 @@ __exportStar(require("./types"), exports);
46
46
  __exportStar(require("./utils/helpers"), exports);
47
47
  __exportStar(require("./utils/merkle"), exports);
48
48
  __exportStar(require("./utils/feeCalculator"), exports);
49
- __exportStar(require("./utils/constants"), exports);
50
- __exportStar(require("./utils/pda"), exports);
51
49
  class TriadProtocol {
52
50
  constructor(connection, wallet, rpcOptions) {
53
51
  this.connection = connection;
@@ -527,7 +525,7 @@ class TriadProtocol {
527
525
  return __awaiter(this, void 0, void 0, function* () {
528
526
  const ixs = [];
529
527
  const addressLookupTableAccounts = [];
530
- const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs(this.program.provider.publicKey);
528
+ const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
531
529
  if (userTradeIxs.length > 0) {
532
530
  ixs.push(...userTradeIxs);
533
531
  }
@@ -660,21 +658,21 @@ class TriadProtocol {
660
658
  /**
661
659
  * Get User Trade Ixs
662
660
  */
663
- getUserTradeIxs(wallet) {
661
+ getUserTradeIxs() {
664
662
  return __awaiter(this, void 0, void 0, function* () {
665
663
  const ixs = [];
666
664
  let myUserTrades = [];
667
- myUserTrades = yield this.getMyUserTrades(wallet);
665
+ myUserTrades = yield this.getMyUserTrades(this.program.provider.publicKey);
668
666
  if (myUserTrades.length === 0) {
669
667
  ixs.push(yield this.program.methods
670
668
  .createUserTrade()
671
669
  .accounts({
672
- signer: wallet,
670
+ signer: this.program.provider.publicKey,
673
671
  payer: this.rpcOptions.payer
674
672
  })
675
673
  .instruction());
676
674
  return {
677
- userTradePDA: this.getUserPDA(wallet),
675
+ userTradePDA: this.getUserPDA(this.program.provider.publicKey),
678
676
  ixs,
679
677
  nonce: 0
680
678
  };
@@ -695,17 +693,17 @@ class TriadProtocol {
695
693
  throw new Error('No open orders found');
696
694
  }
697
695
  return {
698
- userTradePDA: this.getUserPDA(wallet, nonce),
696
+ userTradePDA: this.getUserPDA(this.program.provider.publicKey, nonce),
699
697
  ixs
700
698
  };
701
699
  }
702
700
  catch (_a) {
703
701
  const mainUserTrade = myUserTrades.find((trade) => !trade.isSubUser);
704
- const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, wallet, Number(mainUserTrade.nonce) + 1);
702
+ const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, Number(mainUserTrade.nonce) + 1);
705
703
  ixs.push(yield this.program.methods
706
704
  .createSubUserTrade(subUserTradePDA)
707
705
  .accounts({
708
- signer: wallet,
706
+ signer: this.program.provider.publicKey,
709
707
  payer: this.rpcOptions.payer
710
708
  })
711
709
  .instruction());
@@ -721,20 +719,20 @@ class TriadProtocol {
721
719
  * @param marketId - The ID of the Market
722
720
  * @param orderDirection - The direction of the Order
723
721
  */
724
- getUserTradeNonce(wallet, marketId, orderDirection) {
722
+ getUserTradeNonce(marketId, orderDirection) {
725
723
  var _a;
726
724
  return __awaiter(this, void 0, void 0, function* () {
727
- const userOrders = yield this.getUserOrdersByMarketId(wallet, marketId);
725
+ const userOrders = yield this.getUserOrdersByMarketId(this.program.provider.publicKey, marketId);
728
726
  const userNonce = (_a = userOrders.find((order) => order.orderDirection === orderDirection &&
729
727
  order.orderStatus === types_1.OrderStatus.OPEN &&
730
728
  Number(order.marketId) === marketId)) === null || _a === void 0 ? void 0 : _a.userNonce;
731
729
  let userTradePDA = null;
732
730
  let userTradeIxs = [];
733
731
  if (userNonce) {
734
- userTradePDA = this.getUserPDA(wallet, Number(userNonce));
732
+ userTradePDA = this.getUserPDA(this.program.provider.publicKey, Number(userNonce));
735
733
  }
736
734
  if (!userNonce) {
737
- const { userTradePDA: user, ixs: ixsUser } = yield this.getUserTradeIxs(wallet);
735
+ const { userTradePDA: user, ixs: ixsUser } = yield this.getUserTradeIxs();
738
736
  userTradePDA = user;
739
737
  userTradeIxs = ixsUser;
740
738
  }
@@ -755,8 +753,8 @@ class TriadProtocol {
755
753
  return __awaiter(this, void 0, void 0, function* () {
756
754
  const ixs = [];
757
755
  const addressLookupTableAccounts = [];
758
- if (orders.length > 5) {
759
- throw new Error('You can only place up to 5 orders at a time');
756
+ if (orders.length > 4) {
757
+ throw new Error('You can only place up to 4 orders at a time');
760
758
  }
761
759
  let amountInUSDC = new bn_js_1.default(0);
762
760
  let totalAmount = 0;
@@ -785,6 +783,10 @@ class TriadProtocol {
785
783
  return;
786
784
  }
787
785
  }
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
+ }
788
790
  for (const order of orders) {
789
791
  ixs.push(yield this.program.methods
790
792
  .placeBidOrder({
@@ -796,7 +798,7 @@ class TriadProtocol {
796
798
  signer: this.program.provider.publicKey,
797
799
  market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
798
800
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, order.marketId),
799
- userTrade: this.getUserPDA(this.program.provider.publicKey)
801
+ userTrade: userTradePDA
800
802
  })
801
803
  .instruction());
802
804
  }
@@ -805,8 +807,8 @@ class TriadProtocol {
805
807
  }
806
808
  /**
807
809
  * Place Ask Order
810
+ * @param args.marketId - The ID of the Market
808
811
  * @param args.orders - Array of orders to execute
809
- * @param args.orders.marketId - The ID of the Market
810
812
  * @param args.orders.amount - The amount of the Order
811
813
  * @param args.orders.price - The price of the Order
812
814
  * @param args.orders.bidOrderId - The ID of the Bid Order
@@ -845,8 +847,8 @@ class TriadProtocol {
845
847
  cancelBidOrder({ orders }) {
846
848
  return __awaiter(this, void 0, void 0, function* () {
847
849
  const ixs = [];
848
- if (orders.length > 4) {
849
- throw new Error('You can only cancel up to 4 orders at a time');
850
+ if (orders.length > 5) {
851
+ throw new Error('You can only cancel up to 5 orders at a time');
850
852
  }
851
853
  for (const order of orders) {
852
854
  ixs.push(yield this.program.methods
@@ -858,7 +860,8 @@ class TriadProtocol {
858
860
  signer: this.program.provider.publicKey,
859
861
  payer: this.rpcOptions.payer,
860
862
  market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
861
- orderBook: (0, pda_1.getOrderBookPDA)(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)
862
865
  })
863
866
  .instruction());
864
867
  }
@@ -907,8 +910,7 @@ class TriadProtocol {
907
910
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
908
911
  const ixs = [];
909
912
  const addressLookupTableAccounts = [];
910
- const sellerTradeIxs = [];
911
- const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(this.program.provider.publicKey, marketId, Object.keys(orderDirection)[0]);
913
+ const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(orderDirection)[0]);
912
914
  const orderBook = yield this.getOrderBook(marketId);
913
915
  let remainingUSDC = new bn_js_1.default(amount * Math.pow(10, constants_1.BASE_DECIMALS));
914
916
  if (isTrdPayout) {
@@ -986,10 +988,6 @@ class TriadProtocol {
986
988
  .div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS)));
987
989
  if (usdcAmount.lte(new bn_js_1.default(0)))
988
990
  continue;
989
- const { userTradePDA: sellerTradePDA, userTradeIxs: innerSellerTradeIxs } = yield this.getUserTradeNonce(new web3_js_1.PublicKey(order.authority), marketId, Object.keys(orderDirection)[0]);
990
- if (innerSellerTradeIxs.length > 0 && sellerTradeIxs.length === 0) {
991
- sellerTradeIxs.push(...innerSellerTradeIxs);
992
- }
993
991
  ixs.push(yield this.program.methods
994
992
  .marketBidOrder({
995
993
  amount: new bn_js_1.default(usdcAmount),
@@ -1003,16 +1001,13 @@ class TriadProtocol {
1003
1001
  buyerTrade: userTradePDA,
1004
1002
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
1005
1003
  sellerAuthority: new web3_js_1.PublicKey(order.authority),
1006
- sellerTrade: sellerTradePDA,
1004
+ sellerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
1007
1005
  marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
1008
1006
  buyerAta: (0, pda_1.getTokenATA)(this.program.provider.publicKey, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID)
1009
1007
  })
1010
1008
  .instruction());
1011
1009
  remainingUSDC = remainingUSDC.sub(usdcAmount);
1012
1010
  }
1013
- if (sellerTradeIxs.length > 0) {
1014
- ixs.unshift(...sellerTradeIxs);
1015
- }
1016
1011
  if (userTradeIxs.length > 0) {
1017
1012
  ixs.unshift(...userTradeIxs);
1018
1013
  }
@@ -1030,14 +1025,12 @@ class TriadProtocol {
1030
1025
  * @param args.orders.bidOrderId - The ID of the Bid Order
1031
1026
  * @param args.orders.userNonce - The nonce of the user
1032
1027
  * @param args.orderDirection - The direction of the Order
1033
- * @param args.isTrdPayout - Whether to payout in TRD or not
1034
1028
  */
1035
- marketAskOrder({ marketId, orders, orderDirection, isTrdPayout }) {
1029
+ marketAskOrder({ marketId, orders, orderDirection }) {
1036
1030
  return __awaiter(this, void 0, void 0, function* () {
1037
1031
  const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
1038
1032
  const ixs = [];
1039
1033
  const addressLookupTableAccounts = [];
1040
- const buyerTradeIxs = [];
1041
1034
  const orderBook = yield this.getOrderBook(marketId);
1042
1035
  const bidOrders = Object.keys(orderDirection)[0] === 'hype'
1043
1036
  ? orderBook.hype.bid
@@ -1062,10 +1055,6 @@ class TriadProtocol {
1062
1055
  const effectivePriceDecimal = (0, feeCalculator_1.applySellFee)(orderPrice.toNumber() / 1000000);
1063
1056
  const adjustedPrice = new bn_js_1.default(Math.floor(effectivePriceDecimal * 1000000));
1064
1057
  amountOfUSDC = amountOfUSDC.add(sharesToSell.mul(adjustedPrice).div(new bn_js_1.default(Math.pow(10, constants_1.BASE_DECIMALS))));
1065
- const { userTradePDA: buyerTradePDA, userTradeIxs: innerBuyerTradeIxs } = yield this.getUserTradeNonce(new web3_js_1.PublicKey(order.authority), marketId, Object.keys(orderDirection)[0]);
1066
- if (innerBuyerTradeIxs.length > 0 && buyerTradeIxs.length === 0) {
1067
- buyerTradeIxs.push(...innerBuyerTradeIxs);
1068
- }
1069
1058
  ixs.push(yield this.program.methods
1070
1059
  .marketAskOrder({
1071
1060
  shares: new bn_js_1.default(sharesToSell),
@@ -1078,7 +1067,7 @@ class TriadProtocol {
1078
1067
  payer: this.rpcOptions.payer,
1079
1068
  market: marketPDA,
1080
1069
  buyerAuthority: new web3_js_1.PublicKey(order.authority),
1081
- buyerTrade: buyerTradePDA,
1070
+ buyerTrade: this.getUserPDA(new web3_js_1.PublicKey(order.authority), Number(order.userNonce)),
1082
1071
  orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
1083
1072
  marketAta: (0, pda_1.getTokenATA)(marketPDA, constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID),
1084
1073
  sellerTrade: this.getUserPDA(this.program.provider.publicKey, inputOrder.userNonce)
@@ -1086,24 +1075,6 @@ class TriadProtocol {
1086
1075
  .instruction());
1087
1076
  }
1088
1077
  }
1089
- if (isTrdPayout) {
1090
- const { setupInstructions, swapIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts } = yield (0, swap_1.swap)({
1091
- connection: this.program.provider.connection,
1092
- wallet: this.program.provider.publicKey.toBase58(),
1093
- inToken: constants_1.USDC_MINT.toBase58(),
1094
- outToken: constants_1.TRD_MINT.toString(),
1095
- amount: amountOfUSDC.toNumber() / Math.pow(10, 6),
1096
- payer: this.rpcOptions.payer.toBase58()
1097
- });
1098
- if (swapIxs.length > 0) {
1099
- ixs.push(...setupInstructions);
1100
- ixs.push(...swapIxs);
1101
- addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
1102
- }
1103
- }
1104
- if (buyerTradeIxs.length > 0) {
1105
- ixs.unshift(...buyerTradeIxs);
1106
- }
1107
1078
  if (ixs.length === 0) {
1108
1079
  throw new Error('No matching orders found to fill the requested amount');
1109
1080
  }
@@ -210,6 +210,10 @@
210
210
  "name": "order_book",
211
211
  "writable": true
212
212
  },
213
+ {
214
+ "name": "user_trade",
215
+ "writable": true
216
+ },
213
217
  {
214
218
  "name": "mint",
215
219
  "writable": true,
@@ -286,6 +286,10 @@ export type TriadProtocol = {
286
286
  name: 'orderBook';
287
287
  writable: true;
288
288
  },
289
+ {
290
+ name: 'userTrade';
291
+ writable: true;
292
+ },
289
293
  {
290
294
  name: 'mint';
291
295
  writable: true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "3.3.8-beta",
3
+ "version": "3.4.0-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",