@triadxyz/triad-protocol 1.6.7-beta-dev → 1.6.8-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
@@ -4,13 +4,11 @@ import { TriadProtocol } from './types/triad_protocol';
4
4
  import Trade from './trade';
5
5
  import Stake from './stake';
6
6
  import { RpcOptions, TransferPoseidonArgs } from './types';
7
- import Wheel from './wheel';
8
7
  export default class TriadProtocolClient {
9
8
  program: Program<TriadProtocol>;
10
9
  provider: AnchorProvider;
11
10
  trade: Trade;
12
11
  stake: Stake;
13
- wheel: Wheel;
14
12
  constructor(connection: Connection, wallet: Wallet);
15
13
  /**
16
14
  * Transfer Poseidon
@@ -41,4 +39,32 @@ export default class TriadProtocolClient {
41
39
  * @param options - RPC options
42
40
  */
43
41
  collectRoyalty(collection: string, options?: RpcOptions): Promise<string>;
42
+ /**
43
+ * Add Default Poseidon
44
+ * @param users - Users
45
+ *
46
+ * @param options - RPC options
47
+ */
48
+ addDefaultPoseidon(users: PublicKey[], options?: RpcOptions): Promise<string>;
49
+ /**
50
+ * Add Trader Poseidon
51
+ * @param user - User
52
+ * @param poseidonAsset - Poseidon Asset
53
+ *
54
+ * @param options - RPC options
55
+ */
56
+ addTraderPoseidon({ user, poseidonAsset }: {
57
+ user: PublicKey;
58
+ poseidonAsset: PublicKey;
59
+ }, options?: RpcOptions): Promise<string>;
60
+ /**
61
+ * Remove Trader Poseidon
62
+ * @param user - User
63
+ *
64
+ * @param options - RPC options
65
+ */
66
+ removeTraderPoseidon({ user, poseidonAsset }: {
67
+ user: PublicKey;
68
+ poseidonAsset: PublicKey;
69
+ }, options?: RpcOptions): Promise<string>;
44
70
  }
package/dist/index.js CHANGED
@@ -20,16 +20,15 @@ const stake_1 = __importDefault(require("./stake"));
20
20
  const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
21
21
  const constants_1 = require("./utils/constants");
22
22
  const sendTransactionWithOptions_1 = __importDefault(require("./utils/sendTransactionWithOptions"));
