@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.
@@ -18,8 +18,9 @@ export declare class MixedRouteSDK<TInput extends Currency, TOutput extends Curr
18
18
  * @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
19
19
  * @param input The input token
20
20
  * @param output The output token
21
+ * @param retainsFakePool Set to true to filter out a pool that has a fake eth-weth pool
21
22
  */
22
- constructor(pools: TPool[], input: TInput, output: TOutput);
23
+ constructor(pools: TPool[], input: TInput, output: TOutput, retainFakePools?: boolean);
23
24
  get chainId(): number;
24
25
  /**
25
26
  * Returns the mid price of the route
@@ -4,6 +4,7 @@ import { Route as V4RouteSDK, Pool as V4Pool } from '@uniswap/v4-sdk';
4
4
  import { Protocol } from './protocol';
5
5
  import { Currency, Price, Token } from '@uniswap/sdk-core';
6
6
  import { MixedRouteSDK } from './mixedRoute/route';
7
+ export declare function getPathToken(currency: Currency, pool: Pair | V3Pool): Token;
7
8
  export interface IRoute<TInput extends Currency, TOutput extends Currency, TPool extends Pair | V3Pool | V4Pool> {
8
9
  protocol: Protocol;
9
10
  pools: TPool[];
@@ -11,15 +12,21 @@ export interface IRoute<TInput extends Currency, TOutput extends Currency, TPool
11
12
  midPrice: Price<TInput, TOutput>;
12
13
  input: TInput;
13
14
  output: TOutput;
15
+ pathInput: Currency;
16
+ pathOutput: Currency;
14
17
  }
15
18
  export declare class RouteV2<TInput extends Currency, TOutput extends Currency> extends V2RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, Pair> {
16
19
  readonly protocol: Protocol;
17
20
  readonly pools: Pair[];
21
+ pathInput: Currency;
22
+ pathOutput: Currency;
18
23
  constructor(v2Route: V2RouteSDK<TInput, TOutput>);
19
24
  }
20
25
  export declare class RouteV3<TInput extends Currency, TOutput extends Currency> extends V3RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, V3Pool> {
21
26
  readonly protocol: Protocol;
22
27
  readonly path: Token[];
28
+ pathInput: Currency;
29
+ pathOutput: Currency;
23
30
  constructor(v3Route: V3RouteSDK<TInput, TOutput>);
24
31
  }
25
32
  export declare class RouteV4<TInput extends Currency, TOutput extends Currency> extends V4RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, V4Pool> {
@@ -43,6 +43,24 @@ export declare class Trade<TInput extends Currency, TOutput extends Currency, TT
43
43
  });
44
44
  get inputAmount(): CurrencyAmount<TInput>;
45
45
  get outputAmount(): CurrencyAmount<TOutput>;
46
+ /**
47
+ * Returns the sum of all swaps within the trade
48
+ * @returns
49
+ * inputAmount: total input amount
50
+ * inputAmountNative: total amount of native currency required for ETH input paths
51
+ * - 0 if inputAmount is native but no native input paths
52
+ * - undefined if inputAmount is not native
53
+ * outputAmount: total output amount
54
+ * outputAmountNative: total amount of native currency returned from ETH output paths
55
+ * - 0 if outputAmount is native but no native output paths
56
+ * - undefined if outputAmount is not native
57
+ */
58
+ get amounts(): {
59
+ inputAmount: CurrencyAmount<TInput>;
60
+ inputAmountNative: CurrencyAmount<TInput> | undefined;
61
+ outputAmount: CurrencyAmount<TOutput>;
62
+ outputAmountNative: CurrencyAmount<TOutput> | undefined;
63
+ };
46
64
  private _executionPrice;
