@theliem/xmarket-sdk 3.4.1 → 3.4.2

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
@@ -1875,20 +1875,35 @@ ${logs.join("\n")}`);
1875
1875
  } else {
1876
1876
  throw new InvalidParamError("COMPLEMENTARY requires one BUY and one or more SELLs on same tokenId");
1877
1877
  }
1878
- const crossingSells = sellCandidates.filter((s) => {
1879
- const buy = buySignedOrder.order;
1880
- const sell = s.order;
1881
- const lhs = BigInt(buy.makerAmount.toString()) * BigInt(sell.makerAmount.toString());
1882
- const rhs = BigInt(buy.takerAmount.toString()) * BigInt(sell.takerAmount.toString());
1883
- return lhs >= rhs;
1884
- });
1885
- if (crossingSells.length === 0) {
1886
- throw new InvalidParamError("COMPLEMENTARY: no maker orders cross with the buy order");
1887
- }
1888
- if (crossingSells.length < sellCandidates.length) {
1889
- console.warn(`[matchOrders] filtered ${sellCandidates.length - crossingSells.length} non-crossing maker(s)`);
1878
+ const budget = BigInt(buySignedOrder.order.makerAmount.toString());
1879
+ const totalAsk = sellCandidates.reduce(
1880
+ (s, m) => s + BigInt(m.order.takerAmount.toString()),
1881
+ BigInt(0)
1882
+ );
1883
+ let finalSells;
1884
+ if (budget >= totalAsk) {
1885
+ finalSells = sellCandidates;
1886
+ } else {
1887
+ const sorted = [...sellCandidates].sort((a, b) => {
1888
+ const pa = BigInt(a.order.takerAmount.toString()) * BigInt(b.order.makerAmount.toString());
1889
+ const pb = BigInt(b.order.takerAmount.toString()) * BigInt(a.order.makerAmount.toString());
1890
+ return pa < pb ? -1 : pa > pb ? 1 : 0;
1891
+ });
1892
+ finalSells = [];
1893
+ let remaining = budget;
1894
+ for (const s of sorted) {
1895
+ const cost = BigInt(s.order.takerAmount.toString());
1896
+ if (remaining >= cost) {
1897
+ finalSells.push(s);
1898
+ remaining -= cost;
1899
+ }
1900
+ }
1901
+ if (finalSells.length === 0) {
1902
+ throw new InvalidParamError("COMPLEMENTARY: taker budget insufficient for any maker");
1903
+ }
1904
+ console.warn(`[matchOrders] budget ${budget} < totalAsk ${totalAsk}: matched ${finalSells.length}/${sellCandidates.length} makers`);
1890
1905
  }
1891
- return this.matchComplementary(buySignedOrder, crossingSells, collateralMint, feeRecipient, operatorWallet, alt, opts);
1906
+ return this.matchComplementary(buySignedOrder, finalSells, collateralMint, feeRecipient, operatorWallet, alt, opts);
1892
1907
  }
1893
1908
  const allBuy = t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_BUY);
1894
1909
  const allSell = t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_SELL);