@triadxyz/triad-protocol 1.5.4-beta → 1.5.6-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
@@ -126,9 +126,10 @@ class TriadProtocolClient {
126
126
  return __awaiter(this, void 0, void 0, function* () {
127
127
  const ixs = [];
128
128
  for (const user of users) {
129
+ const userPDA = (0, pda_1.getUserPDA)(this.program.programId, user);
129
130
  ixs.push(yield this.program.methods
130
131
  .migrateUser()
131
- .accounts({ user })
132
+ .accounts({ user: userPDA })
132
133
  .instruction());
133
134
  }
134
135
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, []);
package/dist/trade.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AnchorProvider, Program } from '@coral-xyz/anchor';
2
2
  import { TriadProtocol } from './types/triad_protocol';
3
3
  import { PublicKey } from '@solana/web3.js';
4
- import { InitializeMarketArgs, OpenOrderArgs, OrderDirection } from './types/trade';
4
+ import { InitializeMarketArgs, OpenOrderArgs, OrderDirection, UserTrade } from './types/trade';
5
5
  import { RpcOptions } from './types';
6
6
  import BN from 'bn.js';
7
7
  export default class Trade {
@@ -13,6 +13,18 @@ export default class Trade {
13
13
  *
14
14
  */
15
15
  getAllMarkets(): Promise<import("./types/trade").Market[]>;
16
+ /**
17
+ * Get My User Trades from a user authority
18
+ * @param user - User PublicKey
19
+ *
20
+ */
21
+ getMyUserTrades(user: PublicKey): Promise<UserTrade[]>;
22
+ /**
23
+ * Get User Orders
24
+ * @param user - User PublicKey
25
+ *
26
+ */
27
+ getUserOrders(user: PublicKey): Promise<import("./types/trade").Order[]>;
16
28
  /**
17
29
  * Get Market By ID
18
30
  * @param marketId - The ID of the market
@@ -28,9 +40,10 @@ export default class Trade {
28
40
  /**
29
41
  * Get User Trade
30
42
  * @param user - User PublicKey
43
+ * @param userNonce - The nonce of the user
31
44
  *
32
45
  */
33
- getUserTrade(user: PublicKey): Promise<{
46
+ getUserTrade(user: PublicKey, userNonce?: number): Promise<{
34
47
  bump: number;
35
48
  authority: PublicKey;
36
49
  totalDeposits: BN;
@@ -44,38 +57,18 @@ export default class Trade {
44
57
  status: ({
45
58
  open?: never;
46
59
  closed?: never;
47
- claimed?: never;
48
- liquidated?: never;
49
60
  } & {
50
61
  init: Record<string, never>;
51
62
  }) | ({
52
63
  init?: never;
53
64
  closed?: never;
54
- claimed?: never;
55
- liquidated?: never;
56
65
  } & {
57
66
  open: Record<string, never>;
58
67
  }) | ({
59
68
  init?: never;
60
69
  open?: never;
61
- claimed?: never;
62
- liquidated?: never;
63
70
  } & {
64
71
  closed: Record<string, never>;
65
- }) | ({
66
- init?: never;
67
- open?: never;
68
- closed?: never;
69
- liquidated?: never;
70
- } & {
71
- claimed: Record<string, never>;
72
- }) | ({
73
- init?: never;
74
- open?: never;
75
- closed?: never;
76
- claimed?: never;
77
- } & {
78
- liquidated: Record<string, never>;
79
72
  });
80
73
  price: BN;
81
74
  totalAmount: BN;
@@ -98,8 +91,11 @@ export default class Trade {
98
91
  } & {
99
92
  flop: Record<string, never>;
100
93
  });
94
+ userNonce: number;
101
95
  padding: number[];
102
96
  }[];
97
+ nonce: number;
98
+ isSubUser: boolean;
103
99
  padding: number[];
104
100
  }>;
105
101
  /**
@@ -113,6 +109,12 @@ export default class Trade {
113
109
  *
114
110
  */
115
111
  initializeMarket({ marketId, startTime, endTime, question }: InitializeMarketArgs, options?: RpcOptions): Promise<string>;
112
+ /**
113
+ * Get User Trade Nonce With Slots
114
+ * @param userTrades - User Trades
115
+ *
116
+ */
117
+ getUserTradeNonceWithSlots(userTrades: UserTrade[]): Promise<PublicKey>;
116
118
  /**
117
119
  * Open Order
118
120
  * @param args.marketId - The ID of the Market
@@ -128,13 +130,15 @@ export default class Trade {
128
130
  * Close Order
129
131
  * @param args.marketId - The ID of the Market
130
132
  * @param args.orderId - The ID of the Order
133
+ * @param args.userNonce - The nonce of the user
131
134
  *
132
135
  * @param options - RPC options
133
136
  *
134
137
  */
135
- closeOrder({ marketId, orderId }: {
138
+ closeOrder({ marketId, orderId, userNonce }: {
136
139
  marketId: number;
137
140
  orderId: number;
141
+ userNonce: number;
138
142
  }, options?: RpcOptions): Promise<string>;
139
143
  /**
140
144
  * Resolve Market
@@ -172,20 +176,6 @@ export default class Trade {
172
176
  marketToAddLiquidity: OrderDirection;
173
177
  amount: number;
174
178
  }, options?: RpcOptions): Promise<string>;
175
- /**
176
- * Settle an Order for a V1 Market
177
- * @param args.marketId - The ID of the Market
178
- * @param args.orderId - The ID of the Order to Settle
179
- * @param args.user - The user to settle the order for
180
- *
181
- * @param options - RPC options
182
- *
183
- */
184
- settleOrder({ marketId, orderId, user }: {
185
- marketId: number;
186
- orderId: number;
187
- user: PublicKey;
188
- }, options?: RpcOptions): Promise<string>;
189
179
  /**
190
180
  * Add Liquidity
191
181
  * @param marketId - The ID of the market
@@ -212,13 +202,30 @@ export default class Trade {
212
202
  * Payout Order
213
203
  * @param args.marketId - The ID of the Market
214
204
  * @param args.orderId - The ID of the Order to Payout
205
+ * @param args.userNonce - The nonce of the user
215
206
  *
216
207
  * @param options - RPC options
217
208
  *
218
209
  */
219
- payoutOrder({ marketId, orderId }: {
210
+ payoutOrder({ marketId, orderId, userNonce }: {
220
211
  marketId: number;
221
212
  orderId: number;
213
+ userNonce: number;
222
214
  }, options?: RpcOptions): Promise<string>;
223
- allowMarketPayout(marketId: number, options?: RpcOptions): Promise<string>;
215
+ /**
216
+ * Allow Market to Payout
217
+ * @param marketId - The ID of the market
218
+ *
219
+ * @param options - RPC options
220
+ *
221
+ */
222
+ allowMarketToPayout(marketId: number, options?: RpcOptions): Promise<string>;
223
+ /**
224
+ * Create Sub User Trade
225
+ * @param user - User PublicKey the main user
226
+ *
227
+ * @param options - RPC options
228
+ *
229
+ */
230
+ createSubUserTrade(user: PublicKey, options?: RpcOptions): Promise<string>;
224
231
  }
package/dist/trade.js CHANGED
@@ -34,6 +34,36 @@ class Trade {
34
34
  return marketV2.map(({ account, publicKey }) => (0, helpers_1.formatMarket)(account, publicKey));
35
35
  });
36
36
  }
37
+ /**
38
+ * Get My User Trades from a user authority
39
+ * @param user - User PublicKey
40
+ *
41
+ */
42
+ getMyUserTrades(user) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const response = yield this.program.account.userTrade.all([
45
+ {
46
+ memcmp: {
47
+ offset: 8 + 1,
48
+ bytes: user.toBase58()
49
+ }
50
+ }
51
+ ]);
52
+ return response.map(({ account, publicKey }) => (0, helpers_1.formatUserTrade)(account, publicKey));
53
+ });
54
+ }
55
+ /**
56
+ * Get User Orders
57
+ * @param user - User PublicKey
58
+ *
59
+ */
60
+ getUserOrders(user) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const myUserTrades = yield this.getMyUserTrades(user);
63
+ const orders = myUserTrades.flatMap((userTrade) => userTrade.orders);
64
+ return orders;
65
+ });
66
+ }
37
67
  /**
38
68
  * Get Market By ID
39
69
  * @param marketId - The ID of the market
@@ -60,11 +90,16 @@ class Trade {
60
90
  /**
61
91
  * Get User Trade
62
92
  * @param user - User PublicKey
93
+ * @param userNonce - The nonce of the user
63
94
  *
64
95
  */
