0
# Trade Management
1
2
Multi-protocol trade aggregation system supporting V2, V3, V4, and mixed routes. The Trade class provides price impact calculation, slippage protection, and execution optimization across different Uniswap protocols.
3
4
## Capabilities
5
6
### Trade Class
7
8
Generic trade class for aggregating trades across V2, V3, V4, and mixed routes with comprehensive trade analysis and optimization.
9
10
```typescript { .api }
11
/**
12
* Aggregated trade class supporting multiple Uniswap protocols
13
*/
14
class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
15
/** Array of routes that make up this trade */
16
readonly routes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
17
18
/** Type of trade - exact input or exact output */
19
readonly tradeType: TTradeType;
20
21
/** Individual swap details for each route */
22
readonly swaps: {
23
route: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>;
24
inputAmount: CurrencyAmount<TInput>;
25
outputAmount: CurrencyAmount<TOutput>;
26
}[];
27
28
/**
29
* Create a trade from individual protocol routes
30
* @param routes - Configuration containing v2, v3, v4, and mixed routes
31
*/
32
constructor({
33
v2Routes?: {
34
routev2: V2RouteSDK<TInput, TOutput>;
35
inputAmount: CurrencyAmount<TInput>;
36
outputAmount: CurrencyAmount<TOutput>;
37
}[];
38
v3Routes?: {
39
routev3: V3RouteSDK<TInput, TOutput>;
40
inputAmount: CurrencyAmount<TInput>;
41
outputAmount: CurrencyAmount<TOutput>;
42
}[];
43
v4Routes?: {
44
routev4: V4RouteSDK<TInput, TOutput>;
45
inputAmount: CurrencyAmount<TInput>;
46
outputAmount: CurrencyAmount<TOutput>;
47
}[];
48
mixedRoutes?: {
49
mixedRoute: MixedRouteSDK<TInput, TOutput>;
50
inputAmount: CurrencyAmount<TInput>;
51
outputAmount: CurrencyAmount<TOutput>;
52
}[];
53
tradeType: TTradeType;
54
});
55
}
56
```
57
58
### Trade Properties and Analysis
59
60
Comprehensive trade analysis including amounts, price impact, and execution details.
61
62
```typescript { .api }
63
class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
64
/** Total input amount for the trade */
65
get inputAmount(): CurrencyAmount<TInput>;
66
67
/** Total output amount for the trade */
68
get outputAmount(): CurrencyAmount<TOutput>;
69
70
/**
71
* Detailed amount breakdown including native currency amounts
72
* @returns inputAmount, outputAmount, and native amounts where applicable
73
*/
74
get amounts(): {
75
inputAmount: CurrencyAmount<TInput>;
76
inputAmountNative: CurrencyAmount<TInput> | undefined;
77
outputAmount: CurrencyAmount<TOutput>;
78
outputAmountNative: CurrencyAmount<TOutput> | undefined;
79
};
80
81
/** Number of input wraps required if input is native ETH */
82
get numberOfInputWraps(): number;
83
84
/** Number of input unwraps required if input is WETH */
85
get numberOfInputUnwraps(): number;
86
87
/** Routes that use native ETH as input */
88
get nativeInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
89
90
/** Routes that use WETH as input */
91
get wethInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
92
93
/** The price expressed in terms of output amount/input amount */
94
get executionPrice(): Price<TInput, TOutput>;
95
96
/** Returns the sell tax of the input token */
97
get inputTax(): Percent;
98
99
/** Returns the buy tax of the output token */
100
get outputTax(): Percent;
101
102
/**
103
* Returns the percent difference between the route's mid price and the expected execution price
104
* Excludes token taxes from the price impact calculation
105
*/
106
get priceImpact(): Percent;
107
}
108
```
109
110
### Slippage Protection
111
112
Methods for calculating minimum and maximum amounts with slippage protection.
113
114
```typescript { .api }
115
class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
116
/**
117
* Get the minimum amount that must be received from this trade for the given slippage tolerance
118
* @param slippageTolerance - The tolerance of unfavorable slippage from the execution price
119
* @param amountOut - Optional specific amount out to calculate minimum for
120
* @returns The minimum amount out
121
*/
122
minimumAmountOut(
123
slippageTolerance: Percent,
124
amountOut?: CurrencyAmount<TOutput>
125
): CurrencyAmount<TOutput>;
126
127
/**
128
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
129
* @param slippageTolerance - The tolerance of unfavorable slippage from the execution price
130
* @param amountIn - Optional specific amount in to calculate maximum for
131
* @returns The maximum amount in
132
*/
133
maximumAmountIn(
134
slippageTolerance: Percent,
135
amountIn?: CurrencyAmount<TInput>
136
): CurrencyAmount<TInput>;
137
138
/**
139
* Return the execution price after accounting for slippage tolerance
140
* @param slippageTolerance - The allowed tolerated slippage
141
* @returns The worst-case execution price
142
*/
143
worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
144
}
145
```
146
147
### Static Factory Methods
148
149
Factory methods for creating trades from routes with automatic simulation.
150
151
```typescript { .api }
152
class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
153
/**
154
* Create a trade from multiple protocol routes with automatic quote simulation
155
* @param v2Routes - Array of V2 routes with amounts
156
* @param v3Routes - Array of V3 routes with amounts
157
* @param tradeType - Type of trade (exact input or exact output)
158
* @param mixedRoutes - Optional array of mixed routes
159
* @param v4Routes - Optional array of V4 routes
160
* @returns Promise resolving to constructed Trade
161
*/
162
static async fromRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(
163
v2Routes: {
164
routev2: V2RouteSDK<TInput, TOutput>;
165
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
166
}[],
167
v3Routes: {
168
routev3: V3RouteSDK<TInput, TOutput>;
169
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
170
}[],
171
tradeType: TTradeType,
172
mixedRoutes?: {
173
mixedRoute: MixedRouteSDK<TInput, TOutput>;
174
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
175
}[],
176
v4Routes?: {
177
routev4: V4RouteSDK<TInput, TOutput>;
178
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
179
}[]
180
): Promise<Trade<TInput, TOutput, TTradeType>>;
181
182
/**
183
* Create a trade from a single route of any protocol
184
* @param route - Single route (V2, V3, V4, or Mixed)
185
* @param amount - Amount to trade
186
* @param tradeType - Type of trade (exact input or exact output)
187
* @returns Promise resolving to constructed Trade
188
*/
189
static async fromRoute<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(
190
route: V2RouteSDK<TInput, TOutput> | V3RouteSDK<TInput, TOutput> | V4RouteSDK<TInput, TOutput> | MixedRouteSDK<TInput, TOutput>,
191
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>,
192
tradeType: TTradeType
193
): Promise<Trade<TInput, TOutput, TTradeType>>;
194
}
195
```
196
197
**Usage Examples:**
198
199
```typescript
200
import { Trade, Protocol } from "@uniswap/router-sdk";
201
import { CurrencyAmount, TradeType, Percent, Token } from "@uniswap/sdk-core";
202
import { Route as V3Route, Pool as V3Pool } from "@uniswap/v3-sdk";
203
204
// Create a multi-protocol trade
205
const trade = new Trade({
206
v2Routes: [{
207
routev2: v2Route,
208
inputAmount: CurrencyAmount.fromRawAmount(tokenA, "1000000"),
209
outputAmount: CurrencyAmount.fromRawAmount(tokenB, "2000000")
210
}],
211
v3Routes: [{
212
routev3: v3Route,
213
inputAmount: CurrencyAmount.fromRawAmount(tokenA, "500000"),
214
outputAmount: CurrencyAmount.fromRawAmount(tokenB, "1100000")
215
}],
216
tradeType: TradeType.EXACT_INPUT
217
});
218
219
// Analyze the trade
220
console.log("Input amount:", trade.inputAmount.toExact());
221
console.log("Output amount:", trade.outputAmount.toExact());
222
console.log("Execution price:", trade.executionPrice.toSignificant(6));
223
console.log("Price impact:", trade.priceImpact.toFixed(2) + "%");
224
225
// Calculate slippage-protected amounts
226
const slippage = new Percent(50, 10000); // 0.5%
227
const minOut = trade.minimumAmountOut(slippage);
228
console.log("Minimum out with 0.5% slippage:", minOut.toExact());
229
```
230
231
```typescript
232
// Create trade from routes with automatic simulation
233
const trade = await Trade.fromRoutes(
234
[], // No V2 routes
235
[{
236
routev3: new V3Route([pool1, pool2], tokenA, tokenB),
237
amount: CurrencyAmount.fromRawAmount(tokenA, "1000000")
238
}],
239
TradeType.EXACT_INPUT
240
);
241
242
// Check if this is a better trade than another
243
if (trade.outputAmount.greaterThan(otherTrade.outputAmount)) {
244
console.log("This trade provides more output");
245
}
246
```
247
248
```typescript
249
// Analyze native ETH wrapping requirements
250
if (trade.numberOfInputWraps > 0) {
251
console.log(`Trade requires ${trade.numberOfInputWraps} ETH wraps`);
252
}
253
254
// Get detailed amounts including native portions
255
const { inputAmountNative, outputAmountNative } = trade.amounts;
256
if (inputAmountNative) {
257
console.log("Native ETH input:", inputAmountNative.toExact());
258
}
259
```