@uniswap/router-sdk 1.22.3 → 2.0.0
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/{approveAndCall.d.ts → cjs/src/approveAndCall.d.ts} +33 -33
- package/dist/cjs/src/approveAndCall.js +111 -0
- package/dist/cjs/src/approveAndCall.js.map +1 -0
- package/dist/{constants.d.ts → cjs/src/constants.d.ts} +13 -13
- package/dist/cjs/src/constants.js +22 -0
- package/dist/cjs/src/constants.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/mixedRoute/route.d.ts +29 -29
- package/dist/cjs/src/entities/mixedRoute/route.js +119 -0
- package/dist/cjs/src/entities/mixedRoute/route.js.map +1 -0
- package/dist/cjs/src/entities/mixedRoute/route.test.d.ts +1 -0
- package/dist/cjs/src/entities/mixedRoute/route.test.js +469 -0
- package/dist/cjs/src/entities/mixedRoute/route.test.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/mixedRoute/trade.d.ts +183 -183
- package/dist/cjs/src/entities/mixedRoute/trade.js +357 -0
- package/dist/cjs/src/entities/mixedRoute/trade.js.map +1 -0
- package/dist/cjs/src/entities/mixedRoute/trade.test.d.ts +1 -0
- package/dist/cjs/src/entities/mixedRoute/trade.test.js +1010 -0
- package/dist/cjs/src/entities/mixedRoute/trade.test.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/protocol.d.ts +6 -6
- package/dist/cjs/src/entities/protocol.js +11 -0
- package/dist/cjs/src/entities/protocol.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/route.d.ts +40 -40
- package/dist/cjs/src/entities/route.js +63 -0
- package/dist/cjs/src/entities/route.js.map +1 -0
- package/dist/cjs/src/entities/route.test.d.ts +1 -0
- package/dist/cjs/src/entities/route.test.js +192 -0
- package/dist/cjs/src/entities/route.test.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/trade.d.ts +127 -127
- package/dist/cjs/src/entities/trade.js +366 -0
- package/dist/cjs/src/entities/trade.js.map +1 -0
- package/dist/cjs/src/entities/trade.test.d.ts +1 -0
- package/dist/cjs/src/entities/trade.test.js +1267 -0
- package/dist/cjs/src/entities/trade.test.js.map +1 -0
- package/dist/{index.d.ts → cjs/src/index.d.ts} +14 -14
- package/dist/cjs/src/index.js +18 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/{multicallExtended.d.ts → cjs/src/multicallExtended.d.ts} +11 -11
- package/dist/cjs/src/multicallExtended.js +43 -0
- package/dist/cjs/src/multicallExtended.js.map +1 -0
- package/dist/cjs/src/multicallExtended.test.d.ts +1 -0
- package/dist/cjs/src/multicallExtended.test.js +22 -0
- package/dist/cjs/src/multicallExtended.test.js.map +1 -0
- package/dist/{paymentsExtended.d.ts → cjs/src/paymentsExtended.d.ts} +15 -15
- package/dist/cjs/src/paymentsExtended.js +65 -0
- package/dist/cjs/src/paymentsExtended.js.map +1 -0
- package/dist/cjs/src/paymentsExtended.test.d.ts +1 -0
- package/dist/cjs/src/paymentsExtended.test.js +60 -0
- package/dist/cjs/src/paymentsExtended.test.js.map +1 -0
- package/dist/{swapRouter.d.ts → cjs/src/swapRouter.d.ts} +95 -95
- package/dist/cjs/src/swapRouter.js +439 -0
- package/dist/cjs/src/swapRouter.js.map +1 -0
- package/dist/cjs/src/swapRouter.test.d.ts +1 -0
- package/dist/cjs/src/swapRouter.test.js +1161 -0
- package/dist/cjs/src/swapRouter.test.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/TPool.d.ts +4 -4
- package/dist/cjs/src/utils/TPool.js +3 -0
- package/dist/cjs/src/utils/TPool.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/encodeMixedRouteToPath.d.ts +9 -9
- package/dist/cjs/src/utils/encodeMixedRouteToPath.js +90 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.js.map +1 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.test.d.ts +1 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.test.js +136 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.test.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/index.d.ts +16 -16
- package/dist/cjs/src/utils/index.js +51 -0
- package/dist/cjs/src/utils/index.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/pathCurrency.d.ts +4 -4
- package/dist/cjs/src/utils/pathCurrency.js +35 -0
- package/dist/cjs/src/utils/pathCurrency.js.map +1 -0
- package/dist/cjs/src/utils/pathCurrency.test.d.ts +1 -0
- package/dist/cjs/src/utils/pathCurrency.test.js +17 -0
- package/dist/cjs/src/utils/pathCurrency.test.js.map +1 -0
- package/dist/esm/src/approveAndCall.d.ts +33 -0
- package/dist/esm/src/approveAndCall.js +105 -0
- package/dist/esm/src/approveAndCall.js.map +1 -0
- package/dist/esm/src/constants.d.ts +13 -0
- package/dist/esm/src/constants.js +18 -0
- package/dist/esm/src/constants.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/route.d.ts +29 -0
- package/dist/esm/src/entities/mixedRoute/route.js +114 -0
- package/dist/esm/src/entities/mixedRoute/route.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/route.test.d.ts +1 -0
- package/dist/esm/src/entities/mixedRoute/route.test.js +467 -0
- package/dist/esm/src/entities/mixedRoute/route.test.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/trade.d.ts +183 -0
- package/dist/esm/src/entities/mixedRoute/trade.js +352 -0
- package/dist/esm/src/entities/mixedRoute/trade.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/trade.test.d.ts +1 -0
- package/dist/esm/src/entities/mixedRoute/trade.test.js +1008 -0
- package/dist/esm/src/entities/mixedRoute/trade.test.js.map +1 -0
- package/dist/esm/src/entities/protocol.d.ts +6 -0
- package/dist/esm/src/entities/protocol.js +8 -0
- package/dist/esm/src/entities/protocol.js.map +1 -0
- package/dist/esm/src/entities/route.d.ts +40 -0
- package/dist/esm/src/entities/route.js +55 -0
- package/dist/esm/src/entities/route.js.map +1 -0
- package/dist/esm/src/entities/route.test.d.ts +1 -0
- package/dist/esm/src/entities/route.test.js +190 -0
- package/dist/esm/src/entities/route.test.js.map +1 -0
- package/dist/esm/src/entities/trade.d.ts +127 -0
- package/dist/esm/src/entities/trade.js +362 -0
- package/dist/esm/src/entities/trade.js.map +1 -0
- package/dist/esm/src/entities/trade.test.d.ts +1 -0
- package/dist/esm/src/entities/trade.test.js +1265 -0
- package/dist/esm/src/entities/trade.test.js.map +1 -0
- package/dist/esm/src/index.d.ts +14 -0
- package/dist/esm/src/index.js +15 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/multicallExtended.d.ts +11 -0
- package/dist/esm/src/multicallExtended.js +39 -0
- package/dist/esm/src/multicallExtended.js.map +1 -0
- package/dist/esm/src/multicallExtended.test.d.ts +1 -0
- package/dist/esm/src/multicallExtended.test.js +20 -0
- package/dist/esm/src/multicallExtended.test.js.map +1 -0
- package/dist/esm/src/paymentsExtended.d.ts +15 -0
- package/dist/esm/src/paymentsExtended.js +61 -0
- package/dist/esm/src/paymentsExtended.js.map +1 -0
- package/dist/esm/src/paymentsExtended.test.d.ts +1 -0
- package/dist/esm/src/paymentsExtended.test.js +57 -0
- package/dist/esm/src/paymentsExtended.test.js.map +1 -0
- package/dist/esm/src/swapRouter.d.ts +95 -0
- package/dist/esm/src/swapRouter.js +434 -0
- package/dist/esm/src/swapRouter.js.map +1 -0
- package/dist/esm/src/swapRouter.test.d.ts +1 -0
- package/dist/esm/src/swapRouter.test.js +1159 -0
- package/dist/esm/src/swapRouter.test.js.map +1 -0
- package/dist/esm/src/utils/TPool.d.ts +4 -0
- package/dist/esm/src/utils/TPool.js +2 -0
- package/dist/esm/src/utils/TPool.js.map +1 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.d.ts +9 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.js +86 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.js.map +1 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.test.d.ts +1 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.test.js +134 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.test.js.map +1 -0
- package/dist/esm/src/utils/index.d.ts +16 -0
- package/dist/esm/src/utils/index.js +46 -0
- package/dist/esm/src/utils/index.js.map +1 -0
- package/dist/esm/src/utils/pathCurrency.d.ts +4 -0
- package/dist/esm/src/utils/pathCurrency.js +30 -0
- package/dist/esm/src/utils/pathCurrency.js.map +1 -0
- package/dist/esm/src/utils/pathCurrency.test.d.ts +1 -0
- package/dist/esm/src/utils/pathCurrency.test.js +15 -0
- package/dist/esm/src/utils/pathCurrency.test.js.map +1 -0
- package/dist/types/src/approveAndCall.d.ts +33 -0
- package/dist/types/src/constants.d.ts +13 -0
- package/dist/types/src/entities/mixedRoute/route.d.ts +29 -0
- package/dist/types/src/entities/mixedRoute/route.test.d.ts +1 -0
- package/dist/types/src/entities/mixedRoute/trade.d.ts +183 -0
- package/dist/types/src/entities/mixedRoute/trade.test.d.ts +1 -0
- package/dist/types/src/entities/protocol.d.ts +6 -0
- package/dist/types/src/entities/route.d.ts +40 -0
- package/dist/types/src/entities/route.test.d.ts +1 -0
- package/dist/types/src/entities/trade.d.ts +127 -0
- package/dist/types/src/entities/trade.test.d.ts +1 -0
- package/dist/types/src/index.d.ts +14 -0
- package/dist/types/src/multicallExtended.d.ts +11 -0
- package/dist/types/src/multicallExtended.test.d.ts +1 -0
- package/dist/types/src/paymentsExtended.d.ts +15 -0
- package/dist/types/src/paymentsExtended.test.d.ts +1 -0
- package/dist/types/src/swapRouter.d.ts +95 -0
- package/dist/types/src/swapRouter.test.d.ts +1 -0
- package/dist/types/src/utils/TPool.d.ts +4 -0
- package/dist/types/src/utils/encodeMixedRouteToPath.d.ts +9 -0
- package/dist/types/src/utils/encodeMixedRouteToPath.test.d.ts +1 -0
- package/dist/types/src/utils/index.d.ts +16 -0
- package/dist/types/src/utils/pathCurrency.d.ts +4 -0
- package/dist/types/src/utils/pathCurrency.test.d.ts +1 -0
- package/package.json +33 -18
- package/dist/index.js +0 -8
- package/dist/router-sdk.cjs.development.js +0 -2485
- package/dist/router-sdk.cjs.development.js.map +0 -1
- package/dist/router-sdk.cjs.production.min.js +0 -2
- package/dist/router-sdk.cjs.production.min.js.map +0 -1
- package/dist/router-sdk.esm.js +0 -2452
- package/dist/router-sdk.esm.js.map +0 -1
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import { Interface } from '@ethersproject/abi';
|
|
2
|
-
import { Currency, Percent, TradeType } from '@uniswap/sdk-core';
|
|
3
|
-
import { Trade as V2Trade } from '@uniswap/v2-sdk';
|
|
4
|
-
import { FeeOptions, MethodParameters, PermitOptions, Position, Trade as V3Trade } from '@uniswap/v3-sdk';
|
|
5
|
-
import { ApprovalTypes, CondensedAddLiquidityOptions } from './approveAndCall';
|
|
6
|
-
import { Trade } from './entities/trade';
|
|
7
|
-
import { Validation } from './multicallExtended';
|
|
8
|
-
import { MixedRouteTrade } from './entities/mixedRoute/trade';
|
|
9
|
-
/**
|
|
10
|
-
* Options for producing the arguments to send calls to the router.
|
|
11
|
-
*/
|
|
12
|
-
export interface SwapOptions {
|
|
13
|
-
/**
|
|
14
|
-
* How much the execution price is allowed to move unfavorably from the trade execution price.
|
|
15
|
-
*/
|
|
16
|
-
slippageTolerance: Percent;
|
|
17
|
-
/**
|
|
18
|
-
* The account that should receive the output. If omitted, output is sent to msg.sender.
|
|
19
|
-
*/
|
|
20
|
-
recipient?: string;
|
|
21
|
-
/**
|
|
22
|
-
* Either deadline (when the transaction expires, in epoch seconds), or previousBlockhash.
|
|
23
|
-
*/
|
|
24
|
-
deadlineOrPreviousBlockhash?: Validation;
|
|
25
|
-
/**
|
|
26
|
-
* The optional permit parameters for spending the input.
|
|
27
|
-
*/
|
|
28
|
-
inputTokenPermit?: PermitOptions;
|
|
29
|
-
/**
|
|
30
|
-
* Optional information for taking a fee on output.
|
|
31
|
-
*/
|
|
32
|
-
fee?: FeeOptions;
|
|
33
|
-
}
|
|
34
|
-
export interface SwapAndAddOptions extends SwapOptions {
|
|
35
|
-
/**
|
|
36
|
-
* The optional permit parameters for pulling in remaining output token.
|
|
37
|
-
*/
|
|
38
|
-
outputTokenPermit?: PermitOptions;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
|
|
43
|
-
*/
|
|
44
|
-
export declare abstract class SwapRouter {
|
|
45
|
-
static INTERFACE: Interface;
|
|
46
|
-
/**
|
|
47
|
-
* Cannot be constructed.
|
|
48
|
-
*/
|
|
49
|
-
private constructor();
|
|
50
|
-
/**
|
|
51
|
-
* @notice Generates the calldata for a Swap with a V2 Route.
|
|
52
|
-
* @param trade The V2Trade to encode.
|
|
53
|
-
* @param options SwapOptions to use for the trade.
|
|
54
|
-
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
55
|
-
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
56
|
-
* @returns A string array of calldatas for the trade.
|
|
57
|
-
*/
|
|
58
|
-
private static encodeV2Swap;
|
|
59
|
-
/**
|
|
60
|
-
* @notice Generates the calldata for a Swap with a V3 Route.
|
|
61
|
-
* @param trade The V3Trade to encode.
|
|
62
|
-
* @param options SwapOptions to use for the trade.
|
|
63
|
-
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
64
|
-
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
65
|
-
* @returns A string array of calldatas for the trade.
|
|
66
|
-
*/
|
|
67
|
-
private static encodeV3Swap;
|
|
68
|
-
/**
|
|
69
|
-
* @notice Generates the calldata for a MixedRouteSwap. Since single hop routes are not MixedRoutes, we will instead generate
|
|
70
|
-
* them via the existing encodeV3Swap and encodeV2Swap methods.
|
|
71
|
-
* @param trade The MixedRouteTrade to encode.
|
|
72
|
-
* @param options SwapOptions to use for the trade.
|
|
73
|
-
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
74
|
-
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
75
|
-
* @returns A string array of calldatas for the trade.
|
|
76
|
-
*/
|
|
77
|
-
private static encodeMixedRouteSwap;
|
|
78
|
-
private static encodeSwaps;
|
|
79
|
-
/**
|
|
80
|
-
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
81
|
-
* @param trades to produce call parameters for
|
|
82
|
-
* @param options options for the call parameters
|
|
83
|
-
*/
|
|
84
|
-
static swapCallParameters(trades: Trade<Currency, Currency, TradeType> | V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType> | (V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType>)[], options: SwapOptions): MethodParameters;
|
|
85
|
-
/**
|
|
86
|
-
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
87
|
-
* @param trades to produce call parameters for
|
|
88
|
-
* @param options options for the call parameters
|
|
89
|
-
*/
|
|
90
|
-
static swapAndAddCallParameters(trades: AnyTradeType, options: SwapAndAddOptions, position: Position, addLiquidityOptions: CondensedAddLiquidityOptions, tokenInApprovalType: ApprovalTypes, tokenOutApprovalType: ApprovalTypes): MethodParameters;
|
|
91
|
-
private static riskOfPartialFill;
|
|
92
|
-
private static v3TradeWithHighPriceImpact;
|
|
93
|
-
private static getPositionAmounts;
|
|
94
|
-
}
|
|
95
|
-
export {};
|
|
1
|
+
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import { Currency, Percent, TradeType } from '@uniswap/sdk-core';
|
|
3
|
+
import { Trade as V2Trade } from '@uniswap/v2-sdk';
|
|
4
|
+
import { FeeOptions, MethodParameters, PermitOptions, Position, Trade as V3Trade } from '@uniswap/v3-sdk';
|
|
5
|
+
import { ApprovalTypes, CondensedAddLiquidityOptions } from './approveAndCall';
|
|
6
|
+
import { Trade } from './entities/trade';
|
|
7
|
+
import { Validation } from './multicallExtended';
|
|
8
|
+
import { MixedRouteTrade } from './entities/mixedRoute/trade';
|
|
9
|
+
/**
|
|
10
|
+
* Options for producing the arguments to send calls to the router.
|
|
11
|
+
*/
|
|
12
|
+
export interface SwapOptions {
|
|
13
|
+
/**
|
|
14
|
+
* How much the execution price is allowed to move unfavorably from the trade execution price.
|
|
15
|
+
*/
|
|
16
|
+
slippageTolerance: Percent;
|
|
17
|
+
/**
|
|
18
|
+
* The account that should receive the output. If omitted, output is sent to msg.sender.
|
|
19
|
+
*/
|
|
20
|
+
recipient?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Either deadline (when the transaction expires, in epoch seconds), or previousBlockhash.
|
|
23
|
+
*/
|
|
24
|
+
deadlineOrPreviousBlockhash?: Validation;
|
|
25
|
+
/**
|
|
26
|
+
* The optional permit parameters for spending the input.
|
|
27
|
+
*/
|
|
28
|
+
inputTokenPermit?: PermitOptions;
|
|
29
|
+
/**
|
|
30
|
+
* Optional information for taking a fee on output.
|
|
31
|
+
*/
|
|
32
|
+
fee?: FeeOptions;
|
|
33
|
+
}
|
|
34
|
+
export interface SwapAndAddOptions extends SwapOptions {
|
|
35
|
+
/**
|
|
36
|
+
* The optional permit parameters for pulling in remaining output token.
|
|
37
|
+
*/
|
|
38
|
+
outputTokenPermit?: PermitOptions;
|
|
39
|
+
}
|
|
40
|
+
type AnyTradeType = Trade<Currency, Currency, TradeType> | V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType> | (V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType>)[];
|
|
41
|
+
/**
|
|
42
|
+
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
|
|
43
|
+
*/
|
|
44
|
+
export declare abstract class SwapRouter {
|
|
45
|
+
static INTERFACE: Interface;
|
|
46
|
+
/**
|
|
47
|
+
* Cannot be constructed.
|
|
48
|
+
*/
|
|
49
|
+
private constructor();
|
|
50
|
+
/**
|
|
51
|
+
* @notice Generates the calldata for a Swap with a V2 Route.
|
|
52
|
+
* @param trade The V2Trade to encode.
|
|
53
|
+
* @param options SwapOptions to use for the trade.
|
|
54
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
55
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
56
|
+
* @returns A string array of calldatas for the trade.
|
|
57
|
+
*/
|
|
58
|
+
private static encodeV2Swap;
|
|
59
|
+
/**
|
|
60
|
+
* @notice Generates the calldata for a Swap with a V3 Route.
|
|
61
|
+
* @param trade The V3Trade to encode.
|
|
62
|
+
* @param options SwapOptions to use for the trade.
|
|
63
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
64
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
65
|
+
* @returns A string array of calldatas for the trade.
|
|
66
|
+
*/
|
|
67
|
+
private static encodeV3Swap;
|
|
68
|
+
/**
|
|
69
|
+
* @notice Generates the calldata for a MixedRouteSwap. Since single hop routes are not MixedRoutes, we will instead generate
|
|
70
|
+
* them via the existing encodeV3Swap and encodeV2Swap methods.
|
|
71
|
+
* @param trade The MixedRouteTrade to encode.
|
|
72
|
+
* @param options SwapOptions to use for the trade.
|
|
73
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
74
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
75
|
+
* @returns A string array of calldatas for the trade.
|
|
76
|
+
*/
|
|
77
|
+
private static encodeMixedRouteSwap;
|
|
78
|
+
private static encodeSwaps;
|
|
79
|
+
/**
|
|
80
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
81
|
+
* @param trades to produce call parameters for
|
|
82
|
+
* @param options options for the call parameters
|
|
83
|
+
*/
|
|
84
|
+
static swapCallParameters(trades: Trade<Currency, Currency, TradeType> | V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType> | (V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType>)[], options: SwapOptions): MethodParameters;
|
|
85
|
+
/**
|
|
86
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
87
|
+
* @param trades to produce call parameters for
|
|
88
|
+
* @param options options for the call parameters
|
|
89
|
+
*/
|
|
90
|
+
static swapAndAddCallParameters(trades: AnyTradeType, options: SwapAndAddOptions, position: Position, addLiquidityOptions: CondensedAddLiquidityOptions, tokenInApprovalType: ApprovalTypes, tokenOutApprovalType: ApprovalTypes): MethodParameters;
|
|
91
|
+
private static riskOfPartialFill;
|
|
92
|
+
private static v3TradeWithHighPriceImpact;
|
|
93
|
+
private static getPositionAmounts;
|
|
94
|
+
}
|
|
95
|
+
export {};
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SwapRouter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const abi_1 = require("@ethersproject/abi");
|
|
6
|
+
const sdk_core_1 = require("@uniswap/sdk-core");
|
|
7
|
+
const ISwapRouter02_json_1 = require("@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json");
|
|
8
|
+
const v2_sdk_1 = require("@uniswap/v2-sdk");
|
|
9
|
+
const v3_sdk_1 = require("@uniswap/v3-sdk");
|
|
10
|
+
const v4_sdk_1 = require("@uniswap/v4-sdk");
|
|
11
|
+
const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
|
|
12
|
+
const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
const approveAndCall_1 = require("./approveAndCall");
|
|
15
|
+
const trade_1 = require("./entities/trade");
|
|
16
|
+
const protocol_1 = require("./entities/protocol");
|
|
17
|
+
const route_1 = require("./entities/route");
|
|
18
|
+
const multicallExtended_1 = require("./multicallExtended");
|
|
19
|
+
const paymentsExtended_1 = require("./paymentsExtended");
|
|
20
|
+
const trade_2 = require("./entities/mixedRoute/trade");
|
|
21
|
+
const encodeMixedRouteToPath_1 = require("./utils/encodeMixedRouteToPath");
|
|
22
|
+
const route_2 = require("./entities/mixedRoute/route");
|
|
23
|
+
const utils_1 = require("./utils");
|
|
24
|
+
const ZERO = jsbi_1.default.BigInt(0);
|
|
25
|
+
const REFUND_ETH_PRICE_IMPACT_THRESHOLD = new sdk_core_1.Percent(jsbi_1.default.BigInt(50), jsbi_1.default.BigInt(100));
|
|
26
|
+
/**
|
|
27
|
+
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
|
|
28
|
+
*/
|
|
29
|
+
class SwapRouter {
|
|
30
|
+
/**
|
|
31
|
+
* Cannot be constructed.
|
|
32
|
+
*/
|
|
33
|
+
constructor() { }
|
|
34
|
+
/**
|
|
35
|
+
* @notice Generates the calldata for a Swap with a V2 Route.
|
|
36
|
+
* @param trade The V2Trade to encode.
|
|
37
|
+
* @param options SwapOptions to use for the trade.
|
|
38
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
39
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
40
|
+
* @returns A string array of calldatas for the trade.
|
|
41
|
+
*/
|
|
42
|
+
static encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
43
|
+
const amountIn = (0, v3_sdk_1.toHex)(trade.maximumAmountIn(options.slippageTolerance).quotient);
|
|
44
|
+
const amountOut = (0, v3_sdk_1.toHex)(trade.minimumAmountOut(options.slippageTolerance).quotient);
|
|
45
|
+
const path = trade.route.path.map((token) => token.address);
|
|
46
|
+
const recipient = routerMustCustody
|
|
47
|
+
? constants_1.ADDRESS_THIS
|
|
48
|
+
: typeof options.recipient === 'undefined'
|
|
49
|
+
? constants_1.MSG_SENDER
|
|
50
|
+
: (0, sdk_core_1.validateAndParseAddress)(options.recipient);
|
|
51
|
+
if (trade.tradeType === sdk_core_1.TradeType.EXACT_INPUT) {
|
|
52
|
+
const exactInputParams = [amountIn, performAggregatedSlippageCheck ? 0 : amountOut, path, recipient];
|
|
53
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const exactOutputParams = [amountOut, amountIn, path, recipient];
|
|
57
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapTokensForExactTokens', exactOutputParams);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @notice Generates the calldata for a Swap with a V3 Route.
|
|
62
|
+
* @param trade The V3Trade to encode.
|
|
63
|
+
* @param options SwapOptions to use for the trade.
|
|
64
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
65
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
66
|
+
* @returns A string array of calldatas for the trade.
|
|
67
|
+
*/
|
|
68
|
+
static encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
69
|
+
const calldatas = [];
|
|
70
|
+
for (const { route, inputAmount, outputAmount } of trade.swaps) {
|
|
71
|
+
const amountIn = (0, v3_sdk_1.toHex)(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
72
|
+
const amountOut = (0, v3_sdk_1.toHex)(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient);
|
|
73
|
+
// flag for whether the trade is single hop or not
|
|
74
|
+
const singleHop = route.pools.length === 1;
|
|
75
|
+
const recipient = routerMustCustody
|
|
76
|
+
? constants_1.ADDRESS_THIS
|
|
77
|
+
: typeof options.recipient === 'undefined'
|
|
78
|
+
? constants_1.MSG_SENDER
|
|
79
|
+
: (0, sdk_core_1.validateAndParseAddress)(options.recipient);
|
|
80
|
+
if (singleHop) {
|
|
81
|
+
if (trade.tradeType === sdk_core_1.TradeType.EXACT_INPUT) {
|
|
82
|
+
const exactInputSingleParams = {
|
|
83
|
+
tokenIn: route.tokenPath[0].address,
|
|
84
|
+
tokenOut: route.tokenPath[1].address,
|
|
85
|
+
fee: route.pools[0].fee,
|
|
86
|
+
recipient,
|
|
87
|
+
amountIn,
|
|
88
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut,
|
|
89
|
+
sqrtPriceLimitX96: 0,
|
|
90
|
+
};
|
|
91
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const exactOutputSingleParams = {
|
|
95
|
+
tokenIn: route.tokenPath[0].address,
|
|
96
|
+
tokenOut: route.tokenPath[1].address,
|
|
97
|
+
fee: route.pools[0].fee,
|
|
98
|
+
recipient,
|
|
99
|
+
amountOut,
|
|
100
|
+
amountInMaximum: amountIn,
|
|
101
|
+
sqrtPriceLimitX96: 0,
|
|
102
|
+
};
|
|
103
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutputSingle', [exactOutputSingleParams]));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
const path = (0, v3_sdk_1.encodeRouteToPath)(route, trade.tradeType === sdk_core_1.TradeType.EXACT_OUTPUT);
|
|
108
|
+
if (trade.tradeType === sdk_core_1.TradeType.EXACT_INPUT) {
|
|
109
|
+
const exactInputParams = {
|
|
110
|
+
path,
|
|
111
|
+
recipient,
|
|
112
|
+
amountIn,
|
|
113
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut,
|
|
114
|
+
};
|
|
115
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const exactOutputParams = {
|
|
119
|
+
path,
|
|
120
|
+
recipient,
|
|
121
|
+
amountOut,
|
|
122
|
+
amountInMaximum: amountIn,
|
|
123
|
+
};
|
|
124
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutput', [exactOutputParams]));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return calldatas;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @notice Generates the calldata for a MixedRouteSwap. Since single hop routes are not MixedRoutes, we will instead generate
|
|
132
|
+
* them via the existing encodeV3Swap and encodeV2Swap methods.
|
|
133
|
+
* @param trade The MixedRouteTrade to encode.
|
|
134
|
+
* @param options SwapOptions to use for the trade.
|
|
135
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
136
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
137
|
+
* @returns A string array of calldatas for the trade.
|
|
138
|
+
*/
|
|
139
|
+
static encodeMixedRouteSwap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
140
|
+
const calldatas = [];
|
|
141
|
+
(0, tiny_invariant_1.default)(trade.tradeType === sdk_core_1.TradeType.EXACT_INPUT, 'TRADE_TYPE');
|
|
142
|
+
for (const { route, inputAmount, outputAmount } of trade.swaps) {
|
|
143
|
+
if (route.pools.some((pool) => pool instanceof v4_sdk_1.Pool))
|
|
144
|
+
throw 'Encoding mixed routes with V4 not supported';
|
|
145
|
+
const amountIn = (0, v3_sdk_1.toHex)(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
146
|
+
const amountOut = (0, v3_sdk_1.toHex)(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient);
|
|
147
|
+
// flag for whether the trade is single hop or not
|
|
148
|
+
const singleHop = route.pools.length === 1;
|
|
149
|
+
const recipient = routerMustCustody
|
|
150
|
+
? constants_1.ADDRESS_THIS
|
|
151
|
+
: typeof options.recipient === 'undefined'
|
|
152
|
+
? constants_1.MSG_SENDER
|
|
153
|
+
: (0, sdk_core_1.validateAndParseAddress)(options.recipient);
|
|
154
|
+
const mixedRouteIsAllV3 = (route) => {
|
|
155
|
+
return route.pools.every((pool) => pool instanceof v3_sdk_1.Pool);
|
|
156
|
+
};
|
|
157
|
+
if (singleHop) {
|
|
158
|
+
/// For single hop, since it isn't really a mixedRoute, we'll just mimic behavior of V3 or V2
|
|
159
|
+
/// We don't use encodeV3Swap() or encodeV2Swap() because casting the trade to a V3Trade or V2Trade is overcomplex
|
|
160
|
+
if (mixedRouteIsAllV3(route)) {
|
|
161
|
+
const exactInputSingleParams = {
|
|
162
|
+
tokenIn: route.path[0].wrapped.address,
|
|
163
|
+
tokenOut: route.path[1].wrapped.address,
|
|
164
|
+
fee: route.pools[0].fee,
|
|
165
|
+
recipient,
|
|
166
|
+
amountIn,
|
|
167
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut,
|
|
168
|
+
sqrtPriceLimitX96: 0,
|
|
169
|
+
};
|
|
170
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
const path = route.path.map((token) => token.wrapped.address);
|
|
174
|
+
const exactInputParams = [amountIn, performAggregatedSlippageCheck ? 0 : amountOut, path, recipient];
|
|
175
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const sections = (0, utils_1.partitionMixedRouteByProtocol)(route);
|
|
180
|
+
const isLastSectionInRoute = (i) => {
|
|
181
|
+
return i === sections.length - 1;
|
|
182
|
+
};
|
|
183
|
+
let outputToken;
|
|
184
|
+
let inputToken = route.input.wrapped;
|
|
185
|
+
for (let i = 0; i < sections.length; i++) {
|
|
186
|
+
const section = sections[i];
|
|
187
|
+
/// Now, we get output of this section
|
|
188
|
+
outputToken = (0, utils_1.getOutputOfPools)(section, inputToken);
|
|
189
|
+
const newRouteOriginal = new route_2.MixedRouteSDK([...section], section[0].token0.equals(inputToken) ? section[0].token0 : section[0].token1, outputToken);
|
|
190
|
+
const newRoute = new route_1.MixedRoute(newRouteOriginal);
|
|
191
|
+
/// Previous output is now input
|
|
192
|
+
inputToken = outputToken.wrapped;
|
|
193
|
+
if (mixedRouteIsAllV3(newRoute)) {
|
|
194
|
+
const path = (0, encodeMixedRouteToPath_1.encodeMixedRouteToPath)(newRoute);
|
|
195
|
+
const exactInputParams = {
|
|
196
|
+
path,
|
|
197
|
+
// By default router holds funds until the last swap, then it is sent to the recipient
|
|
198
|
+
// special case exists where we are unwrapping WETH output, in which case `routerMustCustody` is set to true
|
|
199
|
+
// and router still holds the funds. That logic bundled into how the value of `recipient` is calculated
|
|
200
|
+
recipient: isLastSectionInRoute(i) ? recipient : constants_1.ADDRESS_THIS,
|
|
201
|
+
amountIn: i === 0 ? amountIn : 0,
|
|
202
|
+
amountOutMinimum: !isLastSectionInRoute(i) ? 0 : amountOut,
|
|
203
|
+
};
|
|
204
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const exactInputParams = [
|
|
208
|
+
i === 0 ? amountIn : 0,
|
|
209
|
+
!isLastSectionInRoute(i) ? 0 : amountOut,
|
|
210
|
+
newRoute.path.map((token) => token.wrapped.address),
|
|
211
|
+
isLastSectionInRoute(i) ? recipient : constants_1.ADDRESS_THIS, // to
|
|
212
|
+
];
|
|
213
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return calldatas;
|
|
219
|
+
}
|
|
220
|
+
static encodeSwaps(trades, options, isSwapAndAdd) {
|
|
221
|
+
// If dealing with an instance of the aggregated Trade object, unbundle it to individual trade objects.
|
|
222
|
+
if (trades instanceof trade_1.Trade) {
|
|
223
|
+
(0, tiny_invariant_1.default)(trades.swaps.every((swap) => swap.route.protocol === protocol_1.Protocol.V3 ||
|
|
224
|
+
swap.route.protocol === protocol_1.Protocol.V2 ||
|
|
225
|
+
swap.route.protocol === protocol_1.Protocol.MIXED), 'UNSUPPORTED_PROTOCOL (encoding routes with v4 not supported)');
|
|
226
|
+
let individualTrades = [];
|
|
227
|
+
for (const { route, inputAmount, outputAmount } of trades.swaps) {
|
|
228
|
+
if (route.protocol === protocol_1.Protocol.V2) {
|
|
229
|
+
individualTrades.push(new v2_sdk_1.Trade(route, trades.tradeType === sdk_core_1.TradeType.EXACT_INPUT ? inputAmount : outputAmount, trades.tradeType));
|
|
230
|
+
}
|
|
231
|
+
else if (route.protocol === protocol_1.Protocol.V3) {
|
|
232
|
+
individualTrades.push(v3_sdk_1.Trade.createUncheckedTrade({
|
|
233
|
+
route: route,
|
|
234
|
+
inputAmount,
|
|
235
|
+
outputAmount,
|
|
236
|
+
tradeType: trades.tradeType,
|
|
237
|
+
}));
|
|
238
|
+
}
|
|
239
|
+
else if (route.protocol === protocol_1.Protocol.MIXED) {
|
|
240
|
+
individualTrades.push(
|
|
241
|
+
/// we can change the naming of this function on MixedRouteTrade if needed
|
|
242
|
+
trade_2.MixedRouteTrade.createUncheckedTrade({
|
|
243
|
+
route: route,
|
|
244
|
+
inputAmount,
|
|
245
|
+
outputAmount,
|
|
246
|
+
tradeType: trades.tradeType,
|
|
247
|
+
}));
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
throw new Error('UNSUPPORTED_TRADE_PROTOCOL');
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
trades = individualTrades;
|
|
254
|
+
}
|
|
255
|
+
if (!Array.isArray(trades)) {
|
|
256
|
+
trades = [trades];
|
|
257
|
+
}
|
|
258
|
+
const numberOfTrades = trades.reduce((numberOfTrades, trade) => numberOfTrades + (trade instanceof v3_sdk_1.Trade || trade instanceof trade_2.MixedRouteTrade ? trade.swaps.length : 1), 0);
|
|
259
|
+
const sampleTrade = trades[0];
|
|
260
|
+
// All trades should have the same starting/ending currency and trade type
|
|
261
|
+
(0, tiny_invariant_1.default)(trades.every((trade) => trade.inputAmount.currency.equals(sampleTrade.inputAmount.currency)), 'TOKEN_IN_DIFF');
|
|
262
|
+
(0, tiny_invariant_1.default)(trades.every((trade) => trade.outputAmount.currency.equals(sampleTrade.outputAmount.currency)), 'TOKEN_OUT_DIFF');
|
|
263
|
+
(0, tiny_invariant_1.default)(trades.every((trade) => trade.tradeType === sampleTrade.tradeType), 'TRADE_TYPE_DIFF');
|
|
264
|
+
const calldatas = [];
|
|
265
|
+
const inputIsNative = sampleTrade.inputAmount.currency.isNative;
|
|
266
|
+
const outputIsNative = sampleTrade.outputAmount.currency.isNative;
|
|
267
|
+
// flag for whether we want to perform an aggregated slippage check
|
|
268
|
+
// 1. when there are >2 exact input trades. this is only a heuristic,
|
|
269
|
+
// as it's still more gas-expensive even in this case, but has benefits
|
|
270
|
+
// in that the reversion probability is lower
|
|
271
|
+
const performAggregatedSlippageCheck = sampleTrade.tradeType === sdk_core_1.TradeType.EXACT_INPUT && numberOfTrades > 2;
|
|
272
|
+
// flag for whether funds should be send first to the router
|
|
273
|
+
// 1. when receiving ETH (which much be unwrapped from WETH)
|
|
274
|
+
// 2. when a fee on the output is being taken
|
|
275
|
+
// 3. when performing swap and add
|
|
276
|
+
// 4. when performing an aggregated slippage check
|
|
277
|
+
const routerMustCustody = outputIsNative || !!options.fee || !!isSwapAndAdd || performAggregatedSlippageCheck;
|
|
278
|
+
// encode permit if necessary
|
|
279
|
+
if (options.inputTokenPermit) {
|
|
280
|
+
(0, tiny_invariant_1.default)(sampleTrade.inputAmount.currency.isToken, 'NON_TOKEN_PERMIT');
|
|
281
|
+
calldatas.push(v3_sdk_1.SelfPermit.encodePermit(sampleTrade.inputAmount.currency, options.inputTokenPermit));
|
|
282
|
+
}
|
|
283
|
+
for (const trade of trades) {
|
|
284
|
+
if (trade instanceof v2_sdk_1.Trade) {
|
|
285
|
+
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck));
|
|
286
|
+
}
|
|
287
|
+
else if (trade instanceof v3_sdk_1.Trade) {
|
|
288
|
+
for (const calldata of SwapRouter.encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck)) {
|
|
289
|
+
calldatas.push(calldata);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
else if (trade instanceof trade_2.MixedRouteTrade) {
|
|
293
|
+
for (const calldata of SwapRouter.encodeMixedRouteSwap(trade, options, routerMustCustody, performAggregatedSlippageCheck)) {
|
|
294
|
+
calldatas.push(calldata);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
throw new Error('Unsupported trade object');
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const ZERO_IN = sdk_core_1.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
|
|
302
|
+
const ZERO_OUT = sdk_core_1.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
|
|
303
|
+
const minimumAmountOut = trades.reduce((sum, trade) => sum.add(trade.minimumAmountOut(options.slippageTolerance)), ZERO_OUT);
|
|
304
|
+
const quoteAmountOut = trades.reduce((sum, trade) => sum.add(trade.outputAmount), ZERO_OUT);
|
|
305
|
+
const totalAmountIn = trades.reduce((sum, trade) => sum.add(trade.maximumAmountIn(options.slippageTolerance)), ZERO_IN);
|
|
306
|
+
return {
|
|
307
|
+
calldatas,
|
|
308
|
+
sampleTrade,
|
|
309
|
+
routerMustCustody,
|
|
310
|
+
inputIsNative,
|
|
311
|
+
outputIsNative,
|
|
312
|
+
totalAmountIn,
|
|
313
|
+
minimumAmountOut,
|
|
314
|
+
quoteAmountOut,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
319
|
+
* @param trades to produce call parameters for
|
|
320
|
+
* @param options options for the call parameters
|
|
321
|
+
*/
|
|
322
|
+
static swapCallParameters(trades, options) {
|
|
323
|
+
const { calldatas, sampleTrade, routerMustCustody, inputIsNative, outputIsNative, totalAmountIn, minimumAmountOut, } = SwapRouter.encodeSwaps(trades, options);
|
|
324
|
+
// unwrap or sweep
|
|
325
|
+
if (routerMustCustody) {
|
|
326
|
+
if (outputIsNative) {
|
|
327
|
+
calldatas.push(paymentsExtended_1.PaymentsExtended.encodeUnwrapWETH9(minimumAmountOut.quotient, options.recipient, options.fee));
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
calldatas.push(paymentsExtended_1.PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, minimumAmountOut.quotient, options.recipient, options.fee));
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// must refund when paying in ETH: either with an uncertain input amount OR if there's a chance of a partial fill.
|
|
334
|
+
// unlike ERC20's, the full ETH value must be sent in the transaction, so the rest must be refunded.
|
|
335
|
+
if (inputIsNative && (sampleTrade.tradeType === sdk_core_1.TradeType.EXACT_OUTPUT || SwapRouter.riskOfPartialFill(trades))) {
|
|
336
|
+
calldatas.push(v3_sdk_1.Payments.encodeRefundETH());
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
calldata: multicallExtended_1.MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
340
|
+
value: (0, v3_sdk_1.toHex)(inputIsNative ? totalAmountIn.quotient : ZERO),
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
345
|
+
* @param trades to produce call parameters for
|
|
346
|
+
* @param options options for the call parameters
|
|
347
|
+
*/
|
|
348
|
+
static swapAndAddCallParameters(trades, options, position, addLiquidityOptions, tokenInApprovalType, tokenOutApprovalType) {
|
|
349
|
+
const { calldatas, inputIsNative, outputIsNative, sampleTrade, totalAmountIn: totalAmountSwapped, quoteAmountOut, minimumAmountOut, } = SwapRouter.encodeSwaps(trades, options, true);
|
|
350
|
+
// encode output token permit if necessary
|
|
351
|
+
if (options.outputTokenPermit) {
|
|
352
|
+
(0, tiny_invariant_1.default)(quoteAmountOut.currency.isToken, 'NON_TOKEN_PERMIT_OUTPUT');
|
|
353
|
+
calldatas.push(v3_sdk_1.SelfPermit.encodePermit(quoteAmountOut.currency, options.outputTokenPermit));
|
|
354
|
+
}
|
|
355
|
+
const chainId = sampleTrade.route.chainId;
|
|
356
|
+
const zeroForOne = position.pool.token0.wrapped.address === totalAmountSwapped.currency.wrapped.address;
|
|
357
|
+
const { positionAmountIn, positionAmountOut } = SwapRouter.getPositionAmounts(position, zeroForOne);
|
|
358
|
+
// if tokens are native they will be converted to WETH9
|
|
359
|
+
const tokenIn = inputIsNative ? sdk_core_1.WETH9[chainId] : positionAmountIn.currency.wrapped;
|
|
360
|
+
const tokenOut = outputIsNative ? sdk_core_1.WETH9[chainId] : positionAmountOut.currency.wrapped;
|
|
361
|
+
// if swap output does not make up whole outputTokenBalanceDesired, pull in remaining tokens for adding liquidity
|
|
362
|
+
const amountOutRemaining = positionAmountOut.subtract(quoteAmountOut.wrapped);
|
|
363
|
+
if (amountOutRemaining.greaterThan(sdk_core_1.CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
|
|
364
|
+
// if output is native, this means the remaining portion is included as native value in the transaction
|
|
365
|
+
// and must be wrapped. Otherwise, pull in remaining ERC20 token.
|
|
366
|
+
outputIsNative
|
|
367
|
+
? calldatas.push(paymentsExtended_1.PaymentsExtended.encodeWrapETH(amountOutRemaining.quotient))
|
|
368
|
+
: calldatas.push(paymentsExtended_1.PaymentsExtended.encodePull(tokenOut, amountOutRemaining.quotient));
|
|
369
|
+
}
|
|
370
|
+
// if input is native, convert to WETH9, else pull ERC20 token
|
|
371
|
+
inputIsNative
|
|
372
|
+
? calldatas.push(paymentsExtended_1.PaymentsExtended.encodeWrapETH(positionAmountIn.quotient))
|
|
373
|
+
: calldatas.push(paymentsExtended_1.PaymentsExtended.encodePull(tokenIn, positionAmountIn.quotient));
|
|
374
|
+
// approve token balances to NFTManager
|
|
375
|
+
if (tokenInApprovalType !== approveAndCall_1.ApprovalTypes.NOT_REQUIRED)
|
|
376
|
+
calldatas.push(approveAndCall_1.ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
|
|
377
|
+
if (tokenOutApprovalType !== approveAndCall_1.ApprovalTypes.NOT_REQUIRED)
|
|
378
|
+
calldatas.push(approveAndCall_1.ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType));
|
|
379
|
+
// represents a position with token amounts resulting from a swap with maximum slippage
|
|
380
|
+
// hence the minimal amount out possible.
|
|
381
|
+
const minimalPosition = v3_sdk_1.Position.fromAmounts({
|
|
382
|
+
pool: position.pool,
|
|
383
|
+
tickLower: position.tickLower,
|
|
384
|
+
tickUpper: position.tickUpper,
|
|
385
|
+
amount0: zeroForOne ? position.amount0.quotient.toString() : minimumAmountOut.quotient.toString(),
|
|
386
|
+
amount1: zeroForOne ? minimumAmountOut.quotient.toString() : position.amount1.quotient.toString(),
|
|
387
|
+
useFullPrecision: false,
|
|
388
|
+
});
|
|
389
|
+
// encode NFTManager add liquidity
|
|
390
|
+
calldatas.push(approveAndCall_1.ApproveAndCall.encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, options.slippageTolerance));
|
|
391
|
+
// sweep remaining tokens
|
|
392
|
+
inputIsNative
|
|
393
|
+
? calldatas.push(paymentsExtended_1.PaymentsExtended.encodeUnwrapWETH9(ZERO))
|
|
394
|
+
: calldatas.push(paymentsExtended_1.PaymentsExtended.encodeSweepToken(tokenIn, ZERO));
|
|
395
|
+
outputIsNative
|
|
396
|
+
? calldatas.push(paymentsExtended_1.PaymentsExtended.encodeUnwrapWETH9(ZERO))
|
|
397
|
+
: calldatas.push(paymentsExtended_1.PaymentsExtended.encodeSweepToken(tokenOut, ZERO));
|
|
398
|
+
let value;
|
|
399
|
+
if (inputIsNative) {
|
|
400
|
+
value = totalAmountSwapped.wrapped.add(positionAmountIn.wrapped).quotient;
|
|
401
|
+
}
|
|
402
|
+
else if (outputIsNative) {
|
|
403
|
+
value = amountOutRemaining.quotient;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
value = ZERO;
|
|
407
|
+
}
|
|
408
|
+
return {
|
|
409
|
+
calldata: multicallExtended_1.MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
410
|
+
value: value.toString(),
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
// if price impact is very high, there's a chance of hitting max/min prices resulting in a partial fill of the swap
|
|
414
|
+
static riskOfPartialFill(trades) {
|
|
415
|
+
if (Array.isArray(trades)) {
|
|
416
|
+
return trades.some((trade) => {
|
|
417
|
+
return SwapRouter.v3TradeWithHighPriceImpact(trade);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
return SwapRouter.v3TradeWithHighPriceImpact(trades);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
static v3TradeWithHighPriceImpact(trade) {
|
|
425
|
+
return !(trade instanceof v2_sdk_1.Trade) && trade.priceImpact.greaterThan(REFUND_ETH_PRICE_IMPACT_THRESHOLD);
|
|
426
|
+
}
|
|
427
|
+
static getPositionAmounts(position, zeroForOne) {
|
|
428
|
+
const { amount0, amount1 } = position.mintAmounts;
|
|
429
|
+
const currencyAmount0 = sdk_core_1.CurrencyAmount.fromRawAmount(position.pool.token0, amount0);
|
|
430
|
+
const currencyAmount1 = sdk_core_1.CurrencyAmount.fromRawAmount(position.pool.token1, amount1);
|
|
431
|
+
const [positionAmountIn, positionAmountOut] = zeroForOne
|
|
432
|
+
? [currencyAmount0, currencyAmount1]
|
|
433
|
+
: [currencyAmount1, currencyAmount0];
|
|
434
|
+
return { positionAmountIn, positionAmountOut };
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
exports.SwapRouter = SwapRouter;
|
|
438
|
+
SwapRouter.INTERFACE = new abi_1.Interface(ISwapRouter02_json_1.abi);
|
|
439
|
+
//# sourceMappingURL=swapRouter.js.map
|