23
- const wheel_1 = __importDefault(require("./wheel"));
23
+ const trade_2 = require("./utils/pda/trade");
24
24
  class TriadProtocolClient {
25
25
  constructor(connection, wallet) {
26
26
  this.provider = new anchor_1.AnchorProvider(connection, wallet, {
27
- commitment: 'confirmed'
27
+ commitment: 'processed'
28
28
  });
29
29
  this.program = new anchor_1.Program(idl_triad_protocol_json_1.default, this.provider);
30
30
  this.trade = new trade_1.default(this.program, this.provider);
31
31
  this.stake = new stake_1.default(this.program, this.provider);
32
- this.wheel = new wheel_1.default(this.program, this.provider);
33
32
  }
34
33
  /**
35
34
  * Transfer Poseidon
@@ -101,5 +100,64 @@ class TriadProtocolClient {
101
100
  }), options);
102
101
  });
103
102
  }
103
+ /**
104
+ * Add Default Poseidon
105
+ * @param users - Users
106
+ *
107
+ * @param options - RPC options
108
+ */
109
+ addDefaultPoseidon(users, options) {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ const ixs = [];
112
+ for (const user of users) {
113
+ const UserTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, user);
114
+ ixs.push(yield this.program.methods
115
+ .addDefaultPoseidon()
116
+ .accounts({
117
+ signer: this.provider.wallet.publicKey,
118
+ userTrade: UserTradePDA
119
+ })
120
+ .instruction());
121
+ }
122
+ return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options);
123
+ });
124
+ }
125
+ /**
126
+ * Add Trader Poseidon
127
+ * @param user - User
128
+ * @param poseidonAsset - Poseidon Asset
129
+ *
130
+ * @param options - RPC options
131
+ */
132
+ addTraderPoseidon({ user, poseidonAsset }, options) {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const UserTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, user);
135
+ const PoseidonCollectionPDA = (0, pda_1.getCollectionPDA)(this.program.programId, constants_1.POSEIDON_COLLECTION_SYMBOL);
136
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.addTraderPoseidon().accounts({
137
+ poseidonAsset,
138
+ userTrade: UserTradePDA,
139
+ corePoseidonCollection: constants_1.POSEIDON_CORE_COLLECTION,
140
+ poseidonCollection: PoseidonCollectionPDA
141
+ }), options);
142
+ });
143
+ }
144
+ /**
145
+ * Remove Trader Poseidon
146
+ * @param user - User
147
+ *
148
+ * @param options - RPC options
149
+ */
150
+ removeTraderPoseidon({ user, poseidonAsset }, options) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ const UserTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, user);
153
+ const PoseidonCollectionPDA = (0, pda_1.getCollectionPDA)(this.program.programId, constants_1.POSEIDON_COLLECTION_SYMBOL);
154
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.removeTraderPoseidon().accounts({
155
+ userTrade: UserTradePDA,
156
+ poseidonAsset,
157
+ corePoseidonCollection: constants_1.POSEIDON_CORE_COLLECTION,
158
+ poseidonCollection: PoseidonCollectionPDA
159
+ }), options);
160
+ });
161
+ }
104
162
  }
105
163
  exports.default = TriadProtocolClient;
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, PlaceOrderArgs, OrderDirection, UserTrade, OrderStatus } from './types/trade';
4
+ import { InitializeMarketArgs, OpenOrderArgs, UserTrade } from './types/trade';
5
5
  import { RpcOptions } from './types';
6
6
  import BN from 'bn.js';
