@uniswap/universal-router-sdk 4.29.3 → 4.29.4

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.
@@ -21,15 +21,6 @@ export declare class UniswapTrade implements Command {
21
21
  readonly payerIsUser: boolean;
22
22
  constructor(trade: RouterTrade<Currency, Currency, TradeType>, options: SwapOptions);
23
23
  get isAllV4(): boolean;
24
- /**
25
- * Checks if any route in a split trade ends with an ETH-WETH pool.
26
- *
27
- * When a split trade has routes ending in ETH-WETH pools alongside routes ending in WETH,
28
- * V4 swaps that are last in their route must TAKE WETH (not ETH) at the endfor consistency.
29
- * This ensures all routes accumulate WETH, which can then be unwrapped together to ETH
30
- * in a single operation at the end.
31
- */
32
- get shouldForceV4UnwrapForSplitNativeOutput(): boolean;
33
24
  get inputRequiresWrap(): boolean;
34
25
  get inputRequiresUnwrap(): boolean;
35
26
  get outputRequiresWrap(): boolean;
@@ -703,10 +703,10 @@ var UniswapTrade = /*#__PURE__*/function () {
703
703
  addV3Swap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody);
704
704
  break;
705
705
  case routerSdk.Protocol.V4:
706
- addV4Swap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody, this.shouldForceV4UnwrapForSplitNativeOutput);
706
+ addV4Swap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody);
707
707
  break;
708
708
  case routerSdk.Protocol.MIXED:
709
- addMixedSwap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody, this.shouldForceV4UnwrapForSplitNativeOutput);
709
+ addMixedSwap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody);
710
710
  break;
711
711
  default:
712
712
  throw new Error('UNSUPPORTED_TRADE_PROTOCOL');
@@ -774,44 +774,6 @@ var UniswapTrade = /*#__PURE__*/function () {
774
774
  }
775
775
  return result;
776
776
  }
