@lightconexyz/lightcone-sdk 0.1.0

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.
Files changed (158) hide show
  1. package/README.md +232 -0
  2. package/dist/api/client.d.ts +225 -0
  3. package/dist/api/client.d.ts.map +1 -0
  4. package/dist/api/client.js +452 -0
  5. package/dist/api/client.js.map +1 -0
  6. package/dist/api/error.d.ts +58 -0
  7. package/dist/api/error.d.ts.map +1 -0
  8. package/dist/api/error.js +98 -0
  9. package/dist/api/error.js.map +1 -0
  10. package/dist/api/index.d.ts +23 -0
  11. package/dist/api/index.d.ts.map +1 -0
  12. package/dist/api/index.js +51 -0
  13. package/dist/api/index.js.map +1 -0
  14. package/dist/api/types/admin.d.ts +49 -0
  15. package/dist/api/types/admin.d.ts.map +1 -0
  16. package/dist/api/types/admin.js +13 -0
  17. package/dist/api/types/admin.js.map +1 -0
  18. package/dist/api/types/index.d.ts +14 -0
  19. package/dist/api/types/index.d.ts.map +1 -0
  20. package/dist/api/types/index.js +13 -0
  21. package/dist/api/types/index.js.map +1 -0
  22. package/dist/api/types/market.d.ts +186 -0
  23. package/dist/api/types/market.d.ts.map +1 -0
  24. package/dist/api/types/market.js +6 -0
  25. package/dist/api/types/market.js.map +1 -0
  26. package/dist/api/types/order.d.ts +190 -0
  27. package/dist/api/types/order.d.ts.map +1 -0
  28. package/dist/api/types/order.js +6 -0
  29. package/dist/api/types/order.js.map +1 -0
  30. package/dist/api/types/orderbook.d.ts +36 -0
  31. package/dist/api/types/orderbook.d.ts.map +1 -0
  32. package/dist/api/types/orderbook.js +6 -0
  33. package/dist/api/types/orderbook.js.map +1 -0
  34. package/dist/api/types/position.d.ts +60 -0
  35. package/dist/api/types/position.d.ts.map +1 -0
  36. package/dist/api/types/position.js +6 -0
  37. package/dist/api/types/position.js.map +1 -0
  38. package/dist/api/types/price_history.d.ts +68 -0
  39. package/dist/api/types/price_history.d.ts.map +1 -0
  40. package/dist/api/types/price_history.js +13 -0
  41. package/dist/api/types/price_history.js.map +1 -0
  42. package/dist/api/types/trade.d.ts +67 -0
  43. package/dist/api/types/trade.d.ts.map +1 -0
  44. package/dist/api/types/trade.js +13 -0
  45. package/dist/api/types/trade.js.map +1 -0
  46. package/dist/api/validation.d.ts +24 -0
  47. package/dist/api/validation.d.ts.map +1 -0
  48. package/dist/api/validation.js +53 -0
  49. package/dist/api/validation.js.map +1 -0
  50. package/dist/auth.d.ts +80 -0
  51. package/dist/auth.d.ts.map +1 -0
  52. package/dist/auth.js +149 -0
  53. package/dist/auth.js.map +1 -0
  54. package/dist/index.d.ts +55 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +107 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/network.d.ts +5 -0
  59. package/dist/network.d.ts.map +1 -0
  60. package/dist/network.js +8 -0
  61. package/dist/network.js.map +1 -0
  62. package/dist/program/accounts.d.ts +98 -0
  63. package/dist/program/accounts.d.ts.map +1 -0
  64. package/dist/program/accounts.js +319 -0
  65. package/dist/program/accounts.js.map +1 -0
  66. package/dist/program/builder.d.ts +94 -0
  67. package/dist/program/builder.d.ts.map +1 -0
  68. package/dist/program/builder.js +175 -0
  69. package/dist/program/builder.js.map +1 -0
  70. package/dist/program/client.d.ts +56 -0
  71. package/dist/program/client.d.ts.map +1 -0
  72. package/dist/program/client.js +288 -0
  73. package/dist/program/client.js.map +1 -0
  74. package/dist/program/constants.d.ts +108 -0
  75. package/dist/program/constants.d.ts.map +1 -0
  76. package/dist/program/constants.js +112 -0
  77. package/dist/program/constants.js.map +1 -0
  78. package/dist/program/index.d.ts +14 -0
  79. package/dist/program/index.d.ts.map +1 -0
  80. package/dist/program/index.js +149 -0
  81. package/dist/program/index.js.map +1 -0
  82. package/dist/program/instructions.d.ts +248 -0
  83. package/dist/program/instructions.d.ts.map +1 -0
  84. package/dist/program/instructions.js +692 -0
  85. package/dist/program/instructions.js.map +1 -0
  86. package/dist/program/orders.d.ts +151 -0
  87. package/dist/program/orders.d.ts.map +1 -0
  88. package/dist/program/orders.js +417 -0
  89. package/dist/program/orders.js.map +1 -0
  90. package/dist/program/pda.d.ts +73 -0
  91. package/dist/program/pda.d.ts.map +1 -0
  92. package/dist/program/pda.js +131 -0
  93. package/dist/program/pda.js.map +1 -0
  94. package/dist/program/types.d.ts +380 -0
  95. package/dist/program/types.d.ts.map +1 -0
  96. package/dist/program/types.js +27 -0
  97. package/dist/program/types.js.map +1 -0
  98. package/dist/program/utils.d.ts +91 -0
  99. package/dist/program/utils.d.ts.map +1 -0
  100. package/dist/program/utils.js +219 -0
  101. package/dist/program/utils.js.map +1 -0
  102. package/dist/shared/index.d.ts +8 -0
  103. package/dist/shared/index.d.ts.map +1 -0
  104. package/dist/shared/index.js +18 -0
  105. package/dist/shared/index.js.map +1 -0
  106. package/dist/shared/price.d.ts +41 -0
  107. package/dist/shared/price.d.ts.map +1 -0
  108. package/dist/shared/price.js +57 -0
  109. package/dist/shared/price.js.map +1 -0
  110. package/dist/shared/scaling.d.ts +45 -0
  111. package/dist/shared/scaling.d.ts.map +1 -0
  112. package/dist/shared/scaling.js +84 -0
  113. package/dist/shared/scaling.js.map +1 -0
  114. package/dist/shared/types.d.ts +19 -0
  115. package/dist/shared/types.d.ts.map +1 -0
  116. package/dist/shared/types.js +23 -0
  117. package/dist/shared/types.js.map +1 -0
  118. package/dist/websocket/client.d.ts +238 -0
  119. package/dist/websocket/client.d.ts.map +1 -0
  120. package/dist/websocket/client.js +580 -0
  121. package/dist/websocket/client.js.map +1 -0
  122. package/dist/websocket/error.d.ts +47 -0
  123. package/dist/websocket/error.d.ts.map +1 -0
  124. package/dist/websocket/error.js +83 -0
  125. package/dist/websocket/error.js.map +1 -0
  126. package/dist/websocket/handlers.d.ts +97 -0
  127. package/dist/websocket/handlers.d.ts.map +1 -0
  128. package/dist/websocket/handlers.js +277 -0
  129. package/dist/websocket/handlers.js.map +1 -0
  130. package/dist/websocket/index.d.ts +38 -0
  131. package/dist/websocket/index.d.ts.map +1 -0
  132. package/dist/websocket/index.js +75 -0
  133. package/dist/websocket/index.js.map +1 -0
  134. package/dist/websocket/state/index.d.ts +7 -0
  135. package/dist/websocket/state/index.d.ts.map +1 -0
  136. package/dist/websocket/state/index.js +14 -0
  137. package/dist/websocket/state/index.js.map +1 -0
  138. package/dist/websocket/state/orderbook.d.ts +107 -0
  139. package/dist/websocket/state/orderbook.d.ts.map +1 -0
  140. package/dist/websocket/state/orderbook.js +293 -0
  141. package/dist/websocket/state/orderbook.js.map +1 -0
  142. package/dist/websocket/state/price.d.ts +108 -0
  143. package/dist/websocket/state/price.d.ts.map +1 -0
  144. package/dist/websocket/state/price.js +243 -0
  145. package/dist/websocket/state/price.js.map +1 -0
  146. package/dist/websocket/state/user.d.ts +83 -0
  147. package/dist/websocket/state/user.d.ts.map +1 -0
  148. package/dist/websocket/state/user.js +228 -0
  149. package/dist/websocket/state/user.js.map +1 -0
  150. package/dist/websocket/subscriptions.d.ts +143 -0
  151. package/dist/websocket/subscriptions.d.ts.map +1 -0
  152. package/dist/websocket/subscriptions.js +244 -0
  153. package/dist/websocket/subscriptions.js.map +1 -0
  154. package/dist/websocket/types.d.ts +417 -0
  155. package/dist/websocket/types.d.ts.map +1 -0
  156. package/dist/websocket/types.js +195 -0
  157. package/dist/websocket/types.js.map +1 -0
  158. package/package.json +75 -0
