@uniswap/router-sdk 1.21.4 → 1.22.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, Token, CurrencyAmount, Price, 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';
@@ -623,22 +623,6 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
623
623
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
624
624
  }
625
625
 
626
- function isValidTokenPath(prevPool, currentPool, inputToken) {
627
- if (inputToken instanceof Token && currentPool.involvesToken(inputToken)) return true;
628
- if (currentPool instanceof Pool && currentPool.involvesToken(inputToken)) return true;
629
- // throw if both v4 pools, native/wrapped tokens not interchangeable in v4
630
- if (prevPool instanceof Pool && currentPool instanceof Pool) return false;
631
- // v2/v3 --> v4 valid if v2/v3 output is the wrapped version of the v4 pool native currency
632
- if (currentPool instanceof Pool) {
633
- if (currentPool.token0.wrapped.equals(inputToken) || currentPool.token1.wrapped.equals(inputToken)) return true;
634
- }
635
- // v4 --> v2/v3 valid if v4 output is the native version of the v2/v3 wrapped token
636
- if (prevPool instanceof Pool) {
637
- if (currentPool.involvesToken(inputToken.wrapped)) return true;
638
- }
639
- return false;
640
- }
641
-
642
626
  function amountWithPathCurrency(amount, pool) {
643
627
  return CurrencyAmount.fromFractionalAmount(getPathCurrency(amount.currency, pool), amount.numerator, amount.denominator);
644
628
  }
