@uniswap/router-sdk 1.13.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.
@@ -1,4 +1,4 @@
1
- import { Percent, validateAndParseAddress, Price, CurrencyAmount, Fraction, TradeType, sortedInsert, WETH9 } from '@uniswap/sdk-core';
1
+ import { Percent, validateAndParseAddress, CurrencyAmount, Price, 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';
@@ -638,6 +638,30 @@ function isValidTokenPath(prevPool, currentPool, inputToken) {
638
638
  return false;
639
639
  }
640
640
 
641
+ function amountWithPathCurrency(amount, pool) {
642
+ return CurrencyAmount.fromFractionalAmount(getPathCurrency(amount.currency, pool), amount.numerator, amount.denominator);
643
+ }
644
+ function getPathCurrency(currency, pool) {
645
+ // return currency if the currency matches a currency of the pool
646
+ if (pool.involvesToken(currency)) {
647
+ return currency;
648
+ // return if currency.wrapped if pool involves wrapped currency
649
+ } else if (pool.involvesToken(currency.wrapped)) {
650
+ return currency.wrapped;
651
+ // return native currency if pool involves native version of wrapped currency (only applies to V4)
652
+ } else if (pool instanceof Pool) {
653
+ if (pool.currency0.wrapped.equals(currency)) {
654
+ return pool.token0;
655
+ } else if (pool.token1.wrapped.equals(currency)) {
656
+ return pool.token1;
657
+ }
658
+ // otherwise the token is invalid
659
+ } else {
660
+ throw new Error("Expected currency " + currency.symbol + " to be either " + pool.token0.symbol + " or " + pool.token1.symbol);
661
+ }
662
+ return currency; // this line needed for typescript to compile
663
+ }
664
+
641
665
  /**
642
666
  * Represents a list of pools or pairs through which a swap can occur
643
667
  * @template TInput The input token
@@ -658,12 +682,9 @@ var MixedRouteSDK = /*#__PURE__*/function () {
658
682
  return pool.chainId === chainId;
659
683
  });
660
684
  !allOnSameChain ? process.env.NODE_ENV !== "production" ? invariant(false, 'CHAIN_IDS') : invariant(false) : void 0;
661
- if (pools[0] instanceof Pool) {
662
- this.adjustedInput = pools[0].involvesToken(input) ? input : input.wrapped;
663
- } else {
664
- this.adjustedInput = input.wrapped; // no native currencies in v2/v3
665
- }
666
- !pools[0].involvesToken(this.adjustedInput) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
685
+ this.pathInput = getPathCurrency(input, pools[0]);
686
+ this.pathOutput = getPathCurrency(output, pools[pools.length - 1]);
687
+ !pools[0].involvesToken(this.pathInput) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
667
688
  var lastPool = pools[pools.length - 1];