@@ -0,0 +1,692 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildInitializeIx = buildInitializeIx;
4
+ exports.buildCreateMarketIx = buildCreateMarketIx;
5
+ exports.buildAddDepositMintIx = buildAddDepositMintIx;
6
+ exports.buildMintCompleteSetIx = buildMintCompleteSetIx;
7
+ exports.buildMergeCompleteSetIx = buildMergeCompleteSetIx;
8
+ exports.buildCancelOrderIx = buildCancelOrderIx;
9
+ exports.buildIncrementNonceIx = buildIncrementNonceIx;
10
+ exports.buildSettleMarketIx = buildSettleMarketIx;
11
+ exports.buildRedeemWinningsIx = buildRedeemWinningsIx;
12
+ exports.buildSetPausedIx = buildSetPausedIx;
13
+ exports.buildSetOperatorIx = buildSetOperatorIx;
14
+ exports.buildWithdrawFromPositionIx = buildWithdrawFromPositionIx;
15
+ exports.buildActivateMarketIx = buildActivateMarketIx;
16
+ exports.buildMatchOrdersMultiIx = buildMatchOrdersMultiIx;
17
+ exports.buildSetAuthorityIx = buildSetAuthorityIx;
18
+ exports.buildCreateOrderbookIx = buildCreateOrderbookIx;
19
+ const web3_js_1 = require("@solana/web3.js");
20
+ const constants_1 = require("./constants");
21
+ const pda_1 = require("./pda");
22
+ const utils_1 = require("./utils");
23
+ const orders_1 = require("./orders");
24
+ // ============================================================================
25
+ // HELPER FUNCTIONS
26
+ // ============================================================================
27
+ function signerMut(pubkey) {
28
+ return { pubkey, isSigner: true, isWritable: true };
29
+ }
30
+ function writable(pubkey) {
31
+ return { pubkey, isSigner: false, isWritable: true };
32
+ }
33
+ function readonly(pubkey) {
34
+ return { pubkey, isSigner: false, isWritable: false };
35
+ }
36
+ // ============================================================================
37
+ // INSTRUCTION BUILDERS
38
+ // ============================================================================
39
+ /**
40
+ * Build Initialize instruction
41
+ * Creates the exchange account (singleton)
42
+ *
43
+ * Accounts:
44
+ * 0. authority (signer, mut) - Initial admin
45
+ * 1. exchange (mut) - Exchange PDA
46
+ * 2. system_program (readonly)
47
+ *
48
+ * Data: [discriminator (1 byte)]
49
+ */
50
+ function buildInitializeIx(params, programId = constants_1.PROGRAM_ID) {
51
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
52
+ const keys = [
53
+ signerMut(params.authority),
54
+ writable(exchange),
55
+ readonly(constants_1.SYSTEM_PROGRAM_ID),
56
+ ];
57
+ const data = Buffer.from([constants_1.INSTRUCTION.INITIALIZE]);
58
+ return new web3_js_1.TransactionInstruction({
59
+ keys,
60
+ programId,
61
+ data,
62
+ });
63
+ }
64
+ /**
65
+ * Build CreateMarket instruction
66
+ * Creates a new market in Pending status
67
+ *
68
+ * Accounts:
69
+ * 0. authority (signer, mut) - Must be exchange authority
70
+ * 1. exchange (mut) - Exchange PDA
71
+ * 2. market (mut) - Market PDA
72
+ * 3. system_program (readonly)
73
+ *
74
+ * Data: [discriminator, num_outcomes (u8), oracle (32), question_id (32)]
75
+ */
76
+ function buildCreateMarketIx(params, marketId, programId = constants_1.PROGRAM_ID) {
77
+ (0, utils_1.validateOutcomes)(params.numOutcomes);
78
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
79
+ const [market] = (0, pda_1.getMarketPda)(marketId, programId);
80
+ const keys = [
81
+ signerMut(params.authority),
82
+ writable(exchange),
83
+ writable(market),
84
+ readonly(constants_1.SYSTEM_PROGRAM_ID),
85
+ ];
86
+ const data = Buffer.concat([
87
+ Buffer.from([constants_1.INSTRUCTION.CREATE_MARKET]),
88
+ (0, utils_1.toU8)(params.numOutcomes),
89
+ params.oracle.toBuffer(),
90
+ params.questionId,
91
+ ]);
92
+ return new web3_js_1.TransactionInstruction({
93
+ keys,
94
+ programId,
95
+ data,
96
+ });
97
+ }
98
+ /**
99
+ * Serialize outcome metadata for addDepositMint instruction
100
+ */
101
+ function serializeOutcomeMetadata(metadata) {
102
+ const buffers = [];
103
+ for (const m of metadata) {
104
+ buffers.push((0, utils_1.serializeString)(m.name));
105
+ buffers.push((0, utils_1.serializeString)(m.symbol));
106
+ buffers.push((0, utils_1.serializeString)(m.uri));
107
+ }
108
+ return Buffer.concat(buffers);
109
+ }
110
+ /**
111
+ * Build AddDepositMint instruction
112
+ *
113
+ * Accounts:
114
+ * 0. payer (signer)
115
+ * 1. market
116
+ * 2. deposit_mint
117
+ * 3. vault
118
+ * 4. mint_authority
119
+ * 5. token_program (SPL Token)
120
+ * 6. token_2022_program
121
+ * 7. system_program
122
+ * 8+ conditional_mints[0..num_outcomes]
123
+ *
124
+ * Data: [discriminator, ...serialized_metadata]
125
+ */
126
+ function buildAddDepositMintIx(params, market, numOutcomes, programId = constants_1.PROGRAM_ID) {
127
+ if (params.outcomeMetadata.length !== numOutcomes) {
128
+ throw new Error(`Outcome metadata count (${params.outcomeMetadata.length}) must match numOutcomes (${numOutcomes})`);
129
+ }
130
+ const [vault] = (0, pda_1.getVaultPda)(params.depositMint, market, programId);
131
+ const [mintAuthority] = (0, pda_1.getMintAuthorityPda)(market, programId);
132
+ const conditionalMints = (0, pda_1.getAllConditionalMintPdas)(market, params.depositMint, numOutcomes, programId);
133
+ const keys = [
134
+ signerMut(params.authority),
135
+ writable(market),
136
+ readonly(params.depositMint),
137
+ writable(vault),
138
+ readonly(mintAuthority),
139
+ readonly(constants_1.TOKEN_PROGRAM_ID),
140
+ readonly(constants_1.TOKEN_2022_PROGRAM_ID),
141
+ readonly(constants_1.SYSTEM_PROGRAM_ID),
142
+ ];
143
+ for (const [mint] of conditionalMints) {
144
+ keys.push(writable(mint));
145
+ }
146
+ const data = Buffer.concat([
147
+ Buffer.from([constants_1.INSTRUCTION.ADD_DEPOSIT_MINT]),
148
+ serializeOutcomeMetadata(params.outcomeMetadata),
149
+ ]);
150
+ return new web3_js_1.TransactionInstruction({
151
+ keys,
152
+ programId,
153
+ data,
154
+ });
155
+ }
156
+ /**
157
+ * Build MintCompleteSet instruction
158
+ *
159
+ * Accounts:
160
+ * 0. user (signer)
161
+ * 1. exchange
162
+ * 2. market
163
+ * 3. deposit_mint
164
+ * 4. vault
165
+ * 5. user_deposit_ata
166
+ * 6. position
167
+ * 7. position_collateral_ata
168
+ * 8. mint_authority
169
+ * 9. token_program
170
+ * 10. token_2022_program
171
+ * 11. associated_token_program
172
+ * 12. system_program
173
+ * Remaining: [conditional_mint[i], position_conditional_ata[i]]
174
+ *
175
+ * Data: [discriminator, amount (u64)]
176
+ */
177
+ function buildMintCompleteSetIx(params, numOutcomes, programId = constants_1.PROGRAM_ID) {
178
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
179
+ const [vault] = (0, pda_1.getVaultPda)(params.depositMint, params.market, programId);
180
+ const [mintAuthority] = (0, pda_1.getMintAuthorityPda)(params.market, programId);
181
+ const [position] = (0, pda_1.getPositionPda)(params.user, params.market, programId);
182
+ const userDepositAta = (0, utils_1.getDepositTokenAta)(params.depositMint, params.user);
183
+ const positionCollateralAta = (0, utils_1.getDepositTokenAta)(params.depositMint, position);
184
+ const conditionalMints = (0, pda_1.getAllConditionalMintPdas)(params.market, params.depositMint, numOutcomes, programId);
185
+ const keys = [
186
+ signerMut(params.user),
187
+ readonly(exchange),
188
+ readonly(params.market),
189
+ readonly(params.depositMint),
190
+ writable(vault),
191
+ writable(userDepositAta),
192
+ writable(position),
193
+ writable(positionCollateralAta),
194
+ readonly(mintAuthority),
195
+ readonly(constants_1.TOKEN_PROGRAM_ID),
196
+ readonly(constants_1.TOKEN_2022_PROGRAM_ID),
197
+ readonly(constants_1.ASSOCIATED_TOKEN_PROGRAM_ID),
198
+ readonly(constants_1.SYSTEM_PROGRAM_ID),
199
+ ];
200
+ for (const [mint] of conditionalMints) {
201
+ keys.push(writable(mint));
202
+ const positionAta = (0, utils_1.getConditionalTokenAta)(mint, position);
203
+ keys.push(writable(positionAta));
204
+ }
205
+ const data = Buffer.concat([
206
+ Buffer.from([constants_1.INSTRUCTION.MINT_COMPLETE_SET]),
207
+ (0, utils_1.toU64Le)(params.amount),
208
+ ]);
209
+ return new web3_js_1.TransactionInstruction({
210
+ keys,
211
+ programId,
212
+ data,
213
+ });
214
+ }
215
+ /**
216
+ * Build MergeCompleteSet instruction
217
+ *
218
+ * Accounts:
219
+ * 0. user (signer)
220
+ * 1. exchange
221
+ * 2. market
222
+ * 3. deposit_mint
223
+ * 4. vault
224
+ * 5. position
225
+ * 6. user_deposit_ata
226
+ * 7. mint_authority
227
+ * 8. token_program
228
+ * 9. token_2022_program
229
+ * Remaining: [conditional_mint[i], position_conditional_ata[i]]
230
+ *
231
+ * Data: [discriminator, amount (u64)]
232
+ */
233
+ function buildMergeCompleteSetIx(params, numOutcomes, programId = constants_1.PROGRAM_ID) {
234
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
235
+ const [vault] = (0, pda_1.getVaultPda)(params.depositMint, params.market, programId);
236
+ const [mintAuthority] = (0, pda_1.getMintAuthorityPda)(params.market, programId);
237
+ const [position] = (0, pda_1.getPositionPda)(params.user, params.market, programId);
238
+ const userDepositAta = (0, utils_1.getDepositTokenAta)(params.depositMint, params.user);
239
+ const conditionalMints = (0, pda_1.getAllConditionalMintPdas)(params.market, params.depositMint, numOutcomes, programId);
240
+ const keys = [
241
+ signerMut(params.user),
242
+ readonly(exchange),
243
+ readonly(params.market),
244
+ readonly(params.depositMint),
245
+ writable(vault),
246
+ writable(position),
247
+ writable(userDepositAta),
248
+ readonly(mintAuthority),
249
+ readonly(constants_1.TOKEN_PROGRAM_ID),
250
+ readonly(constants_1.TOKEN_2022_PROGRAM_ID),
251
+ ];
252
+ for (const [mint] of conditionalMints) {
253
+ keys.push(writable(mint));
254
+ const positionAta = (0, utils_1.getConditionalTokenAta)(mint, position);
255
+ keys.push(writable(positionAta));
256
+ }
257
+ const data = Buffer.concat([
258
+ Buffer.from([constants_1.INSTRUCTION.MERGE_COMPLETE_SET]),
259
+ (0, utils_1.toU64Le)(params.amount),
260
+ ]);
261
+ return new web3_js_1.TransactionInstruction({
262
+ keys,
263
+ programId,
264
+ data,
265
+ });
266
+ }
267
+ /**
268
+ * Build CancelOrder instruction
269
+ *
270
+ * Accounts:
271
+ * 0. maker (signer, mut)
272
+ * 1. market (readonly)
273
+ * 2. order_status (mut)
274
+ * 3. system_program (readonly)
275
+ *
276
+ * Data: [discriminator, order_hash (32), signed_order (225)]
277
+ */
278
+ function buildCancelOrderIx(maker, order, programId = constants_1.PROGRAM_ID) {
279
+ const orderHash = (0, orders_1.hashOrder)(order);
280
+ const [orderStatus] = (0, pda_1.getOrderStatusPda)(orderHash, programId);
281
+ const keys = [
282
+ signerMut(maker),
283
+ readonly(order.market),
284
+ writable(orderStatus),
285
+ readonly(constants_1.SYSTEM_PROGRAM_ID),
286
+ ];
287
+ const data = Buffer.concat([
288
+ Buffer.from([constants_1.INSTRUCTION.CANCEL_ORDER]),
289
+ orderHash,
290
+ (0, orders_1.serializeSignedOrder)(order),
291
+ ]);
292
+ return new web3_js_1.TransactionInstruction({
293
+ keys,
294
+ programId,
295
+ data,
296
+ });
297
+ }
298
+ /**
299
+ * Build IncrementNonce instruction
300
+ *
301
+ * Accounts:
302
+ * 0. user (signer, mut)
303
+ * 1. user_nonce (mut)
304
+ * 2. system_program (readonly)
305
+ *
306
+ * Data: [discriminator]
307
+ */
308
+ function buildIncrementNonceIx(user, programId = constants_1.PROGRAM_ID) {
309
+ const [userNonce] = (0, pda_1.getUserNoncePda)(user, programId);
310
+ const keys = [
311
+ signerMut(user),
312
+ writable(userNonce),
313
+ readonly(constants_1.SYSTEM_PROGRAM_ID),
314
+ ];
315
+ const data = Buffer.from([constants_1.INSTRUCTION.INCREMENT_NONCE]);
316
+ return new web3_js_1.TransactionInstruction({
317
+ keys,
318
+ programId,
319
+ data,
320
+ });
321
+ }
322
+ /**
323
+ * Build SettleMarket instruction
324
+ *
325
+ * Accounts:
326
+ * 0. oracle (signer)
327
+ * 1. exchange (readonly)
328
+ * 2. market (mut)
329
+ *
330
+ * Data: [discriminator, winning_outcome (u8)]
331
+ */
332
+ function buildSettleMarketIx(params, programId = constants_1.PROGRAM_ID) {
333
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
334
+ const [market] = (0, pda_1.getMarketPda)(params.marketId, programId);
335
+ const keys = [
336
+ signerMut(params.oracle),
337
+ readonly(exchange),
338
+ writable(market),
339
+ ];
340
+ const data = Buffer.concat([
341
+ Buffer.from([constants_1.INSTRUCTION.SETTLE_MARKET]),
342
+ (0, utils_1.toU8)(params.winningOutcome),
343
+ ]);
344
+ return new web3_js_1.TransactionInstruction({
345
+ keys,
346
+ programId,
347
+ data,
348
+ });
349
+ }
350
+ /**
351
+ * Build RedeemWinnings instruction
352
+ *
353
+ * Accounts:
354
+ * 0. user (signer)
355
+ * 1. market
356
+ * 2. deposit_mint
357
+ * 3. vault
358
+ * 4. winning_conditional_mint
359
+ * 5. position
360
+ * 6. position_conditional_ata
361
+ * 7. user_deposit_ata
362
+ * 8. mint_authority
363
+ * 9. token_program
364
+ * 10. token_2022_program
365
+ *
366
+ * Data: [discriminator, amount (u64)]
367
+ */
368
+ function buildRedeemWinningsIx(params, winningOutcome, programId = constants_1.PROGRAM_ID) {
369
+ const [vault] = (0, pda_1.getVaultPda)(params.depositMint, params.market, programId);
370
+ const [mintAuthority] = (0, pda_1.getMintAuthorityPda)(params.market, programId);
371
+ const [position] = (0, pda_1.getPositionPda)(params.user, params.market, programId);
372
+ const [winningMint] = (0, pda_1.getAllConditionalMintPdas)(params.market, params.depositMint, winningOutcome + 1, programId)[winningOutcome];
373
+ const positionWinningAta = (0, utils_1.getConditionalTokenAta)(winningMint, position);
374
+ const userDepositAta = (0, utils_1.getDepositTokenAta)(params.depositMint, params.user);
375
+ const keys = [
376
+ signerMut(params.user),
377
+ readonly(params.market),
378
+ readonly(params.depositMint),
379
+ writable(vault),
380
+ writable(winningMint),
381
+ writable(position),
382
+ writable(positionWinningAta),
383
+ writable(userDepositAta),
384
+ readonly(mintAuthority),
385
+ readonly(constants_1.TOKEN_PROGRAM_ID),
386
+ readonly(constants_1.TOKEN_2022_PROGRAM_ID),
387
+ ];
388
+ const data = Buffer.concat([
389
+ Buffer.from([constants_1.INSTRUCTION.REDEEM_WINNINGS]),
390
+ (0, utils_1.toU64Le)(params.amount),
391
+ ]);
392
+ return new web3_js_1.TransactionInstruction({
393
+ keys,
394
+ programId,
395
+ data,
396
+ });
397
+ }
398
+ /**
399
+ * Build SetPaused instruction
400
+ *
401
+ * Accounts:
402
+ * 0. authority (signer)
403
+ * 1. exchange (mut)
404
+ *
405
+ * Data: [discriminator, paused (u8)]
406
+ */
407
+ function buildSetPausedIx(authority, paused, programId = constants_1.PROGRAM_ID) {
408
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
409
+ const keys = [signerMut(authority), writable(exchange)];
410
+ const data = Buffer.concat([
411
+ Buffer.from([constants_1.INSTRUCTION.SET_PAUSED]),
412
+ (0, utils_1.toU8)(paused ? 1 : 0),
413
+ ]);
414
+ return new web3_js_1.TransactionInstruction({
415
+ keys,
416
+ programId,
417
+ data,
418
+ });
419
+ }
420
+ /**
421
+ * Build SetOperator instruction
422
+ *
423
+ * Accounts:
424
+ * 0. authority (signer)
425
+ * 1. exchange (mut)
426
+ *
427
+ * Data: [discriminator, new_operator (32)]
428
+ */
429
+ function buildSetOperatorIx(authority, newOperator, programId = constants_1.PROGRAM_ID) {
430
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
431
+ const keys = [signerMut(authority), writable(exchange)];
432
+ const data = Buffer.concat([
433
+ Buffer.from([constants_1.INSTRUCTION.SET_OPERATOR]),
434
+ newOperator.toBuffer(),
435
+ ]);
436
+ return new web3_js_1.TransactionInstruction({
437
+ keys,
438
+ programId,
439
+ data,
440
+ });
441
+ }
442
+ /**
443
+ * Build WithdrawFromPosition instruction
444
+ *
445
+ * Accounts:
446
+ * 0. user (signer, mut)
447
+ * 1. market (readonly)
448
+ * 2. position (mut)
449
+ * 3. mint (readonly)
450
+ * 4. position_ata (mut)
451
+ * 5. user_ata (mut)
452
+ * 6. token_program (readonly)
453
+ *
454
+ * Data: [discriminator, amount (u64), outcome_index (u8)]
455
+ */
456
+ function buildWithdrawFromPositionIx(params, isToken2022, programId = constants_1.PROGRAM_ID) {
457
+ const [position] = (0, pda_1.getPositionPda)(params.user, params.market, programId);
458
+ const positionAta = isToken2022
459
+ ? (0, utils_1.getConditionalTokenAta)(params.mint, position)
460
+ : (0, utils_1.getDepositTokenAta)(params.mint, position);
461
+ const userAta = isToken2022
462
+ ? (0, utils_1.getConditionalTokenAta)(params.mint, params.user)
463
+ : (0, utils_1.getDepositTokenAta)(params.mint, params.user);
464
+ const tokenProgram = isToken2022 ? constants_1.TOKEN_2022_PROGRAM_ID : constants_1.TOKEN_PROGRAM_ID;
465
+ const keys = [
466
+ signerMut(params.user),
467
+ readonly(params.market),
468
+ writable(position),
469
+ readonly(params.mint),
470
+ writable(positionAta),
471
+ writable(userAta),
472
+ readonly(tokenProgram),
473
+ ];
474
+ const data = Buffer.concat([
475
+ Buffer.from([constants_1.INSTRUCTION.WITHDRAW_FROM_POSITION]),
476
+ (0, utils_1.toU64Le)(params.amount),
477
+ (0, utils_1.toU8)(params.outcomeIndex),
478
+ ]);
479
+ return new web3_js_1.TransactionInstruction({
480
+ keys,
481
+ programId,
482
+ data,
483
+ });
484
+ }
485
+ /**
486
+ * Build ActivateMarket instruction
487
+ *
488
+ * Accounts:
489
+ * 0. authority (signer)
490
+ * 1. exchange (readonly)
491
+ * 2. market (mut)
492
+ *
493
+ * Data: [discriminator]
494
+ */
495
+ function buildActivateMarketIx(params, programId = constants_1.PROGRAM_ID) {
496
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
497
+ const [market] = (0, pda_1.getMarketPda)(params.marketId, programId);
498
+ const keys = [
499
+ signerMut(params.authority),
500
+ readonly(exchange),
501
+ writable(market),
502
+ ];
503
+ const data = Buffer.from([constants_1.INSTRUCTION.ACTIVATE_MARKET]);
504
+ return new web3_js_1.TransactionInstruction({
505
+ keys,
506
+ programId,
507
+ data,
508
+ });
509
+ }
510
+ /**
511
+ * Build MatchOrdersMulti instruction
512
+ *
513
+ * Dynamic accounts based on full_fill_bitmask:
514
+ * 0. operator (signer)
515
+ * 1. exchange (readonly)
516
+ * 2. market (readonly)
517
+ * [taker_order_status if bit7=0] (mut)
518
+ * taker_nonce (readonly)
519
+ * taker_position (mut)
520
+ * base_mint (readonly)
521
+ * quote_mint (readonly)
522
+ * taker_base_ata (mut)
523
+ * taker_quote_ata (mut)
524
+ * token_2022_program (readonly)
525
+ * system_program (readonly)
526
+ * Per maker:
527
+ * [maker_order_status if bit_i=0] (mut)
528
+ * maker_nonce (readonly)
529
+ * maker_position (mut)
530
+ * maker_base_ata (mut)
531
+ * maker_quote_ata (mut)
532
+ *
533
+ * Data:
534
+ * [0] discriminator
535
+ * [1..30] taker Order (29 bytes)
536
+ * [30..94] taker_signature (64 bytes)
537
+ * [94] num_makers
538
+ * [95] full_fill_bitmask
539
+ * Per maker (109 bytes):
540
+ * [+0..+29] maker Order (29)
541
+ * [+29..+93] maker_signature (64)
542
+ * [+93..+101] maker_fill_amount (8)
543
+ * [+101..+109] taker_fill_amount (8)
544
+ */
545
+ function buildMatchOrdersMultiIx(params, programId = constants_1.PROGRAM_ID) {
546
+ if (params.makerOrders.length === 0) {
547
+ throw new Error("At least one maker order is required");
548
+ }
549
+ if (params.makerOrders.length > 3) {
550
+ throw new Error("Maximum 3 maker orders allowed");
551
+ }
552
+ if (params.makerOrders.length !== params.makerFillAmounts.length) {
553
+ throw new Error("Maker fill amounts must match maker orders count");
554
+ }
555
+ if (params.makerOrders.length !== params.takerFillAmounts.length) {
556
+ throw new Error("Taker fill amounts must match maker orders count");
557
+ }
558
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
559
+ const takerOrderHash = (0, orders_1.hashOrder)(params.takerOrder);
560
+ const [takerNonce] = (0, pda_1.getUserNoncePda)(params.takerOrder.maker, programId);
561
+ const [takerPosition] = (0, pda_1.getPositionPda)(params.takerOrder.maker, params.market, programId);
562
+ const takerBaseAta = (0, utils_1.getConditionalTokenAta)(params.baseMint, takerPosition);
563
+ const takerQuoteAta = (0, utils_1.getConditionalTokenAta)(params.quoteMint, takerPosition);
564
+ const keys = [
565
+ signerMut(params.operator),
566
+ readonly(exchange),
567
+ readonly(params.market),
568
+ ];
569
+ // Taker order status if not fully filled (bit 7 = 0)
570
+ const takerFullFill = (params.fullFillBitmask & 0x80) !== 0;
571
+ if (!takerFullFill) {
572
+ const [takerOrderStatus] = (0, pda_1.getOrderStatusPda)(takerOrderHash, programId);
573
+ keys.push(writable(takerOrderStatus));
574
+ }
575
+ keys.push(readonly(takerNonce));
576
+ keys.push(writable(takerPosition));
577
+ keys.push(readonly(params.baseMint));
578
+ keys.push(readonly(params.quoteMint));
579
+ keys.push(writable(takerBaseAta));
580
+ keys.push(writable(takerQuoteAta));
581
+ keys.push(readonly(constants_1.TOKEN_2022_PROGRAM_ID));
582
+ keys.push(readonly(constants_1.SYSTEM_PROGRAM_ID));
583
+ // Add maker accounts
584
+ for (let i = 0; i < params.makerOrders.length; i++) {
585
+ const makerOrder = params.makerOrders[i];
586
+ const makerFullFill = (params.fullFillBitmask & (1 << i)) !== 0;
587
+ if (!makerFullFill) {
588
+ const makerOrderHash = (0, orders_1.hashOrder)(makerOrder);
589
+ const [makerOrderStatus] = (0, pda_1.getOrderStatusPda)(makerOrderHash, programId);
590
+ keys.push(writable(makerOrderStatus));
591
+ }
592
+ const [makerNonce] = (0, pda_1.getUserNoncePda)(makerOrder.maker, programId);
593
+ const [makerPosition] = (0, pda_1.getPositionPda)(makerOrder.maker, params.market, programId);
594
+ const makerBaseAta = (0, utils_1.getConditionalTokenAta)(params.baseMint, makerPosition);
595
+ const makerQuoteAta = (0, utils_1.getConditionalTokenAta)(params.quoteMint, makerPosition);
596
+ keys.push(readonly(makerNonce));
597
+ keys.push(writable(makerPosition));
598
+ keys.push(writable(makerBaseAta));
599
+ keys.push(writable(makerQuoteAta));
600
+ }
601
+ // Build data
602
+ const takerCompact = (0, orders_1.signedOrderToOrder)(params.takerOrder);
603
+ const dataBuffers = [
604
+ Buffer.from([constants_1.INSTRUCTION.MATCH_ORDERS_MULTI]),
605
+ (0, orders_1.serializeOrder)(takerCompact),
606
+ params.takerOrder.signature,
607
+ (0, utils_1.toU8)(params.makerOrders.length),
608
+ (0, utils_1.toU8)(params.fullFillBitmask),
609
+ ];
610
+ // Add maker data
611
+ for (let i = 0; i < params.makerOrders.length; i++) {
612
+ const makerOrder = params.makerOrders[i];
613
+ const makerCompact = (0, orders_1.signedOrderToOrder)(makerOrder);
614
+ dataBuffers.push((0, orders_1.serializeOrder)(makerCompact));
615
+ dataBuffers.push(makerOrder.signature);
616
+ dataBuffers.push((0, utils_1.toU64Le)(params.makerFillAmounts[i]));
617
+ dataBuffers.push((0, utils_1.toU64Le)(params.takerFillAmounts[i]));
618
+ }
619
+ const data = Buffer.concat(dataBuffers);
620
+ return new web3_js_1.TransactionInstruction({
621
+ keys,
622
+ programId,
623
+ data,
624
+ });
625
+ }
626
+ /**
627
+ * Build SetAuthority instruction
628
+ *
629
+ * Accounts:
630
+ * 0. authority (signer)
631
+ * 1. exchange (mut)
632
+ *
633
+ * Data: [discriminator, new_authority (32)]
634
+ */
635
+ function buildSetAuthorityIx(params, programId = constants_1.PROGRAM_ID) {
636
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
637
+ const keys = [
638
+ signerMut(params.currentAuthority),
639
+ writable(exchange),
640
+ ];
641
+ const data = Buffer.concat([
642
+ Buffer.from([constants_1.INSTRUCTION.SET_AUTHORITY]),
643
+ params.newAuthority.toBuffer(),
644
+ ]);
645
+ return new web3_js_1.TransactionInstruction({
646
+ keys,
647
+ programId,
648
+ data,
649
+ });
650
+ }
651
+ /**
652
+ * Build CreateOrderbook instruction
653
+ *
654
+ * Accounts:
655
+ * 0. payer (signer, mut)
656
+ * 1. market (readonly)
657
+ * 2. mint_a (readonly)
658
+ * 3. mint_b (readonly)
659
+ * 4. orderbook (mut)
660
+ * 5. lookup_table (mut)
661
+ * 6. exchange (readonly)
662
+ * 7. alt_program (readonly)
663
+ * 8. system_program (readonly)
664
+ *
665
+ * Data: [discriminator, recent_slot (u64)]
666
+ */
667
+ function buildCreateOrderbookIx(params, programId = constants_1.PROGRAM_ID) {
668
+ const [exchange] = (0, pda_1.getExchangePda)(programId);
669
+ const [orderbook] = (0, pda_1.getOrderbookPda)(params.mintA, params.mintB, programId);
670
+ const [alt] = (0, pda_1.getAltPda)(orderbook, params.recentSlot);
671
+ const keys = [
672
+ signerMut(params.payer),
673
+ readonly(params.market),
674
+ readonly(params.mintA),
675
+ readonly(params.mintB),
676
+ writable(orderbook),
677
+ writable(alt),
678
+ readonly(exchange),
679
+ readonly(constants_1.ALT_PROGRAM_ID),
680
+ readonly(constants_1.SYSTEM_PROGRAM_ID),
681
+ ];
682
+ const data = Buffer.concat([
683
+ Buffer.from([constants_1.INSTRUCTION.CREATE_ORDERBOOK]),
684
+ (0, utils_1.toU64Le)(params.recentSlot),
685
+ ]);
686
+ return new web3_js_1.TransactionInstruction({
687
+ keys,
688
+ programId,
689
+ data,
690
+ });
691
+ }
692
+ //# sourceMappingURL=instructions.js.map