@theliem/xmarket-sdk 3.6.1 → 3.7.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
@@ -1843,19 +1843,14 @@ ${logs.join("\n")}`);
1843
1843
  }
1844
1844
  /**
1845
1845
  * 1 SELL taker vs N BUY makers.
1846
- * Rust program only supports BUY-as-taker, so we decompose into N instructions
1847
- * (BUY_i as taker, SELL as single maker) combined into one atomic transaction.
1848
- * Engine must use limitPrice for BUY.makerAmount to pass per-pair crossing check.
1846
+ * Program now supports SELL-as-taker natively 1 instruction with all BUY makers
1847
+ * in remaining_accounts (same structure as BUY taker case).
1849
1848
  */
1850
1849
  async matchComplementarySellVsMultiBuy(sellTaker, buyMakers, collateralMint, feeRecipient, operatorWallet, lookupTable, opts) {
1851
- await Promise.all([
1852
- this.registerOrderIfNeeded(sellTaker),
1853
- ...buyMakers.map((m) => this.registerOrderIfNeeded(m))
1854
- ]);
1855
- const sell = sellTaker.order;
1856
1850
  if (buyMakers.length === 0) {
1857
1851
  throw new InvalidParamError("COMPLEMENTARY: no BUY maker orders provided");
1858
1852
  }
1853
+ const sell = sellTaker.order;
1859
1854
  const totalBuyPayment = buyMakers.reduce(
1860
1855
  (acc, b) => acc + BigInt(b.order.makerAmount.toString()),
1861
1856
  BigInt(0)
@@ -1866,24 +1861,20 @@ ${logs.join("\n")}`);
1866
1861
  `COMPLEMENTARY: total buyer payments ${totalBuyPayment} < seller expected ${sellExpected}`
1867
1862
  );
1868
1863
  }
1869
- const crossingBuys = buyMakers;
1870
- const allIxs = [];
1871
- for (const buyMaker of crossingBuys) {
1872
- const ixs = await this.buildMatchComplementaryIxs(
1873
- buyMaker,
1874
- // BUY as taker
1875
- [sellTaker],
1876
- // SELL as maker
1877
- collateralMint,
1878
- feeRecipient,
1879
- operatorWallet.publicKey,
1880
- opts,
1881
- true
1882
- // useTakerPrice: SELL gets filled at BUY's price (maker's price per doc)
1883
- );
1884
- allIxs.push(...ixs);
1885
- }
1886
- const sig = await this.sendMatchTx(allIxs, lookupTable, operatorWallet);
1864
+ await Promise.all([
1865
+ this.registerOrderIfNeeded(sellTaker),
1866
+ ...buyMakers.map((m) => this.registerOrderIfNeeded(m))
1867
+ ]);
1868
+ const ixs = await this.buildMatchComplementaryIxs(
1869
+ sellTaker,
1870
+ buyMakers,
1871
+ collateralMint,
1872
+ feeRecipient,
1873
+ operatorWallet.publicKey,
1874
+ opts,
1875
+ false
1876
+ );
1877
+ const sig = await this.sendMatchTx(ixs, lookupTable, operatorWallet);
1887
1878
  return { signature: sig };
1888
1879
  }
1889
1880
  /**
@@ -2126,18 +2117,17 @@ ${logs.join("\n")}`);
2126
2117
  throw new InvalidParamError("COMPLEMENTARY requires one BUY and one or more SELLs on same tokenId");
2127
2118
  }
2128
2119
  const buy = buySignedOrder.order;
2129
- const finalSells = sellCandidates.filter((s) => {
2130
- const sell = s.order;
2131
- const lhs = BigInt(buy.makerAmount.toString()) * BigInt(sell.makerAmount.toString());
2132
- const rhs = BigInt(buy.takerAmount.toString()) * BigInt(sell.takerAmount.toString());
2133
- return lhs >= rhs;
2134
- });
2135
- if (finalSells.length === 0) {
2136
- throw new InvalidParamError("COMPLEMENTARY: no maker orders cross with the buy order");
2137
- }
2138
- if (finalSells.length < sellCandidates.length) {
2139
- console.warn(`[matchOrders] filtered ${sellCandidates.length - finalSells.length} non-crossing maker(s)`);
2120
+ const totalSellDemand = sellCandidates.reduce(
2121
+ (acc, s) => acc + BigInt(s.order.takerAmount.toString()),
2122
+ BigInt(0)
2123
+ );
2124
+ const buyBudget = BigInt(buy.makerAmount.toString());
2125
+ if (buyBudget < totalSellDemand) {
2126
+ throw new InvalidParamError(
2127
+ `COMPLEMENTARY: total sell demand ${totalSellDemand} > buy budget ${buyBudget}. Build taker order with limitPrice, not averageMatchedPrice.`
2128
+ );
2140
2129
  }
2130
+ const finalSells = sellCandidates;
2141
2131
  return this.matchComplementary(buySignedOrder, finalSells, collateralMint, feeRecipient, operatorWallet, alt, opts);
2142
2132
  }
2143
2133
  const allBuy = t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_BUY);