@uniswap/router-sdk 1.14.0 → 1.14.1

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.
@@ -10,7 +10,8 @@ export declare class MixedRouteSDK<TInput extends Currency, TOutput extends Curr
10
10
  readonly path: Currency[];
11
11
  readonly input: TInput;
12
12
  readonly output: TOutput;
13
- readonly adjustedInput: Currency;
13
+ readonly pathInput: Currency;
14
+ readonly pathOutput: Currency;
14
15
  private _midPrice;
15
16
  /**
16
17
  * Creates an instance of route.
@@ -643,6 +643,30 @@ function isValidTokenPath(prevPool, currentPool, inputToken) {
643
643
  return false;
644
644
  }
645
645
 
646
+ function amountWithPathCurrency(amount, pool) {
647
+ return sdkCore.CurrencyAmount.fromFractionalAmount(getPathCurrency(amount.currency, pool), amount.numerator, amount.denominator);
648
+ }
649
+ function getPathCurrency(currency, pool) {
650
+ // return currency if the currency matches a currency of the pool
651
+ if (pool.involvesToken(currency)) {
652
+ return currency;
653
+ // return if currency.wrapped if pool involves wrapped currency
654
+ } else if (pool.involvesToken(currency.wrapped)) {
655
+ return currency.wrapped;
656
+ // return native currency if pool involves native version of wrapped currency (only applies to V4)
657
+ } else if (pool instanceof v4Sdk.Pool) {
658
+ if (pool.currency0.wrapped.equals(currency)) {
659
+ return pool.token0;
660
+ } else if (pool.token1.wrapped.equals(currency)) {
661
+ return pool.token1;
662
+ }
663
+ // otherwise the token is invalid
664
+ } else {
665
+ throw new Error("Expected currency " + currency.symbol + " to be either " + pool.token0.symbol + " or " + pool.token1.symbol);
666
+ }
667
+ return currency; // this line needed for typescript to compile
668
+ }
669
+
646
670
  /**
647
671
  * Represents a list of pools or pairs through which a swap can occur
648
672
  * @template TInput The input token
@@ -663,12 +687,9 @@ var MixedRouteSDK = /*#__PURE__*/function () {
663
687
  return pool.chainId === chainId;
664
688
  });
665
689
  !allOnSameChain ? invariant(false, 'CHAIN_IDS') : void 0;
666
- if (pools[0] instanceof v4Sdk.Pool) {
667
- this.adjustedInput = pools[0].involvesToken(input) ? input : input.wrapped;
668
- } else {
669
- this.adjustedInput = input.wrapped; // no native currencies in v2/v3
670
- }
671
- !pools[0].involvesToken(this.adjustedInput) ? invariant(false, 'INPUT') : void 0;
690
+ this.pathInput = getPathCurrency(input, pools[0]);
691
+ this.pathOutput = getPathCurrency(output, pools[pools.length - 1]);
692
+ !pools[0].involvesToken(this.pathInput) ? invariant(false, 'INPUT') : void 0;
672
693
  var lastPool = pools[pools.length - 1];