668
689
  if (lastPool instanceof Pool) {
669
690
  !(lastPool.involvesToken(output) || lastPool.involvesToken(output.wrapped)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'OUTPUT') : invariant(false) : void 0;
@@ -673,8 +694,8 @@ var MixedRouteSDK = /*#__PURE__*/function () {
673
694
  /**
674
695
  * Normalizes token0-token1 order and selects the next token/fee step to add to the path
675
696
  * */
676
- var tokenPath = [this.adjustedInput];
677
- pools[0].token0.equals(this.adjustedInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
697
+ var tokenPath = [this.pathInput];
698
+ pools[0].token0.equals(this.pathInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
678
699
  for (var i = 1; i < pools.length; i++) {
679
700
  var prevPool = pools[i - 1];
680
701
  var pool = pools[i];
@@ -710,7 +731,7 @@ var MixedRouteSDK = /*#__PURE__*/function () {
710
731
  nextInput: pool.token0,
711
732
  price: price.multiply(pool.token1Price.asFraction)
712
733
  };
713
- }, this.pools[0].token0.equals(this.adjustedInput) ? {
734
+ }, this.pools[0].token0.equals(this.pathInput) ? {
714
735
  nextInput: this.pools[0].token1,
715
736
  price: this.pools[0].token0Price.asFraction
716
737
  } : {
@@ -723,58 +744,6 @@ var MixedRouteSDK = /*#__PURE__*/function () {
723
744
  return MixedRouteSDK;
724
745
  }();
725
746
 
726
- function getOutputAmount(_x, _x2) {
727
- return _getOutputAmount.apply(this, arguments);
728
- }
729
- function _getOutputAmount() {
730
- _getOutputAmount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pool, amountIn) {
731
- return _regeneratorRuntime().wrap(function _callee$(_context) {
732
- while (1) switch (_context.prev = _context.next) {
733
- case 0:
734
- if (!(pool instanceof Pool)) {
735
- _context.next = 13;
736
- break;
737
- }
738
- if (!pool.involvesCurrency(amountIn.currency)) {
739
- _context.next = 5;
740
- break;
741
- }
742
- _context.next = 4;
743
- return pool.getOutputAmount(amountIn);
744
- case 4:
745
- return _context.abrupt("return", _context.sent);
746
- case 5:
747
- if (!pool.token0.wrapped.equals(amountIn.currency)) {
748
- _context.next = 9;
749
- break;
750
- }
751
- _context.next = 8;
752
- return pool.getOutputAmount(CurrencyAmount.fromRawAmount(pool.token0, amountIn.quotient));
753
- case 8:
754
- return _context.abrupt("return", _context.sent);
755
- case 9:
756
- if (!pool.token1.wrapped.equals(amountIn.currency)) {
757
- _context.next = 13;
758
- break;
759
- }
760
- _context.next = 12;
761
- return pool.getOutputAmount(CurrencyAmount.fromRawAmount(pool.token1, amountIn.quotient));
762
- case 12:
763
- return _context.abrupt("return", _context.sent);
764
- case 13:
765
- _context.next = 15;
766
- return pool.getOutputAmount(amountIn.wrapped);
767
- case 15:
768
- return _context.abrupt("return", _context.sent);
769
- case 16:
770
- case "end":
771
- return _context.stop();
772
- }
773
- }, _callee);
774
- }));
775
- return _getOutputAmount.apply(this, arguments);
776
- }
777
-
778
747
  /**
779
748
  * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
780
749
  * @template TInput The input token, either Ether or an ERC-20
@@ -895,14 +864,14 @@ var MixedRouteTrade = /*#__PURE__*/function () {
895
864
  /*#__PURE__*/
896
865
  function () {
897
866
  var _fromRoute = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(route, amount, tradeType) {
898
- var amounts, inputAmount, outputAmount, i, pool, _yield$getOutputAmoun, _outputAmount;
867
+ var amounts, inputAmount, outputAmount, i, pool, _yield$pool$getOutput, _outputAmount;
899
868
  return _regeneratorRuntime().wrap(function _callee$(_context) {
900
869
  while (1) switch (_context.prev = _context.next) {
901
870
  case 0:
902
871
  amounts = new Array(route.path.length);
903
872
  !(tradeType === TradeType.EXACT_INPUT) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TRADE_TYPE') : invariant(false) : void 0;
904
873
  !amount.currency.equals(route.input) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
905
- amounts[0] = route.pools[0] instanceof Pool ? amount : amount.wrapped;
874
+ amounts[0] = amountWithPathCurrency(amount, route.pools[0]);
906
875
  i = 0;
907
876
  case 5:
908
877
  if (!(i < route.path.length - 1)) {
@@ -911,10 +880,10 @@ var MixedRouteTrade = /*#__PURE__*/function () {
911
880
  }
912
881
  pool = route.pools[i];
913
882
  _context.next = 9;
914
- return getOutputAmount(pool, amounts[i]);
883
+ return pool.getOutputAmount(amountWithPathCurrency(amounts[i], pool));
915
884
  case 9:
916
- _yield$getOutputAmoun = _context.sent;
917
- _outputAmount = _yield$getOutputAmoun[0];
885
+ _yield$pool$getOutput = _context.sent;
886
+ _outputAmount = _yield$pool$getOutput[0];
918
887
  amounts[i + 1] = _outputAmount;
919
888
  case 12:
920
889
  i++;
@@ -957,7 +926,7 @@ var MixedRouteTrade = /*#__PURE__*/function () {
957
926
  /*#__PURE__*/
958
927
  function () {
959
928
  var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(routes, tradeType) {
960
- var populatedRoutes, _iterator3, _step3, _step3$value, route, amount, amounts, inputAmount, outputAmount, i, pool, _yield$getOutputAmoun2, _outputAmount2;
929
+ var populatedRoutes, _iterator3, _step3, _step3$value, route, amount, amounts, inputAmount, outputAmount, i, pool, _yield$pool$getOutput2, _outputAmount2;
961
930
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
962
931
  while (1) switch (_context2.prev = _context2.next) {
963
932
  case 0:
@@ -975,7 +944,7 @@ var MixedRouteTrade = /*#__PURE__*/function () {
975
944
  outputAmount = void 0;
976
945
  !amount.currency.equals(route.input) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
977
946
  inputAmount = CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
978
- amounts[0] = CurrencyAmount.fromFractionalAmount(route.adjustedInput, amount.numerator, amount.denominator);
947
+ amounts[0] = CurrencyAmount.fromFractionalAmount(route.pathInput, amount.numerator, amount.denominator);
979
948
  i = 0;
980
949
  case 12:
981
950
  if (!(i < route.path.length - 1)) {
@@ -984,10 +953,10 @@ var MixedRouteTrade = /*#__PURE__*/function () {
984
953
  }
985
954
  pool = route.pools[i];
986
955
  _context2.next = 16;
987
- return getOutputAmount(pool, amounts[i]);
956
+ return pool.getOutputAmount(amountWithPathCurrency(amounts[i], pool));
988
957
  case 16:
989
- _yield$getOutputAmoun2 = _context2.sent;
990
- _outputAmount2 = _yield$getOutputAmoun2[0];
958
+ _yield$pool$getOutput2 = _context2.sent;
959
+ _outputAmount2 = _yield$pool$getOutput2[0];
991
960
  amounts[i + 1] = _outputAmount2;
992
961
  case 19:
993
962
  i++;
@@ -1825,9 +1794,9 @@ function encodeMixedRouteToPath(route) {
1825
1794
  var path;
1826
1795
  var types;
1827
1796
  if (containsV4Pool) {
1828
- path = [route.adjustedInput.isNative ? ADDRESS_ZERO : route.adjustedInput.address];
1797
+ path = [route.pathInput.isNative ? ADDRESS_ZERO : route.pathInput.address];
1829
1798
  types = ['address'];
1830
- var currencyIn = route.adjustedInput;
1799
+ var currencyIn = route.pathInput;
1831
1800
  for (var _iterator = _createForOfIteratorHelperLoose(route.pools), _step; !(_step = _iterator()).done;) {
1832
1801
  var pool = _step.value;
1833
1802
  var currencyOut = currencyIn.equals(pool.token0) ? pool.token1 : pool.token0;
@@ -1891,7 +1860,7 @@ var partitionMixedRouteByProtocol = function partitionMixedRouteByProtocol(route
1891
1860
  var left = 0;
1892
1861
  var right = 0;
1893
1862
  while (right < route.pools.length) {
1894
- if (route.pools[left] instanceof Pool$1 && route.pools[right] instanceof Pair || route.pools[left] instanceof Pair && route.pools[right] instanceof Pool$1) {
1863
+ if (route.pools[left] instanceof Pool && !(route.pools[right] instanceof Pool) || route.pools[left] instanceof Pool$1 && !(route.pools[right] instanceof Pool$1) || route.pools[left] instanceof Pair && !(route.pools[right] instanceof Pair)) {
1895
1864
  acc.push(route.pools.slice(left, right));
1896
1865
  left = right;
1897
1866
  }