65
- getUserTrade(user) {
96
+ getUserTrade(user, userNonce = 0) {
66
97
  return __awaiter(this, void 0, void 0, function* () {
67
- const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
98
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
99
+ if (userNonce !== 0) {
100
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userNonce);
101
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
102
+ }
68
103
  return this.program.account.userTrade.fetch(userTradePDA);
69
104
  });
70
105
  }
@@ -96,6 +131,39 @@ class Trade {
96
131
  }), options);
97
132
  });
98
133
  }
134
+ /**
135
+ * Get User Trade Nonce With Slots
136
+ * @param userTrades - User Trades
137
+ *
138
+ */
139
+ getUserTradeNonceWithSlots(userTrades) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ let nonce = null;
142
+ for (const userTrade of userTrades) {
143
+ if (nonce !== null) {
144
+ break;
145
+ }
146
+ userTrade.orders.forEach((order) => {
147
+ if (nonce !== null) {
148
+ return;
149
+ }
150
+ let status = Object.keys(order.status)[0];
151
+ if (status !== 'open') {
152
+ nonce = userTrade.nonce;
153
+ }
154
+ });
155
+ }
156
+ if (nonce === null) {
157
+ throw new Error('No open orders found');
158
+ }
159
+ if (nonce === 0) {
160
+ return (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
161
+ }
162
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, nonce);
163
+ const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
164
+ return userTradePDA;
165
+ });
166
+ }
99
167
  /**
100
168
  * Open Order
101
169
  * @param args.marketId - The ID of the Market
@@ -109,12 +177,13 @@ class Trade {
109
177
  openOrder({ marketId, amount, direction, token }, options) {
110
178
  return __awaiter(this, void 0, void 0, function* () {
111
179
  const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
112
- const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
180
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
113
181
  const ixs = [];
114
182
  const addressLookupTableAccounts = [];
115
183
  let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
184
+ let myUserTrades = [];
116
185
  try {
117
- yield this.program.account.userTrade.fetch(userTradePDA);
186
+ myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
118
187
  }
119
188
  catch (_a) {
120
189
  ixs.push(yield this.program.methods
@@ -124,6 +193,21 @@ class Trade {
124
193
  })
125
194
  .instruction());
126
195
  }
196
+ try {
197
+ userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
198
+ console.log(userTradePDA);
199
+ }
200
+ catch (_b) {
201
+ const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
202
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
203
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
204
+ ixs.push(yield this.program.methods
205
+ .createSubUserTrade(subUserTradePDA)
206
+ .accounts({
207
+ signer: this.provider.publicKey
208
+ })
209
+ .instruction());
210
+ }
127
211
  if (token !== constants_1.TRD_MINT.toBase58()) {
128
212
  const { setupInstructions, swapIxs, addressLookupTableAccounts, trdAmount } = yield (0, swap_1.swap)({
129
213
  connection: this.provider.connection,
@@ -158,14 +242,19 @@ class Trade {
158
242
  * Close Order
159
243
  * @param args.marketId - The ID of the Market
160
244
  * @param args.orderId - The ID of the Order
245
+ * @param args.userNonce - The nonce of the user
161
246
  *
162
247
  * @param options - RPC options
163
248
  *
164
249
  */
