@alcorexchange/alcor-swap-sdk 1.1.1 → 1.1.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.
@@ -195,7 +195,7 @@ let Pool = exports.Pool = /*#__PURE__*/function () {
195
195
  } = this.swap(zeroForOne, outputAmount.quotient * _internalConstants.NEGATIVE_ONE, sqrtPriceLimitX64);
196
196
  const inputToken = zeroForOne ? this.tokenA : this.tokenB;
197
197
  const inputAmount = zeroForOne ? amountA : amountB;
198
- const amountOutReceived = zeroForOne ? amountB : amountA * _internalConstants.NEGATIVE_ONE;
198
+ const amountOutReceived = zeroForOne ? amountB * _internalConstants.NEGATIVE_ONE : amountA * _internalConstants.NEGATIVE_ONE;
199
199
  if (!(amountOutReceived === outputAmount.quotient)) {
200
200
  throw new _errors.InsufficientReservesError();
201
201
  }
@@ -492,8 +492,38 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
492
492
  key: "bestTradeExactOut",
493
493
  value: function bestTradeExactOut(routes, currencyAmountOut, maxNumResults = 1) {
494
494
  (0, _tinyInvariant.default)(routes.length > 0, 'ROUTES');
495
+
496
+ // Pre-filter: remove routes with zero-liquidity pools
497
+ const validRoutes = routes.filter(route => route.pools.every(pool => pool.active && pool.liquidity > _internalConstants.ZERO));
498
+
499
+ // Helper: compute min liquidity (no overflow)
500
+ const getMinLiquidity = route => {
501
+ let min = route.pools[0].liquidity;
502
+ for (let i = 1; i < route.pools.length; i++) {
503
+ if (route.pools[i].liquidity < min) {
504
+ min = route.pools[i].liquidity;
505
+ }
506
+ }
507
+ return min;
508
+ };
509
+
510
+ // Precompute min liquidity for sorting
511
+ const routeMinLiq = new Map();
512
+ for (const route of validRoutes) {
513
+ routeMinLiq.set(route, getMinLiquidity(route));
514
+ }
515
+
516
+ // Sort routes: fewer hops first, then by min liquidity desc
517
+ validRoutes.sort((a, b) => {
518
+ if (a.pools.length !== b.pools.length) return a.pools.length - b.pools.length;
519
+ const minLiqA = routeMinLiq.get(a);
520
+ const minLiqB = routeMinLiq.get(b);
521
+ if (minLiqA > minLiqB) return -1;
522
+ if (minLiqA < minLiqB) return 1;
523
+ return 0;
524
+ });
495
525
  const bestTrades = [];
496
- for (const route of routes) {
526
+ for (const route of validRoutes) {
497
527
  let trade;
498
528
  try {
499
529
  trade = Trade.fromRoute(route, currencyAmountOut, _internalConstants.TradeType.EXACT_OUTPUT);
@@ -504,7 +534,7 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
504
534
  }
505
535
  throw error;
506
536
  }
507
- if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0)) continue;
537
+ if (!trade.inputAmount.greaterThan(0)) continue;
508
538
  (0, _sortedInsert.sortedInsert)(bestTrades, trade, maxNumResults, tradeComparator);
509
539
  }
510
540
  return bestTrades;
@@ -189,7 +189,7 @@ export let Pool = /*#__PURE__*/function () {
189
189
  } = this.swap(zeroForOne, outputAmount.quotient * NEGATIVE_ONE, sqrtPriceLimitX64);
190
190
  const inputToken = zeroForOne ? this.tokenA : this.tokenB;
191
191
  const inputAmount = zeroForOne ? amountA : amountB;
192
- const amountOutReceived = zeroForOne ? amountB : amountA * NEGATIVE_ONE;
192
+ const amountOutReceived = zeroForOne ? amountB * NEGATIVE_ONE : amountA * NEGATIVE_ONE;
193
193
  if (!(amountOutReceived === outputAmount.quotient)) {
194
194
  throw new InsufficientReservesError();
195
195
  }
@@ -483,8 +483,38 @@ export let Trade = /*#__PURE__*/function () {
483
483
  key: "bestTradeExactOut",
484
484
  value: function bestTradeExactOut(routes, currencyAmountOut, maxNumResults = 1) {
485
485
  invariant(routes.length > 0, 'ROUTES');
486
+
487
+ // Pre-filter: remove routes with zero-liquidity pools
488
+ const validRoutes = routes.filter(route => route.pools.every(pool => pool.active && pool.liquidity > ZERO));
489
+
490
+ // Helper: compute min liquidity (no overflow)
491
+ const getMinLiquidity = route => {
492
+ let min = route.pools[0].liquidity;
493
+ for (let i = 1; i < route.pools.length; i++) {
494
+ if (route.pools[i].liquidity < min) {
495
+ min = route.pools[i].liquidity;
496
+ }
497
+ }
498
+ return min;
499
+ };
500
+
501
+ // Precompute min liquidity for sorting
502
+ const routeMinLiq = new Map();
503
+ for (const route of validRoutes) {
504
+ routeMinLiq.set(route, getMinLiquidity(route));
505
+ }
506
+
507
+ // Sort routes: fewer hops first, then by min liquidity desc
508
+ validRoutes.sort((a, b) => {
509
+ if (a.pools.length !== b.pools.length) return a.pools.length - b.pools.length;
510
+ const minLiqA = routeMinLiq.get(a);
511
+ const minLiqB = routeMinLiq.get(b);
512
+ if (minLiqA > minLiqB) return -1;
513
+ if (minLiqA < minLiqB) return 1;
514
+ return 0;
515
+ });
486
516
  const bestTrades = [];
487
- for (const route of routes) {
517
+ for (const route of validRoutes) {
488
518
  let trade;
489
519
  try {
490
520
  trade = Trade.fromRoute(route, currencyAmountOut, TradeType.EXACT_OUTPUT);
@@ -495,7 +525,7 @@ export let Trade = /*#__PURE__*/function () {
495
525
  }
496
526
  throw error;
497
527
  }
498
- if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0)) continue;
528
+ if (!trade.inputAmount.greaterThan(0)) continue;
499
529
  sortedInsert(bestTrades, trade, maxNumResults, tradeComparator);
500
530
  }
501
531
  return bestTrades;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alcorexchange/alcor-swap-sdk",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "module": "build/esm/index.js",