777
- /**
778
- * Checks if any route in a split trade ends with an ETH-WETH pool.
779
- *
780
- * When a split trade has routes ending in ETH-WETH pools alongside routes ending in WETH,
781
- * V4 swaps that are last in their route must TAKE WETH (not ETH) at the endfor consistency.
782
- * This ensures all routes accumulate WETH, which can then be unwrapped together to ETH
783
- * in a single operation at the end.
784
- */
785
- }, {
786
- key: "shouldForceV4UnwrapForSplitNativeOutput",
787
- get: function get() {
788
- // Only relevant if output is native and we have split routes
789
- if (!this.trade.outputAmount.currency.isNative || this.trade.swaps.length <= 1) {
790
- return false;
791
- }
792
- for (var _iterator3 = _createForOfIteratorHelperLoose(this.trade.swaps), _step3; !(_step3 = _iterator3()).done;) {
793
- var swap = _step3.value;
794
- if (swap.route.protocol === routerSdk.Protocol.V4) {
795
- var v4Route = swap.route;
796
- var lastPool = v4Route.pools[v4Route.pools.length - 1];
797
- var isEthWethPool = lastPool.currency1.equals(lastPool.currency0.wrapped);
798
- if (isEthWethPool) {
799
- return true;
800
- }
801
- } else if (swap.route.protocol === routerSdk.Protocol.MIXED) {
802
- var mixedRoute = swap.route;
803
- var _lastPool = mixedRoute.pools[mixedRoute.pools.length - 1];
804
- // Check if the last pool in the mixed route is a V4 ETH-WETH pool
805
- if (_lastPool instanceof v4Sdk.Pool) {
806
- var _isEthWethPool = _lastPool.currency1.equals(_lastPool.currency0.wrapped);
807
- if (_isEthWethPool) {
808
- return true;
809
- }
810
- }
811
- }
812
- }
813
- return false;
814
- }
815
777
  // this.trade.swaps is an array of swaps / trades.
816
778
  // we are iterating over one swap (trade) at a time so length is 1
817
779
  // route is either v2, v3, v4, or mixed
@@ -854,7 +816,21 @@ var UniswapTrade = /*#__PURE__*/function () {
854
816
  // if last pool is v4:
855
817
  if (lastPool instanceof v4Sdk.Pool) {
856
818
  // If output currency is not native but path currency output is native, we need to wrap
857
- return !this.trade.outputAmount.currency.isNative && lastRoute.pathOutput.isNative;
819
+ if (!this.trade.outputAmount.currency.isNative) {
820
+ if (lastRoute.pathOutput.isNative) {
821
+ // this means path output is native and we need to wrap
822
+ return true;
823
+ } else if (lastPool.currency1.equals(lastPool.currency0.wrapped) && lastRoute.pools.length > 1) {
824
+ var poolBefore = lastRoute.pools[lastRoute.pools.length - 2];
825
+ // this means last pool is eth-weth and pool before contains weth
826
+ if (poolBefore instanceof v4Sdk.Pool && (poolBefore.currency0.equals(lastPool.currency1) || poolBefore.currency1.equals(lastPool.currency1))) {
827
+ return true;
828
+ } else if (poolBefore.token0.equals(lastPool.currency1) || poolBefore.token1.equals(lastPool.currency1)) {
829
+ // same for v2 and v3 pools
830
+ return true;
831
+ }
832
+ }
833
+ }
858
834
  }
859
835
  // if last pool is not v4:
860
836
  // we do not need to wrap because v2 and v3 pools already require wrapped tokens
@@ -863,18 +839,23 @@ var UniswapTrade = /*#__PURE__*/function () {
863
839
  }, {
864
840
  key: "outputRequiresUnwrap",
865
841
  get: function get() {
866
- // If output is ETH and any V4 route has ETH-WETH last pool,
867
- // we force ALL V4 routes to take WETH, so we need to unwrap at the end
868
- if (this.shouldForceV4UnwrapForSplitNativeOutput) {
869
- return true;
870
- }
871
842
  var swap = this.trade.swaps[0];
872
843
  var lastRoute = swap.route;
873
844
  var lastPool = lastRoute.pools[lastRoute.pools.length - 1];
874
845
  // if last pool is v4:
875
846
  if (lastPool instanceof v4Sdk.Pool) {
876
847
  // If output currency is native and path currency output is not native, we need to unwrap
877
- return this.trade.outputAmount.currency.isNative && !this.trade.swaps[0].route.pathOutput.isNative;
848
+ if (this.trade.outputAmount.currency.isNative) {
849
+ if (!this.trade.swaps[0].route.pathOutput.isNative) {
850
+ // this means path output is weth and we need to unwrap
851
+ return true;
852
+ } else if (lastRoute.pools.length > 1 && lastRoute.pools[lastRoute.pools.length - 2] instanceof v4Sdk.Pool && lastRoute.pools[lastRoute.pools.length - 2].currency0.isNative && lastPool.currency1.equals(lastPool.currency0.wrapped)) {
853
+ // this means last pool is eth-weth and we need to unwrap
854
+ return true;
855
+ } else {
856
+ return false;
857
+ }
858
+ }
878
859
  }
879
860
  // else: if path output currency is native, we need to unwrap because v2 and v3 pools already require wrapped tokens
880
861
  return this.trade.outputAmount.currency.isNative;
@@ -924,14 +905,11 @@ function addV3Swap(planner, _ref2, tradeType, options, payerIsUser, routerMustCu
924
905
  planner.addCommand(exports.CommandType.V3_SWAP_EXACT_OUT, [routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient, trade.minimumAmountOut(options.slippageTolerance).quotient.toString(), trade.maximumAmountIn(options.slippageTolerance).quotient.toString(), path, payerIsUser]);
925
906
  }
926
907
  }
927
- function addV4Swap(planner, _ref3, tradeType, options, payerIsUser, routerMustCustody, shouldForceV4UnwrapForSplitNativeOutput) {
908
+ function addV4Swap(planner, _ref3, tradeType, options, payerIsUser, routerMustCustody) {
928
909
  var _options$recipient;
929
910
  var inputAmount = _ref3.inputAmount,
930
911
  outputAmount = _ref3.outputAmount,
931
912
  route = _ref3.route;
932
- if (shouldForceV4UnwrapForSplitNativeOutput === void 0) {
933
- shouldForceV4UnwrapForSplitNativeOutput = false;
934
- }
935
913
  // create a deep copy of pools since v4Planner encoding tampers with array
936
914
  var pools = route.pools.map(function (p) {
937
915
  return p;
@@ -947,21 +925,26 @@ function addV4Swap(planner, _ref3, tradeType, options, payerIsUser, routerMustCu
947
925
  var v4Planner = new v4Sdk.V4Planner();
948
926
  v4Planner.addTrade(trade, slippageToleranceOnSwap);
949
927
  v4Planner.addSettle(trade.route.pathInput, payerIsUser);
950
- // If any V4 route in split trades has an ETH-WETH last pool and output is ETH,
951
- // ALL V4 routes should use WETH for taking to ensure consistency and allow single unwrap
928
+ // Handle split route output consistency:
929
+ // - If output is ETH and some routes output WETH: force all to output WETH, then unwrap
930
+ // - If output is WETH and some routes output ETH: force all to output ETH, then wrap
952
931
  var pathOutputForTake = trade.route.pathOutput;
953
- if (shouldForceV4UnwrapForSplitNativeOutput) {
954
- pathOutputForTake = pathOutputForTake.wrapped;
932
+ var lastPool = v4Route.pools[v4Route.pools.length - 1];
933
+ var ethWethPool = lastPool.currency1.equals(lastPool.currency0.wrapped);
934
+ if (ethWethPool && v4Route.pools.length > 1) {
935
+ var poolBefore = v4Route.pools[v4Route.pools.length - 2];
936
+ if (pathOutputForTake.isNative && poolBefore.currency0.isNative) {
937
+ pathOutputForTake = pathOutputForTake.wrapped;
938
+ } else if (!pathOutputForTake.isNative && (poolBefore.currency0.equals(lastPool.currency1) || poolBefore.currency1.equals(lastPool.currency1))) {
939
+ pathOutputForTake = lastPool.currency0;
940
+ }
955
941
  }
956
942
  v4Planner.addTake(pathOutputForTake, routerMustCustody ? ROUTER_AS_RECIPIENT : (_options$recipient = options.recipient) != null ? _options$recipient : SENDER_AS_RECIPIENT);
957
943
  planner.addCommand(exports.CommandType.V4_SWAP, [v4Planner.finalize()]);
958
944
  }
959
945
  // encode a mixed route swap, i.e. including both v2 and v3 pools
960
- function addMixedSwap(planner, swap, tradeType, options, payerIsUser, routerMustCustody, shouldForceV4UnwrapForSplitNativeOutput) {
946
+ function addMixedSwap(planner, swap, tradeType, options, payerIsUser, routerMustCustody) {
961
947
  var _options$recipient2;
962
- if (shouldForceV4UnwrapForSplitNativeOutput === void 0) {
963
- shouldForceV4UnwrapForSplitNativeOutput = false;
964
- }
965
948
  var route = swap.route;
966
949
  var inputAmount = swap.inputAmount;
967
950
  var outputAmount = swap.outputAmount;
@@ -969,7 +952,7 @@ function addMixedSwap(planner, swap, tradeType, options, payerIsUser, routerMust
969
952
  // single hop, so it can be reduced to plain swap logic for one protocol version
970
953
  if (route.pools.length === 1) {
971
954
  if (route.pools[0] instanceof v4Sdk.Pool) {
972
- return addV4Swap(planner, swap, tradeType, options, payerIsUser, routerMustCustody, shouldForceV4UnwrapForSplitNativeOutput);
955
+ return addV4Swap(planner, swap, tradeType, options, payerIsUser, routerMustCustody);
973
956
  } else if (route.pools[0] instanceof v3Sdk.Pool) {
974
957
  return addV3Swap(planner, swap, tradeType, options, payerIsUser, routerMustCustody);
975
958
  } else if (route.pools[0] instanceof v2Sdk.Pair) {
@@ -1019,11 +1002,20 @@ function addMixedSwap(planner, swap, tradeType, options, payerIsUser, routerMust
1019
1002
  amountIn: 0,
1020
1003
  amountOutMinimum: !isLastSectionInRoute(i) ? 0 : amountOut
1021
1004
  }]);
1022
- // If any V4 route has ETH-WETH last pool and this is last section outputting ETH,
1023
- // use WETH instead for consistency
1005
+ // Handle split route output consistency for V4 sections in mixed routes
1024
1006
  var outputTokenForTake = outputToken;
1025
- if (shouldForceV4UnwrapForSplitNativeOutput && isLastSectionInRoute(i)) {
1026
- outputTokenForTake = outputToken.wrapped;
1007
+ if (isLastSectionInRoute(i)) {
1008
+ var lastPool = route.pools[route.pools.length - 1];
1009
+ var v4Pool = lastPool instanceof v4Sdk.Pool;
1010
+ var ethWethPool = v4Pool ? lastPool.currency1.equals(lastPool.currency0.wrapped) : false;
1011
+ var poolBefore = route.pools[route.pools.length - 2];
1012
+ if (ethWethPool) {
1013
+ if (outputToken.isNative && poolBefore.token0.isNative) {
1014
+ outputTokenForTake = outputToken.wrapped;
1015
+ } else if (!outputToken.isNative && (poolBefore.token0.equals(lastPool.token1) || poolBefore.token1.equals(lastPool.token1))) {
1016
+ outputTokenForTake = lastPool.token0;
1017
+ }
1018
+ }
1027
1019
  }
1028
1020
  v4Planner.addTake(outputTokenForTake, swapRecipient);
1029
1021
  planner.addCommand(exports.CommandType.V4_SWAP, [v4Planner.finalize()]);