@uniswap/router-sdk 1.11.0 → 1.11.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.
@@ -1,8 +1,5 @@
1
1
  import { Currency, Price } from '@uniswap/sdk-core';
2
- import { Pair } from '@uniswap/v2-sdk';
3
- import { Pool as V3Pool } from '@uniswap/v3-sdk';
4
- import { Pool as V4Pool } from '@uniswap/v4-sdk';
5
- declare type TPool = Pair | V3Pool | V4Pool;
2
+ import { TPool } from '../../utils/TPool';
6
3
  /**
7
4
  * Represents a list of pools or pairs through which a swap can occur
8
5
  * @template TInput The input token
@@ -28,4 +25,3 @@ export declare class MixedRouteSDK<TInput extends Currency, TOutput extends Curr
28
25
  */
29
26
  get midPrice(): Price<TInput, TOutput>;
30
27
  }
31
- export {};
@@ -1,9 +1,7 @@
1
1
  import { Currency, Percent, Price, CurrencyAmount, TradeType } from '@uniswap/sdk-core';
2
- import { Pair } from '@uniswap/v2-sdk';
3
- import { BestTradeOptions, Pool as V3Pool } from '@uniswap/v3-sdk';
4
- import { Pool as V4Pool } from '@uniswap/v4-sdk';
2
+ import { BestTradeOptions } from '@uniswap/v3-sdk';
5
3
  import { MixedRouteSDK } from './route';
6
- declare type TPool = Pair | V3Pool | V4Pool;
4
+ import { TPool } from '../../utils/TPool';
7
5
  /**
8
6
  * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
9
7
  * @template TInput The input token, either Ether or an ERC-20
@@ -183,4 +181,3 @@ export declare class MixedRouteTrade<TInput extends Currency, TOutput extends Cu
183
181
  */
184
182
  static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(pools: TPool[], currencyAmountIn: CurrencyAmount<TInput>, currencyOut: TOutput, { maxNumResults, maxHops }?: BestTradeOptions, currentPools?: TPool[], nextAmountIn?: CurrencyAmount<Currency>, bestTrades?: MixedRouteTrade<TInput, TOutput, TradeType.EXACT_INPUT>[]): Promise<MixedRouteTrade<TInput, TOutput, TradeType.EXACT_INPUT>[]>;
185
183
  }
186
- export {};
@@ -622,6 +622,21 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
622
622
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
623
623
  }
624
624
 
625
+ function isValidTokenPath(prevPool, currentPool, inputToken) {
626
+ if (currentPool.involvesToken(inputToken)) return true;
627
+ // throw if both v4 pools, native/wrapped tokens not interchangeable in v4
628
+ if (prevPool instanceof v4Sdk.Pool && currentPool instanceof v4Sdk.Pool) return false;
629
+ // v2/v3 --> v4 valid if v2/v3 output is the wrapped version of the v4 pool native currency
630
+ if (currentPool instanceof v4Sdk.Pool) {
631
+ if (currentPool.token0.wrapped.equals(inputToken) || currentPool.token1.wrapped.equals(inputToken)) return true;
632
+ }
633
+ // v4 --> v2/v3 valid if v4 output is the native version of the v2/v3 wrapped token
634
+ if (prevPool instanceof v4Sdk.Pool) {
635
+ if (currentPool.involvesToken(inputToken.wrapped)) return true;
636
+ }
637
+ return false;
638
+ }
639
+
625
640
  /**
626
641
  * Represents a list of pools or pairs through which a swap can occur
627
642
  * @template TInput The input token
@@ -658,14 +673,14 @@ var MixedRouteSDK = /*#__PURE__*/function () {
658
673
  * Normalizes token0-token1 order and selects the next token/fee step to add to the path
659
674
  * */
660
675
  var tokenPath = [this.adjustedInput];
661
- for (var _iterator = _createForOfIteratorHelperLoose(pools.entries()), _step; !(_step = _iterator()).done;) {
662
- var _step$value = _step.value,
663
- i = _step$value[0],
664
- pool = _step$value[1];
665
- var currentInputToken = tokenPath[i];
666
- !(currentInputToken.equals(pool.token0) || currentInputToken.equals(pool.token1)) ? invariant(false, 'PATH') : void 0;
667
- var nextToken = currentInputToken.equals(pool.token0) ? pool.token1 : pool.token0;
668
- tokenPath.push(nextToken);
676
+ pools[0].token0.equals(this.adjustedInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
677
+ for (var i = 1; i < pools.length; i++) {
678
+ var prevPool = pools[i - 1];
679
+ var pool = pools[i];
680
+ var inputToken = tokenPath[i];
681
+ var outputToken = pool.token0.wrapped.equals(inputToken.wrapped) ? pool.token1 : pool.token0;
682
+ !isValidTokenPath(prevPool, pool, inputToken) ? invariant(false, 'PATH') : void 0;
683
+ tokenPath.push(outputToken);
669
684
  }
670
685
  this.pools = pools;
671
686
  this.path = tokenPath;
@@ -716,32 +731,41 @@ function _getOutputAmount() {
716
731
  while (1) switch (_context.prev = _context.next) {
717
732
  case 0:
718
733
  if (!(pool instanceof v4Sdk.Pool)) {
719
- _context.next = 11;
734
+ _context.next = 13;
720
735
  break;
721
736
  }
722
737
  if (!pool.involvesCurrency(amountIn.currency)) {
723
- _context.next = 7;
738
+ _context.next = 5;
724
739
  break;
725
740
  }
726
741
  _context.next = 4;
727
742
  return pool.getOutputAmount(amountIn);
728
743
  case 4:
729
744
  return _context.abrupt("return", _context.sent);
730
- case 7:
731
- if (!pool.involvesCurrency(amountIn.currency.wrapped)) {
732
- _context.next = 11;
745
+ case 5:
746
+ if (!pool.token0.wrapped.equals(amountIn.currency)) {
747
+ _context.next = 9;
733
748
  break;
734
749
  }
735
- _context.next = 10;
736
- return pool.getOutputAmount(amountIn.wrapped);
737
- case 10:
750
+ _context.next = 8;
751
+ return pool.getOutputAmount(sdkCore.CurrencyAmount.fromRawAmount(pool.token0, amountIn.quotient));
752
+ case 8:
753
+ return _context.abrupt("return", _context.sent);
754
+ case 9:
755
+ if (!pool.token1.wrapped.equals(amountIn.currency)) {
756
+ _context.next = 13;
757
+ break;
758
+ }
759
+ _context.next = 12;
760
+ return pool.getOutputAmount(sdkCore.CurrencyAmount.fromRawAmount(pool.token1, amountIn.quotient));
761
+ case 12:
738
762
  return _context.abrupt("return", _context.sent);
739
- case 11:
740
- _context.next = 13;
741
- return pool.getOutputAmount(amountIn.wrapped);
742
763
  case 13:
764
+ _context.next = 15;
765
+ return pool.getOutputAmount(amountIn.wrapped);
766
+ case 15:
743
767
  return _context.abrupt("return", _context.sent);
744
- case 14:
768
+ case 16:
745
769
  case "end":
746
770
  return _context.stop();
747
771
  }