@triadxyz/triad-protocol 1.8.0-beta → 1.8.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/trade.js CHANGED
@@ -12,6 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
+ const web3_js_1 = require("@solana/web3.js");
15
16
  const trade_1 = require("./types/trade");
16
17
  const bn_js_1 = __importDefault(require("bn.js"));
17
18
  const constants_1 = require("./utils/constants");
@@ -153,35 +154,12 @@ class Trade {
153
154
  openOrder({ marketId, amount, direction, mint }, options) {
154
155
  return __awaiter(this, void 0, void 0, function* () {
155
156
  const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
156
- let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
157
157
  const ixs = [];
158
158
  const addressLookupTableAccounts = [];
159
159
  let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
160
- let myUserTrades = [];
161
- myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
162
- if (myUserTrades.length === 0) {
163
- ixs.push(yield this.program.methods
164
- .createUserTrade()
165
- .accounts({
166
- signer: this.provider.publicKey
167
- })
168
- .instruction());
169
- }
170
- if (myUserTrades.length > 0) {
171
- try {
172
- userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
173
- }
174
- catch (_a) {
175
- const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
176
- const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
177
- userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
178
- ixs.push(yield this.program.methods
179
- .createSubUserTrade(subUserTradePDA)
180
- .accounts({
181
- signer: this.provider.publicKey
182
- })
183
- .instruction());
184
- }
160
+ const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
161
+ if (userTradeIxs.length > 0) {
162
+ ixs.push(...userTradeIxs);
185
163
  }
186
164
  ixs.push(yield this.program.methods
187
165
  .openOrder({
@@ -542,22 +520,45 @@ class Trade {
542
520
  const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
543
521
  const ixs = [];
544
522
  const { userTradePDA: buyerTrade, ixs: userTradeIxs } = yield this.getUserTradeIxs();
545
- // TODO: Get dynamic until fill all order
546
- const sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
547
- ixs.push(...userTradeIxs);
548
- ixs.push(yield this.program.methods
549
- .marketBidOrder({
550
- amount: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
551
- orderDirection: direction
552
- })
553
- .accounts({
554
- signer: this.provider.publicKey,
555
- market: marketPDA,
556
- buyerTrade,
557
- sellerTrade: sellerTradePDA,
558
- mint
559
- })
560
- .instruction());
523
+ const orderBook = yield this.getOrderBook(marketId);
524
+ let remainingUSDC = amount * Math.pow(10, this.decimals);
525
+ const orders = direction[0].hype ? orderBook.hype.ask : orderBook.flop.ask;
526
+ const sortedOrders = orders.sort((a, b) => Number(a.price) - Number(b.price));
527
+ for (const order of sortedOrders) {
528
+ if (remainingUSDC <= 0)
529
+ break;
530
+ const orderPrice = Number(order.price);
531
+ const maxSharesForPrice = remainingUSDC / orderPrice;
532
+ const availableShares = Number(order.totalShares);
533
+ const sharesToBuy = Math.min(maxSharesForPrice, availableShares);
534
+ const usdcAmount = sharesToBuy * orderPrice;
535
+ let sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority));
536
+ if (order.userNonce !== '0') {
537
+ const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), Number(order.userNonce));
538
+ sellerTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
539
+ }
540
+ ixs.push(yield this.program.methods
541
+ .marketBidOrder({
542
+ amount: new bn_js_1.default(usdcAmount),
543
+ orderDirection: direction
544
+ })
545
+ .accounts({
546
+ signer: this.provider.publicKey,
547
+ market: marketPDA,
548
+ buyerTrade,
549
+ sellerTrade: sellerTradePDA,
550
+ mint,
551
+ tokenProgram: (0, helpers_1.getTokenProgram)(mint)
552
+ })
553
+ .instruction());
554
+ remainingUSDC -= usdcAmount;
555
+ }
556
+ if (userTradeIxs.length > 0) {
557
+ ixs.unshift(...userTradeIxs);
558
+ }
559
+ if (ixs.length === 0) {
560
+ throw new Error('No matching orders found to fill the requested amount');
561
+ }
561
562
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
562
563
  });
563
564
  }
@@ -736,6 +736,115 @@
736
736
  ],
737
737
  "args": []
738
738
  },
