@triadxyz/triad-protocol 1.6.7-beta → 1.6.7-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/trade.d.ts +53 -8
- package/dist/trade.js +51 -28
- package/dist/types/idl_triad_protocol.json +182 -140
- package/dist/types/trade.d.ts +17 -2
- package/dist/types/triad_protocol.d.ts +173 -131
- package/dist/utils/helpers.d.ts +1 -0
- package/dist/utils/helpers.js +10 -1
- package/package.json +1 -1
- package/dist/local-test/index.d.ts +0 -1
- package/dist/local-test/index.js +0 -528
- package/dist/local-test/init-market.d.ts +0 -8
- package/dist/local-test/init-market.js +0 -63
- package/dist/local-test/simulate-market.d.ts +0 -54
- package/dist/local-test/simulate-market.js +0 -156
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,
|
|
4
|
+
import { InitializeMarketArgs, PlaceOrderArgs, OrderDirection, UserTrade, OrderStatus } from './types/trade';
|
|
5
5
|
import { RpcOptions } from './types';
|
|
6
6
|
import BN from 'bn.js';
|
|
7
7
|
export default class Trade {
|
|
@@ -25,6 +25,25 @@ 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
|
+
}[]>;
|
|
28
47
|
/**
|
|
29
48
|
* Get Market By ID
|
|
30
49
|
* @param marketId - The ID of the market
|
|
@@ -52,13 +71,14 @@ export default class Trade {
|
|
|
52
71
|
orders: {
|
|
53
72
|
ts: BN;
|
|
54
73
|
orderId: BN;
|
|
55
|
-
|
|
74
|
+
filledShares: BN;
|
|
56
75
|
marketId: BN;
|
|
57
76
|
status: ({
|
|
58
77
|
open?: never;
|
|
59
78
|
closed?: never;
|
|
60
79
|
claimed?: never;
|
|
61
80
|
liquidated?: never;
|
|
81
|
+
waiting?: never;
|
|
62
82
|
} & {
|
|
63
83
|
init: Record<string, never>;
|
|
64
84
|
}) | ({
|
|
@@ -66,6 +86,7 @@ export default class Trade {
|
|
|
66
86
|
closed?: never;
|
|
67
87
|
claimed?: never;
|
|
68
88
|
liquidated?: never;
|
|
89
|
+
waiting?: never;
|
|
69
90
|
} & {
|
|
70
91
|
open: Record<string, never>;
|
|
71
92
|
}) | ({
|
|
@@ -73,6 +94,7 @@ export default class Trade {
|
|
|
73
94
|
open?: never;
|
|
74
95
|
claimed?: never;
|
|
75
96
|
liquidated?: never;
|
|
97
|
+
waiting?: never;
|
|
76
98
|
} & {
|
|
77
99
|
closed: Record<string, never>;
|
|
78
100
|
}) | ({
|
|
@@ -80,6 +102,7 @@ export default class Trade {
|
|
|
80
102
|
open?: never;
|
|
81
103
|
closed?: never;
|
|
82
104
|
liquidated?: never;
|
|
105
|
+
waiting?: never;
|
|
83
106
|
} & {
|
|
84
107
|
claimed: Record<string, never>;
|
|
85
108
|
}) | ({
|
|
@@ -87,8 +110,17 @@ export default class Trade {
|
|
|
87
110
|
open?: never;
|
|
88
111
|
closed?: never;
|
|
89
112
|
claimed?: never;
|
|
113
|
+
waiting?: never;
|
|
90
114
|
} & {
|
|
91
115
|
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>;
|
|
92
124
|
});
|
|
93
125
|
price: BN;
|
|
94
126
|
totalAmount: BN;
|
|
@@ -102,7 +134,7 @@ export default class Trade {
|
|
|
102
134
|
} & {
|
|
103
135
|
limit: Record<string, never>;
|
|
104
136
|
});
|
|
105
|
-
|
|
137
|
+
orderDirection: ({
|
|
106
138
|
flop?: never;
|
|
107
139
|
} & {
|
|
108
140
|
hype: Record<string, never>;
|
|
@@ -112,10 +144,20 @@ export default class Trade {
|
|
|
112
144
|
flop: Record<string, never>;
|
|
113
145
|
});
|
|
114
146
|
userNonce: number;
|
|
147
|
+
orderSide: ({
|
|
148
|
+
ask?: never;
|
|
149
|
+
} & {
|
|
150
|
+
bid: Record<string, never>;
|
|
151
|
+
}) | ({
|
|
152
|
+
bid?: never;
|
|
153
|
+
} & {
|
|
154
|
+
ask: Record<string, never>;
|
|
155
|
+
});
|
|
115
156
|
padding: number[];
|
|
116
157
|
}[];
|
|
117
158
|
nonce: number;
|
|
118
159
|
isSubUser: boolean;
|
|
160
|
+
poseidon: number;
|
|
119
161
|
padding: number[];
|
|
120
162
|
}>;
|
|
121
163
|
/**
|
|
@@ -136,18 +178,21 @@ export default class Trade {
|
|
|
136
178
|
*/
|
|
137
179
|
getUserTradeNonceWithSlots(userTrades: UserTrade[]): Promise<PublicKey>;
|
|
138
180
|
/**
|
|
139
|
-
*
|
|
181
|
+
* Place Order
|
|
140
182
|
* @param args.marketId - The ID of the Market
|
|
141
183
|
* @param args.amount - The amount of the Order
|
|
142
184
|
* @param args.direction - The direction of the Order
|
|
143
|
-
* @param args.
|
|
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
189
|
*
|
|
145
190
|
* @param options - RPC options
|
|
146
191
|
*
|
|
147
192
|
*/
|
|
148
|
-
|
|
193
|
+
placeOrder({ marketId, amount, direction, orderType, orderSide, mint, price }: PlaceOrderArgs, options?: RpcOptions): Promise<string>;
|
|
149
194
|
/**
|
|
150
|
-
*
|
|
195
|
+
* Cancel Order
|
|
151
196
|
* @param args.marketId - The ID of the Market
|
|
152
197
|
* @param args.orderId - The ID of the Order
|
|
153
198
|
* @param args.userNonce - The nonce of the user
|
|
@@ -155,7 +200,7 @@ export default class Trade {
|
|
|
155
200
|
* @param options - RPC options
|
|
156
201
|
*
|
|
157
202
|
*/
|
|
158
|
-
|
|
203
|
+
cancelOrder({ marketId, orderId, userNonce }: {
|
|
159
204
|
marketId: number;
|
|
160
205
|
orderId: number;
|
|
161
206
|
userNonce: number;
|
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
|
|
21
|
+
const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
|
|
22
22
|
class Trade {
|
|
23
23
|
constructor(program, provider) {
|
|
24
24
|
this.program = program;
|
|
@@ -64,6 +64,37 @@ 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
|
+
}
|
|
67
98
|
/**
|
|
68
99
|
* Get Market By ID
|
|
69
100
|
* @param marketId - The ID of the market
|
|
@@ -165,22 +196,24 @@ class Trade {
|
|
|
165
196
|
});
|
|
166
197
|
}
|
|
167
198
|
/**
|
|
168
|
-
*
|
|
199
|
+
* Place Order
|
|
169
200
|
* @param args.marketId - The ID of the Market
|
|
170
201
|
* @param args.amount - The amount of the Order
|
|
171
202
|
* @param args.direction - The direction of the Order
|
|
172
|
-
* @param args.
|
|
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
|
|
173
207
|
*
|
|
174
208
|
* @param options - RPC options
|
|
175
209
|
*
|
|
176
210
|
*/
|
|
177
|
-
|
|
211
|
+
placeOrder({ marketId, amount, direction, orderType, orderSide, mint, price }, options) {
|
|
178
212
|
return __awaiter(this, void 0, void 0, function* () {
|
|
179
213
|
const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
|
|
180
214
|
let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
181
215
|
const ixs = [];
|
|
182
216
|
const addressLookupTableAccounts = [];
|
|
183
|
-
let amountInTRD = amount * Math.pow(10, constants_1.TRD_DECIMALS);
|
|
184
217
|
let myUserTrades = [];
|
|
185
218
|
myUserTrades = yield this.getMyUserTrades(this.provider.publicKey);
|
|
186
219
|
if (myUserTrades.length === 0) {
|
|
@@ -207,38 +240,27 @@ class Trade {
|
|
|
207
240
|
.instruction());
|
|
208
241
|
}
|
|
209
242
|
}
|
|
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
|
-
}
|
|
225
243
|
ixs.push(yield this.program.methods
|
|
226
|
-
.
|
|
227
|
-
amount: new bn_js_1.default(
|
|
228
|
-
|
|
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
|
|
229
250
|
})
|
|
230
251
|
.accounts({
|
|
231
252
|
signer: this.provider.publicKey,
|
|
232
253
|
market: marketPDA,
|
|
233
254
|
userTrade: userTradePDA,
|
|
234
|
-
mint
|
|
255
|
+
mint,
|
|
256
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(mint)
|
|
235
257
|
})
|
|
236
258
|
.instruction());
|
|
237
259
|
return (0, sendVersionedTransaction_1.default)(this.provider, ixs, options, undefined, addressLookupTableAccounts);
|
|
238
260
|
});
|
|
239
261
|
}
|
|
240
262
|
/**
|
|
241
|
-
*
|
|
263
|
+
* Cancel Order
|
|
242
264
|
* @param args.marketId - The ID of the Market
|
|
243
265
|
* @param args.orderId - The ID of the Order
|
|
244
266
|
* @param args.userNonce - The nonce of the user
|
|
@@ -246,7 +268,7 @@ class Trade {
|
|
|
246
268
|
* @param options - RPC options
|
|
247
269
|
*
|
|
248
270
|
*/
|
|
249
|
-
|
|
271
|
+
cancelOrder({ marketId, orderId, userNonce }, options) {
|
|
250
272
|
return __awaiter(this, void 0, void 0, function* () {
|
|
251
273
|
const marketPDA = (0, trade_1.getMarketPDA)(this.program.programId, marketId);
|
|
252
274
|
let userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, this.provider.publicKey);
|
|
@@ -254,11 +276,12 @@ class Trade {
|
|
|
254
276
|
const subUserTradePDA = (0, trade_1.getSubUserTradePDA)(this.program.programId, this.provider.publicKey, userNonce);
|
|
255
277
|
userTradePDA = (0, trade_1.getUserTradePDA)(this.program.programId, subUserTradePDA);
|
|
256
278
|
}
|
|
257
|
-
return (0, sendTransactionWithOptions_1.default)(this.program.methods.
|
|
279
|
+
return (0, sendTransactionWithOptions_1.default)(this.program.methods.cancelOrder(new bn_js_1.default(orderId)).accounts({
|
|
258
280
|
signer: this.provider.publicKey,
|
|
259
281
|
market: marketPDA,
|
|
260
282
|
mint: constants_1.TRD_MINT,
|
|
261
|
-
userTrade: userTradePDA
|
|
283
|
+
userTrade: userTradePDA,
|
|
284
|
+
tokenProgram: (0, helpers_1.getTokenProgram)(constants_1.TRD_MINT)
|
|
262
285
|
}), options);
|
|
263
286
|
});
|
|
264
287
|
}
|