47
65
  /**
48
66
  * The price expressed in terms of output amount/input amount.
package/dist/index.d.ts CHANGED
@@ -12,4 +12,3 @@ export * from './utils/encodeMixedRouteToPath';
12
12
  export * from './utils/TPool';
13
13
  export * from './utils/pathCurrency';
14
14
  export * from './utils';
15
- export * from './utils/isValidTokenPath';
@@ -628,22 +628,6 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
628
628
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
629
629
  }
630
630
 
631
- function isValidTokenPath(prevPool, currentPool, inputToken) {
632
- if (inputToken instanceof sdkCore.Token && currentPool.involvesToken(inputToken)) return true;
633
- if (currentPool instanceof v4Sdk.Pool && currentPool.involvesToken(inputToken)) return true;
634
- // throw if both v4 pools, native/wrapped tokens not interchangeable in v4
635
- if (prevPool instanceof v4Sdk.Pool && currentPool instanceof v4Sdk.Pool) return false;
636
- // v2/v3 --> v4 valid if v2/v3 output is the wrapped version of the v4 pool native currency
637
- if (currentPool instanceof v4Sdk.Pool) {
638
- if (currentPool.token0.wrapped.equals(inputToken) || currentPool.token1.wrapped.equals(inputToken)) return true;
639
- }
640
- // v4 --> v2/v3 valid if v4 output is the native version of the v2/v3 wrapped token
641
- if (prevPool instanceof v4Sdk.Pool) {
642
- if (currentPool.involvesToken(inputToken.wrapped)) return true;
643
- }
644
- return false;
645
- }
646
-
647
631
  function amountWithPathCurrency(amount, pool) {
648
632
  return sdkCore.CurrencyAmount.fromFractionalAmount(getPathCurrency(amount.currency, pool), amount.numerator, amount.denominator);
649
633
  }
@@ -679,10 +663,18 @@ var MixedRouteSDK = /*#__PURE__*/function () {
679
663
  * @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
680
664
  * @param input The input token
681
665
  * @param output The output token
666
+ * @param retainsFakePool Set to true to filter out a pool that has a fake eth-weth pool
682
667
  */
683
- function MixedRouteSDK(pools, input, output) {
668
+ function MixedRouteSDK(pools, input, output, retainFakePools) {
669
+ if (retainFakePools === void 0) {
670
+ retainFakePools = false;
671
+ }
684
672
  this._midPrice = null;
673
+ pools = retainFakePools ? pools : pools.filter(function (pool) {
674
+ return !(pool instanceof v4Sdk.Pool && pool.tickSpacing === 0);
675
+ });
685
676
  !(pools.length > 0) ? invariant(false, 'POOLS') : void 0;
677
+ // there is a pool mismatched to the path if we do not retain the fake eth-weth pools
686
678
  var chainId = pools[0].chainId;
687
679
  var allOnSameChain = pools.every(function (pool) {
688
680
  return pool.chainId === chainId;
@@ -690,7 +682,11 @@ var MixedRouteSDK = /*#__PURE__*/function () {
690
682
  !allOnSameChain ? invariant(false, 'CHAIN_IDS') : void 0;
691
683
  this.pathInput = getPathCurrency(input, pools[0]);
692
684
  this.pathOutput = getPathCurrency(output, pools[pools.length - 1]);
693
- !pools[0].involvesToken(this.pathInput) ? invariant(false, 'INPUT') : void 0;
685
+ if (!(pools[0] instanceof v4Sdk.Pool)) {
686
+ !pools[0].involvesToken(this.pathInput) ? invariant(false, 'INPUT') : void 0;
687
+ } else {
688
+ !pools[0].v4InvolvesToken(this.pathInput) ? invariant(false, 'INPUT') : void 0;
689
+ }
694
690
  var lastPool = pools[pools.length - 1];
695
691
  if (lastPool instanceof v4Sdk.Pool) {
696
692
  !(lastPool.v4InvolvesToken(output) || lastPool.v4InvolvesToken(output.wrapped)) ? invariant(false, 'OUTPUT') : void 0;
@@ -703,11 +699,31 @@ var MixedRouteSDK = /*#__PURE__*/function () {
703
699
  var tokenPath = [this.pathInput];
704
700
  pools[0].token0.equals(this.pathInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
705
701
  for (var i = 1; i < pools.length; i++) {
706
- var prevPool = pools[i - 1];
707
702
  var pool = pools[i];
708
703
  var inputToken = tokenPath[i];
709
- var outputToken = pool instanceof v4Sdk.Pool ? pool.token0.equals(inputToken) ? pool.token1 : pool.token0 : pool.token0.wrapped.equals(inputToken.wrapped) ? pool.token1 : pool.token0;
710
- !isValidTokenPath(prevPool, pool, inputToken) ? invariant(false, "PATH") : void 0;
704
+ var outputToken = void 0;
705
+ if (
706
+ // 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
707
+ pool instanceof v4Sdk.Pool && !pool.involvesToken(inputToken) || !(pool instanceof v4Sdk.Pool) && inputToken.isNative) {
708
+ // We handle the case where the inputToken =/= pool.token0 or pool.token1. There are 2 specific cases.
709
+ if (inputToken.equals(pool.token0.wrapped)) {
710
+ // 1) the inputToken is WETH and the current pool has ETH
711
+ // for example, pools: USDC-WETH, ETH-PEPE, path: USDC, WETH, PEPE
712
+ // second pool is a v4 pool, the first could be any version
713
+ outputToken = pool.token1;
714
+ } else if (inputToken.wrapped.equals(pool.token0) || inputToken.wrapped.equals(pool.token1)) {
715
+ // 2) the inputToken is ETH and the current pool has WETH
716
+ // for example, pools: USDC-ETH, WETH-PEPE, path: USDC, ETH, PEPE
717
+ // first pool is a v4 pool, the second could be any version
718
+ outputToken = inputToken.wrapped.equals(pool.token0) ? pool.token1 : pool.token0;
719
+ } else {
720
+ throw new Error("POOL_MISMATCH pool: " + JSON.stringify(pool) + " inputToken: " + JSON.stringify(inputToken));
721
+ }
722
+ } else {
723
+ // then the input token must equal either token0 or token1
724
+ !(inputToken.equals(pool.token0) || inputToken.equals(pool.token1)) ? invariant(false, "PATH pool " + JSON.stringify(pool) + " inputToken " + JSON.stringify(inputToken)) : void 0;
725
+ outputToken = inputToken.equals(pool.token0) ? pool.token1 : pool.token0;
726
+ }
711
727
  tokenPath.push(outputToken);
712
728
  }
713
729
  this.pools = pools;
@@ -1285,6 +1301,17 @@ var MixedRouteTrade = /*#__PURE__*/function () {
1285
1301
  Protocol["MIXED"] = "MIXED";
1286
1302
  })(exports.Protocol || (exports.Protocol = {}));
1287
1303
 
1304
+ // Helper function to get the pathInput and pathOutput for a V2 / V3 route
1305
+ // currency could be native so we check against the wrapped version as they don't support native ETH in path
1306
+ function getPathToken(currency, pool) {
1307
+ if (pool.token0.wrapped.equals(currency.wrapped)) {
1308
+ return pool.token0;
1309
+ } else if (pool.token1.wrapped.equals(currency.wrapped)) {
1310
+ return pool.token1;
1311
+ } else {
1312
+ throw new Error("Expected token " + currency.symbol + " to be either " + pool.token0.symbol + " or " + pool.token1.symbol);
1313
+ }
1314
+ }
1288
1315
  // V2 route wrapper
1289
1316
  var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
1290
1317
  _inheritsLoose(RouteV2, _V2RouteSDK);
@@ -1293,6 +1320,8 @@ var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
1293
1320
  _this = _V2RouteSDK.call(this, v2Route.pairs, v2Route.input, v2Route.output) || this;
1294
1321
  _this.protocol = exports.Protocol.V2;
1295
1322
  _this.pools = _this.pairs;
1323
+ _this.pathInput = getPathToken(v2Route.input, _this.pairs[0]);
1324
+ _this.pathOutput = getPathToken(v2Route.output, _this.pairs[_this.pairs.length - 1]);
1296
1325
  return _this;
1297
1326
  }
1298
1327
  return RouteV2;
@@ -1305,6 +1334,8 @@ var RouteV3 = /*#__PURE__*/function (_V3RouteSDK) {
1305
1334
  _this2 = _V3RouteSDK.call(this, v3Route.pools, v3Route.input, v3Route.output) || this;
1306
1335
  _this2.protocol = exports.Protocol.V3;
1307
1336
  _this2.path = v3Route.tokenPath;
1337
+ _this2.pathInput = getPathToken(v3Route.input, _this2.pools[0]);
1338
+ _this2.pathOutput = getPathToken(v3Route.output, _this2.pools[_this2.pools.length - 1]);
1308
1339
  return _this2;
1309
1340
  }
1310
1341
  return RouteV3;
@@ -1696,13 +1727,13 @@ var Trade = /*#__PURE__*/function () {
1696
1727
  if (this._inputAmount) {
1697
1728
  return this._inputAmount;
1698
1729
  }
1699
- var inputCurrency = this.swaps[0].inputAmount.currency;
1730
+ var inputAmountCurrency = this.swaps[0].inputAmount.currency;
1700
1731
  var totalInputFromRoutes = this.swaps.map(function (_ref5) {
1701
- var inputAmount = _ref5.inputAmount;
1702
- return inputAmount;
1732
+ var routeInputAmount = _ref5.inputAmount;
1733
+ return routeInputAmount;
1703
1734
  }).reduce(function (total, cur) {
1704
1735
  return total.add(cur);
1705
- }, sdkCore.CurrencyAmount.fromRawAmount(inputCurrency, 0));
1736
+ }, sdkCore.CurrencyAmount.fromRawAmount(inputAmountCurrency, 0));
1706
1737
  this._inputAmount = totalInputFromRoutes;
1707
1738
  return this._inputAmount;
1708
1739
  }
@@ -1714,14 +1745,50 @@ var Trade = /*#__PURE__*/function () {
1714
1745
  }
1715
1746
  var outputCurrency = this.swaps[0].outputAmount.currency;
1716
1747
  var totalOutputFromRoutes = this.swaps.map(function (_ref6) {
1717
- var outputAmount = _ref6.outputAmount;
1718
- return outputAmount;
1748
+ var routeOutputAmount = _ref6.outputAmount;
1749
+ return routeOutputAmount;
1719
1750
  }).reduce(function (total, cur) {
1720
1751
  return total.add(cur);
1721
1752
  }, sdkCore.CurrencyAmount.fromRawAmount(outputCurrency, 0));
1722
1753
  this._outputAmount = totalOutputFromRoutes;
1723
1754
  return this._outputAmount;
1724
1755
  }
1756
+ /**
1757
+ * Returns the sum of all swaps within the trade
1758
+ * @returns
1759
+ * inputAmount: total input amount
1760
+ * inputAmountNative: total amount of native currency required for ETH input paths
1761
+ * - 0 if inputAmount is native but no native input paths
1762
+ * - undefined if inputAmount is not native
1763
+ * outputAmount: total output amount
1764
+ * outputAmountNative: total amount of native currency returned from ETH output paths
1765
+ * - 0 if outputAmount is native but no native output paths
1766
+ * - undefined if outputAmount is not native
1767
+ */
1768
+ }, {
1769
+ key: "amounts",
1770
+ get: function get() {
1771
+ var _this$swaps$find, _this$swaps$find2;
1772
+ // Find native currencies for reduce below
1773
+ var inputNativeCurrency = (_this$swaps$find = this.swaps.find(function (_ref7) {
1774
+ var inputAmount = _ref7.inputAmount;
1775
+ return inputAmount.currency.isNative;
1776
+ })) == null ? void 0 : _this$swaps$find.inputAmount.currency;
1777
+ var outputNativeCurrency = (_this$swaps$find2 = this.swaps.find(function (_ref8) {
1778
+ var outputAmount = _ref8.outputAmount;
1779
+ return outputAmount.currency.isNative;
1780
+ })) == null ? void 0 : _this$swaps$find2.outputAmount.currency;
1781
+ return {
1782
+ inputAmount: this.inputAmount,
1783
+ inputAmountNative: inputNativeCurrency ? this.swaps.reduce(function (total, swap) {
1784
+ return swap.route.pathInput.isNative ? total.add(swap.inputAmount) : total;
1785
+ }, sdkCore.CurrencyAmount.fromRawAmount(inputNativeCurrency, 0)) : undefined,
1786
+ outputAmount: this.outputAmount,
1787
+ outputAmountNative: outputNativeCurrency ? this.swaps.reduce(function (total, swap) {
1788
+ return swap.route.pathOutput.isNative ? total.add(swap.outputAmount) : total;
1789
+ }, sdkCore.CurrencyAmount.fromRawAmount(outputNativeCurrency, 0)) : undefined
1790
+ };
1791
+ }
1725
1792
  /**
1726
1793
  * The price expressed in terms of output amount/input amount.
1727
1794
  */
@@ -2368,8 +2435,8 @@ exports.amountWithPathCurrency = amountWithPathCurrency;
2368
2435
  exports.encodeMixedRouteToPath = encodeMixedRouteToPath;
2369
2436
  exports.getOutputOfPools = getOutputOfPools;
2370
2437
  exports.getPathCurrency = getPathCurrency;
2438
+ exports.getPathToken = getPathToken;
2371
2439
  exports.isMint = isMint;
2372
- exports.isValidTokenPath = isValidTokenPath;
2373
2440
  exports.partitionMixedRouteByProtocol = partitionMixedRouteByProtocol;
2374
2441
  exports.tradeComparator = tradeComparator;
2375
2442
  //# sourceMappingURL=router-sdk.cjs.development.js.map