673
694
  if (lastPool instanceof v4Sdk.Pool) {
674
695
  !(lastPool.involvesToken(output) || lastPool.involvesToken(output.wrapped)) ? invariant(false, 'OUTPUT') : void 0;
@@ -678,8 +699,8 @@ var MixedRouteSDK = /*#__PURE__*/function () {
678
699
  /**
679
700
  * Normalizes token0-token1 order and selects the next token/fee step to add to the path
680
701
  * */
681
- var tokenPath = [this.adjustedInput];
682
- pools[0].token0.equals(this.adjustedInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
702
+ var tokenPath = [this.pathInput];
703
+ pools[0].token0.equals(this.pathInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
683
704
  for (var i = 1; i < pools.length; i++) {
684
705
  var prevPool = pools[i - 1];
685
706
  var pool = pools[i];
@@ -715,7 +736,7 @@ var MixedRouteSDK = /*#__PURE__*/function () {
715
736
  nextInput: pool.token0,
716
737
  price: price.multiply(pool.token1Price.asFraction)
717
738
  };
718
- }, this.pools[0].token0.equals(this.adjustedInput) ? {
739
+ }, this.pools[0].token0.equals(this.pathInput) ? {
719
740
  nextInput: this.pools[0].token1,
720
741
  price: this.pools[0].token0Price.asFraction
721
742
  } : {
@@ -728,58 +749,6 @@ var MixedRouteSDK = /*#__PURE__*/function () {
728
749
  return MixedRouteSDK;
729
750
  }();
730
751
 
731
- function getOutputAmount(_x, _x2) {
732
- return _getOutputAmount.apply(this, arguments);
733
- }
734
- function _getOutputAmount() {
735
- _getOutputAmount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pool, amountIn) {
736
- return _regeneratorRuntime().wrap(function _callee$(_context) {
737
- while (1) switch (_context.prev = _context.next) {
738
- case 0:
739
- if (!(pool instanceof v4Sdk.Pool)) {
740
- _context.next = 13;
741
- break;
742
- }
743
- if (!pool.involvesCurrency(amountIn.currency)) {
744
- _context.next = 5;
745
- break;
746
- }
747
- _context.next = 4;
748
- return pool.getOutputAmount(amountIn);
749
- case 4:
750
- return _context.abrupt("return", _context.sent);
751
- case 5:
752
- if (!pool.token0.wrapped.equals(amountIn.currency)) {
753
- _context.next = 9;
754
- break;
755
- }
756
- _context.next = 8;
757
- return pool.getOutputAmount(sdkCore.CurrencyAmount.fromRawAmount(pool.token0, amountIn.quotient));
758
- case 8:
759
- return _context.abrupt("return", _context.sent);
760
- case 9:
761
- if (!pool.token1.wrapped.equals(amountIn.currency)) {
762
- _context.next = 13;
763
- break;
764
- }
765
- _context.next = 12;
766
- return pool.getOutputAmount(sdkCore.CurrencyAmount.fromRawAmount(pool.token1, amountIn.quotient));
767
- case 12:
768
- return _context.abrupt("return", _context.sent);
769
- case 13:
770
- _context.next = 15;
771
- return pool.getOutputAmount(amountIn.wrapped);
772
- case 15:
773
- return _context.abrupt("return", _context.sent);
774
- case 16:
775
- case "end":
776
- return _context.stop();
777
- }
778
- }, _callee);
779
- }));
780
- return _getOutputAmount.apply(this, arguments);
781
- }
782
-
783
752
  /**
784
753
  * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
785
754
  * @template TInput The input token, either Ether or an ERC-20
@@ -900,14 +869,14 @@ var MixedRouteTrade = /*#__PURE__*/function () {
900
869
  /*#__PURE__*/
901
870
  function () {
902
871
  var _fromRoute = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(route, amount, tradeType) {
903
- var amounts, inputAmount, outputAmount, i, pool, _yield$getOutputAmoun, _outputAmount;
872
+ var amounts, inputAmount, outputAmount, i, pool, _yield$pool$getOutput, _outputAmount;
904
873
  return _regeneratorRuntime().wrap(function _callee$(_context) {
905
874
  while (1) switch (_context.prev = _context.next) {
906
875
  case 0:
907
876
  amounts = new Array(route.path.length);
908
877
  !(tradeType === sdkCore.TradeType.EXACT_INPUT) ? invariant(false, 'TRADE_TYPE') : void 0;
909
878
  !amount.currency.equals(route.input) ? invariant(false, 'INPUT') : void 0;
910
- amounts[0] = route.pools[0] instanceof v4Sdk.Pool ? amount : amount.wrapped;
879
+ amounts[0] = amountWithPathCurrency(amount, route.pools[0]);
911
880
  i = 0;
912
881
  case 5:
913
882
  if (!(i < route.path.length - 1)) {
@@ -916,10 +885,10 @@ var MixedRouteTrade = /*#__PURE__*/function () {
916
885
  }
917
886
  pool = route.pools[i];
918
887
  _context.next = 9;
919
- return getOutputAmount(pool, amounts[i]);
888
+ return pool.getOutputAmount(amountWithPathCurrency(amounts[i], pool));
920
889
  case 9:
921
- _yield$getOutputAmoun = _context.sent;
922
- _outputAmount = _yield$getOutputAmoun[0];
890
+ _yield$pool$getOutput = _context.sent;
891
+ _outputAmount = _yield$pool$getOutput[0];
923
892
  amounts[i + 1] = _outputAmount;
924
893
  case 12:
925
894
  i++;
@@ -962,7 +931,7 @@ var MixedRouteTrade = /*#__PURE__*/function () {
962
931
  /*#__PURE__*/
963
932
  function () {
964
933
  var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(routes, tradeType) {
965
- var populatedRoutes, _iterator3, _step3, _step3$value, route, amount, amounts, inputAmount, outputAmount, i, pool, _yield$getOutputAmoun2, _outputAmount2;
934
+ var populatedRoutes, _iterator3, _step3, _step3$value, route, amount, amounts, inputAmount, outputAmount, i, pool, _yield$pool$getOutput2, _outputAmount2;
966
935
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
967
936
  while (1) switch (_context2.prev = _context2.next) {
968
937
  case 0:
@@ -980,7 +949,7 @@ var MixedRouteTrade = /*#__PURE__*/function () {
980
949
  outputAmount = void 0;
981
950
  !amount.currency.equals(route.input) ? invariant(false, 'INPUT') : void 0;
982
951
  inputAmount = sdkCore.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
983
- amounts[0] = sdkCore.CurrencyAmount.fromFractionalAmount(route.adjustedInput, amount.numerator, amount.denominator);
952
+ amounts[0] = sdkCore.CurrencyAmount.fromFractionalAmount(route.pathInput, amount.numerator, amount.denominator);
984
953
  i = 0;
985
954
  case 12:
986
955
  if (!(i < route.path.length - 1)) {
@@ -989,10 +958,10 @@ var MixedRouteTrade = /*#__PURE__*/function () {
989
958
  }
990
959
  pool = route.pools[i];
991
960
  _context2.next = 16;
992
- return getOutputAmount(pool, amounts[i]);
961
+ return pool.getOutputAmount(amountWithPathCurrency(amounts[i], pool));
993
962
  case 16:
994
- _yield$getOutputAmoun2 = _context2.sent;
995
- _outputAmount2 = _yield$getOutputAmoun2[0];
963
+ _yield$pool$getOutput2 = _context2.sent;
964
+ _outputAmount2 = _yield$pool$getOutput2[0];
996
965
  amounts[i + 1] = _outputAmount2;
997
966
  case 19:
998
967
  i++;
@@ -1829,9 +1798,9 @@ function encodeMixedRouteToPath(route) {
1829
1798
  var path;
1830
1799
  var types;
1831
1800
  if (containsV4Pool) {
1832
- path = [route.adjustedInput.isNative ? ADDRESS_ZERO : route.adjustedInput.address];
1801
+ path = [route.pathInput.isNative ? ADDRESS_ZERO : route.pathInput.address];
1833
1802
  types = ['address'];
1834
- var currencyIn = route.adjustedInput;
1803
+ var currencyIn = route.pathInput;
1835
1804
  for (var _iterator = _createForOfIteratorHelperLoose(route.pools), _step; !(_step = _iterator()).done;) {
1836
1805
  var pool = _step.value;
1837
1806
  var currencyOut = currencyIn.equals(pool.token0) ? pool.token1 : pool.token0;
@@ -1895,7 +1864,7 @@ var partitionMixedRouteByProtocol = function partitionMixedRouteByProtocol(route
1895
1864
  var left = 0;
1896
1865
  var right = 0;
1897
1866
  while (right < route.pools.length) {
1898
- if (route.pools[left] instanceof v3Sdk.Pool && route.pools[right] instanceof v2Sdk.Pair || route.pools[left] instanceof v2Sdk.Pair && route.pools[right] instanceof v3Sdk.Pool) {
1867
+ if (route.pools[left] instanceof v4Sdk.Pool && !(route.pools[right] instanceof v4Sdk.Pool) || route.pools[left] instanceof v3Sdk.Pool && !(route.pools[right] instanceof v3Sdk.Pool) || route.pools[left] instanceof v2Sdk.Pair && !(route.pools[right] instanceof v2Sdk.Pair)) {
1899
1868
  acc.push(route.pools.slice(left, right));
1900
1869
  left = right;
1901
1870
  }