@theliem/xmarket-sdk 3.9.0 → 3.11.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.
package/dist/index.mjs CHANGED
@@ -1593,10 +1593,10 @@ ${logs.join("\n")}`);
1593
1593
  connection.getAccountInfo(clobYesAta),
1594
1594
  connection.getAccountInfo(clobNoAta)
1595
1595
  ]);
1596
- const { createAssociatedTokenAccountIdempotentInstruction: createAssociatedTokenAccountIdempotentInstruction2 } = await import('@solana/spl-token');
1596
+ const { createAssociatedTokenAccountIdempotentInstruction: createAssociatedTokenAccountIdempotentInstruction3 } = await import('@solana/spl-token');
1597
1597
  const ixs = [];
1598
1598
  if (!yesInfo) {
1599
- ixs.push(createAssociatedTokenAccountIdempotentInstruction2(
1599
+ ixs.push(createAssociatedTokenAccountIdempotentInstruction3(
1600
1600
  this.walletPubkey,
1601
1601
  clobYesAta,
1602
1602
  clobConfig,
@@ -1605,7 +1605,7 @@ ${logs.join("\n")}`);
1605
1605
  ));
1606
1606
  }
1607
1607
  if (!noInfo) {
1608
- ixs.push(createAssociatedTokenAccountIdempotentInstruction2(
1608
+ ixs.push(createAssociatedTokenAccountIdempotentInstruction3(
1609
1609
  this.walletPubkey,
1610
1610
  clobNoAta,
1611
1611
  clobConfig,
@@ -1889,11 +1889,8 @@ ${logs.join("\n")}`);
1889
1889
  * remaining_accounts per NO maker (5):
1890
1890
  * [order_record, buyer_no_token, buyer_no_collateral, buyer_no_position, no_order_status]
1891
1891
  */
1892
- async matchMintOrders(yesSigned, noMakers, collateralMint, _feeRecipient, operatorWallet, lookupTable) {
1893
- await Promise.all([
1894
- this.registerOrderIfNeeded(yesSigned),
1895
- ...noMakers.map((m) => this.registerOrderIfNeeded(m))
1896
- ]);
1892
+ /** Build the match_mint_orders instruction (no signing, no sending). */
1893
+ async _buildMintIx(yesSigned, noMakers, collateralMint, operator, payer) {
1897
1894
  const condition = yesSigned.order.condition;
1898
1895
  const taker = yesSigned.order.maker;
1899
1896
  const takerNonce = yesSigned.order.nonce;
@@ -1941,9 +1938,9 @@ ${logs.join("\n")}`);
1941
1938
  { pubkey: hookConfig, isSigner: false, isWritable: false },
1942
1939
  { pubkey: hookProgram, isSigner: false, isWritable: false }
1943
1940
  );
1944
- const matchIx = await this.program.methods.matchMintOrders(takerNonce, fillAmount, true).accounts({
1945
- operator: operatorWallet.publicKey,
1946
- payer: this.walletPubkey,
1941
+ return this.program.methods.matchMintOrders(takerNonce, fillAmount, true).accounts({
1942
+ operator,
1943
+ payer,
1947
1944
  clobConfig,
1948
1945
  condition,
1949
1946
  taker,
@@ -1970,6 +1967,19 @@ ${logs.join("\n")}`);
1970
1967
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
1971
1968
  systemProgram: SystemProgram.programId
1972
1969
  }).remainingAccounts(remainingAccounts).instruction();
1970
+ }
1971
+ async matchMintOrders(yesSigned, noMakers, collateralMint, _feeRecipient, operatorWallet, lookupTable) {
1972
+ await Promise.all([
1973
+ this.registerOrderIfNeeded(yesSigned),
1974
+ ...noMakers.map((m) => this.registerOrderIfNeeded(m))
1975
+ ]);
1976
+ const condition = yesSigned.order.condition;
1977
+ const clobConfig = this.configPda();
1978
+ const [yesMint] = PDA.yesMint(condition, this.programIds);
1979
+ const [noMint] = PDA.noMint(condition, this.programIds);
1980
+ const clobYesAta = getAssociatedTokenAddressSync(yesMint, clobConfig, true, TOKEN_2022_PROGRAM_ID);
1981
+ const clobNoAta = getAssociatedTokenAddressSync(noMint, clobConfig, true, TOKEN_2022_PROGRAM_ID);
1982
+ const matchIx = await this._buildMintIx(yesSigned, noMakers, collateralMint, operatorWallet.publicKey, this.walletPubkey);
1973
1983
  await this._ensureClobOutcomeAtas(yesMint, noMint, clobConfig, clobYesAta, clobNoAta);
1974
1984
  const sig = await this.sendMatchTx([matchIx], lookupTable, operatorWallet);
1975
1985
  return { signature: sig };
@@ -1983,11 +1993,8 @@ ${logs.join("\n")}`);
1983
1993
  * [order_record, seller_no_token, seller_no_collateral, seller_no_position, no_order_status]
1984
1994
  * After all makers, optional 5 fee accounts.
1985
1995
  */
1986
- async matchMergeOrders(yesSigned, noMakers, collateralMint, feeRecipient, operatorWallet, lookupTable, opts) {
1987
- await Promise.all([
1988
- this.registerOrderIfNeeded(yesSigned),
1989
- ...noMakers.map((m) => this.registerOrderIfNeeded(m))
1990
- ]);
1996
+ /** Build the match_merge_orders instruction (no signing, no sending). */
1997
+ async _buildMergeIx(yesSigned, noMakers, collateralMint, operator, payer, opts) {
1991
1998
  const condition = yesSigned.order.condition;
1992
1999
  const sellerYes = yesSigned.order.maker;
1993
2000
  const takerNonce = yesSigned.order.nonce;
@@ -2040,7 +2047,7 @@ ${logs.join("\n")}`);
2040
2047
  const feeOverridePda = PDA.marketFeeOverride(condition, this.programIds)[0];
2041
2048
  const feeOverrideExists = await this.provider.connection.getAccountInfo(feeOverridePda);
2042
2049
  if (feeOverrideExists) {
2043
- const oracleVault = opts?.marketOracleVault ?? this.walletPubkey;
2050
+ const oracleVault = opts?.marketOracleVault ?? payer;
2044
2051
  remainingAccounts.push(
2045
2052
  { pubkey: this.programIds.feeManagement, isSigner: false, isWritable: false },
2046
2053
  { pubkey: PDA.feeConfig(this.feeConfigOwner, this.programIds)[0], isSigner: false, isWritable: false },
@@ -2051,9 +2058,9 @@ ${logs.join("\n")}`);
2051
2058
  }
2052
2059
  }
2053
2060
  }
2054
- const matchIx = await this.program.methods.matchMergeOrders(takerNonce, fillAmount, true).accounts({
2055
- operator: operatorWallet.publicKey,
2056
- payer: this.walletPubkey,
2061
+ return this.program.methods.matchMergeOrders(takerNonce, fillAmount, true).accounts({
2062
+ operator,
2063
+ payer,
2057
2064
  clobConfig,
2058
2065
  condition,
2059
2066
  taker: sellerYes,
@@ -2079,6 +2086,19 @@ ${logs.join("\n")}`);
2079
2086
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
2080
2087
  systemProgram: SystemProgram.programId
2081
2088
  }).remainingAccounts(remainingAccounts).instruction();
2089
+ }
2090
+ async matchMergeOrders(yesSigned, noMakers, collateralMint, _feeRecipient, operatorWallet, lookupTable, opts) {
2091
+ await Promise.all([
2092
+ this.registerOrderIfNeeded(yesSigned),
2093
+ ...noMakers.map((m) => this.registerOrderIfNeeded(m))
2094
+ ]);
2095
+ const condition = yesSigned.order.condition;
2096
+ const clobConfig = this.configPda();
2097
+ const [yesMint] = PDA.yesMint(condition, this.programIds);
2098
+ const [noMint] = PDA.noMint(condition, this.programIds);
2099
+ const clobYesAta = getAssociatedTokenAddressSync(yesMint, clobConfig, true, TOKEN_2022_PROGRAM_ID);
2100
+ const clobNoAta = getAssociatedTokenAddressSync(noMint, clobConfig, true, TOKEN_2022_PROGRAM_ID);
2101
+ const matchIx = await this._buildMergeIx(yesSigned, noMakers, collateralMint, operatorWallet.publicKey, this.walletPubkey, opts);
2082
2102
  await this._ensureClobOutcomeAtas(yesMint, noMint, clobConfig, clobYesAta, clobNoAta);
2083
2103
  const sig = await this.sendMatchTx([matchIx], lookupTable, operatorWallet);
2084
2104
  return { signature: sig };
@@ -2103,7 +2123,7 @@ ${logs.join("\n")}`);
2103
2123
  const cfg = await this.fetchConfig();
2104
2124
  if (!cfg) throw new InvalidParamError("CLOB config not found on-chain");
2105
2125
  const feeRecipient = cfg.feeRecipient;
2106
- const operatorWallet = this.provider.wallet;
2126
+ const operatorWallet = opts?.operatorWallet ?? this.provider.wallet;
2107
2127
  const alt = await this.ensureAlt(
2108
2128
  taker.order.condition,
2109
2129
  collateralMint,
@@ -2130,10 +2150,19 @@ ${logs.join("\n")}`);
2130
2150
  const allBuy = t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_BUY);
2131
2151
  const allSell = t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_SELL);
2132
2152
  if (!allBuy && !allSell) throw new InvalidParamError("MINT/MERGE: all orders must be same side");
2133
- if (t.tokenId !== 1) throw new InvalidParamError("MINT/MERGE: taker must be YES (tokenId=1)");
2134
- if (!makers.every((m) => m.order.tokenId === 0)) throw new InvalidParamError("MINT/MERGE: makers must be NO (tokenId=0)");
2135
- if (allBuy) return this.matchMintOrders(taker, makers, collateralMint, feeRecipient, operatorWallet, alt);
2136
- return this.matchMergeOrders(taker, makers, collateralMint, feeRecipient, operatorWallet, alt, opts);
2153
+ const takerIsYes = t.tokenId === 1 && makers.every((m) => m.order.tokenId === 0);
2154
+ const takerIsNo = t.tokenId === 0 && makers.every((m) => m.order.tokenId === 1);
2155
+ if (!takerIsYes && !takerIsNo) throw new InvalidParamError("MINT/MERGE: orders must be complementary YES+NO pair");
2156
+ if (takerIsYes) {
2157
+ if (allBuy) return this.matchMintOrders(taker, makers, collateralMint, feeRecipient, operatorWallet, alt);
2158
+ return this.matchMergeOrders(taker, makers, collateralMint, feeRecipient, operatorWallet, alt, opts);
2159
+ }
2160
+ let lastResult;
2161
+ for (const yesMaker of makers) {
2162
+ if (allBuy) lastResult = await this.matchMintOrders(yesMaker, [taker], collateralMint, feeRecipient, operatorWallet, alt);
2163
+ else lastResult = await this.matchMergeOrders(yesMaker, [taker], collateralMint, feeRecipient, operatorWallet, alt, opts);
2164
+ }
2165
+ return lastResult;
2137
2166
  }
2138
2167
  /**
2139
2168
  * High-level match: caller passes price + quantity + keypair — SDK builds,
@@ -2182,6 +2211,124 @@ ${logs.join("\n")}`);
2182
2211
  const makersSigned = sortedMakers.map(buildSigned);
2183
2212
  return this.matchOrders(takerSigned, makersSigned, opts);
2184
2213
  }
2214
+ /**
2215
+ * Build an unsigned VersionedTransaction for a set of match instructions.
2216
+ * Callers sign and send externally — mirrors createQuestionAdmin pattern.
2217
+ */
2218
+ async _buildUnsignedVtx(instructions, lookupTable, payer) {
2219
+ const { connection } = this.provider;
2220
+ const { blockhash } = await connection.getLatestBlockhash();
2221
+ const cuLimit = ComputeBudgetProgram.setComputeUnitLimit({ units: 14e5 });
2222
+ const heapFrame = ComputeBudgetProgram.requestHeapFrame({ bytes: 262144 });
2223
+ const message = new TransactionMessage({
2224
+ payerKey: payer,
2225
+ recentBlockhash: blockhash,
2226
+ instructions: [cuLimit, heapFrame, ...instructions]
2227
+ }).compileToV0Message(lookupTable ? [lookupTable] : []);
2228
+ return new VersionedTransaction(message);
2229
+ }
2230
+ /**
2231
+ * Build unsigned VersionedTransaction(s) for matching orders.
2232
+ *
2233
+ * Mirrors createQuestionAdmin pattern — SDK registers orders internally but
2234
+ * returns the match tx unsigned. BE signs with both keypairs and sends:
2235
+ *
2236
+ * const [vtx] = await sdk.clob.buildMatchOrdersTx(taker, makers, operatorPk, payerPk);
2237
+ * vtx.sign([feePayerKeypair, operatorKeypair]);
2238
+ * await connection.sendRawTransaction(vtx.serialize());
2239
+ *
2240
+ * Returns an array because NO-taker MINT/MERGE decomposes into N txs (one per YES maker).
2241
+ * Most direct/single-pair cases return a single-element array.
2242
+ *
2243
+ * @param operator - Whitelisted CLOB operator pubkey (must sign)
2244
+ * @param payer - Fee payer pubkey (must sign, pays tx fee + rent)
2245
+ */
2246
+ async buildMatchOrdersTx(taker, makers, operator, payer, opts) {
2247
+ if (makers.length === 0) throw new InvalidParamError("At least 1 maker required");
2248
+ const collateralMint = this.networkConfig.defaultCollateral.mint;
2249
+ const cfg = await this.fetchConfig();
2250
+ if (!cfg) throw new InvalidParamError("CLOB config not found on-chain");
2251
+ const feeRecipient = cfg.feeRecipient;
2252
+ const alt = await this.ensureAlt(taker.order.condition, collateralMint, taker, makers);
2253
+ const t = taker.order;
2254
+ const m0 = makers[0].order;
2255
+ const SIDE_BUY = 0;
2256
+ const SIDE_SELL = 1;
2257
+ if (t.tokenId === m0.tokenId) {
2258
+ let buySignedOrder, sellCandidates;
2259
+ if (t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_SELL)) {
2260
+ buySignedOrder = taker;
2261
+ sellCandidates = makers;
2262
+ } else if (t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_BUY)) {
2263
+ await Promise.all([
2264
+ this.registerOrderIfNeeded(taker),
2265
+ ...makers.map((m) => this.registerOrderIfNeeded(m))
2266
+ ]);
2267
+ const ixs2 = [];
2268
+ for (const buyMaker of makers) {
2269
+ const built = await this.buildMatchComplementaryIxs(
2270
+ buyMaker,
2271
+ [taker],
2272
+ collateralMint,
2273
+ feeRecipient,
2274
+ operator,
2275
+ opts,
2276
+ true,
2277
+ true
2278
+ );
2279
+ ixs2.push(...built);
2280
+ }
2281
+ return [await this._buildUnsignedVtx(ixs2, alt, payer)];
2282
+ } else {
2283
+ throw new InvalidParamError("COMPLEMENTARY requires one BUY and one or more SELLs on same tokenId");
2284
+ }
2285
+ await Promise.all([
2286
+ this.registerOrderIfNeeded(buySignedOrder),
2287
+ ...sellCandidates.map((m) => this.registerOrderIfNeeded(m))
2288
+ ]);
2289
+ const ixs = await this.buildMatchComplementaryIxs(
2290
+ buySignedOrder,
2291
+ sellCandidates,
2292
+ collateralMint,
2293
+ feeRecipient,
2294
+ operator,
2295
+ opts
2296
+ );
2297
+ return [await this._buildUnsignedVtx(ixs, alt, payer)];
2298
+ }
2299
+ const allBuy = t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_BUY);
2300
+ const allSell = t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_SELL);
2301
+ if (!allBuy && !allSell) throw new InvalidParamError("MINT/MERGE: all orders must be same side");
2302
+ const takerIsYes = t.tokenId === 1 && makers.every((m) => m.order.tokenId === 0);
2303
+ const takerIsNo = t.tokenId === 0 && makers.every((m) => m.order.tokenId === 1);
2304
+ if (!takerIsYes && !takerIsNo) throw new InvalidParamError("MINT/MERGE: orders must be complementary YES+NO pair");
2305
+ const condition = t.condition;
2306
+ const clobConfig = this.configPda();
2307
+ const [yesMint] = PDA.yesMint(condition, this.programIds);
2308
+ const [noMint] = PDA.noMint(condition, this.programIds);
2309
+ const clobYesAta = getAssociatedTokenAddressSync(yesMint, clobConfig, true, TOKEN_2022_PROGRAM_ID);
2310
+ const clobNoAta = getAssociatedTokenAddressSync(noMint, clobConfig, true, TOKEN_2022_PROGRAM_ID);
2311
+ if (takerIsYes) {
2312
+ await Promise.all([
2313
+ this.registerOrderIfNeeded(taker),
2314
+ ...makers.map((m) => this.registerOrderIfNeeded(m))
2315
+ ]);
2316
+ await this._ensureClobOutcomeAtas(yesMint, noMint, clobConfig, clobYesAta, clobNoAta);
2317
+ const ix = allBuy ? await this._buildMintIx(taker, makers, collateralMint, operator, payer) : await this._buildMergeIx(taker, makers, collateralMint, operator, payer, opts);
2318
+ return [await this._buildUnsignedVtx([ix], alt, payer)];
2319
+ }
2320
+ const vtxs = [];
2321
+ for (const yesMaker of makers) {
2322
+ await Promise.all([
2323
+ this.registerOrderIfNeeded(yesMaker),
2324
+ this.registerOrderIfNeeded(taker)
2325
+ ]);
2326
+ await this._ensureClobOutcomeAtas(yesMint, noMint, clobConfig, clobYesAta, clobNoAta);
2327
+ const ix = allBuy ? await this._buildMintIx(yesMaker, [taker], collateralMint, operator, payer) : await this._buildMergeIx(yesMaker, [taker], collateralMint, operator, payer, opts);
2328
+ vtxs.push(await this._buildUnsignedVtx([ix], alt, payer));
2329
+ }
2330
+ return vtxs;
2331
+ }
2185
2332
  // ─── Queries ─────────────────────────────────────────────────────────────────
2186
2333
  async fetchConfig() {
2187
2334
  try {
@@ -14981,6 +15128,33 @@ var XMarketSDK = class {
14981
15128
  }
14982
15129
  };
14983
15130
  var MAX_APPROVE_AMOUNT = new BN4("18446744073709551615");
15131
+ function buildCreateUserAtasTx(condition, user, payer, programIds) {
15132
+ const [yesMint] = PDA.yesMint(condition, programIds);
15133
+ const [noMint] = PDA.noMint(condition, programIds);
15134
+ const yesAta = getAssociatedTokenAddressSync(yesMint, user, false, TOKEN_2022_PROGRAM_ID);
15135
+ const noAta = getAssociatedTokenAddressSync(noMint, user, false, TOKEN_2022_PROGRAM_ID);
15136
+ const tx = new Transaction();
15137
+ tx.feePayer = payer;
15138
+ tx.add(
15139
+ createAssociatedTokenAccountIdempotentInstruction(
15140
+ payer,
15141
+ yesAta,
15142
+ user,
15143
+ yesMint,
15144
+ TOKEN_2022_PROGRAM_ID,
15145
+ ASSOCIATED_TOKEN_PROGRAM_ID
15146
+ ),
15147
+ createAssociatedTokenAccountIdempotentInstruction(
15148
+ payer,
15149
+ noAta,
15150
+ user,
15151
+ noMint,
15152
+ TOKEN_2022_PROGRAM_ID,
15153
+ ASSOCIATED_TOKEN_PROGRAM_ID
15154
+ )
15155
+ );
15156
+ return tx;
15157
+ }
14984
15158
  function buildApproveCollateralTx(collateralMint, signer, payer, delegate, amount = MAX_APPROVE_AMOUNT) {
14985
15159
  const ownerAta = getAssociatedTokenAddressSync(collateralMint, signer, false, TOKEN_PROGRAM_ID);
14986
15160
  const approveIx = createApproveInstruction(
@@ -15024,6 +15198,6 @@ function buildApproveAllOutcomeTokensTx(condition, signer, payer, delegate, prog
15024
15198
  return tx;
15025
15199
  }
15026
15200
 
15027
- export { AccountNotFoundError, AdminClient, ClobClient, CtfClient, FEE_DENOMINATOR, FeeManagementClient, HookClient, IX_SYSVAR, InvalidParamError, MAX_APPROVE_AMOUNT, MarketClient, MarketOracleClient, OracleClient, PDA, PresaleClient, QuestionStatus, SEEDS, UnauthorizedError, XMarketError, XMarketSDK, buildApproveAllOutcomeTokensTx, buildApproveCollateralTx, buildBatchedEd25519Instruction, buildOrder, buildOrderFromPrice, deserializeSignedOrder, detectMatchType, generateContentHash, generateQuestionId, getOrderSignBytes, orderAmountsFromPrice, serializeOrderToBytes, serializeSignedOrder, signOrder, signOrderWithKeypair, verifySignedOrder };
15201
+ export { AccountNotFoundError, AdminClient, ClobClient, CtfClient, FEE_DENOMINATOR, FeeManagementClient, HookClient, IX_SYSVAR, InvalidParamError, MAX_APPROVE_AMOUNT, MarketClient, MarketOracleClient, OracleClient, PDA, PresaleClient, QuestionStatus, SEEDS, UnauthorizedError, XMarketError, XMarketSDK, buildApproveAllOutcomeTokensTx, buildApproveCollateralTx, buildBatchedEd25519Instruction, buildCreateUserAtasTx, buildOrder, buildOrderFromPrice, deserializeSignedOrder, detectMatchType, generateContentHash, generateQuestionId, getOrderSignBytes, orderAmountsFromPrice, serializeOrderToBytes, serializeSignedOrder, signOrder, signOrderWithKeypair, verifySignedOrder };
15028
15202
  //# sourceMappingURL=index.mjs.map
15029
15203
  //# sourceMappingURL=index.mjs.map