@uniswap/universal-router-sdk 4.6.1 → 4.6.2
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.
- package/dist/entities/actions/uniswap.d.ts +3 -0
- package/dist/universal-router-sdk.cjs.development.js +74 -35
- package/dist/universal-router-sdk.cjs.development.js.map +1 -1
- package/dist/universal-router-sdk.cjs.production.min.js +1 -1
- package/dist/universal-router-sdk.cjs.production.min.js.map +1 -1
- package/dist/universal-router-sdk.esm.js +75 -36
- package/dist/universal-router-sdk.esm.js.map +1 -1
- package/dist/utils/getCurrencyAddress.d.ts +2 -0
- package/package.json +4 -4
|
@@ -22,6 +22,9 @@ export declare class UniswapTrade implements Command {
|
|
|
22
22
|
constructor(trade: RouterTrade<Currency, Currency, TradeType>, options: SwapOptions);
|
|
23
23
|
get isAllV4(): boolean;
|
|
24
24
|
get inputRequiresWrap(): boolean;
|
|
25
|
+
get inputRequiresUnwrap(): boolean;
|
|
26
|
+
get outputRequiresWrap(): boolean;
|
|
25
27
|
get outputRequiresUnwrap(): boolean;
|
|
28
|
+
get outputRequiresTransition(): boolean;
|
|
26
29
|
encode(planner: RoutePlanner, _config: TradeConfig): void;
|
|
27
30
|
}
|
|
@@ -383,17 +383,13 @@ function getPathCurrency(currency, pool) {
|
|
|
383
383
|
} else if (pool.involvesToken(currency.wrapped)) {
|
|
384
384
|
return currency.wrapped;
|
|
385
385
|
// return native currency if pool involves native version of wrapped currency (only applies to V4)
|
|
386
|
-
} else if (pool instanceof v4Sdk.Pool) {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
return pool.token1;
|
|
391
|
-
}
|
|
392
|
-
// otherwise the token is invalid
|
|
386
|
+
} else if (pool instanceof v4Sdk.Pool && pool.token0.wrapped.equals(currency)) {
|
|
387
|
+
return pool.token0;
|
|
388
|
+
} else if (pool instanceof v4Sdk.Pool && pool.token1.wrapped.equals(currency)) {
|
|
389
|
+
return pool.token1;
|
|
393
390
|
} else {
|
|
394
391
|
throw new Error("Expected currency " + currency.symbol + " to be either " + pool.token0.symbol + " or " + pool.token1.symbol);
|
|
395
392
|
}
|
|
396
|
-
return currency; // this line needed for typescript to compile
|
|
397
393
|
}
|
|
398
394
|
|
|
399
395
|
(function (RouterActionType) {
|
|
@@ -608,6 +604,10 @@ var E_ETH_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
|
|
|
608
604
|
var SENDER_AS_RECIPIENT = '0x0000000000000000000000000000000000000001';
|
|
609
605
|
var ROUTER_AS_RECIPIENT = '0x0000000000000000000000000000000000000002';
|
|
610
606
|
|
|
607
|
+
function getCurrencyAddress(currency) {
|
|
608
|
+
return currency.isNative ? ETH_ADDRESS : currency.wrapped.address;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
611
|
function encodeFeeBips(fee) {
|
|
612
612
|
return v3Sdk.toHex(fee.multiply(10000).quotient);
|
|
613
613
|
}
|
|
@@ -621,7 +621,11 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
621
621
|
this.options = options;
|
|
622
622
|
this.tradeType = exports.RouterActionType.UniswapTrade;
|
|
623
623
|
if (!!options.fee && !!options.flatFee) throw new Error('Only one fee option permitted');
|
|
624
|
-
if (this.inputRequiresWrap
|
|
624
|
+
if (this.inputRequiresWrap || this.inputRequiresUnwrap || this.options.useRouterBalance) {
|
|
625
|
+
this.payerIsUser = false;
|
|
626
|
+
} else {
|
|
627
|
+
this.payerIsUser = true;
|
|
628
|
+
}
|
|
625
629
|
}
|
|
626
630
|
var _proto = UniswapTrade.prototype;
|
|
627
631
|
_proto.encode = function encode(planner, _config) {
|
|
@@ -630,6 +634,10 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
630
634
|
if (this.inputRequiresWrap) {
|
|
631
635
|
// TODO: optimize if only one v2 pool we can directly send this to the pool
|
|
632
636
|
planner.addCommand(exports.CommandType.WRAP_ETH, [ROUTER_AS_RECIPIENT, this.trade.maximumAmountIn(this.options.slippageTolerance).quotient.toString()]);
|
|
637
|
+
} else if (this.inputRequiresUnwrap) {
|
|
638
|
+
// send wrapped token to router to unwrap
|
|
639
|
+
planner.addCommand(exports.CommandType.PERMIT2_TRANSFER_FROM, [this.trade.inputAmount.currency.address, ROUTER_AS_RECIPIENT, this.trade.maximumAmountIn(this.options.slippageTolerance).quotient.toString()]);
|
|
640
|
+
planner.addCommand(exports.CommandType.UNWRAP_WETH, [ROUTER_AS_RECIPIENT, 0]);
|
|
633
641
|
}
|
|
634
642
|
// The overall recipient at the end of the trade, SENDER_AS_RECIPIENT uses the msg.sender
|
|
635
643
|
this.options.recipient = (_this$options$recipie = this.options.recipient) != null ? _this$options$recipie : SENDER_AS_RECIPIENT;
|
|
@@ -638,7 +646,7 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
638
646
|
// as it's still more gas-expensive even in this case, but has benefits
|
|
639
647
|
// in that the reversion probability is lower
|
|
640
648
|
var performAggregatedSlippageCheck = this.trade.tradeType === sdkCore.TradeType.EXACT_INPUT && this.trade.routes.length > 2;
|
|
641
|
-
var routerMustCustody = performAggregatedSlippageCheck || this.
|
|
649
|
+
var routerMustCustody = performAggregatedSlippageCheck || this.outputRequiresTransition || hasFeeOption(this.options);
|
|
642
650
|
for (var _iterator = _createForOfIteratorHelperLoose(this.trade.swaps), _step; !(_step = _iterator()).done;) {
|
|
643
651
|
var swap = _step.value;
|
|
644
652
|
switch (swap.route.protocol) {
|
|
@@ -661,11 +669,13 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
661
669
|
var minimumAmountOut = ethers.BigNumber.from(this.trade.minimumAmountOut(this.options.slippageTolerance).quotient.toString());
|
|
662
670
|
// The router custodies for 3 reasons: to unwrap, to take a fee, and/or to do a slippage check
|
|
663
671
|
if (routerMustCustody) {
|
|
672
|
+
var pools = this.trade.swaps[0].route.pools;
|
|
673
|
+
var pathOutputCurrencyAddress = getCurrencyAddress(getPathCurrency(this.trade.outputAmount.currency, pools[pools.length - 1]));
|
|
664
674
|
// If there is a fee, that percentage is sent to the fee recipient
|
|
665
675
|
// In the case where ETH is the output currency, the fee is taken in WETH (for gas reasons)
|
|
666
676
|
if (!!this.options.fee) {
|
|
667
677
|
var feeBips = encodeFeeBips(this.options.fee.fee);
|
|
668
|
-
planner.addCommand(exports.CommandType.PAY_PORTION, [
|
|
678
|
+
planner.addCommand(exports.CommandType.PAY_PORTION, [pathOutputCurrencyAddress, this.options.fee.recipient, feeBips]);
|
|
669
679
|
// If the trade is exact output, and a fee was taken, we must adjust the amount out to be the amount after the fee
|
|
670
680
|
// Otherwise we continue as expected with the trade's normal expected output
|
|
671
681
|
if (this.trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT) {
|
|
@@ -677,7 +687,7 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
677
687
|
if (!!this.options.flatFee) {
|
|
678
688
|
var feeAmount = this.options.flatFee.amount;
|
|
679
689
|
if (minimumAmountOut.lt(feeAmount)) throw new Error('Flat fee amount greater than minimumAmountOut');
|
|
680
|
-
planner.addCommand(exports.CommandType.TRANSFER, [
|
|
690
|
+
planner.addCommand(exports.CommandType.TRANSFER, [pathOutputCurrencyAddress, this.options.flatFee.recipient, feeAmount]);
|
|
681
691
|
// If the trade is exact output, and a fee was taken, we must adjust the amount out to be the amount after the fee
|
|
682
692
|
// Otherwise we continue as expected with the trade's normal expected output
|
|
683
693
|
if (this.trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT) {
|
|
@@ -688,14 +698,23 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
688
698
|
// by this if-else clause.
|
|
689
699
|
if (this.outputRequiresUnwrap) {
|
|
690
700
|
planner.addCommand(exports.CommandType.UNWRAP_WETH, [this.options.recipient, minimumAmountOut]);
|
|
701
|
+
} else if (this.outputRequiresWrap) {
|
|
702
|
+
planner.addCommand(exports.CommandType.WRAP_ETH, [this.options.recipient, CONTRACT_BALANCE]);
|
|
691
703
|
} else {
|
|
692
|
-
planner.addCommand(exports.CommandType.SWEEP, [this.trade.outputAmount.currency
|
|
704
|
+
planner.addCommand(exports.CommandType.SWEEP, [getCurrencyAddress(this.trade.outputAmount.currency), this.options.recipient, minimumAmountOut]);
|
|
693
705
|
}
|
|
694
706
|
}
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
707
|
+
// for exactOutput swaps with native input or that perform an inputToken transition (wrap or unwrap)
|
|
708
|
+
// we need to send back the change to the user
|
|
709
|
+
if (this.trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT || riskOfPartialFill(this.trade)) {
|
|
710
|
+
if (this.inputRequiresWrap) {
|
|
711
|
+
planner.addCommand(exports.CommandType.UNWRAP_WETH, [this.options.recipient, 0]);
|
|
712
|
+
} else if (this.inputRequiresUnwrap) {
|
|
713
|
+
planner.addCommand(exports.CommandType.WRAP_ETH, [this.options.recipient, CONTRACT_BALANCE]);
|
|
714
|
+
} else if (this.trade.inputAmount.currency.isNative) {
|
|
715
|
+
// must refund extra native currency sent along for native v4 trades (no input transition)
|
|
716
|
+
planner.addCommand(exports.CommandType.SWEEP, [ETH_ADDRESS, this.options.recipient, 0]);
|
|
717
|
+
}
|
|
699
718
|
}
|
|
700
719
|
if (this.options.safeMode) planner.addCommand(exports.CommandType.SWEEP, [ETH_ADDRESS, this.options.recipient, 0]);
|
|
701
720
|
};
|
|
@@ -712,24 +731,42 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
712
731
|
}, {
|
|
713
732
|
key: "inputRequiresWrap",
|
|
714
733
|
get: function get() {
|
|
715
|
-
if (
|
|
716
|
-
return this.trade.inputAmount.currency.isNative;
|
|
734
|
+
if (this.isAllV4) {
|
|
735
|
+
return this.trade.inputAmount.currency.isNative && !this.trade.swaps[0].route.pathInput.isNative;
|
|
717
736
|
} else {
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
737
|
+
return this.trade.inputAmount.currency.isNative;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}, {
|
|
741
|
+
key: "inputRequiresUnwrap",
|
|
742
|
+
get: function get() {
|
|
743
|
+
if (this.isAllV4) {
|
|
744
|
+
return !this.trade.inputAmount.currency.isNative && this.trade.swaps[0].route.pathInput.isNative;
|
|
721
745
|
}
|
|
746
|
+
return false;
|
|
747
|
+
}
|
|
748
|
+
}, {
|
|
749
|
+
key: "outputRequiresWrap",
|
|
750
|
+
get: function get() {
|
|
751
|
+
if (this.isAllV4) {
|
|
752
|
+
return !this.trade.outputAmount.currency.isNative && this.trade.swaps[0].route.pathOutput.isNative;
|
|
753
|
+
}
|
|
754
|
+
return false;
|
|
722
755
|
}
|
|
723
756
|
}, {
|
|
724
757
|
key: "outputRequiresUnwrap",
|
|
725
758
|
get: function get() {
|
|
726
|
-
if (
|
|
727
|
-
return this.trade.outputAmount.currency.isNative;
|
|
759
|
+
if (this.isAllV4) {
|
|
760
|
+
return this.trade.outputAmount.currency.isNative && !this.trade.swaps[0].route.pathOutput.isNative;
|
|
728
761
|
} else {
|
|
729
|
-
|
|
730
|
-
return this.trade.outputAmount.currency.isNative && !this.trade.swaps[0].route.output.isNative;
|
|
762
|
+
return this.trade.outputAmount.currency.isNative;
|
|
731
763
|
}
|
|
732
764
|
}
|
|
765
|
+
}, {
|
|
766
|
+
key: "outputRequiresTransition",
|
|
767
|
+
get: function get() {
|
|
768
|
+
return this.outputRequiresWrap || this.outputRequiresUnwrap;
|
|
769
|
+
}
|
|
733
770
|
}]);
|
|
734
771
|
return UniswapTrade;
|
|
735
772
|
}();
|
|
@@ -773,23 +810,25 @@ function addV3Swap(planner, _ref2, tradeType, options, payerIsUser, routerMustCu
|
|
|
773
810
|
}
|
|
774
811
|
function addV4Swap(planner, _ref3, tradeType, options, payerIsUser, routerMustCustody) {
|
|
775
812
|
var _options$recipient;
|
|
776
|
-
var
|
|
777
|
-
|
|
778
|
-
|
|
813
|
+
var inputAmount = _ref3.inputAmount,
|
|
814
|
+
outputAmount = _ref3.outputAmount,
|
|
815
|
+
route = _ref3.route;
|
|
816
|
+
// create a deep copy of pools since v4Planner encoding tampers with array
|
|
817
|
+
var pools = route.pools.map(function (p) {
|
|
818
|
+
return p;
|
|
819
|
+
});
|
|
820
|
+
var v4Route = new v4Sdk.Route(pools, inputAmount.currency, outputAmount.currency);
|
|
779
821
|
var trade = v4Sdk.Trade.createUncheckedTrade({
|
|
780
|
-
route:
|
|
822
|
+
route: v4Route,
|
|
781
823
|
inputAmount: inputAmount,
|
|
782
824
|
outputAmount: outputAmount,
|
|
783
825
|
tradeType: tradeType
|
|
784
826
|
});
|
|
785
827
|
var slippageToleranceOnSwap = routerMustCustody && tradeType == sdkCore.TradeType.EXACT_INPUT ? undefined : options.slippageTolerance;
|
|
786
|
-
var inputWethFromRouter = inputAmount.currency.isNative && !route.input.isNative;
|
|
787
|
-
if (inputWethFromRouter && !payerIsUser) throw new Error('Inconsistent payer');
|
|
788
828
|
var v4Planner = new v4Sdk.V4Planner();
|
|
789
829
|
v4Planner.addTrade(trade, slippageToleranceOnSwap);
|
|
790
|
-
v4Planner.addSettle(
|
|
791
|
-
|
|
792
|
-
v4Planner.addTake(outputAmount.currency, routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient);
|
|
830
|
+
v4Planner.addSettle(trade.route.pathInput, payerIsUser);
|
|
831
|
+
v4Planner.addTake(trade.route.pathOutput, routerMustCustody ? ROUTER_AS_RECIPIENT : (_options$recipient = options.recipient) != null ? _options$recipient : SENDER_AS_RECIPIENT);
|
|
793
832
|
planner.addCommand(exports.CommandType.V4_SWAP, [v4Planner.finalize()]);
|
|
794
833
|
}
|
|
795
834
|
// encode a mixed route swap, i.e. including both v2 and v3 pools
|