@@ -674,10 +658,18 @@ var MixedRouteSDK = /*#__PURE__*/function () {
674
658
  * @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
675
659
  * @param input The input token
676
660
  * @param output The output token
661
+ * @param retainsFakePool Set to true to filter out a pool that has a fake eth-weth pool
677
662
  */
678
- function MixedRouteSDK(pools, input, output) {
663
+ function MixedRouteSDK(pools, input, output, retainFakePools) {
664
+ if (retainFakePools === void 0) {
665
+ retainFakePools = false;
666
+ }
679
667
  this._midPrice = null;
668
+ pools = retainFakePools ? pools : pools.filter(function (pool) {
669
+ return !(pool instanceof Pool && pool.tickSpacing === 0);
670
+ });
680
671
  !(pools.length > 0) ? process.env.NODE_ENV !== "production" ? invariant(false, 'POOLS') : invariant(false) : void 0;
672
+ // there is a pool mismatched to the path if we do not retain the fake eth-weth pools
681
673
  var chainId = pools[0].chainId;
682
674
  var allOnSameChain = pools.every(function (pool) {
683
675
  return pool.chainId === chainId;
@@ -685,7 +677,11 @@ var MixedRouteSDK = /*#__PURE__*/function () {
685
677
  !allOnSameChain ? process.env.NODE_ENV !== "production" ? invariant(false, 'CHAIN_IDS') : invariant(false) : void 0;
686
678
  this.pathInput = getPathCurrency(input, pools[0]);
687
679
  this.pathOutput = getPathCurrency(output, pools[pools.length - 1]);
688
- !pools[0].involvesToken(this.pathInput) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
680
+ if (!(pools[0] instanceof Pool)) {
681
+ !pools[0].involvesToken(this.pathInput) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
682
+ } else {
683
+ !pools[0].v4InvolvesToken(this.pathInput) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
684
+ }
689
685
  var lastPool = pools[pools.length - 1];
690
686
  if (lastPool instanceof Pool) {
691
687
  !(lastPool.v4InvolvesToken(output) || lastPool.v4InvolvesToken(output.wrapped)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'OUTPUT') : invariant(false) : void 0;
@@ -698,11 +694,31 @@ var MixedRouteSDK = /*#__PURE__*/function () {
698
694
  var tokenPath = [this.pathInput];
699
695
  pools[0].token0.equals(this.pathInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
700
696
  for (var i = 1; i < pools.length; i++) {
701
- var prevPool = pools[i - 1];
702
697
  var pool = pools[i];
703
698
  var inputToken = tokenPath[i];
704
- var outputToken = pool instanceof Pool ? pool.token0.equals(inputToken) ? pool.token1 : pool.token0 : pool.token0.wrapped.equals(inputToken.wrapped) ? pool.token1 : pool.token0;
705
- !isValidTokenPath(prevPool, pool, inputToken) ? process.env.NODE_ENV !== "production" ? invariant(false, "PATH") : invariant(false) : void 0;
699
+ var outputToken = void 0;
700
+ if (
701
+ // we hit an edge case if it's a v4 pool and neither of the tokens are in the pool OR it is not a v4 pool but the input currency is eth
702
+ pool instanceof Pool && !pool.involvesToken(inputToken) || !(pool instanceof Pool) && inputToken.isNative) {
703
+ // We handle the case where the inputToken =/= pool.token0 or pool.token1. There are 2 specific cases.
704
+ if (inputToken.equals(pool.token0.wrapped)) {
705
+ // 1) the inputToken is WETH and the current pool has ETH
706
+ // for example, pools: USDC-WETH, ETH-PEPE, path: USDC, WETH, PEPE
707
+ // second pool is a v4 pool, the first could be any version
708
+ outputToken = pool.token1;
709
+ } else if (inputToken.wrapped.equals(pool.token0) || inputToken.wrapped.equals(pool.token1)) {
710
+ // 2) the inputToken is ETH and the current pool has WETH
711
+ // for example, pools: USDC-ETH, WETH-PEPE, path: USDC, ETH, PEPE
712
+ // first pool is a v4 pool, the second could be any version
713
+ outputToken = inputToken.wrapped.equals(pool.token0) ? pool.token1 : pool.token0;
714
+ } else {
715
+ throw new Error("POOL_MISMATCH pool: " + JSON.stringify(pool) + " inputToken: " + JSON.stringify(inputToken));
716
+ }
717
+ } else {
718
+ // then the input token must equal either token0 or token1
719
+ !(inputToken.equals(pool.token0) || inputToken.equals(pool.token1)) ? process.env.NODE_ENV !== "production" ? invariant(false, "PATH pool " + JSON.stringify(pool) + " inputToken " + JSON.stringify(inputToken)) : invariant(false) : void 0;
720
+ outputToken = inputToken.equals(pool.token0) ? pool.token1 : pool.token0;
721
+ }
706
722
  tokenPath.push(outputToken);
707
723
  }
708
724
  this.pools = pools;
@@ -1281,6 +1297,17 @@ var Protocol;
1281
1297
  Protocol["MIXED"] = "MIXED";
1282
1298
  })(Protocol || (Protocol = {}));
1283
1299
 
1300
+ // Helper function to get the pathInput and pathOutput for a V2 / V3 route
1301
+ // currency could be native so we check against the wrapped version as they don't support native ETH in path
1302
+ function getPathToken(currency, pool) {
1303
+ if (pool.token0.wrapped.equals(currency.wrapped)) {
1304
+ return pool.token0;
1305
+ } else if (pool.token1.wrapped.equals(currency.wrapped)) {
1306
+ return pool.token1;
1307
+ } else {
1308
+ throw new Error("Expected token " + currency.symbol + " to be either " + pool.token0.symbol + " or " + pool.token1.symbol);
1309
+ }
1310
+ }
1284
1311
  // V2 route wrapper
1285
1312
  var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
1286
1313
  _inheritsLoose(RouteV2, _V2RouteSDK);
@@ -1289,6 +1316,8 @@ var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
1289
1316
  _this = _V2RouteSDK.call(this, v2Route.pairs, v2Route.input, v2Route.output) || this;
1290
1317
  _this.protocol = Protocol.V2;
1291
1318
  _this.pools = _this.pairs;
1319
+ _this.pathInput = getPathToken(v2Route.input, _this.pairs[0]);
1320
+ _this.pathOutput = getPathToken(v2Route.output, _this.pairs[_this.pairs.length - 1]);
1292
1321
  return _this;
1293
1322
  }
1294
1323
  return RouteV2;
@@ -1301,6 +1330,8 @@ var RouteV3 = /*#__PURE__*/function (_V3RouteSDK) {
1301
1330
  _this2 = _V3RouteSDK.call(this, v3Route.pools, v3Route.input, v3Route.output) || this;
1302
1331
  _this2.protocol = Protocol.V3;
1303
1332
  _this2.path = v3Route.tokenPath;
1333
+ _this2.pathInput = getPathToken(v3Route.input, _this2.pools[0]);
1334
+ _this2.pathOutput = getPathToken(v3Route.output, _this2.pools[_this2.pools.length - 1]);
1304
1335
  return _this2;
1305
1336
  }
1306
1337
  return RouteV3;
@@ -1692,13 +1723,13 @@ var Trade = /*#__PURE__*/function () {
1692
1723
  if (this._inputAmount) {
1693
1724
  return this._inputAmount;
1694
1725
  }
1695
- var inputCurrency = this.swaps[0].inputAmount.currency;
1726
+ var inputAmountCurrency = this.swaps[0].inputAmount.currency;
1696
1727
  var totalInputFromRoutes = this.swaps.map(function (_ref5) {
1697
- var inputAmount = _ref5.inputAmount;
1698
- return inputAmount;
1728
+ var routeInputAmount = _ref5.inputAmount;
1729
+ return routeInputAmount;
1699
1730
  }).reduce(function (total, cur) {
1700
1731
  return total.add(cur);
1701
- }, CurrencyAmount.fromRawAmount(inputCurrency, 0));
1732
+ }, CurrencyAmount.fromRawAmount(inputAmountCurrency, 0));
1702
1733
  this._inputAmount = totalInputFromRoutes;
1703
1734
  return this._inputAmount;
1704
1735
  }
@@ -1710,14 +1741,50 @@ var Trade = /*#__PURE__*/function () {
1710
1741
  }
1711
1742
  var outputCurrency = this.swaps[0].outputAmount.currency;
1712
1743
  var totalOutputFromRoutes = this.swaps.map(function (_ref6) {
1713
- var outputAmount = _ref6.outputAmount;
1714
- return outputAmount;
1744
+ var routeOutputAmount = _ref6.outputAmount;
1745
+ return routeOutputAmount;
1715
1746
  }).reduce(function (total, cur) {
1716
1747
  return total.add(cur);
1717
1748
  }, CurrencyAmount.fromRawAmount(outputCurrency, 0));
1718
1749
  this._outputAmount = totalOutputFromRoutes;
1719
1750
  return this._outputAmount;
1720
1751
  }
1752
+ /**
1753
+ * Returns the sum of all swaps within the trade
1754
+ * @returns
1755
+ * inputAmount: total input amount
1756
+ * inputAmountNative: total amount of native currency required for ETH input paths
1757
+ * - 0 if inputAmount is native but no native input paths
1758
+ * - undefined if inputAmount is not native
1759
+ * outputAmount: total output amount
1760
+ * outputAmountNative: total amount of native currency returned from ETH output paths
1761
+ * - 0 if outputAmount is native but no native output paths
1762
+ * - undefined if outputAmount is not native
1763
+ */
1764
+ }, {
1765
+ key: "amounts",
1766
+ get: function get() {
1767
+ var _this$swaps$find, _this$swaps$find2;
1768
+ // Find native currencies for reduce below
1769
+ var inputNativeCurrency = (_this$swaps$find = this.swaps.find(function (_ref7) {
1770
+ var inputAmount = _ref7.inputAmount;
1771
+ return inputAmount.currency.isNative;
1772
+ })) == null ? void 0 : _this$swaps$find.inputAmount.currency;
1773
+ var outputNativeCurrency = (_this$swaps$find2 = this.swaps.find(function (_ref8) {
1774
+ var outputAmount = _ref8.outputAmount;
1775
+ return outputAmount.currency.isNative;
1776
+ })) == null ? void 0 : _this$swaps$find2.outputAmount.currency;
1777
+ return {
1778
+ inputAmount: this.inputAmount,
1779
+ inputAmountNative: inputNativeCurrency ? this.swaps.reduce(function (total, swap) {
1780
+ return swap.route.pathInput.isNative ? total.add(swap.inputAmount) : total;
1781
+ }, CurrencyAmount.fromRawAmount(inputNativeCurrency, 0)) : undefined,
1782
+ outputAmount: this.outputAmount,
1783
+ outputAmountNative: outputNativeCurrency ? this.swaps.reduce(function (total, swap) {
1784
+ return swap.route.pathOutput.isNative ? total.add(swap.outputAmount) : total;
1785
+ }, CurrencyAmount.fromRawAmount(outputNativeCurrency, 0)) : undefined
1786
+ };
1787
+ }
1721
1788
  /**
1722
1789
  * The price expressed in terms of output amount/input amount.
1723
1790
  */
@@ -2338,5 +2405,5 @@ var SwapRouter = /*#__PURE__*/function () {
2338
2405
  }();
2339
2406
  SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$3);
2340
2407
 
2341
- export { ADDRESS_THIS, ADDRESS_ZERO, ApprovalTypes, ApproveAndCall, MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER, MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER, MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER, MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER, MSG_SENDER, MixedRoute, MixedRouteSDK, MixedRouteTrade, MulticallExtended, ONE, ONE_HUNDRED_PERCENT, PaymentsExtended, Protocol, RouteV2, RouteV3, RouteV4, SwapRouter, Trade, ZERO, ZERO_PERCENT, amountWithPathCurrency, encodeMixedRouteToPath, getOutputOfPools, getPathCurrency, isMint, isValidTokenPath, partitionMixedRouteByProtocol, tradeComparator };
2408
+ export { ADDRESS_THIS, ADDRESS_ZERO, ApprovalTypes, ApproveAndCall, MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER, MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER, MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER, MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER, MSG_SENDER, MixedRoute, MixedRouteSDK, MixedRouteTrade, MulticallExtended, ONE, ONE_HUNDRED_PERCENT, PaymentsExtended, Protocol, RouteV2, RouteV3, RouteV4, SwapRouter, Trade, ZERO, ZERO_PERCENT, amountWithPathCurrency, encodeMixedRouteToPath, getOutputOfPools, getPathCurrency, getPathToken, isMint, partitionMixedRouteByProtocol, tradeComparator };
2342
2409
  //# sourceMappingURL=router-sdk.esm.js.map