@theliem/xmarket-sdk 3.4.0 → 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.js CHANGED
@@ -1891,13 +1891,45 @@ ${logs.join("\n")}`);
1891
1891
  const SIDE_BUY = 0;
1892
1892
  const SIDE_SELL = 1;
1893
1893
  if (t.tokenId === m0.tokenId) {
1894
+ let buySignedOrder, sellCandidates;
1894
1895
  if (t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_SELL)) {
1895
- return this.matchComplementary(taker, makers, collateralMint, feeRecipient, operatorWallet, alt, opts);
1896
+ buySignedOrder = taker;
1897
+ sellCandidates = makers;
1898
+ } else if (t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_BUY)) {
1899
+ buySignedOrder = makers[0];
1900
+ sellCandidates = [taker, ...makers.slice(1)];
1901
+ } else {
1902
+ throw new InvalidParamError("COMPLEMENTARY requires one BUY and one or more SELLs on same tokenId");
1896
1903
  }
1897
- if (t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_BUY)) {
1898
- return this.matchComplementary(makers[0], [taker, ...makers.slice(1)], collateralMint, feeRecipient, operatorWallet, alt, opts);
1904
+ const budget = BigInt(buySignedOrder.order.makerAmount.toString());
1905
+ const totalAsk = sellCandidates.reduce(
1906
+ (s, m) => s + BigInt(m.order.takerAmount.toString()),
1907
+ BigInt(0)
1908
+ );
1909
+ let finalSells;
1910
+ if (budget >= totalAsk) {
1911
+ finalSells = sellCandidates;
1912
+ } else {
1913
+ const sorted = [...sellCandidates].sort((a, b) => {
1914
+ const pa = BigInt(a.order.takerAmount.toString()) * BigInt(b.order.makerAmount.toString());
1915
+ const pb = BigInt(b.order.takerAmount.toString()) * BigInt(a.order.makerAmount.toString());
1916
+ return pa < pb ? -1 : pa > pb ? 1 : 0;
1917
+ });
1918
+ finalSells = [];
1919
+ let remaining = budget;
1920
+ for (const s of sorted) {
1921
+ const cost = BigInt(s.order.takerAmount.toString());
1922
+ if (remaining >= cost) {
1923
+ finalSells.push(s);
1924
+ remaining -= cost;
1925
+ }
1926
+ }
1927
+ if (finalSells.length === 0) {
1928
+ throw new InvalidParamError("COMPLEMENTARY: taker budget insufficient for any maker");
1929
+ }
1930
+ console.warn(`[matchOrders] budget ${budget} < totalAsk ${totalAsk}: matched ${finalSells.length}/${sellCandidates.length} makers`);
1899
1931
  }
1900
- throw new InvalidParamError("COMPLEMENTARY requires one BUY and one SELL on same tokenId");
1932
+ return this.matchComplementary(buySignedOrder, finalSells, collateralMint, feeRecipient, operatorWallet, alt, opts);
1901
1933
  }
1902
1934
  const allBuy = t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_BUY);
1903
1935
  const allSell = t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_SELL);