739
+ {
740
+ "name": "market_ask_order",
741
+ "discriminator": [189, 66, 162, 254, 3, 85, 152, 54],
742
+ "accounts": [
743
+ {
744
+ "name": "signer",
745
+ "writable": true,
746
+ "signer": true
747
+ },
748
+ {
749
+ "name": "squads",
750
+ "writable": true,
751
+ "address": "6fcSf6qfwPNR9AUUNC1UWYZDy5cQ4TzTb2aaipN2zFdq"
752
+ },
753
+ {
754
+ "name": "seller_trade",
755
+ "writable": true
756
+ },
757
+ {
758
+ "name": "buyer_trade",
759
+ "writable": true
760
+ },
761
+ {
762
+ "name": "market",
763
+ "writable": true
764
+ },
765
+ {
766
+ "name": "mint",
767
+ "writable": true
768
+ },
769
+ {
770
+ "name": "user_from_ata",
771
+ "writable": true,
772
+ "pda": {
773
+ "seeds": [
774
+ {
775
+ "kind": "account",
776
+ "path": "signer"
777
+ },
778
+ {
779
+ "kind": "account",
780
+ "path": "token_program"
781
+ },
782
+ {
783
+ "kind": "account",
784
+ "path": "mint"
785
+ }
786
+ ],
787
+ "program": {
788
+ "kind": "const",
789
+ "value": [
790
+ 140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142,
791
+ 13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216,
792
+ 219, 233, 248, 89
793
+ ]
794
+ }
795
+ }
796
+ },
797
+ {
798
+ "name": "market_ata",
799
+ "writable": true,
800
+ "pda": {
801
+ "seeds": [
802
+ {
803
+ "kind": "account",
804
+ "path": "market"
805
+ },
806
+ {
807
+ "kind": "account",
808
+ "path": "token_program"
809
+ },
810
+ {
811
+ "kind": "account",
812
+ "path": "mint"
813
+ }
814
+ ],
815
+ "program": {
816
+ "kind": "const",
817
+ "value": [
818
+ 140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142,
819
+ 13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216,
820
+ 219, 233, 248, 89
821
+ ]
822
+ }
823
+ }
824
+ },
825
+ {
826
+ "name": "token_program"
827
+ },
828
+ {
829
+ "name": "associated_token_program",
830
+ "address": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
831
+ },
832
+ {
833
+ "name": "system_program",
834
+ "address": "11111111111111111111111111111111"
835
+ }
836
+ ],
837
+ "args": [
838
+ {
839
+ "name": "args",
840
+ "type": {
841
+ "defined": {
842
+ "name": "MarketAskOrderArgs"
843
+ }
844
+ }
845
+ }
846
+ ]
847
+ },
739
848
  {
740
849
  "name": "market_bid_order",
741
850
  "discriminator": [145, 89, 7, 166, 85, 59, 48, 149],
@@ -822,6 +931,38 @@
822
931
  }
823
932
  }
824
933
  },
934
+ {
935
+ "name": "seller_authority",
936
+ "writable": true
937
+ },
938
+ {
939
+ "name": "seller_to_ata",
940
+ "writable": true,
941
+ "pda": {
942
+ "seeds": [
943
+ {
944
+ "kind": "account",
945
+ "path": "seller_authority"
946
+ },
947
+ {
948
+ "kind": "account",
949
+ "path": "token_program"
950
+ },
951
+ {
952
+ "kind": "account",
953
+ "path": "mint"
954
+ }
955
+ ],
956
+ "program": {
957
+ "kind": "const",
958
+ "value": [
959
+ 140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142,
960
+ 13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216,
961
+ 219, 233, 248, 89
962
+ ]
963
+ }
964
+ }
965
+ },
825
966
  {
826
967
  "name": "token_program"
827
968
  },
