@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,4 +1,4 @@
1
- import { Percent, validateAndParseAddress, Price, Fraction, CurrencyAmount, TradeType, sortedInsert, WETH9 } from '@uniswap/sdk-core';
1
+ import { Percent, validateAndParseAddress, Price, CurrencyAmount, Fraction, TradeType, sortedInsert, WETH9 } from '@uniswap/sdk-core';
2
2
  import JSBI from 'jsbi';
3
3
  import { Interface } from '@ethersproject/abi';
4
4
  import invariant from 'tiny-invariant';
@@ -617,6 +617,21 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
617
617
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
618
618
  }
619
619
 
620
+ function isValidTokenPath(prevPool, currentPool, inputToken) {
621
+ if (currentPool.involvesToken(inputToken)) return true;
622
+ // throw if both v4 pools, native/wrapped tokens not interchangeable in v4
623
+ if (prevPool instanceof Pool && currentPool instanceof Pool) return false;
624
+ // v2/v3 --> v4 valid if v2/v3 output is the wrapped version of the v4 pool native currency
625
+ if (currentPool instanceof Pool) {
626
+ if (currentPool.token0.wrapped.equals(inputToken) || currentPool.token1.wrapped.equals(inputToken)) return true;
627
+ }
628
+ // v4 --> v2/v3 valid if v4 output is the native version of the v2/v3 wrapped token
629
+ if (prevPool instanceof Pool) {
630
+ if (currentPool.involvesToken(inputToken.wrapped)) return true;
631
+ }
632
+ return false;
633
+ }
634
+
620
635
  /**
621
636
  * Represents a list of pools or pairs through which a swap can occur
622
637
  * @template TInput The input token
@@ -653,14 +668,14 @@ var MixedRouteSDK = /*#__PURE__*/function () {
653
668
  * Normalizes token0-token1 order and selects the next token/fee step to add to the path
654
669
  * */
655
670
  var tokenPath = [this.adjustedInput];
656
- for (var _iterator = _createForOfIteratorHelperLoose(pools.entries()), _step; !(_step = _iterator()).done;) {
657
- var _step$value = _step.value,
658
- i = _step$value[0],
659
- pool = _step$value[1];
660
- var currentInputToken = tokenPath[i];
661
- !(currentInputToken.equals(pool.token0) || currentInputToken.equals(pool.token1)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'PATH') : invariant(false) : void 0;
662
- var nextToken = currentInputToken.equals(pool.token0) ? pool.token1 : pool.token0;
663
- tokenPath.push(nextToken);
671
+ pools[0].token0.equals(this.adjustedInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
672
+ for (var i = 1; i < pools.length; i++) {
673
+ var prevPool = pools[i - 1];
674
+ var pool = pools[i];
675
+ var inputToken = tokenPath[i];
676
+ var outputToken = pool.token0.wrapped.equals(inputToken.wrapped) ? pool.token1 : pool.token0;
677
+ !isValidTokenPath(prevPool, pool, inputToken) ? process.env.NODE_ENV !== "production" ? invariant(false, 'PATH') : invariant(false) : void 0;
678
+ tokenPath.push(outputToken);
664
679
  }
665
680
  this.pools = pools;
666
681
  this.path = tokenPath;
@@ -711,32 +726,41 @@ function _getOutputAmount() {
711
726
  while (1) switch (_context.prev = _context.next) {
712
727
  case 0:
713
728
  if (!(pool instanceof Pool)) {
714
- _context.next = 11;
729
+ _context.next = 13;
715
730
  break;
716
731
  }
717
732
  if (!pool.involvesCurrency(amountIn.currency)) {
718
- _context.next = 7;
733
+ _context.next = 5;
719
734
  break;
720
735
  }
721
736
  _context.next = 4;
722
737
  return pool.getOutputAmount(amountIn);
723
738
  case 4:
724
739
  return _context.abrupt("return", _context.sent);
725
- case 7:
726
- if (!pool.involvesCurrency(amountIn.currency.wrapped)) {
727
- _context.next = 11;
740
+ case 5:
741
+ if (!pool.token0.wrapped.equals(amountIn.currency)) {
742
+ _context.next = 9;
728
743
  break;
729
744
  }
730
- _context.next = 10;
731
- return pool.getOutputAmount(amountIn.wrapped);
732
- case 10:
745
+ _context.next = 8;
746
+ return pool.getOutputAmount(CurrencyAmount.fromRawAmount(pool.token0, amountIn.quotient));
747
+ case 8:
748
+ return _context.abrupt("return", _context.sent);
749
+ case 9:
750
+ if (!pool.token1.wrapped.equals(amountIn.currency)) {
751
+ _context.next = 13;
752
+ break;
753
+ }
754
+ _context.next = 12;
755
+ return pool.getOutputAmount(CurrencyAmount.fromRawAmount(pool.token1, amountIn.quotient));
756
+ case 12:
733
757
  return _context.abrupt("return", _context.sent);
734
- case 11:
735
- _context.next = 13;
736
- return pool.getOutputAmount(amountIn.wrapped);
737
758
  case 13:
759
+ _context.next = 15;
760
+ return pool.getOutputAmount(amountIn.wrapped);
761
+ case 15:
738
762
  return _context.abrupt("return", _context.sent);
739
- case 14:
763
+ case 16:
740
764
  case "end":
741
765
  return _context.stop();
742
766
  }