@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.mjs CHANGED
@@ -1865,13 +1865,45 @@ ${logs.join("\n")}`);
1865
1865
  const SIDE_BUY = 0;
1866
1866
  const SIDE_SELL = 1;
1867
1867
  if (t.tokenId === m0.tokenId) {
1868
+ let buySignedOrder, sellCandidates;
1868
1869
  if (t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_SELL)) {
1869
- return this.matchComplementary(taker, makers, collateralMint, feeRecipient, operatorWallet, alt, opts);
1870
+ buySignedOrder = taker;
1871
+ sellCandidates = makers;
1872
+ } else if (t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_BUY)) {
1873
+ buySignedOrder = makers[0];
1874
+ sellCandidates = [taker, ...makers.slice(1)];
1875
+ } else {
1876
+ throw new InvalidParamError("COMPLEMENTARY requires one BUY and one or more SELLs on same tokenId");
1870
1877
  }
1871
- if (t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_BUY)) {
1872
- return this.matchComplementary(makers[0], [taker, ...makers.slice(1)], collateralMint, feeRecipient, operatorWallet, alt, opts);
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`);
1873
1905
  }
1874
- throw new InvalidParamError("COMPLEMENTARY requires one BUY and one SELL on same tokenId");
1906
+ return this.matchComplementary(buySignedOrder, finalSells, collateralMint, feeRecipient, operatorWallet, alt, opts);
1875
1907
  }
1876
1908
  const allBuy = t.side === SIDE_BUY && makers.every((m) => m.order.side === SIDE_BUY);
1877
1909
  const allSell = t.side === SIDE_SELL && makers.every((m) => m.order.side === SIDE_SELL);