@triadxyz/triad-protocol 1.6.7-beta-dev → 1.6.8-beta-dev
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 +0 -2
- package/dist/index.js +0 -2
- package/dist/stake.d.ts +1 -9
- package/dist/stake.js +4 -10
- package/dist/trade.d.ts +55 -41
- package/dist/trade.js +242 -80
- package/dist/types/idl_triad_protocol.json +392 -590
- package/dist/types/trade.d.ts +72 -36
- package/dist/types/trade.js +25 -1
- package/dist/types/triad_protocol.d.ts +370 -626
- package/dist/utils/constants.d.ts +1 -0
- package/dist/utils/constants.js +2 -1
- package/dist/utils/helpers.d.ts +31 -3
- package/dist/utils/helpers.js +64 -18
- package/package.json +1 -1
- package/dist/wheel.d.ts +0 -62
- package/dist/wheel.js +0 -149
package/dist/trade.js
CHANGED
|
@@ -12,10 +12,11 @@ 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 trade_1 = require("./types/trade");
|
|
15
16
|
const bn_js_1 = __importDefault(require("bn.js"));
|
|
16
17
|
const constants_1 = require("./utils/constants");
|
|
17
18
|
const helpers_1 = require("./utils/helpers");
|
|
18
|
-
const
|
|
19
|
+
const trade_2 = require("./utils/pda/trade");
|
|
19
20
|
const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
|
|
20
21
|
const sendTransactionWithOptions_1 = __importDefault(require("./utils/sendTransactionWithOptions"));
|
|
21
22
|
const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
|
|
@@ -23,6 +24,8 @@ class Trade {
|
|
|
23
24
|
constructor(program, provider) {
|
|
24
25
|
this.program = program;
|
|
25
26
|
this.provider = provider;
|
|
27
|
+
this.mint = constants_1.USDC_MINT_DEVNET;
|
|
28
|
+
this.decimals = 6;
|
|
26
29
|
}
|
|
27
30
|
/**
|
|
28
31
|
* Get All Markets
|
|
@@ -81,7 +84,7 @@ class Trade {
|
|
|
81
84
|
8 + // total_withdraws
|
|
82
85
|
8 + // opened_orders
|
|
83
86
|
// 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 +
|
|
87
|
+
index * (8 + 8 + 8 + 8 + 1 + 8 + 8 + 8 + 1 + 1 + 4 + 1 + 27) +
|
|
85
88
|
// Then offset to market_id: 8 + 8 + 8
|
|
86
89
|
(8 + 8 + 8),
|
|
87
90
|
bytes: marketIdBytes
|
|
@@ -90,9 +93,39 @@ class Trade {
|
|
|
90
93
|
const allResponses = yield Promise.all(memcmpFilters.map((filter) => this.program.account.userTrade.all([filter])));
|
|
91
94
|
const uniqueResponses = Array.from(new Map(allResponses.flat().map((item) => [item.publicKey.toString(), item])).values());
|
|
92
95
|
const userTrades = uniqueResponses.map(({ account, publicKey }) => (0, helpers_1.formatUserTrade)(account, publicKey));
|
|
93
|
-
const orders = userTrades.flatMap((userTrade) => userTrade.orders.map((order) => (Object.assign(
|
|
94
|
-
const filteredOrders = orders.filter((order) => order.marketId === marketId.toString()
|
|
95
|
-
|
|
96
|
+
const orders = userTrades.flatMap((userTrade) => userTrade.orders.map((order) => (Object.assign({}, order))));
|
|
97
|
+
const filteredOrders = orders.filter((order) => order.marketId === marketId.toString() &&
|
|
98
|
+
order.orderType === trade_1.OrderType.LIMIT &&
|
|
99
|
+
order.status === trade_1.OrderStatus.WAITING);
|
|
100
|
+
const orderBook = {
|
|
101
|
+
hype: {
|
|
102
|
+
bid: [],
|
|
103
|
+
ask: []
|
|
104
|
+
},
|
|
105
|
+
flop: {
|
|
106
|
+
bid: [],
|
|
107
|
+
ask: []
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
filteredOrders.forEach((order) => {
|
|
111
|
+
if (order.orderSide === trade_1.OrderSide.BID &&
|
|
112
|
+
order.direction === trade_1.OrderDirection.HYPE) {
|
|
113
|
+
orderBook.hype.bid.push(order);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (order.orderSide === trade_1.OrderSide.ASK &&
|
|
117
|
+
order.direction === trade_1.OrderDirection.HYPE) {
|
|
118
|
+
orderBook.hype.ask.push(order);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (order.orderSide === trade_1.OrderSide.BID &&
|
|
122
|
+
order.direction === trade_1.OrderDirection.FLOP) {
|
|
123
|
+
orderBook.flop.bid.push(order);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
orderBook.flop.ask.push(order);
|
|
127
|
+
});
|
|
128
|
+
return orderBook;
|
|
96
129
|
});
|
|
97
130
|
}
|
|
98
131
|
/**
|
|
@@ -102,7 +135,7 @@ class Trade {
|
|
|
102
135
|
*/
|
|
103
136
|
getMarketById(marketId) {
|
|
104
137
|
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
const marketPDA = (0,
|
|
138
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
106
139
|
const response = yield this.program.account.marketV2.fetch(marketPDA);
|
|
107
140
|
return (0, helpers_1.formatMarket)(response, marketPDA);
|
|
108
141
|
});
|
|
@@ -126,16 +159,16 @@ class Trade {
|
|
|
126
159
|
*/
|
|
127
160
|
getUserTrade(user, userNonce = 0) {
|
|
128
161
|
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
-
let userTradePDA = (0,
|
|
162
|
+
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, user);
|
|
130
163
|
if (userNonce !== 0) {
|
|
131
|
-
const subUserTradePDA = (0,
|
|
132
|
-
userTradePDA = (0,
|
|
164
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userNonce);
|
|
165
|
+
userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
133
166
|
}
|
|
134
167
|
return this.program.account.userTrade.fetch(userTradePDA);
|
|
135
168
|
});
|
|
136
169
|
}
|
|
137
170
|
/**
|
|
138
|
-
*
|
|
171
|
+
* Create Market
|
|
139
172
|
* @param args.marketId - new markert id - length + 1
|
|
140
173
|
* @param args.startTime - start time
|
|
141
174
|
* @param args.endTime - end time
|
|
@@ -144,21 +177,24 @@ class Trade {
|
|
|
144
177
|
* @param options - RPC options
|
|
145
178
|
*
|
|
146
179
|
*/
|
|
147
|
-
|
|
180
|
+
createMarket({ marketId, startTime, endTime, question, feeBps, customer }, options) {
|
|
148
181
|
return __awaiter(this, void 0, void 0, function* () {
|
|
149
182
|
if (question.length > 80) {
|
|
150
183
|
throw new Error('Question must be less than 80 characters');
|
|
151
184
|
}
|
|
152
185
|
return (0, sendTransactionWithOptions_1.default)(this.program.methods
|
|
153
|
-
.
|
|
186
|
+
.createMarket({
|
|
154
187
|
marketId: new bn_js_1.default(marketId),
|
|
155
188
|
question: (0, helpers_1.encodeString)(question, 80),
|
|
156
189
|
marketStart: new bn_js_1.default(startTime),
|
|
157
|
-
marketEnd: new bn_js_1.default(endTime)
|
|
190
|
+
marketEnd: new bn_js_1.default(endTime),
|
|
191
|
+
feeBps
|
|
158
192
|
})
|
|
159
193
|
.accounts({
|
|
160
194
|
signer: this.provider.publicKey,
|
|
161
|
-
mint:
|
|
195
|
+
mint: this.mint,
|
|
196
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(this.mint),
|
|
197
|
+
customer
|
|
162
198
|
}), options);
|
|
163
199
|
});
|
|
164
200
|
}
|
|
@@ -174,24 +210,30 @@ class Trade {
|
|
|
174
210
|
if (nonce !== null) {
|
|
175
211
|
break;
|
|
176
212
|
}
|
|
213
|
+
let freeSlots = 0;
|
|
177
214
|
userTrade.orders.forEach((order) => {
|
|
178
215
|
if (nonce !== null) {
|
|
179
216
|
return;
|
|
180
217
|
}
|
|
181
|
-
|
|
182
|
-
|
|
218
|
+
if (order.status !== trade_1.OrderStatus.OPEN &&
|
|
219
|
+
order.status !== trade_1.OrderStatus.WAITING &&
|
|
220
|
+
freeSlots >= 2) {
|
|
183
221
|
nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
|
|
184
222
|
}
|
|
223
|
+
if (order.status !== trade_1.OrderStatus.OPEN &&
|
|
224
|
+
order.status !== trade_1.OrderStatus.WAITING) {
|
|
225
|
+
freeSlots += 1;
|
|
226
|
+
}
|
|
185
227
|
});
|
|
186
228
|
}
|
|
187
229
|
if (nonce === null) {
|
|
188
230
|
throw new Error('No open orders found');
|
|
189
231
|
}
|
|
190
232
|
if (nonce === 0) {
|
|
191
|
-
return (0,
|
|
233
|
+
return (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
192
234
|
}
|
|
193
|
-
const subUserTradePDA = (0,
|
|
194
|
-
const userTradePDA = (0,
|
|
235
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, nonce);
|
|
236
|
+
const userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
195
237
|
return userTradePDA;
|
|
196
238
|
});
|
|
197
239
|
}
|
|
@@ -200,7 +242,6 @@ class Trade {
|
|
|
200
242
|
* @param args.marketId - The ID of the Market
|
|
201
243
|
* @param args.amount - The amount of the Order
|
|
202
244
|
* @param args.direction - The direction of the Order
|
|
203
|
-
* @param args.orderType - The type of the Order
|
|
204
245
|
* @param args.orderSide - The side of the Order
|
|
205
246
|
* @param args.mint - The mint of the Order
|
|
206
247
|
* @param args.price - The price of the Order
|
|
@@ -208,10 +249,10 @@ class Trade {
|
|
|
208
249
|
* @param options - RPC options
|
|
209
250
|
*
|
|
210
251
|
*/
|
|
211
|
-
placeOrder({ marketId, amount, direction,
|
|
252
|
+
placeOrder({ marketId, amount, direction, orderSide, mint, price }, options) {
|
|
212
253
|
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
-
const marketPDA = (0,
|
|
214
|
-
let userTradePDA = (0,
|
|
254
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
255
|
+
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
215
256
|
const ixs = [];
|
|
216
257
|
const addressLookupTableAccounts = [];
|
|
217
258
|
let myUserTrades = [];
|
|
@@ -227,11 +268,31 @@ class Trade {
|
|
|
227
268
|
if (myUserTrades.length > 0) {
|
|
228
269
|
try {
|
|
229
270
|
userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
|
|
271
|
+
console.log(userTradePDA);
|
|
272
|
+
// Check if we need to create a new sub user trade
|
|
273
|
+
// This happens when all existing slots have open orders
|
|
274
|
+
const allSlotsHaveOpenOrders = myUserTrades.every((trade) => trade.orders.some((order) => Object.keys(order.status)[0] === 'open' ||
|
|
275
|
+
Object.keys(order.status)[0] === 'waiting'));
|
|
276
|
+
if (allSlotsHaveOpenOrders) {
|
|
277
|
+
// Find the main user trade to get the current nonce
|
|
278
|
+
const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
|
|
279
|
+
if (!mainUserTrade) {
|
|
280
|
+
throw new Error('No main user trade found');
|
|
281
|
+
}
|
|
282
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
|
|
283
|
+
ixs.push(yield this.program.methods
|
|
284
|
+
.createSubUserTrade(subUserTradePDA)
|
|
285
|
+
.accounts({
|
|
286
|
+
signer: this.provider.publicKey
|
|
287
|
+
})
|
|
288
|
+
.instruction());
|
|
289
|
+
}
|
|
230
290
|
}
|
|
231
|
-
catch (
|
|
291
|
+
catch (error) {
|
|
292
|
+
console.log(error);
|
|
232
293
|
const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
|
|
233
|
-
const subUserTradePDA = (0,
|
|
234
|
-
userTradePDA = (0,
|
|
294
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
|
|
295
|
+
userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
235
296
|
ixs.push(yield this.program.methods
|
|
236
297
|
.createSubUserTrade(subUserTradePDA)
|
|
237
298
|
.accounts({
|
|
@@ -240,22 +301,36 @@ class Trade {
|
|
|
240
301
|
.instruction());
|
|
241
302
|
}
|
|
242
303
|
}
|
|
243
|
-
|
|
244
|
-
.
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
304
|
+
if (Object.keys(orderSide)[0] === 'bid') {
|
|
305
|
+
ixs.push(yield this.program.methods
|
|
306
|
+
.placeBidOrder({
|
|
307
|
+
amount: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
|
|
308
|
+
price: new bn_js_1.default(price * Math.pow(10, this.decimals)),
|
|
309
|
+
orderDirection: direction
|
|
310
|
+
})
|
|
311
|
+
.accounts({
|
|
312
|
+
signer: this.provider.publicKey,
|
|
313
|
+
market: marketPDA,
|
|
314
|
+
userTrade: userTradePDA,
|
|
315
|
+
mint,
|
|
316
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(mint)
|
|
317
|
+
})
|
|
318
|
+
.instruction());
|
|
319
|
+
}
|
|
320
|
+
if (Object.keys(orderSide)[0] === 'ask') {
|
|
321
|
+
ixs.push(yield this.program.methods
|
|
322
|
+
.placeAskOrder({
|
|
323
|
+
shares: new bn_js_1.default(amount * Math.pow(10, this.decimals)),
|
|
324
|
+
price: new bn_js_1.default(price * Math.pow(10, this.decimals)),
|
|
325
|
+
orderDirection: direction
|
|
326
|
+
})
|
|
327
|
+
.accounts({
|
|
328
|
+
signer: this.provider.publicKey,
|
|
329
|
+
market: marketPDA,
|
|
330
|
+
userTrade: userTradePDA
|
|
331
|
+
})
|
|
332
|
+
.instruction());
|
|
333
|
+
}
|
|
259
334
|
return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
|
|
260
335
|
});
|
|
261
336
|
}
|
|
@@ -270,41 +345,21 @@ class Trade {
|
|
|
270
345
|
*/
|
|
271
346
|
cancelOrder({ marketId, orderId, userNonce }, options) {
|
|
272
347
|
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
-
const marketPDA = (0,
|
|
274
|
-
let userTradePDA = (0,
|
|
348
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
349
|
+
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
275
350
|
if (userNonce !== 0) {
|
|
276
|
-
const subUserTradePDA = (0,
|
|
277
|
-
userTradePDA = (0,
|
|
351
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
|
|
352
|
+
userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
278
353
|
}
|
|
279
354
|
return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelOrder(new bn_js_1.default(orderId)).accounts({
|
|
280
355
|
signer: this.provider.publicKey,
|
|
281
356
|
market: marketPDA,
|
|
282
|
-
mint:
|
|
357
|
+
mint: this.mint,
|
|
283
358
|
userTrade: userTradePDA,
|
|
284
|
-
tokenProgram: (0, helpers_1.getTokenProgram)(
|
|
359
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(this.mint)
|
|
285
360
|
}), options);
|
|
286
361
|
});
|
|
287
362
|
}
|
|
288
|
-
/**
|
|
289
|
-
* Resolve Market
|
|
290
|
-
* @param args.marketId - The ID of the Market
|
|
291
|
-
* @param args.winningDirection - The Winning Direction of the Market
|
|
292
|
-
*
|
|
293
|
-
* @param options - RPC options
|
|
294
|
-
*
|
|
295
|
-
*/
|
|
296
|
-
resolveMarket({ marketId, winningDirection }, options) {
|
|
297
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
298
|
-
const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
|
|
299
|
-
const method = this.program.methods
|
|
300
|
-
.resolveMarket(winningDirection)
|
|
301
|
-
.accounts({
|
|
302
|
-
signer: this.provider.publicKey,
|
|
303
|
-
market: marketPDA
|
|
304
|
-
});
|
|
305
|
-
return (0, sendTransactionWithOptions_1.default)(method, options);
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
363
|
/**
|
|
309
364
|
* Collect Remaining Liquidity
|
|
310
365
|
* @param marketId - The ID of the market
|
|
@@ -314,11 +369,11 @@ class Trade {
|
|
|
314
369
|
*/
|
|
315
370
|
collectRemainingLiquidity(marketId, options) {
|
|
316
371
|
return __awaiter(this, void 0, void 0, function* () {
|
|
317
|
-
const marketPDA = (0,
|
|
372
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
318
373
|
return (0, sendTransactionWithOptions_1.default)(this.program.methods.collectRemainingLiquidity().accounts({
|
|
319
374
|
signer: this.provider.publicKey,
|
|
320
375
|
market: marketPDA,
|
|
321
|
-
mint:
|
|
376
|
+
mint: this.mint
|
|
322
377
|
}), options);
|
|
323
378
|
});
|
|
324
379
|
}
|
|
@@ -333,17 +388,17 @@ class Trade {
|
|
|
333
388
|
*/
|
|
334
389
|
payoutOrder({ marketId, orderId, userNonce }, options) {
|
|
335
390
|
return __awaiter(this, void 0, void 0, function* () {
|
|
336
|
-
const marketPDA = (0,
|
|
337
|
-
let userTradePDA = (0,
|
|
391
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
392
|
+
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
338
393
|
if (userNonce !== 0) {
|
|
339
|
-
const subUserTradePDA = (0,
|
|
340
|
-
userTradePDA = (0,
|
|
394
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
|
|
395
|
+
userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
341
396
|
}
|
|
342
397
|
return (0, sendTransactionWithOptions_1.default)(this.program.methods.payoutOrder(new bn_js_1.default(orderId)).accounts({
|
|
343
398
|
signer: this.provider.publicKey,
|
|
344
399
|
userTrade: userTradePDA,
|
|
345
400
|
market: marketPDA,
|
|
346
|
-
mint:
|
|
401
|
+
mint: this.mint
|
|
347
402
|
}), options);
|
|
348
403
|
});
|
|
349
404
|
}
|
|
@@ -356,7 +411,7 @@ class Trade {
|
|
|
356
411
|
*/
|
|
357
412
|
allowMarketToPayout(marketId, options) {
|
|
358
413
|
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
-
const marketPDA = (0,
|
|
414
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
360
415
|
return (0, sendTransactionWithOptions_1.default)(this.program.methods.allowMarketToPayout().accounts({
|
|
361
416
|
signer: this.provider.publicKey,
|
|
362
417
|
market: marketPDA
|
|
@@ -373,7 +428,7 @@ class Trade {
|
|
|
373
428
|
createSubUserTrade(user, options) {
|
|
374
429
|
return __awaiter(this, void 0, void 0, function* () {
|
|
375
430
|
const userTrade = yield this.getUserTrade(user);
|
|
376
|
-
const subUserTradePDA = (0,
|
|
431
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
|
|
377
432
|
return (0, sendTransactionWithOptions_1.default)(this.program.methods.createSubUserTrade(subUserTradePDA).accounts({
|
|
378
433
|
signer: this.provider.publicKey
|
|
379
434
|
}), options);
|
|
@@ -383,18 +438,125 @@ class Trade {
|
|
|
383
438
|
* Update Market
|
|
384
439
|
* @param marketId - The ID of the market
|
|
385
440
|
* @param marketEnd - The end time of the market
|
|
441
|
+
* @param winningDirection - The winning direction of the market
|
|
386
442
|
*
|
|
387
443
|
* @param options - RPC options
|
|
388
444
|
*
|
|
389
445
|
*/
|
|
390
|
-
updateMarket(marketId, marketEnd, options) {
|
|
446
|
+
updateMarket({ marketId, marketEnd, winningDirection }, options) {
|
|
391
447
|
return __awaiter(this, void 0, void 0, function* () {
|
|
392
|
-
const marketPDA = (0,
|
|
393
|
-
return (0, sendTransactionWithOptions_1.default)(this.program.methods
|
|
448
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
449
|
+
return (0, sendTransactionWithOptions_1.default)(this.program.methods
|
|
450
|
+
.updateMarket({
|
|
451
|
+
marketEnd: marketEnd ? new bn_js_1.default(marketEnd) : undefined,
|
|
452
|
+
winningDirection
|
|
453
|
+
})
|
|
454
|
+
.accounts({
|
|
394
455
|
signer: this.provider.publicKey,
|
|
395
456
|
market: marketPDA
|
|
396
457
|
}), options);
|
|
397
458
|
});
|
|
398
459
|
}
|
|
460
|
+
/**
|
|
461
|
+
* Create Customer
|
|
462
|
+
* @param args.id - The ID of the customer
|
|
463
|
+
* @param args.name - The name of the customer
|
|
464
|
+
* @param args.authority - The authority of the customer
|
|
465
|
+
* @param args.shadowStorage - The shadow storage of the customer
|
|
466
|
+
*
|
|
467
|
+
* @param options - RPC options
|
|
468
|
+
*
|
|
469
|
+
*/
|
|
470
|
+
createCustomer({ id, name, authority, shadowStorage }, options) {
|
|
471
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
472
|
+
return (0, sendTransactionWithOptions_1.default)(this.program.methods
|
|
473
|
+
.createCustomer({ id, name, authority, shadowStorage })
|
|
474
|
+
.accounts({
|
|
475
|
+
signer: this.provider.publicKey
|
|
476
|
+
}), options);
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Open Order
|
|
481
|
+
* @param args.marketId - The ID of the Market
|
|
482
|
+
* @param args.amount - The amount of the Order
|
|
483
|
+
* @param args.direction - The direction of the Order
|
|
484
|
+
*
|
|
485
|
+
* @param options - RPC options
|
|
486
|
+
*
|
|
487
|
+
*/
|
|
488
|
+
openOrder({ marketId, amount, direction }, options) {
|
|
489
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
490
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
491
|
+
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
492
|
+
const ixs = [];
|
|
493
|
+
const addressLookupTableAccounts = [];
|
|
494
|
+
let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
|
|
495
|
+
let myUserTrades = [];
|
|
496
|
+
myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
|
|
497
|
+
if (myUserTrades.length === 0) {
|
|
498
|
+
ixs.push(yield this.program.methods
|
|
499
|
+
.createUserTrade()
|
|
500
|
+
.accounts({
|
|
501
|
+
signer: this.provider.publicKey
|
|
502
|
+
})
|
|
503
|
+
.instruction());
|
|
504
|
+
}
|
|
505
|
+
if (myUserTrades.length > 0) {
|
|
506
|
+
try {
|
|
507
|
+
userTradePDA = yield this.getUserTradeNonceWithSlots(myUserTrades);
|
|
508
|
+
}
|
|
509
|
+
catch (_a) {
|
|
510
|
+
const mainUserTrade = myUserTrades.find((userTrade) => !userTrade.isSubUser);
|
|
511
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, Number(mainUserTrade.nonce) + 1);
|
|
512
|
+
userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
513
|
+
ixs.push(yield this.program.methods
|
|
514
|
+
.createSubUserTrade(subUserTradePDA)
|
|
515
|
+
.accounts({
|
|
516
|
+
signer: this.provider.publicKey
|
|
517
|
+
})
|
|
518
|
+
.instruction());
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
ixs.push(yield this.program.methods
|
|
522
|
+
.openOrder({
|
|
523
|
+
amount: new bn_js_1.default(amountInTRD),
|
|
524
|
+
direction: direction
|
|
525
|
+
})
|
|
526
|
+
.accounts({
|
|
527
|
+
signer: this.provider.publicKey,
|
|
528
|
+
market: marketPDA,
|
|
529
|
+
userTrade: userTradePDA,
|
|
530
|
+
mint: this.mint
|
|
531
|
+
})
|
|
532
|
+
.instruction());
|
|
533
|
+
return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Close Order
|
|
538
|
+
* @param args.marketId - The ID of the Market
|
|
539
|
+
* @param args.orderId - The ID of the Order
|
|
540
|
+
* @param args.userNonce - The nonce of the user
|
|
541
|
+
*
|
|
542
|
+
* @param options - RPC options
|
|
543
|
+
*
|
|
544
|
+
*/
|
|
545
|
+
closeOrder({ marketId, orderId, userNonce }, options) {
|
|
546
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
547
|
+
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
548
|
+
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
549
|
+
if (userNonce !== 0) {
|
|
550
|
+
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
|
|
551
|
+
userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
552
|
+
}
|
|
553
|
+
return (0, sendTransactionWithOptions_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
|
|
554
|
+
signer: this.provider.publicKey,
|
|
555
|
+
market: marketPDA,
|
|
556
|
+
mint: this.mint,
|
|
557
|
+
userTrade: userTradePDA
|
|
558
|
+
}), options);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
399
561
|
}
|
|
400
562
|
exports.default = Trade;
|