@@ -2178,6 +2319,26 @@
2178
2319
  ]
2179
2320
  }
2180
2321
  },
2322
+ {
2323
+ "name": "MarketAskOrderArgs",
2324
+ "type": {
2325
+ "kind": "struct",
2326
+ "fields": [
2327
+ {
2328
+ "name": "shares",
2329
+ "type": "u64"
2330
+ },
2331
+ {
2332
+ "name": "order_direction",
2333
+ "type": {
2334
+ "defined": {
2335
+ "name": "OrderDirection"
2336
+ }
2337
+ }
2338
+ }
2339
+ ]
2340
+ }
2341
+ },
2181
2342
  {
2182
2343
  "name": "MarketBidOrderArgs",
2183
2344
  "type": {
@@ -1003,6 +1003,173 @@ export type TriadProtocol = {
1003
1003
  ];
1004
1004
  args: [];
1005
1005
  },
1006
+ {
1007
+ name: 'marketAskOrder';
1008
+ discriminator: [189, 66, 162, 254, 3, 85, 152, 54];
1009
+ accounts: [
1010
+ {
1011
+ name: 'signer';
1012
+ writable: true;
1013
+ signer: true;
1014
+ },
1015
+ {
1016
+ name: 'squads';
1017
+ writable: true;
1018
+ address: '6fcSf6qfwPNR9AUUNC1UWYZDy5cQ4TzTb2aaipN2zFdq';
1019
+ },
1020
+ {
1021
+ name: 'sellerTrade';
1022
+ writable: true;
1023
+ },
1024
+ {
1025
+ name: 'buyerTrade';
1026
+ writable: true;
1027
+ },
1028
+ {
1029
+ name: 'market';
1030
+ writable: true;
1031
+ },
1032
+ {
1033
+ name: 'mint';
1034
+ writable: true;
1035
+ },
1036
+ {
1037
+ name: 'userFromAta';
1038
+ writable: true;
1039
+ pda: {
1040
+ seeds: [
1041
+ {
1042
+ kind: 'account';
1043
+ path: 'signer';
1044
+ },
1045
+ {
1046
+ kind: 'account';
1047
+ path: 'tokenProgram';
1048
+ },
1049
+ {
1050
+ kind: 'account';
1051
+ path: 'mint';
1052
+ }
1053
+ ];
1054
+ program: {
1055
+ kind: 'const';
1056
+ value: [
1057
+ 140,
1058
+ 151,
1059
+ 37,
1060
+ 143,
1061
+ 78,
1062
+ 36,
1063
+ 137,
1064
+ 241,
1065
+ 187,
1066
+ 61,
1067
+ 16,
1068
+ 41,
1069
+ 20,
1070
+ 142,
1071
+ 13,
1072
+ 131,
1073
+ 11,
1074
+ 90,
1075
+ 19,
1076
+ 153,
1077
+ 218,
1078
+ 255,
1079
+ 16,
1080
+ 132,
1081
+ 4,
1082
+ 142,
1083
+ 123,
1084
+ 216,
1085
+ 219,
1086
+ 233,
1087
+ 248,
1088
+ 89
1089
+ ];
1090
+ };
1091
+ };
1092
+ },
1093
+ {
1094
+ name: 'marketAta';
1095
+ writable: true;
1096
+ pda: {
1097
+ seeds: [
1098
+ {
1099
+ kind: 'account';
1100
+ path: 'market';
1101
+ },
1102
+ {
1103
+ kind: 'account';
1104
+ path: 'tokenProgram';
1105
+ },
1106
+ {
1107
+ kind: 'account';
1108
+ path: 'mint';
1109
+ }
1110
+ ];
1111
+ program: {
1112
+ kind: 'const';
1113
+ value: [
1114
+ 140,
1115
+ 151,
1116
+ 37,
1117
+ 143,
1118
+ 78,
1119
+ 36,
1120
+ 137,
1121
+ 241,
1122
+ 187,
1123
+ 61,
1124
+ 16,
1125
+ 41,
1126
+ 20,
1127
+ 142,
1128
+ 13,
1129
+ 131,
1130
+ 11,
1131
+ 90,
1132
+ 19,
1133
+ 153,
1134
+ 218,
1135
+ 255,
1136
+ 16,
1137
+ 132,
1138
+ 4,
1139
+ 142,
1140
+ 123,
1141
+ 216,
1142
+ 219,
1143
+ 233,
1144
+ 248,
1145
+ 89
1146
+ ];
1147
+ };
1148
+ };
1149
+ },
1150
+ {
1151
+ name: 'tokenProgram';
1152
+ },
1153
+ {
1154
+ name: 'associatedTokenProgram';
1155
+ address: 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
1156
+ },
1157
+ {
1158
+ name: 'systemProgram';
1159
+ address: '11111111111111111111111111111111';
1160
+ }
1161
+ ];
1162
+ args: [
1163
+ {
1164
+ name: 'args';
1165
+ type: {
1166
+ defined: {
1167
+ name: 'marketAskOrderArgs';
1168
+ };
1169
+ };
1170
+ }
1171
+ ];
1172
+ },
1006
1173
  {
1007
1174
  name: 'marketBidOrder';
1008
1175
  discriminator: [145, 89, 7, 166, 85, 59, 48, 149];
@@ -1147,6 +1314,67 @@ export type TriadProtocol = {
1147
1314
  };
1148
1315
  };
1149
1316
  },
