0
# Swap Routing
1
2
Core swap routing functionality for encoding transactions across Uniswap V2, V3, and V4 protocols. The SwapRouter class handles single and multi-protocol routes with automatic optimization and gas-efficient execution.
3
4
## Capabilities
5
6
### SwapRouter Class
7
8
Abstract class representing the Uniswap V2 + V3 SwapRouter02 contract, providing static methods for encoding swap transactions.
9
10
```typescript { .api }
11
/**
12
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades
13
*/
14
abstract class SwapRouter {
15
/** Contract interface for ISwapRouter02 */
16
static INTERFACE: Interface;
17
18
/**
19
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade
20
* @param trades - Single trade or array of trades to execute
21
* @param options - Options for the call parameters
22
* @returns Method parameters with calldata and value
23
*/
24
static swapCallParameters(
25
trades: Trade<Currency, Currency, TradeType>
26
| V2Trade<Currency, Currency, TradeType>
27
| V3Trade<Currency, Currency, TradeType>
28
| MixedRouteTrade<Currency, Currency, TradeType>
29
| (V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType>)[],
30
options: SwapOptions
31
): MethodParameters;
32
33
/**
34
* Produces call parameters for swap and add liquidity operations
35
* @param trades - Trades to execute before adding liquidity
36
* @param options - Swap and add options
37
* @param position - Target liquidity position
38
* @param addLiquidityOptions - Options for adding liquidity
39
* @param tokenInApprovalType - Approval type for input token
40
* @param tokenOutApprovalType - Approval type for output token
41
* @returns Method parameters with calldata and value
42
*/
43
static swapAndAddCallParameters(
44
trades: AnyTradeType,
45
options: SwapAndAddOptions,
46
position: Position,
47
addLiquidityOptions: CondensedAddLiquidityOptions,
48
tokenInApprovalType: ApprovalTypes,
49
tokenOutApprovalType: ApprovalTypes
50
): MethodParameters;
51
}
52
```
53
54
### Swap Options
55
56
Configuration options for swap operations including slippage tolerance, recipient, and validation.
57
58
```typescript { .api }
59
/**
60
* Options for producing the arguments to send calls to the router
61
*/
62
interface SwapOptions {
63
/** How much the execution price is allowed to move unfavorably from the trade execution price */
64
slippageTolerance: Percent;
65
/** The account that should receive the output. If omitted, output is sent to msg.sender */
66
recipient?: string;
67
/** Either deadline (when the transaction expires, in epoch seconds), or previousBlockhash */
68
deadlineOrPreviousBlockhash?: Validation;
69
/** The optional permit parameters for spending the input */
70
inputTokenPermit?: PermitOptions;
71
/** Optional information for taking a fee on output */
72
fee?: FeeOptions;
73
}
74
75
/**
76
* Extended options for swap and add operations
77
*/
78
interface SwapAndAddOptions extends SwapOptions {
79
/** The optional permit parameters for pulling in remaining output token */
80
outputTokenPermit?: PermitOptions;
81
}
82
```
83
84
### Method Parameters
85
86
Return type for swap encoding methods containing transaction calldata and value.
87
88
```typescript { .api }
89
/**
90
* Parameters for calling router methods
91
*/
92
interface MethodParameters {
93
/** Hex-encoded calldata for the transaction */
94
calldata: string;
95
/** Hex-encoded value to send with the transaction */
96
value: string;
97
}
98
```
99
100
**Usage Examples:**
101
102
```typescript
103
import { SwapRouter, Trade, SwapOptions } from "@uniswap/router-sdk";
104
import { CurrencyAmount, TradeType, Percent, Token } from "@uniswap/sdk-core";
105
106
// Create a simple V3 trade
107
const trade = new V3Trade(route, amount, TradeType.EXACT_INPUT);
108
109
// Configure swap options
110
const options: SwapOptions = {
111
slippageTolerance: new Percent(50, 10000), // 0.5%
112
recipient: "0x742d35Cc6435C6329Eb54F0d86C05B1E11a02E6B",
113
deadlineOrPreviousBlockhash: Math.floor(Date.now() / 1000) + 1800, // 30 minutes
114
};
115
116
// Generate swap call parameters
117
const { calldata, value } = SwapRouter.swapCallParameters(trade, options);
118
119
// Use with ethers.js
120
const tx = await wallet.sendTransaction({
121
to: SWAP_ROUTER_ADDRESS,
122
data: calldata,
123
value: value,
124
});
125
```
126
127
```typescript
128
// Multi-protocol trade example
129
const aggregatedTrade = new Trade({
130
v2Routes: [{ routev2: v2Route, inputAmount, outputAmount }],
131
v3Routes: [{ routev3: v3Route, inputAmount, outputAmount }],
132
tradeType: TradeType.EXACT_INPUT
133
});
134
135
const params = SwapRouter.swapCallParameters(aggregatedTrade, options);
136
```
137
138
```typescript
139
// Swap and add liquidity example
140
import { Position } from "@uniswap/v3-sdk";
141
142
const position = new Position({
143
pool: v3Pool,
144
liquidity: targetLiquidity,
145
tickLower: -887220,
146
tickUpper: 887220
147
});
148
149
const swapAndAddOptions: SwapAndAddOptions = {
150
...options,
151
outputTokenPermit: {
152
v: 27,
153
r: "0x...",
154
s: "0x...",
155
deadline: deadline,
156
amount: permitAmount
157
}
158
};
159
160
const params = SwapRouter.swapAndAddCallParameters(
161
trade,
162
swapAndAddOptions,
163
position,
164
addLiquidityOptions,
165
ApprovalTypes.MAX,
166
ApprovalTypes.NOT_REQUIRED
167
);
168
```
169
170
### Constants
171
172
Routing-related constants used throughout the SDK.
173
174
```typescript { .api }
175
/** Zero address constant */
176
const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000";
177
178
/** Message sender placeholder address */
179
const MSG_SENDER = "0x0000000000000000000000000000000000000001";
180
181
/** Contract address placeholder */
182
const ADDRESS_THIS = "0x0000000000000000000000000000000000000002";
183
184
/** Mixed quoter fee path placeholders for different protocol combinations */
185
const MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER = 8388608;
186
const MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER = 32;
187
const MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER = 3145728;
188
const MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER = 4194304;
189
190
/** Percentage constants */
191
const ZERO_PERCENT: Percent;
192
const ONE_HUNDRED_PERCENT: Percent;
193
```