165
- closeOrder({ marketId, orderId }, options) {
250
+ closeOrder({ marketId, orderId, userNonce }, options) {
166
251
  return __awaiter(this, void 0, void 0, function* () {
167
252
  const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
168
- const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
253
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
254
+ if (userNonce !== 0) {
255
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
256
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
257
+ }
169
258
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
170
259
  signer: this.provider.publicKey,
171
260
  market: marketPDA,
@@ -229,28 +318,6 @@ class Trade {
229
318
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
230
319
  });
231
320
  }
232
- /**
233
- * Settle an Order for a V1 Market
234
- * @param args.marketId - The ID of the Market
235
- * @param args.orderId - The ID of the Order to Settle
236
- * @param args.user - The user to settle the order for
237
- *
238
- * @param options - RPC options
239
- *
240
- */
241
- settleOrder({ marketId, orderId, user }, options) {
242
- return __awaiter(this, void 0, void 0, function* () {
243
- const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
244
- const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
245
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.settleOrder(new bn_js_1.default(orderId)).accounts({
246
- signer: this.provider.publicKey,
247
- user: user,
248
- userTrade: userTradePDA,
249
- market: marketPDA,
250
- mint: constants_1.TRD_MINT
251
- }), options);
252
- });
253
- }
254
321
  /**
255
322
  * Add Liquidity
256
323
  * @param marketId - The ID of the market
@@ -296,14 +363,19 @@ class Trade {
296
363
  * Payout Order
297
364
  * @param args.marketId - The ID of the Market
298
365
  * @param args.orderId - The ID of the Order to Payout
366
+ * @param args.userNonce - The nonce of the user
299
367
  *
300
368
  * @param options - RPC options
301
369
  *
302
370
  */
303
- payoutOrder({ marketId, orderId }, options) {
371
+ payoutOrder({ marketId, orderId, userNonce }, options) {
304
372
  return __awaiter(this, void 0, void 0, function* () {
305
373
  const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
306
- const userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
374
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
375
+ if (userNonce !== 0) {
376
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
377
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
378
+ }
307
379
  return (0, sendTransactionWithOptions_1.default)(this.program.methods.payoutOrder(new bn_js_1.default(orderId)).accounts({
308
380
  signer: this.provider.publicKey,
309
381
  userTrade: userTradePDA,
@@ -312,14 +384,37 @@ class Trade {
312
384
  }), options);
313
385
  });
314
386
  }
315
- allowMarketPayout(marketId, options) {
387
+ /**
388
+ * Allow Market to Payout
389
+ * @param marketId - The ID of the market
390
+ *
391
+ * @param options - RPC options
392
+ *
393
+ */
394
+ allowMarketToPayout(marketId, options) {
316
395
  return __awaiter(this, void 0, void 0, function* () {
317
396
  const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
318
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.allowMarketPayout().accounts({
397
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.allowMarketToPayout().accounts({
319
398
  signer: this.provider.publicKey,
320
399
  market: marketPDA
321
400
  }), options);
322
401
  });
323
402
  }
403
+ /**
404
+ * Create Sub User Trade
405
+ * @param user - User PublicKey the main user
406
+ *
407
+ * @param options - RPC options
408
+ *
409
+ */
410
+ createSubUserTrade(user, options) {
411
+ return __awaiter(this, void 0, void 0, function* () {
412
+ const userTrade = yield this.getUserTrade(user);
413
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
414
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.createSubUserTrade(subUserTradePDA).accounts({
415
+ signer: this.provider.publicKey
416
+ }), options);
417
+ });
418
+ }
324
419
  }
325
420
  exports.default = Trade;