@fenine/router-sdk 1.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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +16 -0
  3. package/dist/cjs/src/approveAndCall.d.ts +33 -0
  4. package/dist/cjs/src/approveAndCall.js +111 -0
  5. package/dist/cjs/src/approveAndCall.js.map +1 -0
  6. package/dist/cjs/src/constants.d.ts +13 -0
  7. package/dist/cjs/src/constants.js +22 -0
  8. package/dist/cjs/src/constants.js.map +1 -0
  9. package/dist/cjs/src/entities/mixedRoute/route.d.ts +29 -0
  10. package/dist/cjs/src/entities/mixedRoute/route.js +119 -0
  11. package/dist/cjs/src/entities/mixedRoute/route.js.map +1 -0
  12. package/dist/cjs/src/entities/mixedRoute/trade.d.ts +183 -0
  13. package/dist/cjs/src/entities/mixedRoute/trade.js +354 -0
  14. package/dist/cjs/src/entities/mixedRoute/trade.js.map +1 -0
  15. package/dist/cjs/src/entities/protocol.d.ts +6 -0
  16. package/dist/cjs/src/entities/protocol.js +11 -0
  17. package/dist/cjs/src/entities/protocol.js.map +1 -0
  18. package/dist/cjs/src/entities/route.d.ts +40 -0
  19. package/dist/cjs/src/entities/route.js +63 -0
  20. package/dist/cjs/src/entities/route.js.map +1 -0
  21. package/dist/cjs/src/entities/trade.d.ts +136 -0
  22. package/dist/cjs/src/entities/trade.js +370 -0
  23. package/dist/cjs/src/entities/trade.js.map +1 -0
  24. package/dist/cjs/src/index.d.ts +14 -0
  25. package/dist/cjs/src/index.js +18 -0
  26. package/dist/cjs/src/index.js.map +1 -0
  27. package/dist/cjs/src/multicallExtended.d.ts +11 -0
  28. package/dist/cjs/src/multicallExtended.js +44 -0
  29. package/dist/cjs/src/multicallExtended.js.map +1 -0
  30. package/dist/cjs/src/paymentsExtended.d.ts +15 -0
  31. package/dist/cjs/src/paymentsExtended.js +66 -0
  32. package/dist/cjs/src/paymentsExtended.js.map +1 -0
  33. package/dist/cjs/src/swapRouter.d.ts +95 -0
  34. package/dist/cjs/src/swapRouter.js +439 -0
  35. package/dist/cjs/src/swapRouter.js.map +1 -0
  36. package/dist/cjs/src/utils/TPool.d.ts +4 -0
  37. package/dist/cjs/src/utils/TPool.js +3 -0
  38. package/dist/cjs/src/utils/TPool.js.map +1 -0
  39. package/dist/cjs/src/utils/encodeMixedRouteToPath.d.ts +10 -0
  40. package/dist/cjs/src/utils/encodeMixedRouteToPath.js +91 -0
  41. package/dist/cjs/src/utils/encodeMixedRouteToPath.js.map +1 -0
  42. package/dist/cjs/src/utils/index.d.ts +16 -0
  43. package/dist/cjs/src/utils/index.js +51 -0
  44. package/dist/cjs/src/utils/index.js.map +1 -0
  45. package/dist/cjs/src/utils/pathCurrency.d.ts +4 -0
  46. package/dist/cjs/src/utils/pathCurrency.js +35 -0
  47. package/dist/cjs/src/utils/pathCurrency.js.map +1 -0
  48. package/dist/esm/src/approveAndCall.d.ts +33 -0
  49. package/dist/esm/src/approveAndCall.js +105 -0
  50. package/dist/esm/src/approveAndCall.js.map +1 -0
  51. package/dist/esm/src/constants.d.ts +13 -0
  52. package/dist/esm/src/constants.js +18 -0
  53. package/dist/esm/src/constants.js.map +1 -0
  54. package/dist/esm/src/entities/mixedRoute/route.d.ts +29 -0
  55. package/dist/esm/src/entities/mixedRoute/route.js +114 -0
  56. package/dist/esm/src/entities/mixedRoute/route.js.map +1 -0
  57. package/dist/esm/src/entities/mixedRoute/trade.d.ts +183 -0
  58. package/dist/esm/src/entities/mixedRoute/trade.js +348 -0
  59. package/dist/esm/src/entities/mixedRoute/trade.js.map +1 -0
  60. package/dist/esm/src/entities/protocol.d.ts +6 -0
  61. package/dist/esm/src/entities/protocol.js +8 -0
  62. package/dist/esm/src/entities/protocol.js.map +1 -0
  63. package/dist/esm/src/entities/route.d.ts +40 -0
  64. package/dist/esm/src/entities/route.js +55 -0
  65. package/dist/esm/src/entities/route.js.map +1 -0
  66. package/dist/esm/src/entities/trade.d.ts +136 -0
  67. package/dist/esm/src/entities/trade.js +365 -0
  68. package/dist/esm/src/entities/trade.js.map +1 -0
  69. package/dist/esm/src/index.d.ts +14 -0
  70. package/dist/esm/src/index.js +15 -0
  71. package/dist/esm/src/index.js.map +1 -0
  72. package/dist/esm/src/multicallExtended.d.ts +11 -0
  73. package/dist/esm/src/multicallExtended.js +39 -0
  74. package/dist/esm/src/multicallExtended.js.map +1 -0
  75. package/dist/esm/src/paymentsExtended.d.ts +15 -0
  76. package/dist/esm/src/paymentsExtended.js +61 -0
  77. package/dist/esm/src/paymentsExtended.js.map +1 -0
  78. package/dist/esm/src/swapRouter.d.ts +95 -0
  79. package/dist/esm/src/swapRouter.js +434 -0
  80. package/dist/esm/src/swapRouter.js.map +1 -0
  81. package/dist/esm/src/utils/TPool.d.ts +4 -0
  82. package/dist/esm/src/utils/TPool.js +2 -0
  83. package/dist/esm/src/utils/TPool.js.map +1 -0
  84. package/dist/esm/src/utils/encodeMixedRouteToPath.d.ts +10 -0
  85. package/dist/esm/src/utils/encodeMixedRouteToPath.js +87 -0
  86. package/dist/esm/src/utils/encodeMixedRouteToPath.js.map +1 -0
  87. package/dist/esm/src/utils/index.d.ts +16 -0
  88. package/dist/esm/src/utils/index.js +46 -0
  89. package/dist/esm/src/utils/index.js.map +1 -0
  90. package/dist/esm/src/utils/pathCurrency.d.ts +4 -0
  91. package/dist/esm/src/utils/pathCurrency.js +30 -0
  92. package/dist/esm/src/utils/pathCurrency.js.map +1 -0
  93. package/dist/types/src/approveAndCall.d.ts +33 -0
  94. package/dist/types/src/constants.d.ts +13 -0
  95. package/dist/types/src/entities/mixedRoute/route.d.ts +29 -0
  96. package/dist/types/src/entities/mixedRoute/trade.d.ts +183 -0
  97. package/dist/types/src/entities/protocol.d.ts +6 -0
  98. package/dist/types/src/entities/route.d.ts +40 -0
  99. package/dist/types/src/entities/trade.d.ts +136 -0
  100. package/dist/types/src/index.d.ts +14 -0
  101. package/dist/types/src/multicallExtended.d.ts +11 -0
  102. package/dist/types/src/paymentsExtended.d.ts +15 -0
  103. package/dist/types/src/swapRouter.d.ts +95 -0
  104. package/dist/types/src/utils/TPool.d.ts +4 -0
  105. package/dist/types/src/utils/encodeMixedRouteToPath.d.ts +10 -0
  106. package/dist/types/src/utils/index.d.ts +16 -0
  107. package/dist/types/src/utils/pathCurrency.d.ts +4 -0
  108. package/package.json +66 -0
