@alcorexchange/alcor-swap-sdk 1.0.16 → 1.0.17

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.
@@ -33,7 +33,7 @@ function tradeComparator(a, b) {
33
33
  (0, tiny_invariant_1.default)(a.outputAmount.currency.equals(b.outputAmount.currency), 'OUTPUT_CURRENCY');
34
34
  if (a.outputAmount.equalTo(b.outputAmount)) {
35
35
  if (a.inputAmount.equalTo(b.inputAmount)) {
36
- // consider the number of hops since each hop costs gas
36
+ // consider the number of hops since each hop costs cpu
37
37
  const aHops = a.swaps.reduce((total, cur) => total + cur.route.tokenPath.length, 0);
38
38
  const bHops = b.swaps.reduce((total, cur) => total + cur.route.tokenPath.length, 0);
39
39
  return aHops - bHops;
@@ -467,9 +467,11 @@ class Trade {
467
467
  static bestTradeExactIn2(routes, pools, currencyAmountIn, maxNumResults = 3) {
468
468
  return __awaiter(this, void 0, void 0, function* () {
469
469
  (0, tiny_invariant_1.default)(pools.length > 0, 'POOLS');
470
+ const poolsMap = new Map(pools.map(p => [p.id, p]));
470
471
  const bestTrades = [];
471
472
  for (const route of routes) {
472
- const trade = yield Trade.fromRoute(route, currencyAmountIn, internalConstants_1.TradeType.EXACT_INPUT);
473
+ const freshPools = route.pools.map(p => poolsMap.get(p.id));
474
+ const trade = yield Trade.fromRoute(new route_1.Route([...freshPools], route.input, route.output), currencyAmountIn, internalConstants_1.TradeType.EXACT_INPUT);
473
475
  if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0))
474
476
  continue;
475
477
  (0, utils_1.sortedInsert)(bestTrades, trade, maxNumResults, tradeComparator);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alcorexchange/alcor-swap-sdk",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -26,7 +26,7 @@ export function tradeComparator<TInput extends Currency, TOutput extends Currenc
26
26
  invariant(a.outputAmount.currency.equals(b.outputAmount.currency), 'OUTPUT_CURRENCY')
27
27
  if (a.outputAmount.equalTo(b.outputAmount)) {
28
28
  if (a.inputAmount.equalTo(b.inputAmount)) {
29
- // consider the number of hops since each hop costs gas
29
+ // consider the number of hops since each hop costs cpu
30
30
  const aHops = a.swaps.reduce((total, cur) => total + cur.route.tokenPath.length, 0)
31
31
  const bHops = b.swaps.reduce((total, cur) => total + cur.route.tokenPath.length, 0)
32
32
  return aHops - bHops
@@ -660,12 +660,14 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
660
660
  maxNumResults = 3,
661
661
  ): Promise<Trade<TInput, TOutput, TradeType.EXACT_INPUT>[]> {
662
662
  invariant(pools.length > 0, 'POOLS')
663
+ const poolsMap = new Map(pools.map(p => [p.id, p]))
663
664
 
664
665
  const bestTrades: Trade<TInput, TOutput, TradeType.EXACT_INPUT>[] = []
665
-
666
666
  for (const route of routes) {
667
+ const freshPools = route.pools.map(p => poolsMap.get(p.id)!)
668
+
667
669
  const trade = await Trade.fromRoute(
668
- route,
670
+ new Route([...freshPools], route.input, route.output),
669
671
  currencyAmountIn,
670
672
  TradeType.EXACT_INPUT
671
673
  )