This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
trade.ts
387 lines (362 loc) · 14.6 KB
/
trade.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import {
computePriceImpact,
Token,
Currency,
CurrencyAmount,
Fraction,
Percent,
Price,
sortedInsert,
TradeType
} from '@uniswap/sdk-core'
import { ONE, ZERO } from '../constants'
import invariant from 'tiny-invariant'
import { Pair } from './pair'
import { Route } from './route'
// minimal interface so the input output comparator may be shared across types
interface InputOutput<TInput extends Currency, TOutput extends Currency> {
readonly inputAmount: CurrencyAmount<TInput>
readonly outputAmount: CurrencyAmount<TOutput>
}
// comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts
// in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first
export function inputOutputComparator<TInput extends Currency, TOutput extends Currency>(
a: InputOutput<TInput, TOutput>,
b: InputOutput<TInput, TOutput>
): number {
// must have same input and output token for comparison
invariant(a.inputAmount.currency.equals(b.inputAmount.currency), 'INPUT_CURRENCY')
invariant(a.outputAmount.currency.equals(b.outputAmount.currency), 'OUTPUT_CURRENCY')
if (a.outputAmount.equalTo(b.outputAmount)) {
if (a.inputAmount.equalTo(b.inputAmount)) {
return 0
}
// trade A requires less input than trade B, so A should come first
if (a.inputAmount.lessThan(b.inputAmount)) {
return -1
} else {
return 1
}
} else {
// tradeA has less output than trade B, so should come second
if (a.outputAmount.lessThan(b.outputAmount)) {
return 1
} else {
return -1
}
}
}
// extension of the input output comparator that also considers other dimensions of the trade in ranking them
export function tradeComparator<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(
a: Trade<TInput, TOutput, TTradeType>,
b: Trade<TInput, TOutput, TTradeType>
) {
const ioComp = inputOutputComparator(a, b)
if (ioComp !== 0) {
return ioComp
}
// consider lowest slippage next, since these are less likely to fail
if (a.priceImpact.lessThan(b.priceImpact)) {
return -1
} else if (a.priceImpact.greaterThan(b.priceImpact)) {
return 1
}
// finally consider the number of hops since each hop costs gas
return a.route.path.length - b.route.path.length
}
export interface BestTradeOptions {
// how many results to return
maxNumResults?: number
// the maximum number of hops a trade should contain
maxHops?: number
}
/**
* Represents a trade executed against a list of pairs.
* Does not account for slippage, i.e. trades that front run this trade and move the price.
*/
export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
/**
* The route of the trade, i.e. which pairs the trade goes through and the input/output currencies.
*/
public readonly route: Route<TInput, TOutput>
/**
* The type of the trade, either exact in or exact out.
*/
public readonly tradeType: TTradeType
/**
* The input amount for the trade assuming no slippage.
*/
public readonly inputAmount: CurrencyAmount<TInput>
/**
* The output amount for the trade assuming no slippage.
*/
public readonly outputAmount: CurrencyAmount<TOutput>
/**
* The price expressed in terms of output amount/input amount.
*/
public readonly executionPrice: Price<TInput, TOutput>
/**
* The percent difference between the mid price before the trade and the trade execution price.
*/
public readonly priceImpact: Percent
/**
* Constructs an exact in trade with the given amount in and route
* @param route route of the exact in trade
* @param amountIn the amount being passed in
*/
public static exactIn<TInput extends Currency, TOutput extends Currency>(
route: Route<TInput, TOutput>,
amountIn: CurrencyAmount<TInput>
): Trade<TInput, TOutput, TradeType.EXACT_INPUT> {
return new Trade(route, amountIn, TradeType.EXACT_INPUT)
}
/**
* Constructs an exact out trade with the given amount out and route
* @param route route of the exact out trade
* @param amountOut the amount returned by the trade
*/
public static exactOut<TInput extends Currency, TOutput extends Currency>(
route: Route<TInput, TOutput>,
amountOut: CurrencyAmount<TOutput>
): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT> {
return new Trade(route, amountOut, TradeType.EXACT_OUTPUT)
}
public constructor(
route: Route<TInput, TOutput>,
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>,
tradeType: TTradeType
) {
this.route = route
this.tradeType = tradeType
const tokenAmounts: CurrencyAmount<Token>[] = new Array(route.path.length)
if (tradeType === TradeType.EXACT_INPUT) {
invariant(amount.currency.equals(route.input), 'INPUT')
tokenAmounts[0] = amount.wrapped
for (let i = 0; i < route.path.length - 1; i++) {
const pair = route.pairs[i]
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i])
tokenAmounts[i + 1] = outputAmount
}
this.inputAmount = CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator)
this.outputAmount = CurrencyAmount.fromFractionalAmount(
route.output,
tokenAmounts[tokenAmounts.length - 1].numerator,
tokenAmounts[tokenAmounts.length - 1].denominator
)
} else {
invariant(amount.currency.equals(route.output), 'OUTPUT')
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped
for (let i = route.path.length - 1; i > 0; i--) {
const pair = route.pairs[i - 1]
const [inputAmount] = pair.getInputAmount(tokenAmounts[i])
tokenAmounts[i - 1] = inputAmount
}
this.inputAmount = CurrencyAmount.fromFractionalAmount(
route.input,
tokenAmounts[0].numerator,
tokenAmounts[0].denominator
)
this.outputAmount = CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator)
}
this.executionPrice = new Price(
this.inputAmount.currency,
this.outputAmount.currency,
this.inputAmount.quotient,
this.outputAmount.quotient
)
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount)
}
/**
* Get the minimum amount that must be received from this trade for the given slippage tolerance
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
*/
public minimumAmountOut(slippageTolerance: Percent): CurrencyAmount<TOutput> {
invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE')
if (this.tradeType === TradeType.EXACT_OUTPUT) {
return this.outputAmount
} else {
const slippageAdjustedAmountOut = new Fraction(ONE)
.add(slippageTolerance)
.invert()
.multiply(this.outputAmount.quotient).quotient
return CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut)
}
}
/**
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
*/
public maximumAmountIn(slippageTolerance: Percent): CurrencyAmount<TInput> {
invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE')
if (this.tradeType === TradeType.EXACT_INPUT) {
return this.inputAmount
} else {
const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.quotient)
.quotient
return CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn)
}
}
/**
* Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
* amount to an output token, making at most `maxHops` hops.
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
* the amount in among multiple routes.
* @param pairs the pairs to consider in finding the best trade
* @param nextAmountIn exact amount of input currency to spend
* @param currencyOut the desired currency out
* @param maxNumResults maximum number of results to return
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
* @param currentPairs used in recursion; the current list of pairs
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
* @param bestTrades used in recursion; the current list of best trades
*/
public static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(
pairs: Pair[],
currencyAmountIn: CurrencyAmount<TInput>,
currencyOut: TOutput,
{ maxNumResults = 3, maxHops = 3 }: BestTradeOptions = {},
// used in recursion.
currentPairs: Pair[] = [],
nextAmountIn: CurrencyAmount<Currency> = currencyAmountIn,
bestTrades: Trade<TInput, TOutput, TradeType.EXACT_INPUT>[] = []
): Trade<TInput, TOutput, TradeType.EXACT_INPUT>[] {
invariant(pairs.length > 0, 'PAIRS')
invariant(maxHops > 0, 'MAX_HOPS')
invariant(currencyAmountIn === nextAmountIn || currentPairs.length > 0, 'INVALID_RECURSION')
const amountIn = nextAmountIn.wrapped
const tokenOut = currencyOut.wrapped
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i]
// pair irrelevant
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency)) continue
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue
let amountOut: CurrencyAmount<Token>
try {
;[amountOut] = pair.getOutputAmount(amountIn)
} catch (error) {
// input too low
if (error.isInsufficientInputAmountError) {
continue
}
throw error
}
// we have arrived at the output token, so this is the final trade of one of the paths
if (amountOut.currency.equals(tokenOut)) {
sortedInsert(
bestTrades,
new Trade(
new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut),
currencyAmountIn,
TradeType.EXACT_INPUT
),
maxNumResults,
tradeComparator
)
} else if (maxHops > 1 && pairs.length > 1) {
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length))
// otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
Trade.bestTradeExactIn(
pairsExcludingThisPair,
currencyAmountIn,
currencyOut,
{
maxNumResults,
maxHops: maxHops - 1
},
[...currentPairs, pair],
amountOut,
bestTrades
)
}
}
return bestTrades
}
/**
* Return the execution price after accounting for slippage tolerance
* @param slippageTolerance the allowed tolerated slippage
*/
public worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput> {
return new Price(
this.inputAmount.currency,
this.outputAmount.currency,
this.maximumAmountIn(slippageTolerance).quotient,
this.minimumAmountOut(slippageTolerance).quotient
)
}
/**
* similar to the above method but instead targets a fixed output amount
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
* to an output token amount, making at most `maxHops` hops
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
* the amount in among multiple routes.
* @param pairs the pairs to consider in finding the best trade
* @param currencyIn the currency to spend
* @param nextAmountOut the exact amount of currency out
* @param maxNumResults maximum number of results to return
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
* @param currentPairs used in recursion; the current list of pairs
* @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
* @param bestTrades used in recursion; the current list of best trades
*/
public static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(
pairs: Pair[],
currencyIn: TInput,
currencyAmountOut: CurrencyAmount<TOutput>,
{ maxNumResults = 3, maxHops = 3 }: BestTradeOptions = {},
// used in recursion.
currentPairs: Pair[] = [],
nextAmountOut: CurrencyAmount<Currency> = currencyAmountOut,
bestTrades: Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[] = []
): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[] {
invariant(pairs.length > 0, 'PAIRS')
invariant(maxHops > 0, 'MAX_HOPS')
invariant(currencyAmountOut === nextAmountOut || currentPairs.length > 0, 'INVALID_RECURSION')
const amountOut = nextAmountOut.wrapped
const tokenIn = currencyIn.wrapped
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i]
// pair irrelevant
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency)) continue
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue
let amountIn: CurrencyAmount<Token>
try {
;[amountIn] = pair.getInputAmount(amountOut)
} catch (error) {
// not enough liquidity in this pair
if (error.isInsufficientReservesError) {
continue
}
throw error
}
// we have arrived at the input token, so this is the first trade of one of the paths
if (amountIn.currency.equals(tokenIn)) {
sortedInsert(
bestTrades,
new Trade(
new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency),
currencyAmountOut,
TradeType.EXACT_OUTPUT
),
maxNumResults,
tradeComparator
)
} else if (maxHops > 1 && pairs.length > 1) {
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length))
// otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
Trade.bestTradeExactOut(
pairsExcludingThisPair,
currencyIn,
currencyAmountOut,
{
maxNumResults,
maxHops: maxHops - 1
},
[pair, ...currentPairs],
amountIn,
bestTrades
)
}
}
return bestTrades
}
}