@triadxyz/triad-protocol 1.9.8-beta → 2.0.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.js +20 -20
- package/dist/stake.d.ts +2 -3
- package/dist/stake.js +30 -46
- package/dist/trade.d.ts +3 -4
- package/dist/trade.js +152 -162
- package/dist/types/idl_triad_protocol.json +93 -103
- package/dist/types/triad_protocol.d.ts +138 -129
- package/dist/utils/constants.d.ts +0 -3
- package/dist/utils/constants.js +1 -4
- package/dist/utils/helpers.d.ts +7 -5
- package/dist/utils/helpers.js +15 -15
- package/dist/utils/pda/index.d.ts +4 -5
- package/dist/utils/pda/index.js +17 -17
- package/dist/utils/pda/poseidon.d.ts +4 -0
- package/dist/utils/pda/poseidon.js +20 -0
- package/dist/utils/pda/stake.d.ts +0 -1
- package/dist/utils/pda/stake.js +1 -5
- package/dist/utils/{sendTransactionWithOptions.d.ts → sendMethod.d.ts} +2 -2
- package/dist/utils/{sendTransactionWithOptions.js → sendMethod.js} +2 -2
- package/dist/utils/sendVersionedTransaction.d.ts +3 -2
- package/dist/utils/sendVersionedTransaction.js +4 -8
- package/package.json +5 -5
- package/dist/utils/formatNumber.d.ts +0 -3
- package/dist/utils/formatNumber.js +0 -6
- package/dist/utils/pda/trade.d.ts +0 -5
- package/dist/utils/pda/trade.js +0 -28
package/dist/trade.js
CHANGED
|
@@ -17,15 +17,14 @@ const trade_1 = require("./types/trade");
|
|
|
17
17
|
const bn_js_1 = __importDefault(require("bn.js"));
|
|
18
18
|
const constants_1 = require("./utils/constants");
|
|
19
19
|
const helpers_1 = require("./utils/helpers");
|
|
20
|
-
const
|
|
20
|
+
const pda_1 = require("./utils/pda");
|
|
21
21
|
const sendVersionedTransaction_1 = __importDefault(require("./utils/sendVersionedTransaction"));
|
|
22
|
-
const
|
|
22
|
+
const sendMethod_1 = __importDefault(require("./utils/sendMethod"));
|
|
23
23
|
const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
|
|
24
|
-
const
|
|
24
|
+
const pda_2 = require("./utils/pda");
|
|
25
25
|
class Trade {
|
|
26
|
-
constructor(program
|
|
26
|
+
constructor(program) {
|
|
27
27
|
this.program = program;
|
|
28
|
-
this.provider = provider;
|
|
29
28
|
this.decimals = constants_1.TRD_DECIMALS; // We're using only TRD or USDC so we can use the same decimals 6
|
|
30
29
|
}
|
|
31
30
|
/**
|
|
@@ -64,8 +63,7 @@ class Trade {
|
|
|
64
63
|
getUserOrders(user) {
|
|
65
64
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
65
|
const myUserTrades = yield this.getMyUserTrades(user);
|
|
67
|
-
|
|
68
|
-
return orders;
|
|
66
|
+
return myUserTrades.flatMap((userTrade) => userTrade.orders);
|
|
69
67
|
});
|
|
70
68
|
}
|
|
71
69
|
/**
|
|
@@ -75,7 +73,7 @@ class Trade {
|
|
|
75
73
|
*/
|
|
76
74
|
getMarketById(marketId) {
|
|
77
75
|
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
-
const marketPDA = (0,
|
|
76
|
+
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
79
77
|
const response = yield this.program.account.marketV2.fetch(marketPDA);
|
|
80
78
|
return (0, helpers_1.formatMarket)(response, marketPDA);
|
|
81
79
|
});
|
|
@@ -99,10 +97,10 @@ class Trade {
|
|
|
99
97
|
*/
|
|
100
98
|
getUserTrade(user, userNonce = 0) {
|
|
101
99
|
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
let userTradePDA = (0,
|
|
100
|
+
let userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, user);
|
|
103
101
|
if (userNonce !== 0) {
|
|
104
|
-
const subUserTradePDA = (0,
|
|
105
|
-
userTradePDA = (0,
|
|
102
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, user, userNonce);
|
|
103
|
+
userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
106
104
|
}
|
|
107
105
|
return this.program.account.userTrade.fetch(userTradePDA);
|
|
108
106
|
});
|
|
@@ -120,26 +118,24 @@ class Trade {
|
|
|
120
118
|
*
|
|
121
119
|
*/
|
|
122
120
|
createMarket({ marketId, startTime, endTime, question, feeBps, customer, payoutFee, mint }, options) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}), options);
|
|
142
|
-
});
|
|
121
|
+
if (question.length > 80) {
|
|
122
|
+
throw new Error('Question must be less than 80 characters');
|
|
123
|
+
}
|
|
124
|
+
return (0, sendMethod_1.default)(this.program.methods
|
|
125
|
+
.createMarket({
|
|
126
|
+
marketId: new bn_js_1.default(marketId),
|
|
127
|
+
question: (0, helpers_1.encodeString)(question, 80),
|
|
128
|
+
marketStart: new bn_js_1.default(startTime),
|
|
129
|
+
marketEnd: new bn_js_1.default(endTime),
|
|
130
|
+
feeBps,
|
|
131
|
+
payoutFee
|
|
132
|
+
})
|
|
133
|
+
.accounts({
|
|
134
|
+
signer: this.program.provider.publicKey,
|
|
135
|
+
mint,
|
|
136
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(mint),
|
|
137
|
+
customer
|
|
138
|
+
}), options);
|
|
143
139
|
}
|
|
144
140
|
/**
|
|
145
141
|
* Open Order
|
|
@@ -153,27 +149,25 @@ class Trade {
|
|
|
153
149
|
*/
|
|
154
150
|
openOrder({ marketId, amount, direction, mint }, options) {
|
|
155
151
|
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
-
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
157
152
|
const ixs = [];
|
|
158
153
|
const addressLookupTableAccounts = [];
|
|
159
|
-
let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
|
|
160
154
|
const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
|
|
161
155
|
if (userTradeIxs.length > 0) {
|
|
162
156
|
ixs.push(...userTradeIxs);
|
|
163
157
|
}
|
|
164
158
|
ixs.push(yield this.program.methods
|
|
165
159
|
.openOrder({
|
|
166
|
-
amount: new bn_js_1.default(
|
|
160
|
+
amount: new bn_js_1.default(amount * Math.pow(10, constants_1.TRD_DECIMALS)),
|
|
167
161
|
direction: direction
|
|
168
162
|
})
|
|
169
163
|
.accounts({
|
|
170
|
-
signer: this.provider.publicKey,
|
|
171
|
-
market:
|
|
164
|
+
signer: this.program.provider.publicKey,
|
|
165
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
|
|
172
166
|
userTrade: userTradePDA,
|
|
173
167
|
mint
|
|
174
168
|
})
|
|
175
169
|
.instruction());
|
|
176
|
-
return (0, sendVersionedTransaction_1.default)(this.
|
|
170
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options, addressLookupTableAccounts);
|
|
177
171
|
});
|
|
178
172
|
}
|
|
179
173
|
/**
|
|
@@ -187,15 +181,14 @@ class Trade {
|
|
|
187
181
|
*/
|
|
188
182
|
closeOrder({ marketId, orderId, userNonce }, options) {
|
|
189
183
|
return __awaiter(this, void 0, void 0, function* () {
|
|
190
|
-
|
|
191
|
-
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
184
|
+
let userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
192
185
|
if (userNonce !== 0) {
|
|
193
|
-
const subUserTradePDA = (0,
|
|
194
|
-
userTradePDA = (0,
|
|
186
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, userNonce);
|
|
187
|
+
userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
195
188
|
}
|
|
196
|
-
return (0,
|
|
197
|
-
signer: this.provider.publicKey,
|
|
198
|
-
market:
|
|
189
|
+
return (0, sendMethod_1.default)(this.program.methods.closeOrder(new bn_js_1.default(orderId)).accounts({
|
|
190
|
+
signer: this.program.provider.publicKey,
|
|
191
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
|
|
199
192
|
mint: constants_1.TRD_MINT,
|
|
200
193
|
userTrade: userTradePDA
|
|
201
194
|
}), options);
|
|
@@ -210,16 +203,16 @@ class Trade {
|
|
|
210
203
|
*
|
|
211
204
|
*/
|
|
212
205
|
resolveMarket({ marketId, winningDirection }, options) {
|
|
213
|
-
return
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
});
|
|
206
|
+
return (0, sendMethod_1.default)(this.program.methods
|
|
207
|
+
.updateMarket({
|
|
208
|
+
winningDirection,
|
|
209
|
+
allowPayout: null,
|
|
210
|
+
marketEnd: null
|
|
211
|
+
})
|
|
212
|
+
.accounts({
|
|
213
|
+
signer: this.program.provider.publicKey,
|
|
214
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId)
|
|
215
|
+
}), options);
|
|
223
216
|
}
|
|
224
217
|
/**
|
|
225
218
|
* Collect Remaining Liquidity
|
|
@@ -230,10 +223,10 @@ class Trade {
|
|
|
230
223
|
*/
|
|
231
224
|
collectRemainingLiquidity(marketId, options) {
|
|
232
225
|
return __awaiter(this, void 0, void 0, function* () {
|
|
233
|
-
const marketPDA = (0,
|
|
226
|
+
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
234
227
|
const market = yield this.getMarketByAddress(marketPDA);
|
|
235
|
-
return (0,
|
|
236
|
-
signer: this.provider.publicKey,
|
|
228
|
+
return (0, sendMethod_1.default)(this.program.methods.collectRemainingLiquidity().accounts({
|
|
229
|
+
signer: this.program.provider.publicKey,
|
|
237
230
|
market: marketPDA,
|
|
238
231
|
mint: market.mint
|
|
239
232
|
}), options);
|
|
@@ -255,24 +248,23 @@ class Trade {
|
|
|
255
248
|
throw new Error('Max 10 orders per transaction');
|
|
256
249
|
}
|
|
257
250
|
for (const order of orders) {
|
|
258
|
-
|
|
259
|
-
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
251
|
+
let userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
260
252
|
if (order.userNonce !== 0) {
|
|
261
|
-
const subUserTradePDA = (0,
|
|
262
|
-
userTradePDA = (0,
|
|
253
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, order.userNonce);
|
|
254
|
+
userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
263
255
|
}
|
|
264
256
|
ixs.push(yield this.program.methods
|
|
265
257
|
.payoutOrder(new bn_js_1.default(order.orderId))
|
|
266
258
|
.accounts({
|
|
267
|
-
signer: this.provider.publicKey,
|
|
259
|
+
signer: this.program.provider.publicKey,
|
|
268
260
|
userTrade: userTradePDA,
|
|
269
|
-
market:
|
|
261
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, order.marketId),
|
|
270
262
|
mint: order.mint,
|
|
271
263
|
tokenProgram: (0, helpers_1.getTokenProgram)(order.mint)
|
|
272
264
|
})
|
|
273
265
|
.instruction());
|
|
274
266
|
}
|
|
275
|
-
return (0, sendVersionedTransaction_1.default)(this.
|
|
267
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
|
|
276
268
|
});
|
|
277
269
|
}
|
|
278
270
|
/**
|
|
@@ -283,13 +275,16 @@ class Trade {
|
|
|
283
275
|
*
|
|
284
276
|
*/
|
|
285
277
|
allowMarketToPayout(marketId, options) {
|
|
286
|
-
return
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
278
|
+
return (0, sendMethod_1.default)(this.program.methods
|
|
279
|
+
.updateMarket({
|
|
280
|
+
winningDirection: null,
|
|
281
|
+
allowPayout: true,
|
|
282
|
+
marketEnd: null
|
|
283
|
+
})
|
|
284
|
+
.accounts({
|
|
285
|
+
signer: this.program.provider.publicKey,
|
|
286
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId)
|
|
287
|
+
}), options);
|
|
293
288
|
}
|
|
294
289
|
/**
|
|
295
290
|
* Create Sub User Trade
|
|
@@ -301,9 +296,9 @@ class Trade {
|
|
|
301
296
|
createSubUserTrade(user, options) {
|
|
302
297
|
return __awaiter(this, void 0, void 0, function* () {
|
|
303
298
|
const userTrade = yield this.getUserTrade(user);
|
|
304
|
-
const subUserTradePDA = (0,
|
|
305
|
-
return (0,
|
|
306
|
-
signer: this.provider.publicKey
|
|
299
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, user, userTrade.nonce + 1);
|
|
300
|
+
return (0, sendMethod_1.default)(this.program.methods.createSubUserTrade(subUserTradePDA).accounts({
|
|
301
|
+
signer: this.program.provider.publicKey
|
|
307
302
|
}), options);
|
|
308
303
|
});
|
|
309
304
|
}
|
|
@@ -316,13 +311,16 @@ class Trade {
|
|
|
316
311
|
*
|
|
317
312
|
*/
|
|
318
313
|
updateMarket(marketId, marketEnd, options) {
|
|
319
|
-
return
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
314
|
+
return (0, sendMethod_1.default)(this.program.methods
|
|
315
|
+
.updateMarket({
|
|
316
|
+
marketEnd: new bn_js_1.default(marketEnd),
|
|
317
|
+
winningDirection: null,
|
|
318
|
+
allowPayout: null
|
|
319
|
+
})
|
|
320
|
+
.accounts({
|
|
321
|
+
signer: this.program.provider.publicKey,
|
|
322
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId)
|
|
323
|
+
}), options);
|
|
326
324
|
}
|
|
327
325
|
/**
|
|
328
326
|
* Create Customer
|
|
@@ -334,13 +332,11 @@ class Trade {
|
|
|
334
332
|
*
|
|
335
333
|
*/
|
|
336
334
|
createCustomer({ id, name, authority, feeRecipient }, options) {
|
|
337
|
-
return
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}), options);
|
|
343
|
-
});
|
|
335
|
+
return (0, sendMethod_1.default)(this.program.methods
|
|
336
|
+
.createCustomer({ id, name, authority, feeRecipient })
|
|
337
|
+
.accounts({
|
|
338
|
+
signer: this.program.provider.publicKey
|
|
339
|
+
}), options);
|
|
344
340
|
}
|
|
345
341
|
/**
|
|
346
342
|
* Get User Trade Nonce With Slots
|
|
@@ -348,50 +344,48 @@ class Trade {
|
|
|
348
344
|
*
|
|
349
345
|
*/
|
|
350
346
|
getUserTradeNonceWithSlots(userTrades) {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
347
|
+
let nonce = null;
|
|
348
|
+
for (const userTrade of userTrades.reverse()) {
|
|
349
|
+
if (nonce !== null) {
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
let freeSlots = 0;
|
|
353
|
+
userTrade.orders.forEach((order) => {
|
|
354
354
|
if (nonce !== null) {
|
|
355
|
-
|
|
355
|
+
return;
|
|
356
356
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
return (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
378
|
-
}
|
|
379
|
-
const subUserTradePDA = (0, trade_2.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, nonce);
|
|
380
|
-
const userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
381
|
-
return userTradePDA;
|
|
382
|
-
});
|
|
357
|
+
if (order.orderStatus !== trade_1.OrderStatus.OPEN &&
|
|
358
|
+
order.orderStatus !== trade_1.OrderStatus.WAITING &&
|
|
359
|
+
freeSlots >= 2) {
|
|
360
|
+
nonce = userTrade.isSubUser ? Number(userTrade.nonce) : 0;
|
|
361
|
+
}
|
|
362
|
+
if (order.orderStatus !== trade_1.OrderStatus.OPEN &&
|
|
363
|
+
order.orderStatus !== trade_1.OrderStatus.WAITING) {
|
|
364
|
+
freeSlots += 1;
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
if (nonce === null) {
|
|
369
|
+
throw new Error('No open orders found');
|
|
370
|
+
}
|
|
371
|
+
if (nonce === 0) {
|
|
372
|
+
return (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
373
|
+
}
|
|
374
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, nonce);
|
|
375
|
+
const userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
376
|
+
return userTradePDA;
|
|
383
377
|
}
|
|
384
378
|
getUserTradeIxs() {
|
|
385
379
|
return __awaiter(this, void 0, void 0, function* () {
|
|
386
|
-
let userTradePDA = (0,
|
|
380
|
+
let userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
387
381
|
const ixs = [];
|
|
388
382
|
let myUserTrades = [];
|
|
389
|
-
myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
|
|
383
|
+
myUserTrades = yield this.getMyUserTrades(this.program.provider.publicKey);
|
|
390
384
|
if (myUserTrades.length === 0) {
|
|
391
385
|
ixs.push(yield this.program.methods
|
|
392
386
|
.createUserTrade()
|
|
393
387
|
.accounts({
|
|
394
|
-
signer: this.provider.publicKey
|
|
388
|
+
signer: this.program.provider.publicKey
|
|
395
389
|
})
|
|
396
390
|
.instruction());
|
|
397
391
|
return {
|
|
@@ -401,20 +395,20 @@ class Trade {
|
|
|
401
395
|
};
|
|
402
396
|
}
|
|
403
397
|
try {
|
|
404
|
-
const userTradePDA =
|
|
398
|
+
const userTradePDA = this.getUserTradeNonceWithSlots(myUserTrades);
|
|
405
399
|
return { userTradePDA, ixs };
|
|
406
400
|
}
|
|
407
401
|
catch (_a) {
|
|
408
402
|
const mainUserTrade = myUserTrades.find((trade) => !trade.isSubUser);
|
|
409
|
-
const subUserTradePDA = (0,
|
|
403
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, Number(mainUserTrade.nonce) + 1);
|
|
410
404
|
ixs.push(yield this.program.methods
|
|
411
405
|
.createSubUserTrade(subUserTradePDA)
|
|
412
406
|
.accounts({
|
|
413
|
-
signer: this.provider.publicKey
|
|
407
|
+
signer: this.program.provider.publicKey
|
|
414
408
|
})
|
|
415
409
|
.instruction());
|
|
416
410
|
return {
|
|
417
|
-
userTradePDA: (0,
|
|
411
|
+
userTradePDA: (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA),
|
|
418
412
|
ixs
|
|
419
413
|
};
|
|
420
414
|
}
|
|
@@ -432,7 +426,6 @@ class Trade {
|
|
|
432
426
|
*/
|
|
433
427
|
placeBidOrder({ marketId, amount, direction, mint, price }, options) {
|
|
434
428
|
return __awaiter(this, void 0, void 0, function* () {
|
|
435
|
-
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
436
429
|
const ixs = [];
|
|
437
430
|
const { userTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
|
|
438
431
|
if (userTradeIxs.length > 0) {
|
|
@@ -445,14 +438,14 @@ class Trade {
|
|
|
445
438
|
orderDirection: direction
|
|
446
439
|
})
|
|
447
440
|
.accounts({
|
|
448
|
-
signer: this.provider.publicKey,
|
|
449
|
-
market:
|
|
441
|
+
signer: this.program.provider.publicKey,
|
|
442
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
|
|
450
443
|
userTrade: userTradePDA,
|
|
451
444
|
mint,
|
|
452
445
|
tokenProgram: (0, helpers_1.getTokenProgram)(mint)
|
|
453
446
|
})
|
|
454
447
|
.instruction());
|
|
455
|
-
return (0, sendVersionedTransaction_1.default)(this.
|
|
448
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
|
|
456
449
|
});
|
|
457
450
|
}
|
|
458
451
|
/**
|
|
@@ -469,16 +462,15 @@ class Trade {
|
|
|
469
462
|
*/
|
|
470
463
|
placeAskOrder({ marketId, amount, direction, price, bidOrderId, bidNonce }, options) {
|
|
471
464
|
return __awaiter(this, void 0, void 0, function* () {
|
|
472
|
-
const marketPDA = (0, trade_2.getMarketPDA)(this.program.programId, marketId);
|
|
473
465
|
const ixs = [];
|
|
474
466
|
const { userTradePDA: askUserTradePDA, ixs: userTradeIxs } = yield this.getUserTradeIxs();
|
|
475
467
|
if (userTradeIxs.length > 0) {
|
|
476
468
|
ixs.push(...userTradeIxs);
|
|
477
469
|
}
|
|
478
|
-
let bidUserTradePDA = (0,
|
|
470
|
+
let bidUserTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
479
471
|
if (bidNonce !== 0) {
|
|
480
|
-
const subUserTradePDA = (0,
|
|
481
|
-
bidUserTradePDA = (0,
|
|
472
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, bidNonce);
|
|
473
|
+
bidUserTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
482
474
|
}
|
|
483
475
|
ixs.push(yield this.program.methods
|
|
484
476
|
.placeAskOrder({
|
|
@@ -488,13 +480,13 @@ class Trade {
|
|
|
488
480
|
orderDirection: direction
|
|
489
481
|
})
|
|
490
482
|
.accounts({
|
|
491
|
-
signer: this.provider.publicKey,
|
|
492
|
-
market:
|
|
483
|
+
signer: this.program.provider.publicKey,
|
|
484
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
|
|
493
485
|
askUserTrade: askUserTradePDA,
|
|
494
486
|
bidUserTrade: bidUserTradePDA
|
|
495
487
|
})
|
|
496
488
|
.instruction());
|
|
497
|
-
return (0, sendVersionedTransaction_1.default)(this.
|
|
489
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
|
|
498
490
|
});
|
|
499
491
|
}
|
|
500
492
|
/**
|
|
@@ -507,15 +499,14 @@ class Trade {
|
|
|
507
499
|
*/
|
|
508
500
|
cancelBidOrder({ marketId, orderId, userNonce, mint }, options) {
|
|
509
501
|
return __awaiter(this, void 0, void 0, function* () {
|
|
510
|
-
|
|
511
|
-
let userTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
502
|
+
let userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
512
503
|
if (userNonce !== 0) {
|
|
513
|
-
const subUserTradePDA = (0,
|
|
514
|
-
userTradePDA = (0,
|
|
504
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, userNonce);
|
|
505
|
+
userTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
515
506
|
}
|
|
516
|
-
return (0,
|
|
517
|
-
signer: this.provider.publicKey,
|
|
518
|
-
market:
|
|
507
|
+
return (0, sendMethod_1.default)(this.program.methods.cancelBidOrder(new bn_js_1.default(orderId)).accounts({
|
|
508
|
+
signer: this.program.provider.publicKey,
|
|
509
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
|
|
519
510
|
userTrade: userTradePDA,
|
|
520
511
|
mint,
|
|
521
512
|
tokenProgram: (0, helpers_1.getTokenProgram)(mint)
|
|
@@ -533,20 +524,19 @@ class Trade {
|
|
|
533
524
|
*/
|
|
534
525
|
cancelAskOrder({ marketId, orderId, userNonce, userNonceBidOrder }, options) {
|
|
535
526
|
return __awaiter(this, void 0, void 0, function* () {
|
|
536
|
-
|
|
537
|
-
let askUserTradePDA = (0, trade_2.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
527
|
+
let askUserTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
538
528
|
if (userNonce !== 0) {
|
|
539
|
-
const subUserTradePDA = (0,
|
|
540
|
-
askUserTradePDA = (0,
|
|
529
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, userNonce);
|
|
530
|
+
askUserTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
541
531
|
}
|
|
542
|
-
let bidUserTradePDA = (0,
|
|
532
|
+
let bidUserTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, this.program.provider.publicKey);
|
|
543
533
|
if (userNonceBidOrder !== 0) {
|
|
544
|
-
const subUserTradePDA = (0,
|
|
545
|
-
bidUserTradePDA = (0,
|
|
534
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, this.program.provider.publicKey, userNonceBidOrder);
|
|
535
|
+
bidUserTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
546
536
|
}
|
|
547
|
-
return (0,
|
|
548
|
-
signer: this.provider.publicKey,
|
|
549
|
-
market:
|
|
537
|
+
return (0, sendMethod_1.default)(this.program.methods.cancelAskOrder(new bn_js_1.default(orderId)).accounts({
|
|
538
|
+
signer: this.program.provider.publicKey,
|
|
539
|
+
market: (0, pda_1.getMarketPDA)(this.program.programId, marketId),
|
|
550
540
|
askUserTrade: askUserTradePDA,
|
|
551
541
|
bidUserTrade: bidUserTradePDA
|
|
552
542
|
}), options);
|
|
@@ -563,7 +553,7 @@ class Trade {
|
|
|
563
553
|
*/
|
|
564
554
|
marketBidOrder({ marketId, amount, direction, mint }, options) {
|
|
565
555
|
return __awaiter(this, void 0, void 0, function* () {
|
|
566
|
-
const marketPDA = (0,
|
|
556
|
+
const marketPDA = (0, pda_1.getMarketPDA)(this.program.programId, marketId);
|
|
567
557
|
const ixs = [];
|
|
568
558
|
const { userTradePDA: buyerTrade, ixs: userTradeIxs } = yield this.getUserTradeIxs();
|
|
569
559
|
const orderBook = yield this.getOrderBook(marketId);
|
|
@@ -575,7 +565,7 @@ class Trade {
|
|
|
575
565
|
for (const order of sortedOrders) {
|
|
576
566
|
if (remainingUSDC <= 0)
|
|
577
567
|
break;
|
|
578
|
-
if (order.authority === this.provider.publicKey.toBase58()) {
|
|
568
|
+
if (order.authority === this.program.provider.publicKey.toBase58()) {
|
|
579
569
|
continue;
|
|
580
570
|
}
|
|
581
571
|
const orderPrice = Number(order.price);
|
|
@@ -583,10 +573,10 @@ class Trade {
|
|
|
583
573
|
const availableShares = Number(order.totalShares);
|
|
584
574
|
const sharesToBuy = Math.min(maxSharesForPrice, availableShares);
|
|
585
575
|
const usdcAmount = sharesToBuy * orderPrice;
|
|
586
|
-
let sellerTradePDA = (0,
|
|
576
|
+
let sellerTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority));
|
|
587
577
|
if (order.userNonce !== '0') {
|
|
588
|
-
const subUserTradePDA = (0,
|
|
589
|
-
sellerTradePDA = (0,
|
|
578
|
+
const subUserTradePDA = (0, pda_1.getSubUserTradePDA)(this.program.programId, new web3_js_1.PublicKey(order.authority), Number(order.userNonce));
|
|
579
|
+
sellerTradePDA = (0, pda_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
590
580
|
}
|
|
591
581
|
const tokenProgram = (0, helpers_1.getTokenProgram)(mint);
|
|
592
582
|
ixs.push(yield this.program.methods
|
|
@@ -596,15 +586,15 @@ class Trade {
|
|
|
596
586
|
askOrderId: new bn_js_1.default(order.orderId)
|
|
597
587
|
})
|
|
598
588
|
.accounts({
|
|
599
|
-
signer: this.provider.publicKey,
|
|
589
|
+
signer: this.program.provider.publicKey,
|
|
600
590
|
market: marketPDA,
|
|
601
591
|
buyerTrade,
|
|
602
592
|
sellerTrade: sellerTradePDA,
|
|
603
593
|
mint,
|
|
604
594
|
tokenProgram,
|
|
605
595
|
sellerAuthority: new web3_js_1.PublicKey(order.authority),
|
|
606
|
-
|
|
607
|
-
|
|
596
|
+
marketAta: (0, pda_2.getTokenATA)(marketPDA, mint, tokenProgram),
|
|
597
|
+
buyerAta: (0, pda_2.getTokenATA)(this.program.provider.publicKey, mint, tokenProgram)
|
|
608
598
|
})
|
|
609
599
|
.instruction());
|
|
610
600
|
remainingUSDC -= usdcAmount;
|
|
@@ -615,7 +605,7 @@ class Trade {
|
|
|
615
605
|
if (ixs.length === 0) {
|
|
616
606
|
throw new Error('No matching orders found to fill the requested amount');
|
|
617
607
|
}
|
|
618
|
-
return (0, sendVersionedTransaction_1.default)(this.
|
|
608
|
+
return (0, sendVersionedTransaction_1.default)(this.program, ixs, options);
|
|
619
609
|
});
|
|
620
610
|
}
|
|
621
611
|
/**
|