0
# Route Handling
1
2
Route wrapper classes providing unified interfaces for V2, V3, V4, and mixed protocol routes. These wrappers enable seamless integration across different Uniswap versions while maintaining protocol-specific functionality.
3
4
## Capabilities
5
6
### Common Route Interface
7
8
Unified interface implemented by all route types, providing consistent access to route properties across protocols.
9
10
```typescript { .api }
11
/**
12
* Generic route interface implemented by all protocol-specific routes
13
*/
14
interface IRoute<TInput extends Currency, TOutput extends Currency, TPool extends Pair | V3Pool | V4Pool> {
15
/** Protocol identifier (V2, V3, V4, or MIXED) */
16
protocol: Protocol;
17
18
/** Array of pools or pairs that make up this route */
19
pools: TPool[];
20
21
/** Array of currencies in the swap path */
22
path: Currency[];
23
24
/** Mid price of the route calculated from pool reserves */
25
midPrice: Price<TInput, TOutput>;
26
27
/** Input currency for the route */
28
input: TInput;
29
30
/** Output currency for the route */
31
output: TOutput;
32
33
/** Input currency as it appears in the path (may be wrapped) */
34
pathInput: Currency;
35
36
/** Output currency as it appears in the path (may be wrapped) */
37
pathOutput: Currency;
38
}
39
```
40
41
### Protocol Enumeration
42
43
Enumeration defining the supported Uniswap protocols.
44
45
```typescript { .api }
46
/**
47
* Supported Uniswap protocol versions
48
*/
49
enum Protocol {
50
/** Uniswap V2 protocol */
51
V2 = 'V2',
52
53
/** Uniswap V3 protocol */
54
V3 = 'V3',
55
56
/** Uniswap V4 protocol */
57
V4 = 'V4',
58
59
/** Mixed protocol routing across multiple versions */
60
MIXED = 'MIXED'
61
}
62
```
63
64
### V2 Route Wrapper
65
66
Wrapper class for Uniswap V2 routes extending the V2 SDK functionality.
67
68
```typescript { .api }
69
/**
70
* V2 route wrapper extending V2RouteSDK with unified interface
71
*/
72
class RouteV2<TInput extends Currency, TOutput extends Currency>
73
extends V2RouteSDK<TInput, TOutput>
74
implements IRoute<TInput, TOutput, Pair> {
75
76
/** Always Protocol.V2 for V2 routes */
77
readonly protocol: Protocol.V2;
78
79
/** Array of V2 pairs in the route */
80
readonly pools: Pair[];
81
82
/** Input currency as it appears in the first pair */
83
pathInput: Currency;
84
85
/** Output currency as it appears in the last pair */
86
pathOutput: Currency;
87
88
/**
89
* Create a V2 route wrapper
90
* @param v2Route - Original V2 route from @uniswap/v2-sdk
91
*/
92
constructor(v2Route: V2RouteSDK<TInput, TOutput>);
93
}
94
```
95
96
### V3 Route Wrapper
97
98
Wrapper class for Uniswap V3 routes extending the V3 SDK functionality.
99
100
```typescript { .api }
101
/**
102
* V3 route wrapper extending V3RouteSDK with unified interface
103
*/
104
class RouteV3<TInput extends Currency, TOutput extends Currency>
105
extends V3RouteSDK<TInput, TOutput>
106
implements IRoute<TInput, TOutput, V3Pool> {
107
108
/** Always Protocol.V3 for V3 routes */
109
readonly protocol: Protocol.V3;
110
111
/** Array of tokens in the route path */
112
readonly path: Token[];
113
114
/** Input currency as it appears in the first pool */
115
pathInput: Currency;
116
117
/** Output currency as it appears in the last pool */
118
pathOutput: Currency;
119
120
/**
121
* Create a V3 route wrapper
122
* @param v3Route - Original V3 route from @uniswap/v3-sdk
123
*/
124
constructor(v3Route: V3RouteSDK<TInput, TOutput>);
125
}
126
```
127
128
### V4 Route Wrapper
129
130
Wrapper class for Uniswap V4 routes extending the V4 SDK functionality.
131
132
```typescript { .api }
133
/**
134
* V4 route wrapper extending V4RouteSDK with unified interface
135
*/
136
class RouteV4<TInput extends Currency, TOutput extends Currency>
137
extends V4RouteSDK<TInput, TOutput>
138
implements IRoute<TInput, TOutput, V4Pool> {
139
140
/** Always Protocol.V4 for V4 routes */
141
readonly protocol: Protocol.V4;
142
143
/** Array of currencies in the route path */
144
readonly path: Currency[];
145
146
/**
147
* Create a V4 route wrapper
148
* @param v4Route - Original V4 route from @uniswap/v4-sdk
149
*/
150
constructor(v4Route: V4RouteSDK<TInput, TOutput>);
151
}
152
```
153
154
### Mixed Route Wrapper
155
156
Wrapper class for mixed protocol routes that span multiple Uniswap versions.
157
158
```typescript { .api }
159
/**
160
* Mixed route wrapper extending MixedRouteSDK with unified interface
161
*/
162
class MixedRoute<TInput extends Currency, TOutput extends Currency>
163
extends MixedRouteSDK<TInput, TOutput>
164
implements IRoute<TInput, TOutput, Pair | V3Pool | V4Pool> {
165
166
/** Always Protocol.MIXED for mixed routes */
167
readonly protocol: Protocol.MIXED;
168
169
/**
170
* Create a mixed route wrapper
171
* @param mixedRoute - Original mixed route from the mixed route SDK
172
*/
173
constructor(mixedRoute: MixedRouteSDK<TInput, TOutput>);
174
}
175
```
176
177
### Path Token Helper
178
179
Utility function for determining the correct token to use in routing paths.
180
181
```typescript { .api }
182
/**
183
* Helper function to get the path token for a currency in a specific pool
184
* Handles native ETH vs wrapped ETH conversions for routing
185
* @param currency - The currency to find in the pool
186
* @param pool - The pool to search in
187
* @returns The token as it appears in the pool
188
* @throws Error if currency is not found in the pool
189
*/
190
function getPathToken(currency: Currency, pool: Pair | V3Pool): Token;
191
```
192
193
**Usage Examples:**
194
195
```typescript
196
import { RouteV2, RouteV3, Protocol, getPathToken } from "@uniswap/router-sdk";
197
import { Route as V2RouteSDK, Pair } from "@uniswap/v2-sdk";
198
import { Route as V3RouteSDK, Pool as V3Pool } from "@uniswap/v3-sdk";
199
200
// Create V2 route wrapper
201
const v2RouteSDK = new V2RouteSDK([pair1, pair2], tokenA, tokenB);
202
const v2Route = new RouteV2(v2RouteSDK);
203
204
console.log(v2Route.protocol); // "V2"
205
console.log(v2Route.pools); // [pair1, pair2]
206
console.log(v2Route.midPrice.toSignificant(6));
207
208
// Create V3 route wrapper
209
const v3RouteSDK = new V3RouteSDK([pool1, pool2], tokenA, tokenB);
210
const v3Route = new RouteV3(v3RouteSDK);
211
212
console.log(v3Route.protocol); // "V3"
213
console.log(v3Route.path); // [tokenA, tokenIntermediate, tokenB]
214
console.log(v3Route.pathInput); // pathInput currency
215
```
216
217
```typescript
218
// Working with different route types uniformly
219
function analyzeRoute(route: IRoute<Currency, Currency, any>) {
220
console.log(`Protocol: ${route.protocol}`);
221
console.log(`Input: ${route.input.symbol}`);
222
console.log(`Output: ${route.output.symbol}`);
223
console.log(`Path length: ${route.path.length}`);
224
console.log(`Mid price: ${route.midPrice.toSignificant(6)}`);
225
226
// Protocol-specific handling
227
switch (route.protocol) {
228
case Protocol.V2:
229
console.log(`V2 pairs: ${route.pools.length}`);
230
break;
231
case Protocol.V3:
232
console.log(`V3 pools: ${route.pools.length}`);
233
break;
234
case Protocol.MIXED:
235
console.log(`Mixed pools: ${route.pools.length}`);
236
break;
237
}
238
}
239
240
// Use with any route type
241
analyzeRoute(v2Route);
242
analyzeRoute(v3Route);
243
analyzeRoute(mixedRoute);
244
```
245
246
```typescript
247
// Helper function usage
248
const pathToken = getPathToken(ETH, pool);
249
console.log(`Path token: ${pathToken.symbol}`); // May be WETH if pool uses wrapped version
250
251
// Route comparison
252
function compareRoutes(routeA: IRoute<Currency, Currency, any>, routeB: IRoute<Currency, Currency, any>) {
253
const priceA = routeA.midPrice;
254
const priceB = routeB.midPrice;
255
256
if (priceA.greaterThan(priceB)) {
257
return `Route A (${routeA.protocol}) offers better price`;
258
} else if (priceB.greaterThan(priceA)) {
259
return `Route B (${routeB.protocol}) offers better price`;
260
} else {
261
return "Routes offer same price";
262
}
263
}
264
```
265
266
```typescript
267
// Create routes for trade construction
268
const routes: IRoute<Currency, Currency, any>[] = [];
269
270
// Add V2 routes
271
v2RoutesSDK.forEach(routeSDK => {
272
routes.push(new RouteV2(routeSDK));
273
});
274
275
// Add V3 routes
276
v3RoutesSDK.forEach(routeSDK => {
277
routes.push(new RouteV3(routeSDK));
278
});
279
280
// Filter by protocol
281
const v3Routes = routes.filter(route => route.protocol === Protocol.V3);
282
const allProtocols = routes.map(route => route.protocol);
283
```