@triadxyz/triad-protocol 3.0.9-beta → 3.1.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
@@ -45,6 +45,11 @@ export default class TriadProtocolClient {
45
45
  *
46
46
  */
47
47
  getUserOrdersByMarketId(wallet: PublicKey, marketId: number): Promise<import("./types").Order[]>;
48
+ /**
49
+ * Get User Book Orders
50
+ *
51
+ */
52
+ getUserBookOrders(wallet: PublicKey, marketId: number): Promise<BookOrder[]>;
48
53
  /**
49
54
  * Get Pool By ID
50
55
  * @param poolId - The ID of the pool
@@ -362,7 +367,7 @@ export default class TriadProtocolClient {
362
367
  * @param refer - The refer public key
363
368
  *
364
369
  */
365
- getUserTradeIxs(refer?: PublicKey): Promise<{
370
+ getUserTradeIxs(): Promise<{
366
371
  userTradePDA: PublicKey;
367
372
  ixs: TransactionInstruction[];
368
373
  nonce: number;
@@ -392,7 +397,7 @@ export default class TriadProtocolClient {
392
397
  *
393
398
  * @param options - RPC options
394
399
  */
395
- placeBidOrder({ marketId, amount, direction, mint, price, isTrdPayout }: PlaceBidOrderArgs, options?: RpcOptions): Promise<string>;
400
+ placeBidOrder({ marketId, orders, mint, isTrdPayout }: PlaceBidOrderArgs, options?: RpcOptions): Promise<string>;
396
401
  /**
397
402
  * Place Ask Order
398
403
  * @param args.marketId - The ID of the Market
@@ -415,7 +420,7 @@ export default class TriadProtocolClient {
415
420
  *
416
421
  * @param options - RPC options
417
422
  */
418
- cancelBidOrder({ marketId, orderId, userNonce, mint, direction }: CancelBidOrderArgs, options?: RpcOptions): Promise<string>;
423
+ cancelBidOrder({ marketId, orders, mint }: CancelBidOrderArgs, options?: RpcOptions): Promise<string>;
419
424
  /**
420
425
  * Cancel Ask Order
421
426
  * @param args.marketId - The ID of the Market
@@ -426,7 +431,7 @@ export default class TriadProtocolClient {
426
431
  *
427
432
  * @param options - RPC options
428
433
  */
429
- cancelAskOrder({ marketId, orderId, userNonce, direction }: CancelAskOrderArgs, options?: RpcOptions): Promise<string>;
434
+ cancelAskOrder({ marketId, orders }: CancelAskOrderArgs, options?: RpcOptions): Promise<string>;
430
435
  /**
431
436
  * Market Bid Order
432
437
  * @param args.marketId - The ID of the Market
package/dist/index.js CHANGED
@@ -113,6 +113,20 @@ class TriadProtocolClient {
113
113
  return userTrades.flatMap((userTrade) => userTrade.orders.filter((order) => order.marketId === marketId.toString()));
114
114
  });
115
115
  }
116
+ /**
117
+ * Get User Book Orders
118
+ *
119
+ */
120
+ getUserBookOrders(wallet, marketId) {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ const orderBook = yield this.program.account.orderBook.fetch((0, pda_1.getOrderBookPDA)(this.program.programId, marketId));
123
+ const orders = [...orderBook.hypeOrders, ...orderBook.flopOrders];
124
+ return orders
125
+ .map((order) => (0, helpers_1.formatBookOrder)(order))
126
+ .filter((order) => order.authority === wallet.toBase58() &&
127
+ order.linkedBookOrderId === constants_1.BOOK_ORDER_NULL.toString());
128
+ });
129
+ }
116
130
  /**
117
131
  * Get Pool By ID
118
132
  * @param poolId - The ID of the pool
@@ -516,7 +530,7 @@ class TriadProtocolClient {
516
530
  wallet: this.program.provider.publicKey.toBase58(),
517
531
  inToken: constants_1.USDC_MINT.toBase58(),
518
532
  outToken: constants_1.TRD_MINT.toString(),
519
- amount: order.shares / Math.pow(10, this.decimals)
533
+ amount: Math.floor(order.shares / Math.pow(10, this.decimals))
520
534
  });
521
535
  if (swapIxs.length === 0) {
522
536
  return;
@@ -644,8 +658,8 @@ class TriadProtocolClient {
644
658
  break;
645
659
  }
646
660
  userTrade.orders.forEach((order) => {
647
- if (order.orderStatus !== types_1.OrderStatus.OPEN &&
648
- order.orderStatus !== types_1.OrderStatus.WAITING) {
661
+ if (order.orderStatus === types_1.OrderStatus.INIT ||
662
+ order.orderStatus === types_1.OrderStatus.CLOSED) {
649
663
  nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
650
664
  }
651
665
  });
@@ -660,15 +674,12 @@ class TriadProtocolClient {
660
674
  * @param refer - The refer public key
661
675
  *
662
676
  */
663
- getUserTradeIxs(refer) {
677
+ getUserTradeIxs() {
664
678
  return __awaiter(this, void 0, void 0, function* () {
665
679
  const ixs = [];
666
680
  let myUserTrades = [];
667
681
  myUserTrades = yield this.getMyUserTrades(this.program.provider.publicKey);
668
682
  let referPDA = null;
669
- if (refer) {
670
- referPDA = (0, pda_1.getReferPDA)(this.program.programId, refer);
671
- }
672
683
  if (myUserTrades.length === 0) {
673
684
  ixs.push(yield this.program.methods
674
685
  .createUserTrade()
@@ -739,14 +750,21 @@ class TriadProtocolClient {
739
750
  *
740
751
  * @param options - RPC options
741
752
  */
742
- placeBidOrder({ marketId, amount, direction, mint, price, isTrdPayout }, options) {
753
+ placeBidOrder({ marketId, orders, mint, isTrdPayout }, options) {
743
754
  return __awaiter(this, void 0, void 0, function* () {
744
755
  const ixs = [];
745
756
  const addressLookupTableAccounts = [];
757
+ if (orders.length > 4) {
758
+ throw new Error('You can only place up to 4 orders at a time');
759
+ }
746
760
  let amountInUSDC = new bn_js_1.default(0);
761
+ let totalAmount = 0;
762
+ for (const order of orders) {
763
+ totalAmount += order.amount;
764
+ }
747
765
  if (isTrdPayout) {
748
766
  const trdPrice = yield (0, swap_1.getPrice)(constants_1.TRD_MINT.toString());
749
- const amountInTRD = (amount / trdPrice) * 1.06;
767
+ const amountInTRD = (totalAmount / trdPrice) * 1.06;
750
768
  const { swapIxs, addressLookupTableAccounts: swapAddressLookupTableAccounts, outAmount, setupInstructions } = yield (0, swap_1.swap)({
751
769
  connection: this.program.provider.connection,
752
770
  wallet: this.program.provider.publicKey.toBase58(),
@@ -761,29 +779,31 @@ class TriadProtocolClient {
761
779
  ixs.push(...setupInstructions);
762
780
  ixs.push(...swapIxs);
763
781
  addressLookupTableAccounts.push(...swapAddressLookupTableAccounts);
764
- if (amountInUSDC.lt(new bn_js_1.default(amount * Math.pow(10, this.decimals)))) {
782
+ if (amountInUSDC.lt(new bn_js_1.default(totalAmount * Math.pow(10, this.decimals)))) {
765
783
  return;
766
784
  }
767
785
  }
768
- const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(direction)[0]);
786
+ const { userTradePDA, userTradeIxs } = yield this.getUserTradeNonce(marketId, Object.keys(orders[0].direction)[0]);
769
787
  if (userTradeIxs.length > 0) {
770
788
  ixs.push(...userTradeIxs);
771
789
  }
772
- ixs.push(yield this.program.methods
773
- .placeBidOrder({
774
- amount: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
775
- price: new bn_js_1.default(price * Math.pow(10, this.decimals)),
776
- orderDirection: direction
777
- })
778
- .accounts({
779
- signer: this.program.provider.publicKey,
780
- market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
781
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
782
- userTrade: userTradePDA,
783
- mint,
784
- tokenProgram: (0, helpers_1.getTokenProgram)(mint)
785
- })
786
- .instruction());
790
+ for (const order of orders) {
791
+ ixs.push(yield this.program.methods
792
+ .placeBidOrder({
793
+ amount: new bn_js_1.default(order.amount * Math.pow(10, this.decimals)),
794
+ price: new bn_js_1.default(order.price * Math.pow(10, this.decimals)),
795
+ orderDirection: order.direction
796
+ })
797
+ .accounts({
798
+ signer: this.program.provider.publicKey,
799
+ market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
800
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
801
+ userTrade: userTradePDA,
802
+ mint,
803
+ tokenProgram: (0, helpers_1.getTokenProgram)(mint)
804
+ })
805
+ .instruction());
806
+ }
787
807
  return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
788
808
  });
789
809
  }
@@ -802,6 +822,9 @@ class TriadProtocolClient {
802
822
  placeAskOrder({ marketId, orders }, options) {
803
823
  return __awaiter(this, void 0, void 0, function* () {
804
824
  const ixs = [];
825
+ if (orders.length > 4) {
826
+ throw new Error('You can only place up to 4 orders at a time');
827
+ }
805
828
  for (const order of orders) {
806
829
  ixs.push(yield this.program.methods
807
830
  .placeAskOrder({
@@ -829,23 +852,28 @@ class TriadProtocolClient {
829
852
  *
830
853
  * @param options - RPC options
831
854
  */
832
- cancelBidOrder({ marketId, orderId, userNonce, mint, direction }, options) {
855
+ cancelBidOrder({ marketId, orders, mint }, options) {
833
856
  return __awaiter(this, void 0, void 0, function* () {
834
857
  const ixs = [];
835
- ixs.push(yield this.program.methods
836
- .cancelBidOrder({
837
- orderId: new bn_js_1.default(orderId),
838
- orderDirection: direction
839
- })
840
- .accounts({
841
- signer: this.program.provider.publicKey,
842
- market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
843
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
844
- userTrade: this.getUserPDA(this.program.provider.publicKey, userNonce),
845
- mint,
846
- tokenProgram: (0, helpers_1.getTokenProgram)(mint)
847
- })
848
- .instruction());
858
+ if (orders.length > 4) {
859
+ throw new Error('You can only cancel up to 4 orders at a time');
860
+ }
861
+ for (const order of orders) {
862
+ ixs.push(yield this.program.methods
863
+ .cancelBidOrder({
864
+ orderId: new bn_js_1.default(order.orderId),
865
+ orderDirection: order.direction
866
+ })
867
+ .accounts({
868
+ signer: this.program.provider.publicKey,
869
+ market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
870
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
871
+ userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce),
872
+ mint,
873
+ tokenProgram: (0, helpers_1.getTokenProgram)(mint)
874
+ })
875
+ .instruction());
876
+ }
849
877
  return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
850
878
  });
851
879
  }
@@ -859,21 +887,26 @@ class TriadProtocolClient {
859
887
  *
860
888
  * @param options - RPC options
861
889
  */
862
- cancelAskOrder({ marketId, orderId, userNonce, direction }, options) {
890
+ cancelAskOrder({ marketId, orders }, options) {
863
891
  return __awaiter(this, void 0, void 0, function* () {
864
892
  const ixs = [];
865
- ixs.push(yield this.program.methods
866
- .cancelAskOrder({
867
- orderId: new bn_js_1.default(orderId),
868
- orderDirection: direction
869
- })
870
- .accounts({
871
- signer: this.program.provider.publicKey,
872
- market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
873
- orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
874
- userTrade: this.getUserPDA(this.program.provider.publicKey, userNonce)
875
- })
876
- .instruction());
893
+ if (orders.length > 4) {
894
+ throw new Error('You can only cancel up to 4 orders at a time');
895
+ }
896
+ for (const order of orders) {
897
+ ixs.push(yield this.program.methods
898
+ .cancelAskOrder({
899
+ orderId: new bn_js_1.default(order.orderId),
900
+ orderDirection: order.direction
901
+ })
902
+ .accounts({
903
+ signer: this.program.provider.publicKey,
904
+ market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
905
+ orderBook: (0, pda_1.getOrderBookPDA)(this.program.programId, marketId),
906
+ userTrade: this.getUserPDA(this.program.provider.publicKey, order.userNonce)
907
+ })
908
+ .instruction());
909
+ }
877
910
  return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
878
911
  });
879
912
  }
@@ -108,13 +108,15 @@ export declare enum OrderSide {
108
108
  }
109
109
  export type PlaceBidOrderArgs = {
110
110
  marketId: number;
111
- amount: number;
112
- price: number;
113
- direction: {
114
- hype: {};
115
- } | {
116
- flop: {};
117
- };
111
+ orders: {
112
+ amount: number;
113
+ price: number;
114
+ direction: {
115
+ hype: {};
116
+ } | {
117
+ flop: {};
118
+ };
119
+ }[];
118
120
  mint: PublicKey;
119
121
  isTrdPayout: boolean;
120
122
  };
@@ -188,24 +190,28 @@ export type CreatePoolArgs = {
188
190
  };
189
191
  export type CancelBidOrderArgs = {
190
192
  marketId: number;
191
- orderId: number;
192
- userNonce: number;
193
+ orders: {
194
+ orderId: number;
195
+ userNonce: number;
196
+ direction: {
197
+ hype: {};
198
+ } | {
199
+ flop: {};
200
+ };
201
+ }[];
193
202
  mint: PublicKey;
194
- direction: {
195
- hype: {};
196
- } | {
197
- flop: {};
198
- };
199
203
  };
200
204
  export type CancelAskOrderArgs = {
201
205
  marketId: number;
202
- orderId: number;
203
- userNonce: number;
204
- direction: {
205
- hype: {};
206
- } | {
207
- flop: {};
208
- };
206
+ orders: {
207
+ orderId: number;
208
+ userNonce: number;
209
+ direction: {
210
+ hype: {};
211
+ } | {
212
+ flop: {};
213
+ };
214
+ }[];
209
215
  };
210
216
  export type MarketBidOrderArgs = {
211
217
  marketId: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "3.0.9-beta",
3
+ "version": "3.1.0-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",