7
7
  export default class Trade {
@@ -25,25 +25,6 @@ export default class Trade {
25
25
  *
26
26
  */
27
27
  getUserOrders(user: PublicKey): Promise<import("./types/trade").Order[]>;
28
- /**
29
- * Get Orders By Market ID
30
- * @param marketId - The ID of the market
31
- *
32
- */
33
- getLimitOrdersByMarketId(marketId: number): Promise<{
34
- userAuthority: string;
35
- ts: string;
36
- orderId: string;
37
- questionId: string;
38
- marketId: string;
39
- status: OrderStatus;
40
- price: string;
41
- totalAmount: string;
42
- totalShares: string;
43
- orderType: import("./types/trade").OrderType;
44
- direction: OrderDirection;
45
- userNonce: string;
46
- }[]>;
47
28
  /**
48
29
  * Get Market By ID
49
30
  * @param marketId - The ID of the market
@@ -71,14 +52,13 @@ export default class Trade {
71
52
  orders: {
72
53
  ts: BN;
73
54
  orderId: BN;
74
- filledShares: BN;
55
+ questionId: BN;
75
56
  marketId: BN;
76
57
  status: ({
77
58
  open?: never;
78
59
  closed?: never;
79
60
  claimed?: never;
80
61
  liquidated?: never;
81
- waiting?: never;
82
62
  } & {
83
63
  init: Record<string, never>;
84
64
  }) | ({
@@ -86,7 +66,6 @@ export default class Trade {
86
66
  closed?: never;
87
67
  claimed?: never;
88
68
  liquidated?: never;
89
- waiting?: never;
90
69
  } & {
91
70
  open: Record<string, never>;
92
71
  }) | ({
@@ -94,7 +73,6 @@ export default class Trade {
94
73
  open?: never;
95
74
  claimed?: never;
96
75
  liquidated?: never;
97
- waiting?: never;
98
76
  } & {
99
77
  closed: Record<string, never>;
100
78
  }) | ({
@@ -102,7 +80,6 @@ export default class Trade {
102
80
  open?: never;
103
81
  closed?: never;
104
82
  liquidated?: never;
105
- waiting?: never;
106
83
  } & {
107
84
  claimed: Record<string, never>;
108
85
  }) | ({
@@ -110,17 +87,8 @@ export default class Trade {
110
87
  open?: never;
111
88
  closed?: never;
112
89
  claimed?: never;
113
- waiting?: never;
114
90
  } & {
115
91
  liquidated: Record<string, never>;
116
- }) | ({
117
- init?: never;
118
- open?: never;
119
- closed?: never;
120
- claimed?: never;
121
- liquidated?: never;
122
- } & {
123
- waiting: Record<string, never>;
124
92
  });
125
93
  price: BN;
126
94
  totalAmount: BN;
@@ -134,7 +102,7 @@ export default class Trade {
134
102
  } & {
135
103
  limit: Record<string, never>;
136
104
  });
137
- orderDirection: ({
105
+ direction: ({
138
106
  flop?: never;
139
107
  } & {
140
108
  hype: Record<string, never>;
@@ -144,15 +112,6 @@ export default class Trade {
144
112
  flop: Record<string, never>;
145
113
  });
146
114
  userNonce: number;
147
- orderSide: ({
148
- ask?: never;
149
- } & {
150
- bid: Record<string, never>;
151
- }) | ({
152
- bid?: never;
153
- } & {
154
- ask: Record<string, never>;
155
- });
156
115
  padding: number[];
157
116
  }[];
158
117
  nonce: number;
@@ -178,21 +137,18 @@ export default class Trade {
178
137
  */
179
138
  getUserTradeNonceWithSlots(userTrades: UserTrade[]): Promise<PublicKey>;
180
139
  /**
181
- * Place Order
140
+ * Open Order
182
141
  * @param args.marketId - The ID of the Market
183
142
  * @param args.amount - The amount of the Order
184
143
  * @param args.direction - The direction of the Order
185
- * @param args.orderType - The type of the Order
186
- * @param args.orderSide - The side of the Order
187
- * @param args.mint - The mint of the Order
188
- * @param args.price - The price of the Order
144
+ * @param args.token - The token to use for the Order
189
145
  *
190
146
  * @param options - RPC options
191
147
  *
192
148
  */
193
- placeOrder({ marketId, amount, direction, orderType, orderSide, mint, price }: PlaceOrderArgs, options?: RpcOptions): Promise<string>;
149
+ openOrder({ marketId, amount, direction, token }: OpenOrderArgs, options?: RpcOptions): Promise<string>;
194
150
  /**
195
- * Cancel Order
151
+ * Close Order
196
152
  * @param args.marketId - The ID of the Market
197
153
  * @param args.orderId - The ID of the Order
198
154
  * @param args.userNonce - The nonce of the user
@@ -200,7 +156,7 @@ export default class Trade {
200
156
  * @param options - RPC options
201
157
  *
202
158
  */
203
- cancelOrder({ marketId, orderId, userNonce }: {
159
+ closeOrder({ marketId, orderId, userNonce }: {
204
160
  marketId: number;
205
161
  orderId: number;
206
162
  userNonce: number;
@@ -272,4 +228,20 @@ export default class Trade {
272
228
  *
273
229
  */
274
230
  updateMarket(marketId: number, marketEnd: number, options?: RpcOptions): Promise<string>;
231
+ /**
232
+ * Force Cancel Order
233
+ * @param args.marketId - The ID of the Market
234
+ * @param args.user - The user to force cancel the order for
235
+ * @param args.orderId - The ID of the Order
236
+ * @param args.userNonce - The nonce of the user
237
+ *
238
+ * @param options - RPC options
239
+ *
240
+ */
241
+ forceCancelOrder({ marketId, user, orderId, userNonce }: {
242
+ marketId: number;
243
+ user: PublicKey;
244
+ orderId: number;
245
+ userNonce: number;
246
+ }, options?: RpcOptions): Promise<string>;
275
247
  }
package/dist/trade.js CHANGED
@@ -18,7 +18,7 @@ const helpers_1 = require("./utils/helpers");
18
18
  const trade_1 = require("./utils/pda/trade");
19
19
  const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
20
20
  const sendTransactionWithOptions_1 = __importDefault(require("./utils/sendTransactionWithOptions"));
21
- const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
21
+ const swap_1 = require("./utils/swap");
22
22
  class Trade {
23
23
  constructor(program, provider) {
24
24
  this.program = program;
@@ -64,37 +64,6 @@ class Trade {
64
64
  return orders;
65
65
  });
66
66
  }
67
- /**
68
- * Get Orders By Market ID
69
- * @param marketId - The ID of the market
70
- *
71
- */
72
- getLimitOrdersByMarketId(marketId) {
73
- return __awaiter(this, void 0, void 0, function* () {
74
- const marketIdBytes = bytes_1.bs58.encode(new bn_js_1.default(marketId).toArrayLike(Buffer, 'le', 8));
75
- const memcmpFilters = Array.from({ length: 10 }).map((_, index) => ({
76
- memcmp: {
77
- offset: 8 + // discriminator
78
- 1 + // bump
79
- 32 + // authority
80
- 8 + // total_deposits
81
- 8 + // total_withdraws
82
- 8 + // opened_orders
83
- // Per order: index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 28)
84
- index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 28) +
85
- // Then offset to market_id: 8 + 8 + 8
86
- (8 + 8 + 8),
87
- bytes: marketIdBytes
88
- }
89
- }));
90
- const allResponses = yield Promise.all(memcmpFilters.map((filter) => this.program.account.userTrade.all([filter])));
91
- const uniqueResponses = Array.from(new Map(allResponses.flat().map((item) => [item.publicKey.toString(), item])).values());
92
- const userTrades = uniqueResponses.map(({ account, publicKey }) => (0, helpers_1.formatUserTrade)(account, publicKey));
93
- const orders = userTrades.flatMap((userTrade) => userTrade.orders.map((order) => (Object.assign(Object.assign({}, order), { userAuthority: userTrade.user }))));
94
- const filteredOrders = orders.filter((order) => order.marketId === marketId.toString());
95
- return filteredOrders;
96
- });
97
- }
98
67
  /**
99
68
  * Get Market By ID
100
69
  * @param marketId - The ID of the market
@@ -196,24 +165,22 @@ class Trade {
196
165
  });
197
166
  }
198
167
  /**
199
- * Place Order
168
+ * Open Order
200
169
  * @param args.marketId - The ID of the Market
201
170
  * @param args.amount - The amount of the Order
202
171
  * @param args.direction - The direction of the Order
203
- * @param args.orderType - The type of the Order
204
- * @param args.orderSide - The side of the Order
205
- * @param args.mint - The mint of the Order
206
- * @param args.price - The price of the Order
172
+ * @param args.token - The token to use for the Order
207
173
  *
208
174
  * @param options - RPC options
209
175
  *
210
176
  */
211
- placeOrder({ marketId, amount, direction, orderType, orderSide, mint, price }, options) {
177
+ openOrder({ marketId, amount, direction, token }, options) {
212
178
  return __awaiter(this, void 0, void 0, function* () {
213
179
  const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
214
180
  let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
215
181
  const ixs = [];
216
182
  const addressLookupTableAccounts = [];
183
+ let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
217
184
  let myUserTrades = [];
218
185
  myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
219
186
  if (myUserTrades.length === 0) {
@@ -240,27 +207,38 @@ class Trade {
240
207
  .instruction());
241
208
  }
242
209
  }
210
+ if (token !== constants_1.TRD_MINT.toBase58()) {
211
+ const { setupInstructions, swapIxs, addressLookupTableAccounts, trdAmount } = yield (0, swap_1.swap)({
212
+ connection: this.provider.connection,
213
+ wallet: this.provider.publicKey.toBase58(),
214
+ inToken: token,
215
+ amount
216
+ });
217
+ amountInTRD = trdAmount;
218
+ if (swapIxs.length === 0) {
219
+ return;
220
+ }
221
+ ixs.push(...setupInstructions);
222
+ ixs.push(...swapIxs);
223
+ addressLookupTableAccounts.push(...addressLookupTableAccounts);
224
+ }
243
225
  ixs.push(yield this.program.methods
244
- .placeOrder({
245
- amount: new bn_js_1.default(amount * Math.pow(10, constants_1.TRD_DECIMALS)),
246
- price: new bn_js_1.default(price),
247
- orderDirection: direction,
248
- orderType,
249
- orderSide
226
+ .openOrder({
227
+ amount: new bn_js_1.default(amountInTRD),
228
+ direction: direction
250
229
  })
251
230
  .accounts({
252
231
  signer: this.provider.publicKey,
253
232
  market: marketPDA,
254
233
  userTrade: userTradePDA,
255
- mint,
256
- tokenProgram: (0, helpers_1.getTokenProgram)(mint)
234
+ mint: constants_1.TRD_MINT
257
235
  })
258
236
  .instruction());
259
237
  return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
260
238
  });
261
239
  }
262
240
  /**
263
- * Cancel Order
241
+ * Close Order
264
242
  * @param args.marketId - The ID of the Market
265
243
  * @param args.orderId - The ID of the Order
266
244
  * @param args.userNonce - The nonce of the user
@@ -268,7 +246,7 @@ class Trade {
268
246
  * @param options - RPC options
269
247
  *
270
248
  */
271
- cancelOrder({ marketId, orderId, userNonce }, options) {
249
+ closeOrder({ marketId, orderId, userNonce }, options) {
272
250
  return __awaiter(this, void 0, void 0, function* () {
273
251
  const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
274
252
  let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
@@ -276,12 +254,11 @@ class Trade {
276
254
  const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
277
255
  userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
278
256
  }
279
- return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelOrder(new bn_js_1.default(orderId)).accounts({
257
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
280
258
  signer: this.provider.publicKey,
281
259
  market: marketPDA,
282
260
  mint: constants_1.TRD_MINT,
283
- userTrade: userTradePDA,
284
- tokenProgram: (0, helpers_1.getTokenProgram)(constants_1.TRD_MINT)
261
+ userTrade: userTradePDA
285
262
  }), options);
286
263
  });
287
264
  }
@@ -396,5 +373,32 @@ class Trade {
396
373
  }), options);
397
374
  });
398
375
  }
376
+ /**
377
+ * Force Cancel Order
378
+ * @param args.marketId - The ID of the Market
379
+ * @param args.user - The user to force cancel the order for
380
+ * @param args.orderId - The ID of the Order
381
+ * @param args.userNonce - The nonce of the user
382
+ *
383
+ * @param options - RPC options
384
+ *
385
+ */
386
+ forceCancelOrder({ marketId, user, orderId, userNonce }, options) {
387
+ return __awaiter(this, void 0, void 0, function* () {
388
+ const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
389
+ let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, user);
390
+ if (userNonce !== 0) {
391
+ const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, user, userNonce);
392
+ userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
393
+ }
394
+ return (0, sendTransactionWithOptions_1.default)(this.program.methods.forceCancelOrder(new bn_js_1.default(orderId)).accounts({
395
+ signer: this.provider.publicKey,
396
+ user,
397
+ market: marketPDA,
398
+ mint: constants_1.TRD_MINT,
399
+ userTrade: userTradePDA
400
+ }), options);
401
+ });
402
+ }
399
403
  }
400
404
  exports.default = Trade;