@@ -0,0 +1,354 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MixedRouteTrade = exports.tradeComparator = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const sdk_core_1 = require("@fenine/sdk-core");
6
+ const v2_sdk_1 = require("@uniswap/v2-sdk");
7
+ const v3_sdk_1 = require("@fenine/v3-sdk");
8
+ const v4_sdk_1 = require("@uniswap/v4-sdk");
9
+ const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
10
+ const constants_1 = require("../../constants");
11
+ const route_1 = require("./route");
12
+ const pathCurrency_1 = require("../../utils/pathCurrency");
13
+ /**
14
+ * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
15
+ * @template TInput The input token, either Ether or an ERC-20
16
+ * @template TOutput The output token, either Ether or an ERC-20
17
+ * @template TTradeType The trade type, either exact input or exact output
18
+ * @param a The first trade to compare
19
+ * @param b The second trade to compare
20
+ * @returns A sorted ordering for two neighboring elements in a trade array
21
+ */
22
+ function tradeComparator(a, b) {
23
+ // must have same input and output token for comparison
24
+ (0, tiny_invariant_1.default)(a.inputAmount.currency.equals(b.inputAmount.currency), 'INPUT_CURRENCY');
25
+ (0, tiny_invariant_1.default)(a.outputAmount.currency.equals(b.outputAmount.currency), 'OUTPUT_CURRENCY');
26
+ if (a.outputAmount.equalTo(b.outputAmount)) {
27
+ if (a.inputAmount.equalTo(b.inputAmount)) {
28
+ // consider the number of hops since each hop costs gas
29
+ const aHops = a.swaps.reduce((total, cur) => total + cur.route.path.length, 0);
30
+ const bHops = b.swaps.reduce((total, cur) => total + cur.route.path.length, 0);
31
+ return aHops - bHops;
32
+ }
33
+ // trade A requires less input than trade B, so A should come first
34
+ if (a.inputAmount.lessThan(b.inputAmount)) {
35
+ return -1;
36
+ }
37
+ else {
38
+ return 1;
39
+ }
40
+ }
41
+ else {
42
+ // tradeA has less output than trade B, so should come second
43
+ if (a.outputAmount.lessThan(b.outputAmount)) {
44
+ return 1;
45
+ }
46
+ else {
47
+ return -1;
48
+ }
49
+ }
50
+ }
51
+ exports.tradeComparator = tradeComparator;
52
+ /**
53
+ * Represents a trade executed against a set of routes where some percentage of the input is
54
+ * split across each route.
55
+ *
56
+ * Each route has its own set of pools. Pools can not be re-used across routes.
57
+ *
58
+ * Does not account for slippage, i.e., changes in price environment that can occur between
59
+ * the time the trade is submitted and when it is executed.
60
+ * @notice This class is functionally the same as the `Trade` class in the `@uniswap/v3-sdk` package, aside from typing and some input validation.
61
+ * @template TInput The input token, either Ether or an ERC-20
62
+ * @template TOutput The output token, either Ether or an ERC-20
63
+ * @template TTradeType The trade type, either exact input or exact output
64
+ */
65
+ class MixedRouteTrade {
66
+ /**
67
+ * @deprecated Deprecated in favor of 'swaps' property. If the trade consists of multiple routes
68
+ * this will return an error.
69
+ *
70
+ * When the trade consists of just a single route, this returns the route of the trade,
71
+ * i.e. which pools the trade goes through.
72
+ */
73
+ get route() {
74
+ (0, tiny_invariant_1.default)(this.swaps.length === 1, 'MULTIPLE_ROUTES');
75
+ return this.swaps[0].route;
76
+ }
77
+ /**
78
+ * The input amount for the trade assuming no slippage.
79
+ */
80
+ get inputAmount() {
81
+ if (this._inputAmount) {
82
+ return this._inputAmount;
83
+ }
84
+ const inputCurrency = this.swaps[0].inputAmount.currency;
85
+ const totalInputFromRoutes = this.swaps
86
+ .map(({ inputAmount }) => inputAmount)
87
+ .reduce((total, cur) => total.add(cur), sdk_core_1.CurrencyAmount.fromRawAmount(inputCurrency, 0));
88
+ this._inputAmount = totalInputFromRoutes;
89
+ return this._inputAmount;
90
+ }
91
+ /**
92
+ * The output amount for the trade assuming no slippage.
93
+ */
94
+ get outputAmount() {
95
+ if (this._outputAmount) {
96
+ return this._outputAmount;
97
+ }
98
+ const outputCurrency = this.swaps[0].outputAmount.currency;
99
+ const totalOutputFromRoutes = this.swaps
100
+ .map(({ outputAmount }) => outputAmount)
101
+ .reduce((total, cur) => total.add(cur), sdk_core_1.CurrencyAmount.fromRawAmount(outputCurrency, 0));
102
+ this._outputAmount = totalOutputFromRoutes;
103
+ return this._outputAmount;
104
+ }
105
+ /**
106
+ * The price expressed in terms of output amount/input amount.
107
+ */
108
+ get executionPrice() {
109
+ var _a;
110
+ return ((_a = this._executionPrice) !== null && _a !== void 0 ? _a : (this._executionPrice = new sdk_core_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient)));
111
+ }
112
+ /**
113
+ * Returns the percent difference between the route's mid price and the price impact
114
+ */
115
+ get priceImpact() {
116
+ if (this._priceImpact) {
117
+ return this._priceImpact;
118
+ }
119
+ let spotOutputAmount = sdk_core_1.CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
120
+ for (const { route, inputAmount } of this.swaps) {
121
+ const midPrice = route.midPrice;
122
+ spotOutputAmount = spotOutputAmount.add(midPrice.quote(inputAmount));
123
+ }
124
+ const priceImpact = spotOutputAmount.subtract(this.outputAmount).divide(spotOutputAmount);
125
+ this._priceImpact = new sdk_core_1.Percent(priceImpact.numerator, priceImpact.denominator);
126
+ return this._priceImpact;
127
+ }
128
+ /**
129
+ * Constructs a trade by simulating swaps through the given route
130
+ * @template TInput The input token, either Ether or an ERC-20.
131
+ * @template TOutput The output token, either Ether or an ERC-20.
132
+ * @template TTradeType The type of the trade, either exact in or exact out.
133
+ * @param route route to swap through
134
+ * @param amount the amount specified, either input or output, depending on tradeType
135
+ * @param tradeType whether the trade is an exact input or exact output swap
136
+ * @returns The route
137
+ */
138
+ static async fromRoute(route, amount, tradeType) {
139
+ const amounts = new Array(route.path.length);
140
+ let inputAmount;
141
+ let outputAmount;
142
+ (0, tiny_invariant_1.default)(tradeType === sdk_core_1.TradeType.EXACT_INPUT, 'TRADE_TYPE');
143
+ (0, tiny_invariant_1.default)(amount.currency.equals(route.input), 'INPUT');
144
+ amounts[0] = (0, pathCurrency_1.amountWithPathCurrency)(amount, route.pools[0]);
145
+ for (let i = 0; i < route.path.length - 1; i++) {
146
+ const pool = route.pools[i];
147
+ const [outputAmount] = await pool.getOutputAmount((0, pathCurrency_1.amountWithPathCurrency)(amounts[i], pool));
148
+ amounts[i + 1] = outputAmount;
149
+ }
150
+ inputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
151
+ outputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.output, amounts[amounts.length - 1].numerator, amounts[amounts.length - 1].denominator);
152
+ return new MixedRouteTrade({
153
+ routes: [{ inputAmount, outputAmount, route }],
154
+ tradeType,
155
+ });
156
+ }
157
+ /**
158
+ * Constructs a trade from routes by simulating swaps
159
+ *
160
+ * @template TInput The input token, either Ether or an ERC-20.
161
+ * @template TOutput The output token, either Ether or an ERC-20.
162
+ * @template TTradeType The type of the trade, either exact in or exact out.
163
+ * @param routes the routes to swap through and how much of the amount should be routed through each
164
+ * @param tradeType whether the trade is an exact input or exact output swap
165
+ * @returns The trade
166
+ */
167
+ static async fromRoutes(routes, tradeType) {
168
+ const populatedRoutes = [];
169
+ (0, tiny_invariant_1.default)(tradeType === sdk_core_1.TradeType.EXACT_INPUT, 'TRADE_TYPE');
170
+ for (const { route, amount } of routes) {
171
+ const amounts = new Array(route.path.length);
172
+ let inputAmount;
173
+ let outputAmount;
174
+ (0, tiny_invariant_1.default)(amount.currency.equals(route.input), 'INPUT');
175
+ inputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
176
+ amounts[0] = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.pathInput, amount.numerator, amount.denominator);
177
+ for (let i = 0; i < route.path.length - 1; i++) {
178
+ const pool = route.pools[i];
179
+ const [outputAmount] = await pool.getOutputAmount((0, pathCurrency_1.amountWithPathCurrency)(amounts[i], pool));
180
+ amounts[i + 1] = outputAmount;
181
+ }
182
+ outputAmount = sdk_core_1.CurrencyAmount.fromFractionalAmount(route.output, amounts[amounts.length - 1].numerator, amounts[amounts.length - 1].denominator);
183
+ populatedRoutes.push({ route, inputAmount, outputAmount });
184
+ }
185
+ return new MixedRouteTrade({
186
+ routes: populatedRoutes,
187
+ tradeType,
188
+ });
189
+ }
190
+ /**
191
+ * Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
192
+ * elsewhere and do not have any tick data
193
+ * @template TInput The input token, either Ether or an ERC-20
194
+ * @template TOutput The output token, either Ether or an ERC-20
195
+ * @template TTradeType The type of the trade, either exact in or exact out
196
+ * @param constructorArguments The arguments passed to the trade constructor
197
+ * @returns The unchecked trade
198
+ */
199
+ static createUncheckedTrade(constructorArguments) {
200
+ return new MixedRouteTrade({
201
+ ...constructorArguments,
202
+ routes: [
203
+ {
204
+ inputAmount: constructorArguments.inputAmount,
205
+ outputAmount: constructorArguments.outputAmount,
206
+ route: constructorArguments.route,
207
+ },
208
+ ],
209
+ });
210
+ }
211
+ /**
212
+ * Creates a trade without computing the result of swapping through the routes. Useful when you have simulated the trade
213
+ * elsewhere and do not have any tick data
214
+ * @template TInput The input token, either Ether or an ERC-20
215
+ * @template TOutput The output token, either Ether or an ERC-20
216
+ * @template TTradeType The type of the trade, either exact in or exact out
217
+ * @param constructorArguments The arguments passed to the trade constructor
218
+ * @returns The unchecked trade
219
+ */
220
+ static createUncheckedTradeWithMultipleRoutes(constructorArguments) {
221
+ return new MixedRouteTrade(constructorArguments);
222
+ }
223
+ /**
224
+ * Construct a trade by passing in the pre-computed property values
225
+ * @param routes The routes through which the trade occurs
226
+ * @param tradeType The type of trade, exact input or exact output
227
+ */
228
+ constructor({ routes, tradeType, }) {
229
+ const inputCurrency = routes[0].inputAmount.currency;
230
+ const outputCurrency = routes[0].outputAmount.currency;
231
+ (0, tiny_invariant_1.default)(routes.every(({ route }) => inputCurrency.wrapped.equals(route.input.wrapped)), 'INPUT_CURRENCY_MATCH');
232
+ (0, tiny_invariant_1.default)(routes.every(({ route }) => outputCurrency.wrapped.equals(route.output.wrapped)), 'OUTPUT_CURRENCY_MATCH');
233
+ const numPools = routes.map(({ route }) => route.pools.length).reduce((total, cur) => total + cur, 0);
234
+ const poolIdentifierSet = new Set();
235
+ for (const { route } of routes) {
236
+ for (const pool of route.pools) {
237
+ if (pool instanceof v4_sdk_1.Pool) {
238
+ poolIdentifierSet.add(pool.poolId);
239
+ }
240
+ else if (pool instanceof v3_sdk_1.Pool) {
241
+ poolIdentifierSet.add(v3_sdk_1.Pool.getAddress(pool.token0, pool.token1, pool.fee));
242
+ }
243
+ else if (pool instanceof v2_sdk_1.Pair) {
244
+ const pair = pool;
245
+ poolIdentifierSet.add(v2_sdk_1.Pair.getAddress(pair.token0, pair.token1));
246
+ }
247
+ else {
248
+ throw new Error('Unexpected pool type in route when constructing trade object');
249
+ }
250
+ }
251
+ }
252
+ (0, tiny_invariant_1.default)(numPools === poolIdentifierSet.size, 'POOLS_DUPLICATED');
253
+ (0, tiny_invariant_1.default)(tradeType === sdk_core_1.TradeType.EXACT_INPUT, 'TRADE_TYPE');
254
+ this.swaps = routes;
255
+ this.tradeType = tradeType;
256
+ }
257
+ /**
258
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
259
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
260
+ * @returns The amount out
261
+ */
262
+ minimumAmountOut(slippageTolerance, amountOut = this.outputAmount) {
263
+ (0, tiny_invariant_1.default)(!slippageTolerance.lessThan(constants_1.ZERO), 'SLIPPAGE_TOLERANCE');
264
+ /// does not support exactOutput, as enforced in the constructor
265
+ const slippageAdjustedAmountOut = new sdk_core_1.Fraction(constants_1.ONE)
266
+ .add(slippageTolerance)
267
+ .invert()
268
+ .multiply(amountOut.quotient).quotient;
269
+ return sdk_core_1.CurrencyAmount.fromRawAmount(amountOut.currency, slippageAdjustedAmountOut);
270
+ }
271
+ /**
272
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
273
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
274
+ * @returns The amount in
275
+ */
276
+ maximumAmountIn(slippageTolerance, amountIn = this.inputAmount) {
277
+ (0, tiny_invariant_1.default)(!slippageTolerance.lessThan(constants_1.ZERO), 'SLIPPAGE_TOLERANCE');
278
+ return amountIn;
279
+ /// does not support exactOutput
280
+ }
281
+ /**
282
+ * Return the execution price after accounting for slippage tolerance
283
+ * @param slippageTolerance the allowed tolerated slippage
284
+ * @returns The execution price
285
+ */
286
+ worstExecutionPrice(slippageTolerance) {
287
+ return new sdk_core_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
288
+ }
289
+ /**
290
+ * Given a list of pools, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
291
+ * amount to an output token, making at most `maxHops` hops.
292
+ * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
293
+ * the amount in among multiple routes.
294
+ * @param pools the pools to consider in finding the best trade
295
+ * @param nextAmountIn exact amount of input currency to spend
296
+ * @param currencyOut the desired currency out
297
+ * @param maxNumResults maximum number of results to return
298
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
299
+ * @param currentPools used in recursion; the current list of pools
300
+ * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
301
+ * @param bestTrades used in recursion; the current list of best trades
302
+ * @returns The exact in trade
303
+ */
304
+ static async bestTradeExactIn(pools, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {},
305
+ // used in recursion.
306
+ currentPools = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
307
+ (0, tiny_invariant_1.default)(pools.length > 0, 'POOLS');
308
+ (0, tiny_invariant_1.default)(maxHops > 0, 'MAX_HOPS');
309
+ (0, tiny_invariant_1.default)(currencyAmountIn === nextAmountIn || currentPools.length > 0, 'INVALID_RECURSION');
310
+ const amountIn = nextAmountIn;
311
+ for (let i = 0; i < pools.length; i++) {
312
+ const pool = pools[i];
313
+ const amountInAdjusted = pool instanceof v4_sdk_1.Pool ? amountIn : amountIn.wrapped;
314
+ // pool irrelevant
315
+ if (!pool.token0.equals(amountInAdjusted.currency) && !pool.token1.equals(amountInAdjusted.currency))
316
+ continue;
317
+ if (pool instanceof v2_sdk_1.Pair) {
318
+ if (pool.reserve0.equalTo(constants_1.ZERO) || pool.reserve1.equalTo(constants_1.ZERO))
319
+ continue;
320
+ }
321
+ let amountOut;
322
+ try {
323
+ ;
324
+ [amountOut] =
325
+ pool instanceof v4_sdk_1.Pool
326
+ ? await pool.getOutputAmount(amountInAdjusted)
327
+ : await pool.getOutputAmount(amountInAdjusted.wrapped);
328
+ }
329
+ catch (error) {
330
+ // input too low
331
+ // @ts-ignore[2571] error is unknown
332
+ if (error.isInsufficientInputAmountError) {
333
+ continue;
334
+ }
335
+ throw error;
336
+ }
337
+ // we have arrived at the output token, so this is the final trade of one of the paths
338
+ if (amountOut.currency.wrapped.equals(currencyOut.wrapped)) {
339
+ (0, sdk_core_1.sortedInsert)(bestTrades, await MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([...currentPools, pool], currencyAmountIn.currency, currencyOut), currencyAmountIn, sdk_core_1.TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
340
+ }
341
+ else if (maxHops > 1 && pools.length > 1) {
342
+ const poolsExcludingThisPool = pools.slice(0, i).concat(pools.slice(i + 1, pools.length));
343
+ // otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
344
+ await MixedRouteTrade.bestTradeExactIn(poolsExcludingThisPool, currencyAmountIn, currencyOut, {
345
+ maxNumResults,
346
+ maxHops: maxHops - 1,
347
+ }, [...currentPools, pool], amountOut, bestTrades);
348
+ }
349
+ }
350
+ return bestTrades;
351
+ }
352
+ }
353
+ exports.MixedRouteTrade = MixedRouteTrade;
354
+ //# sourceMappingURL=trade.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trade.js","sourceRoot":"","sources":["../../../../../src/entities/mixedRoute/trade.ts"],"names":[],"mappings":";;;;AAAA,+CAAqH;AACrH,4CAAsC;AACtC,2CAAiE;AACjE,4CAAgD;AAChD,4EAAsC;AACtC,+CAA2C;AAC3C,mCAAuC;AACvC,2DAAiE;AAGjE;;;;;;;;GAQG;AACH,SAAgB,eAAe,CAC7B,CAA+C,EAC/C,CAA+C;IAE/C,uDAAuD;IACvD,IAAA,wBAAS,EAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,gBAAgB,CAAC,CAAA;IAClF,IAAA,wBAAS,EAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,EAAE,iBAAiB,CAAC,CAAA;IACrF,IAAI,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,EAAE;QAC1C,IAAI,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,EAAE;YACxC,uDAAuD;YACvD,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;YAC9E,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;YAC9E,OAAO,KAAK,GAAG,KAAK,CAAA;SACrB;QACD,mEA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C,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;gBAEzF,4GAA4G;gBAC5G,MAAM,eAAe,CAAC,gBAAgB,CACpC,sBAAsB,EACtB,gBAAgB,EAChB,WAAW,EACX;oBACE,aAAa;oBACb,OAAO,EAAE,OAAO,GAAG,CAAC;iBACrB,EACD,CAAC,GAAG,YAAY,EAAE,IAAI,CAAC,EACvB,SAAS,EACT,UAAU,CACX,CAAA;aACF;SACF;QAED,OAAO,UAAU,CAAA;IACnB,CAAC;CACF;AAvcD,0CAucC"}
@@ -0,0 +1,6 @@
1
+ export declare enum Protocol {
2
+ V2 = "V2",
3
+ V3 = "V3",
4
+ V4 = "V4",
5
+ MIXED = "MIXED"
6
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Protocol = void 0;
4
+ var Protocol;
5
+ (function (Protocol) {
6
+ Protocol["V2"] = "V2";
7
+ Protocol["V3"] = "V3";
8
+ Protocol["V4"] = "V4";
9
+ Protocol["MIXED"] = "MIXED";
10
+ })(Protocol = exports.Protocol || (exports.Protocol = {}));
11
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../../../src/entities/protocol.ts"],"names":[],"mappings":";;;AAAA,IAAY,QAKX;AALD,WAAY,QAAQ;IAClB,qBAAS,CAAA;IACT,qBAAS,CAAA;IACT,qBAAS,CAAA;IACT,2BAAe,CAAA;AACjB,CAAC,EALW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAKnB"}
@@ -0,0 +1,40 @@
1
+ import { Route as V2RouteSDK, Pair } from '@uniswap/v2-sdk';
2
+ import { Route as V3RouteSDK, Pool as V3Pool } from '@fenine/v3-sdk';
3
+ import { Route as V4RouteSDK, Pool as V4Pool } from '@uniswap/v4-sdk';
4
+ import { Protocol } from './protocol';
5
+ import { Currency, Price, Token } from '@fenine/sdk-core';
6
+ import { MixedRouteSDK } from './mixedRoute/route';
7
+ export declare function getPathToken(currency: Currency, pool: Pair | V3Pool): Token;
8
+ export interface IRoute<TInput extends Currency, TOutput extends Currency, TPool extends Pair | V3Pool | V4Pool> {
9
+ protocol: Protocol;
10
+ pools: TPool[];
11
+ path: Currency[];
12
+ midPrice: Price<TInput, TOutput>;
13
+ input: TInput;
14
+ output: TOutput;
15
+ pathInput: Currency;
16
+ pathOutput: Currency;
17
+ }
18
+ export declare class RouteV2<TInput extends Currency, TOutput extends Currency> extends V2RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, Pair> {
19
+ readonly protocol: Protocol;
20
+ readonly pools: Pair[];
21
+ pathInput: Currency;
22
+ pathOutput: Currency;
23
+ constructor(v2Route: V2RouteSDK<TInput, TOutput>);
24
+ }
25
+ export declare class RouteV3<TInput extends Currency, TOutput extends Currency> extends V3RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, V3Pool> {
26
+ readonly protocol: Protocol;
27
+ readonly path: Token[];
28
+ pathInput: Currency;
29
+ pathOutput: Currency;
30
+ constructor(v3Route: V3RouteSDK<TInput, TOutput>);
31
+ }
32
+ export declare class RouteV4<TInput extends Currency, TOutput extends Currency> extends V4RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, V4Pool> {
33
+ readonly protocol: Protocol;
34
+ readonly path: Currency[];
35
+ constructor(v4Route: V4RouteSDK<TInput, TOutput>);
36
+ }
37
+ export declare class MixedRoute<TInput extends Currency, TOutput extends Currency> extends MixedRouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, Pair | V3Pool | V4Pool> {
38
+ readonly protocol: Protocol;
39
+ constructor(mixedRoute: MixedRouteSDK<TInput, TOutput>);
40
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ // entities/route.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.MixedRoute = exports.RouteV4 = exports.RouteV3 = exports.RouteV2 = exports.getPathToken = void 0;
5
+ const v2_sdk_1 = require("@uniswap/v2-sdk");
6
+ const v3_sdk_1 = require("@fenine/v3-sdk");
7
+ const v4_sdk_1 = require("@uniswap/v4-sdk");
8
+ const protocol_1 = require("./protocol");
9
+ const route_1 = require("./mixedRoute/route");
10
+ // Helper function to get the pathInput and pathOutput for a V2 / V3 route
11
+ // currency could be native so we check against the wrapped version as they don't support native ETH in path
12
+ function getPathToken(currency, pool) {
13
+ if (pool.token0.wrapped.equals(currency.wrapped)) {
14
+ return pool.token0;
15
+ }
16
+ else if (pool.token1.wrapped.equals(currency.wrapped)) {
17
+ return pool.token1;
18
+ }
19
+ else {
20
+ throw new Error(`Expected token ${currency.symbol} to be either ${pool.token0.symbol} or ${pool.token1.symbol}`);
21
+ }
22
+ }
23
+ exports.getPathToken = getPathToken;
24
+ // V2 route wrapper
25
+ class RouteV2 extends v2_sdk_1.Route {
26
+ constructor(v2Route) {
27
+ super(v2Route.pairs, v2Route.input, v2Route.output);
28
+ this.protocol = protocol_1.Protocol.V2;
29
+ this.pools = this.pairs;
30
+ this.pathInput = getPathToken(v2Route.input, this.pairs[0]);
31
+ this.pathOutput = getPathToken(v2Route.output, this.pairs[this.pairs.length - 1]);
32
+ }
33
+ }
34
+ exports.RouteV2 = RouteV2;
35
+ // V3 route wrapper
36
+ class RouteV3 extends v3_sdk_1.Route {
37
+ constructor(v3Route) {
38
+ super(v3Route.pools, v3Route.input, v3Route.output);
39
+ this.protocol = protocol_1.Protocol.V3;
40
+ this.path = v3Route.tokenPath;
41
+ this.pathInput = getPathToken(v3Route.input, this.pools[0]);
42
+ this.pathOutput = getPathToken(v3Route.output, this.pools[this.pools.length - 1]);
43
+ }
44
+ }
45
+ exports.RouteV3 = RouteV3;
46
+ // V4 route wrapper
47
+ class RouteV4 extends v4_sdk_1.Route {
48
+ constructor(v4Route) {
49
+ super(v4Route.pools, v4Route.input, v4Route.output);
50
+ this.protocol = protocol_1.Protocol.V4;
51
+ this.path = v4Route.currencyPath;
52
+ }
53
+ }
54
+ exports.RouteV4 = RouteV4;
55
+ // Mixed route wrapper
56
+ class MixedRoute extends route_1.MixedRouteSDK {
57
+ constructor(mixedRoute) {
58
+ super(mixedRoute.pools, mixedRoute.input, mixedRoute.output);
59
+ this.protocol = protocol_1.Protocol.MIXED;
60
+ }
61
+ }
62
+ exports.MixedRoute = MixedRoute;
63
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../src/entities/route.ts"],"names":[],"mappings":";AAAA,oBAAoB;;;AAEpB,4CAA2D;AAC3D,2CAAoE;AACpE,4CAAqE;AACrE,yCAAqC;AAErC,8CAAkD;AAElD,0EAA0E;AAC1E,4GAA4G;AAC5G,SAAgB,YAAY,CAAC,QAAkB,EAAE,IAAmB;IAClE,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;QAChD,OAAO,IAAI,CAAC,MAAM,CAAA;KACnB;SAAM,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;QACvD,OAAO,IAAI,CAAC,MAAM,CAAA;KACnB;SAAM;QACL,MAAM,IAAI,KAAK,CAAC,kBAAkB,QAAQ,CAAC,MAAM,iBAAiB,IAAI,CAAC,MAAM,CAAC,MAAM,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAA;KACjH;AACH,CAAC;AARD,oCAQC;AAcD,mBAAmB;AACnB,MAAa,OACX,SAAQ,cAA2B;IAQnC,YAAY,OAAoC;QAC9C,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;QANrC,aAAQ,GAAa,mBAAQ,CAAC,EAAE,CAAA;QAO9C,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAA;QACvB,IAAI,CAAC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AAK,EAAE,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC,CAAA;QAH9C,aAAQ,GAAa,mBAAQ,CAAC,KAAK,CAAA;IAInD,CAAC;CACF;AATD,gCASC"}
@@ -0,0 +1,136 @@
1
+ import { Currency, CurrencyAmount, Percent, Price, TradeType } from '@fenine/sdk-core';
2
+ import { Pair, Route as V2RouteSDK } from '@uniswap/v2-sdk';
3
+ import { Pool as V3Pool, Route as V3RouteSDK } from '@fenine/v3-sdk';
4
+ import { Pool as V4Pool, Route as V4RouteSDK } from '@uniswap/v4-sdk';
5
+ import { MixedRouteSDK } from './mixedRoute/route';
6
+ import { IRoute } from './route';
7
+ export declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
8
+ readonly routes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
9
+ readonly tradeType: TTradeType;
10
+ private _outputAmount;
11
+ private _inputAmount;
12
+ private _nativeInputRoutes;
13
+ private _wethInputRoutes;
14
+ /**
15
+ * The swaps of the trade, i.e. which routes and how much is swapped in each that
16
+ * make up the trade. May consist of swaps in v2 or v3.
17
+ */
18
+ readonly swaps: {
19
+ route: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>;
20
+ inputAmount: CurrencyAmount<TInput>;
21
+ outputAmount: CurrencyAmount<TOutput>;
22
+ minHopPriceX36?: bigint[];
23
+ }[];
24
+ constructor({ v2Routes, v3Routes, v4Routes, mixedRoutes, tradeType, }: {
25
+ v2Routes?: {
26
+ routev2: V2RouteSDK<TInput, TOutput>;
27
+ inputAmount: CurrencyAmount<TInput>;
28
+ outputAmount: CurrencyAmount<TOutput>;
29
+ minHopPriceX36?: bigint[];
30
+ }[];
31
+ v3Routes?: {
32
+ routev3: V3RouteSDK<TInput, TOutput>;
33
+ inputAmount: CurrencyAmount<TInput>;
34
+ outputAmount: CurrencyAmount<TOutput>;
35
+ minHopPriceX36?: bigint[];
36
+ }[];
37
+ v4Routes?: {
38
+ routev4: V4RouteSDK<TInput, TOutput>;
39
+ inputAmount: CurrencyAmount<TInput>;
40
+ outputAmount: CurrencyAmount<TOutput>;
41
+ minHopPriceX36?: bigint[];
42
+ }[];
43
+ mixedRoutes?: {
44
+ mixedRoute: MixedRouteSDK<TInput, TOutput>;
45
+ inputAmount: CurrencyAmount<TInput>;
46
+ outputAmount: CurrencyAmount<TOutput>;
47
+ minHopPriceX36?: bigint[];
48
+ }[];
49
+ tradeType: TTradeType;
50
+ });
51
+ get inputAmount(): CurrencyAmount<TInput>;
52
+ get outputAmount(): CurrencyAmount<TOutput>;
53
+ /**
54
+ * Returns the sum of all swaps within the trade
55
+ * @returns
56
+ * inputAmount: total input amount
57
+ * inputAmountNative: total amount of native currency required for ETH input paths
58
+ * - 0 if inputAmount is native but no native input paths
59
+ * - undefined if inputAmount is not native
60
+ * outputAmount: total output amount
61
+ * outputAmountNative: total amount of native currency returned from ETH output paths
62
+ * - 0 if outputAmount is native but no native output paths
63
+ * - undefined if outputAmount is not native
64
+ */
65
+ get amounts(): {
66
+ inputAmount: CurrencyAmount<TInput>;
67
+ inputAmountNative: CurrencyAmount<TInput> | undefined;
68
+ outputAmount: CurrencyAmount<TOutput>;
69
+ outputAmountNative: CurrencyAmount<TOutput> | undefined;
70
+ };
71
+ get numberOfInputWraps(): number;
72
+ get numberOfInputUnwraps(): number;
73
+ get nativeInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
74
+ get wethInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
75
+ private _executionPrice;
76
+ /**
77
+ * The price expressed in terms of output amount/input amount.
78
+ */
79
+ get executionPrice(): Price<TInput, TOutput>;
80
+ /**
81
+ * Returns the sell tax of the input token
82
+ */
83
+ get inputTax(): Percent;
84
+ /**
85
+ * Returns the buy tax of the output token
86
+ */
87
+ get outputTax(): Percent;
88
+ private isWrappedNative;
89
+ /**
90
+ * The cached result of the price impact computation
91
+ * @private
92
+ */
93
+ private _priceImpact;
94
+ /**
95
+ * Returns the percent difference between the route's mid price and the expected execution price
96
+ * In order to exclude token taxes from the price impact calculation, the spot price is calculated
97
+ * using a ratio of values that go into the pools, which are the post-tax input amount and pre-tax output amount.
98
+ */
99
+ get priceImpact(): Percent;
100
+ /**
101
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
102
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
103
+ * @returns The amount out
104
+ */
105
+ minimumAmountOut(slippageTolerance: Percent, amountOut?: CurrencyAmount<TOutput>): CurrencyAmount<TOutput>;
106
+ /**
107
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
108
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
109
+ * @returns The amount in
110
+ */
111
+ maximumAmountIn(slippageTolerance: Percent, amountIn?: CurrencyAmount<TInput>): CurrencyAmount<TInput>;
112
+ /**
113
+ * Return the execution price after accounting for slippage tolerance
114
+ * @param slippageTolerance the allowed tolerated slippage
115
+ * @returns The execution price
116
+ */
117
+ worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
118
+ static fromRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(v2Routes: {
119
+ routev2: V2RouteSDK<TInput, TOutput>;
120
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
121
+ minHopPriceX36?: bigint[];
122
+ }[], v3Routes: {
123
+ routev3: V3RouteSDK<TInput, TOutput>;
124
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
125
+ minHopPriceX36?: bigint[];
126
+ }[], tradeType: TTradeType, mixedRoutes?: {
127
+ mixedRoute: MixedRouteSDK<TInput, TOutput>;
128
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
129
+ minHopPriceX36?: bigint[];
130
+ }[], v4Routes?: {
131
+ routev4: V4RouteSDK<TInput, TOutput>;
132
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
133
+ minHopPriceX36?: bigint[];
134
+ }[]): Promise<Trade<TInput, TOutput, TTradeType>>;
135
+ static fromRoute<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(route: V2RouteSDK<TInput, TOutput> | V3RouteSDK<TInput, TOutput> | V4RouteSDK<TInput, TOutput> | MixedRouteSDK<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType): Promise<Trade<TInput, TOutput, TTradeType>>;
136
+ }