1317
+ {
1318
+ name: 'sellerAuthority';
1319
+ writable: true;
1320
+ },
1321
+ {
1322
+ name: 'sellerToAta';
1323
+ writable: true;
1324
+ pda: {
1325
+ seeds: [
1326
+ {
1327
+ kind: 'account';
1328
+ path: 'sellerAuthority';
1329
+ },
1330
+ {
1331
+ kind: 'account';
1332
+ path: 'tokenProgram';
1333
+ },
1334
+ {
1335
+ kind: 'account';
1336
+ path: 'mint';
1337
+ }
1338
+ ];
1339
+ program: {
1340
+ kind: 'const';
1341
+ value: [
1342
+ 140,
1343
+ 151,
1344
+ 37,
1345
+ 143,
1346
+ 78,
1347
+ 36,
1348
+ 137,
1349
+ 241,
1350
+ 187,
1351
+ 61,
1352
+ 16,
1353
+ 41,
1354
+ 20,
1355
+ 142,
1356
+ 13,
1357
+ 131,
1358
+ 11,
1359
+ 90,
1360
+ 19,
1361
+ 153,
1362
+ 218,
1363
+ 255,
1364
+ 16,
1365
+ 132,
1366
+ 4,
1367
+ 142,
1368
+ 123,
1369
+ 216,
1370
+ 219,
1371
+ 233,
1372
+ 248,
1373
+ 89
1374
+ ];
1375
+ };
1376
+ };
1377
+ },
1150
1378
  {
1151
1379
  name: 'tokenProgram';
1152
1380
  },
@@ -2851,6 +3079,26 @@ export type TriadProtocol = {
2851
3079
  ];
2852
3080
  };
2853
3081
  },
3082
+ {
3083
+ name: 'marketAskOrderArgs';
3084
+ type: {
3085
+ kind: 'struct';
3086
+ fields: [
3087
+ {
3088
+ name: 'shares';
3089
+ type: 'u64';
3090
+ },
3091
+ {
3092
+ name: 'orderDirection';
3093
+ type: {
3094
+ defined: {
3095
+ name: 'orderDirection';
3096
+ };
3097
+ };
3098
+ }
3099
+ ];
3100
+ };
3101
+ },
2854
3102
  {
2855
3103
  name: 'marketBidOrderArgs';
2856
3104
  type: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "1.8.0-beta",
3
+ "version": "1.8.2-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",