@kittycorn-labs/v2-sdk 1.0.13 → 2.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 (74) hide show
  1. package/README.md +9 -0
  2. package/dist/{constants.d.ts → cjs/src/constants.d.ts} +19 -19
  3. package/dist/cjs/src/constants.js +23 -0
  4. package/dist/cjs/src/constants.js.map +1 -0
  5. package/dist/{entities → cjs/src/entities}/index.d.ts +3 -3
  6. package/dist/cjs/src/entities/index.js +7 -0
  7. package/dist/cjs/src/entities/index.js.map +1 -0
  8. package/dist/{entities → cjs/src/entities}/pair.d.ts +147 -147
  9. package/dist/cjs/src/entities/pair.js +318 -0
  10. package/dist/cjs/src/entities/pair.js.map +1 -0
  11. package/dist/{entities → cjs/src/entities}/route.d.ts +12 -12
  12. package/dist/cjs/src/entities/route.js +45 -0
  13. package/dist/cjs/src/entities/route.js.map +1 -0
  14. package/dist/{entities → cjs/src/entities}/trade.d.ts +103 -103
  15. package/dist/cjs/src/entities/trade.js +263 -0
  16. package/dist/cjs/src/entities/trade.js.map +1 -0
  17. package/dist/{errors.d.ts → cjs/src/errors.d.ts} +16 -16
  18. package/dist/cjs/src/errors.js +34 -0
  19. package/dist/cjs/src/errors.js.map +1 -0
  20. package/dist/{index.d.ts → cjs/src/index.d.ts} +4 -4
  21. package/dist/cjs/src/index.js +12 -0
  22. package/dist/cjs/src/index.js.map +1 -0
  23. package/dist/{router.d.ts → cjs/src/router.d.ts} +64 -64
  24. package/dist/cjs/src/router.js +94 -0
  25. package/dist/cjs/src/router.js.map +1 -0
  26. package/dist/esm/package.json +1 -0
  27. package/dist/esm/src/constants.d.ts +19 -0
  28. package/dist/esm/src/constants.js +19 -0
  29. package/dist/esm/src/constants.js.map +1 -0
  30. package/dist/esm/src/entities/index.d.ts +3 -0
  31. package/dist/esm/src/entities/index.js +4 -0
  32. package/dist/esm/src/entities/index.js.map +1 -0
  33. package/dist/esm/src/entities/pair.d.ts +147 -0
  34. package/dist/esm/src/entities/pair.js +312 -0
  35. package/dist/esm/src/entities/pair.js.map +1 -0
  36. package/dist/esm/src/entities/route.d.ts +12 -0
  37. package/dist/esm/src/entities/route.js +40 -0
  38. package/dist/esm/src/entities/route.js.map +1 -0
  39. package/dist/esm/src/entities/trade.d.ts +103 -0
  40. package/dist/esm/src/entities/trade.js +256 -0
  41. package/dist/esm/src/entities/trade.js.map +1 -0
  42. package/dist/esm/src/errors.d.ts +16 -0
  43. package/dist/esm/src/errors.js +29 -0
  44. package/dist/esm/src/errors.js.map +1 -0
  45. package/dist/esm/src/index.d.ts +4 -0
  46. package/dist/esm/src/index.js +5 -0
  47. package/dist/esm/src/index.js.map +1 -0
  48. package/dist/esm/src/router.d.ts +64 -0
  49. package/dist/esm/src/router.js +89 -0
  50. package/dist/esm/src/router.js.map +1 -0
  51. package/dist/types/src/constants.d.ts +20 -0
  52. package/dist/types/src/constants.d.ts.map +1 -0
  53. package/dist/types/src/entities/index.d.ts +4 -0
  54. package/dist/types/src/entities/index.d.ts.map +1 -0
  55. package/dist/types/src/entities/pair.d.ts +148 -0
  56. package/dist/types/src/entities/pair.d.ts.map +1 -0
  57. package/dist/types/src/entities/route.d.ts +13 -0
  58. package/dist/types/src/entities/route.d.ts.map +1 -0
  59. package/dist/types/src/entities/trade.d.ts +104 -0
  60. package/dist/types/src/entities/trade.d.ts.map +1 -0
  61. package/dist/types/src/errors.d.ts +17 -0
  62. package/dist/types/src/errors.d.ts.map +1 -0
  63. package/dist/types/src/index.d.ts +5 -0
  64. package/dist/types/src/index.d.ts.map +1 -0
  65. package/dist/types/src/router.d.ts +65 -0
  66. package/dist/types/src/router.d.ts.map +1 -0
  67. package/package.json +23 -34
  68. package/dist/index.js +0 -8
  69. package/dist/v2-sdk.cjs.development.js +0 -897
  70. package/dist/v2-sdk.cjs.development.js.map +0 -1
  71. package/dist/v2-sdk.cjs.production.min.js +0 -2
  72. package/dist/v2-sdk.cjs.production.min.js.map +0 -1
  73. package/dist/v2-sdk.esm.js +0 -880
  74. package/dist/v2-sdk.esm.js.map +0 -1
@@ -0,0 +1,103 @@
1
+ import { Currency, CurrencyAmount, Percent, Price, TradeType } from '@kittycorn-labs/sdk-core';
2
+ import { Pair } from './pair';
3
+ import { Route } from './route';
4
+ interface InputOutput<TInput extends Currency, TOutput extends Currency> {
5
+ readonly inputAmount: CurrencyAmount<TInput>;
6
+ readonly outputAmount: CurrencyAmount<TOutput>;
7
+ }
8
+ export declare function inputOutputComparator<TInput extends Currency, TOutput extends Currency>(a: InputOutput<TInput, TOutput>, b: InputOutput<TInput, TOutput>): number;
9
+ export declare function tradeComparator<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(a: Trade<TInput, TOutput, TTradeType>, b: Trade<TInput, TOutput, TTradeType>): number;
10
+ export interface BestTradeOptions {
11
+ maxNumResults?: number;
12
+ maxHops?: number;
13
+ }
14
+ /**
15
+ * Represents a trade executed against a list of pairs.
16
+ * Does not account for slippage, i.e. trades that front run this trade and move the price.
17
+ */
18
+ export declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
19
+ /**
20
+ * The route of the trade, i.e. which pairs the trade goes through and the input/output currencies.
21
+ */
22
+ readonly route: Route<TInput, TOutput>;
23
+ /**
24
+ * The type of the trade, either exact in or exact out.
25
+ */
26
+ readonly tradeType: TTradeType;
27
+ /**
28
+ * The input amount for the trade assuming no slippage.
29
+ */
30
+ readonly inputAmount: CurrencyAmount<TInput>;
31
+ /**
32
+ * The output amount for the trade assuming no slippage.
33
+ */
34
+ readonly outputAmount: CurrencyAmount<TOutput>;
35
+ /**
36
+ * The price expressed in terms of output amount/input amount.
37
+ */
38
+ readonly executionPrice: Price<TInput, TOutput>;
39
+ /**
40
+ * The percent difference between the mid price before the trade and the trade execution price.
41
+ */
42
+ readonly priceImpact: Percent;
43
+ /**
44
+ * Constructs an exact in trade with the given amount in and route
45
+ * @param route route of the exact in trade
46
+ * @param amountIn the amount being passed in
47
+ */
48
+ static exactIn<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountIn: CurrencyAmount<TInput>): Trade<TInput, TOutput, TradeType.EXACT_INPUT>;
49
+ /**
50
+ * Constructs an exact out trade with the given amount out and route
51
+ * @param route route of the exact out trade
52
+ * @param amountOut the amount returned by the trade
53
+ */
54
+ static exactOut<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountOut: CurrencyAmount<TOutput>): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>;
55
+ constructor(route: Route<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType);
56
+ /**
57
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
58
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
59
+ */
60
+ minimumAmountOut(slippageTolerance: Percent): CurrencyAmount<TOutput>;
61
+ /**
62
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
63
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
64
+ */
65
+ maximumAmountIn(slippageTolerance: Percent): CurrencyAmount<TInput>;
66
+ /**
67
+ * Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
68
+ * amount to an output token, making at most `maxHops` hops.
69
+ * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
70
+ * the amount in among multiple routes.
71
+ * @param pairs the pairs to consider in finding the best trade
72
+ * @param nextAmountIn exact amount of input currency to spend
73
+ * @param currencyOut the desired currency out
74
+ * @param maxNumResults maximum number of results to return
75
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
76
+ * @param currentPairs used in recursion; the current list of pairs
77
+ * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
78
+ * @param bestTrades used in recursion; the current list of best trades
79
+ */
80
+ static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(pairs: Pair[], currencyAmountIn: CurrencyAmount<TInput>, currencyOut: TOutput, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], nextAmountIn?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_INPUT>[]): Trade<TInput, TOutput, TradeType.EXACT_INPUT>[];
81
+ /**
82
+ * Return the execution price after accounting for slippage tolerance
83
+ * @param slippageTolerance the allowed tolerated slippage
84
+ */
85
+ worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
86
+ /**
87
+ * similar to the above method but instead targets a fixed output amount
88
+ * given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
89
+ * to an output token amount, making at most `maxHops` hops
90
+ * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
91
+ * the amount in among multiple routes.
92
+ * @param pairs the pairs to consider in finding the best trade
93
+ * @param currencyIn the currency to spend
94
+ * @param nextAmountOut the exact amount of currency out
95
+ * @param maxNumResults maximum number of results to return
96
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
97
+ * @param currentPairs used in recursion; the current list of pairs
98
+ * @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
99
+ * @param bestTrades used in recursion; the current list of best trades
100
+ */
101
+ static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(pairs: Pair[], currencyIn: TInput, currencyAmountOut: CurrencyAmount<TOutput>, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], nextAmountOut?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[]): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[];
102
+ }
103
+ export {};
@@ -0,0 +1,256 @@
1
+ import { computePriceImpact, CurrencyAmount, Fraction, Price, sortedInsert, TradeType, } from '@kittycorn-labs/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ import { ONE, ZERO } from '../constants.js';
4
+ import invariant from 'tiny-invariant';
5
+ import { Route } from './route.js';
6
+ // comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts
7
+ // in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first
8
+ export function inputOutputComparator(a, b) {
9
+ // must have same input and output token for comparison
10
+ invariant(a.inputAmount.currency.equals(b.inputAmount.currency), 'INPUT_CURRENCY');
11
+ invariant(a.outputAmount.currency.equals(b.outputAmount.currency), 'OUTPUT_CURRENCY');
12
+ if (a.outputAmount.equalTo(b.outputAmount)) {
13
+ if (a.inputAmount.equalTo(b.inputAmount)) {
14
+ return 0;
15
+ }
16
+ // trade A requires less input than trade B, so A should come first
17
+ if (a.inputAmount.lessThan(b.inputAmount)) {
18
+ return -1;
19
+ }
20
+ else {
21
+ return 1;
22
+ }
23
+ }
24
+ else {
25
+ // tradeA has less output than trade B, so should come second
26
+ if (a.outputAmount.lessThan(b.outputAmount)) {
27
+ return 1;
28
+ }
29
+ else {
30
+ return -1;
31
+ }
32
+ }
33
+ }
34
+ // extension of the input output comparator that also considers other dimensions of the trade in ranking them
35
+ export function tradeComparator(a, b) {
36
+ const ioComp = inputOutputComparator(a, b);
37
+ if (ioComp !== 0) {
38
+ return ioComp;
39
+ }
40
+ // consider lowest slippage next, since these are less likely to fail
41
+ if (a.priceImpact.lessThan(b.priceImpact)) {
42
+ return -1;
43
+ }
44
+ else if (a.priceImpact.greaterThan(b.priceImpact)) {
45
+ return 1;
46
+ }
47
+ // finally consider the number of hops since each hop costs gas
48
+ return a.route.path.length - b.route.path.length;
49
+ }
50
+ /**
51
+ * Represents a trade executed against a list of pairs.
52
+ * Does not account for slippage, i.e. trades that front run this trade and move the price.
53
+ */
54
+ export class Trade {
55
+ /**
56
+ * Constructs an exact in trade with the given amount in and route
57
+ * @param route route of the exact in trade
58
+ * @param amountIn the amount being passed in
59
+ */
60
+ static exactIn(route, amountIn) {
61
+ return new Trade(route, amountIn, TradeType.EXACT_INPUT);
62
+ }
63
+ /**
64
+ * Constructs an exact out trade with the given amount out and route
65
+ * @param route route of the exact out trade
66
+ * @param amountOut the amount returned by the trade
67
+ */
68
+ static exactOut(route, amountOut) {
69
+ return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
70
+ }
71
+ constructor(route, amount, tradeType) {
72
+ this.route = route;
73
+ this.tradeType = tradeType;
74
+ const tokenAmounts = new Array(route.path.length);
75
+ if (tradeType === TradeType.EXACT_INPUT) {
76
+ invariant(amount.currency.equals(route.input), 'INPUT');
77
+ tokenAmounts[0] = amount.wrapped;
78
+ for (let i = 0; i < route.path.length - 1; i++) {
79
+ const pair = route.pairs[i];
80
+ const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
81
+ tokenAmounts[i + 1] = outputAmount;
82
+ }
83
+ this.inputAmount = CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
84
+ this.outputAmount = CurrencyAmount.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
85
+ }
86
+ else {
87
+ invariant(amount.currency.equals(route.output), 'OUTPUT');
88
+ tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
89
+ for (let i = route.path.length - 1; i > 0; i--) {
90
+ const pair = route.pairs[i - 1];
91
+ const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
92
+ tokenAmounts[i - 1] = inputAmount;
93
+ }
94
+ this.inputAmount = CurrencyAmount.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
95
+ this.outputAmount = CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
96
+ }
97
+ this.executionPrice = new Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
98
+ this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
99
+ }
100
+ /**
101
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
102
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
103
+ */
104
+ minimumAmountOut(slippageTolerance) {
105
+ invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE');
106
+ if (this.tradeType === TradeType.EXACT_OUTPUT) {
107
+ return this.outputAmount;
108
+ }
109
+ else {
110
+ const slippageAdjustedAmountOut = new Fraction(ONE)
111
+ .subtract(slippageTolerance)
112
+ .multiply(this.outputAmount.quotient).quotient;
113
+ const clampedAmount = JSBI.greaterThan(slippageAdjustedAmountOut, JSBI.BigInt(0))
114
+ ? slippageAdjustedAmountOut
115
+ : JSBI.BigInt(0);
116
+ return CurrencyAmount.fromRawAmount(this.outputAmount.currency, clampedAmount);
117
+ }
118
+ }
119
+ /**
120
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
121
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
122
+ */
123
+ maximumAmountIn(slippageTolerance) {
124
+ invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE');
125
+ if (this.tradeType === TradeType.EXACT_INPUT) {
126
+ return this.inputAmount;
127
+ }
128
+ else {
129
+ const slippageAdjustedAmountIn = new Fraction(ONE)
130
+ .add(slippageTolerance)
131
+ .multiply(this.inputAmount.quotient).quotient;
132
+ return CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
133
+ }
134
+ }
135
+ /**
136
+ * Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
137
+ * amount to an output token, making at most `maxHops` hops.
138
+ * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
139
+ * the amount in among multiple routes.
140
+ * @param pairs the pairs to consider in finding the best trade
141
+ * @param nextAmountIn exact amount of input currency to spend
142
+ * @param currencyOut the desired currency out
143
+ * @param maxNumResults maximum number of results to return
144
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
145
+ * @param currentPairs used in recursion; the current list of pairs
146
+ * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
147
+ * @param bestTrades used in recursion; the current list of best trades
148
+ */
149
+ static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {},
150
+ // used in recursion.
151
+ currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
152
+ invariant(pairs.length > 0, 'PAIRS');
153
+ invariant(maxHops > 0, 'MAX_HOPS');
154
+ invariant(currencyAmountIn === nextAmountIn || currentPairs.length > 0, 'INVALID_RECURSION');
155
+ const amountIn = nextAmountIn.wrapped;
156
+ const tokenOut = currencyOut.wrapped;
157
+ for (let i = 0; i < pairs.length; i++) {
158
+ const pair = pairs[i];
159
+ // pair irrelevant
160
+ if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
161
+ continue;
162
+ if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
163
+ continue;
164
+ let amountOut;
165
+ try {
166
+ ;
167
+ [amountOut] = pair.getOutputAmount(amountIn);
168
+ }
169
+ catch (error) {
170
+ // input too low
171
+ if (error.isInsufficientInputAmountError) {
172
+ continue;
173
+ }
174
+ throw error;
175
+ }
176
+ // we have arrived at the output token, so this is the final trade of one of the paths
177
+ if (amountOut.currency.equals(tokenOut)) {
178
+ sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
179
+ }
180
+ else if (maxHops > 1 && pairs.length > 1) {
181
+ const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
182
+ // otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
183
+ Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
184
+ maxNumResults,
185
+ maxHops: maxHops - 1,
186
+ }, [...currentPairs, pair], amountOut, bestTrades);
187
+ }
188
+ }
189
+ return bestTrades;
190
+ }
191
+ /**
192
+ * Return the execution price after accounting for slippage tolerance
193
+ * @param slippageTolerance the allowed tolerated slippage
194
+ */
195
+ worstExecutionPrice(slippageTolerance) {
196
+ return new Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
197
+ }
198
+ /**
199
+ * similar to the above method but instead targets a fixed output amount
200
+ * given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
201
+ * to an output token amount, making at most `maxHops` hops
202
+ * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
203
+ * the amount in among multiple routes.
204
+ * @param pairs the pairs to consider in finding the best trade
205
+ * @param currencyIn the currency to spend
206
+ * @param nextAmountOut the exact amount of currency out
207
+ * @param maxNumResults maximum number of results to return
208
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
209
+ * @param currentPairs used in recursion; the current list of pairs
210
+ * @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
211
+ * @param bestTrades used in recursion; the current list of best trades
212
+ */
213
+ static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {},
214
+ // used in recursion.
215
+ currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
216
+ invariant(pairs.length > 0, 'PAIRS');
217
+ invariant(maxHops > 0, 'MAX_HOPS');
218
+ invariant(currencyAmountOut === nextAmountOut || currentPairs.length > 0, 'INVALID_RECURSION');
219
+ const amountOut = nextAmountOut.wrapped;
220
+ const tokenIn = currencyIn.wrapped;
221
+ for (let i = 0; i < pairs.length; i++) {
222
+ const pair = pairs[i];
223
+ // pair irrelevant
224
+ if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
225
+ continue;
226
+ if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
227
+ continue;
228
+ let amountIn;
229
+ try {
230
+ ;
231
+ [amountIn] = pair.getInputAmount(amountOut);
232
+ }
233
+ catch (error) {
234
+ // not enough liquidity in this pair
235
+ if (error.isInsufficientReservesError) {
236
+ continue;
237
+ }
238
+ throw error;
239
+ }
240
+ // we have arrived at the input token, so this is the first trade of one of the paths
241
+ if (amountIn.currency.equals(tokenIn)) {
242
+ sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
243
+ }
244
+ else if (maxHops > 1 && pairs.length > 1) {
245
+ const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
246
+ // otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
247
+ Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
248
+ maxNumResults,
249
+ maxHops: maxHops - 1,
250
+ }, [pair, ...currentPairs], amountIn, bestTrades);
251
+ }
252
+ }
253
+ return bestTrades;
254
+ }
255
+ }
256
+ //# sourceMappingURL=trade.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trade.js","sourceRoot":"","sources":["../../../../src/entities/trade.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAGlB,cAAc,EACd,QAAQ,EAER,KAAK,EACL,YAAY,EACZ,SAAS,GACV,MAAM,0BAA0B,CAAA;AACjC,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,SAAS,MAAM,gBAAgB,CAAA;AAGtC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAQ/B,sHAAsH;AACtH,4GAA4G;AAC5G,MAAM,UAAU,qBAAqB,CACnC,CAA+B,EAC/B,CAA+B;IAE/B,uDAAuD;IACvD,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,gBAAgB,CAAC,CAAA;IAClF,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,C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CAAC,cAAc,CAAC,SAAS,CAAC,CAAA;aAC7C;YAAC,OAAO,KAAK,EAAE;gBACd,oCAAoC;gBACpC,IAAK,KAAa,CAAC,2BAA2B,EAAE;oBAC9C,SAAQ;iBACT;gBACD,MAAM,KAAK,CAAA;aACZ;YACD,qFAAqF;YACrF,IAAI,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE;gBACrC,YAAY,CACV,UAAU,EACV,IAAI,KAAK,CACP,IAAI,KAAK,CAAC,CAAC,IAAI,EAAE,GAAG,YAAY,CAAC,EAAE,UAAU,EAAE,iBAAiB,CAAC,QAAQ,CAAC,EAC1E,iBAAiB,EACjB,SAAS,CAAC,YAAY,CACvB,EACD,aAAa,EACb,eAAe,CAChB,CAAA;aACF;iBAAM,IAAI,OAAO,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;gBAC1C,MAAM,sBAAsB,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;gBAEzF,4GAA4G;gBAC5G,KAAK,CAAC,iBAAiB,CACrB,sBAAsB,EACtB,UAAU,EACV,iBAAiB,EACjB;oBACE,aAAa;oBACb,OAAO,EAAE,OAAO,GAAG,CAAC;iBACrB,EACD,CAAC,IAAI,EAAE,GAAG,YAAY,CAAC,EACvB,QAAQ,EACR,UAAU,CACX,CAAA;aACF;SACF;QAED,OAAO,UAAU,CAAA;IACnB,CAAC;CACF"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be
3
+ * obtained by sending any amount of input.
4
+ */
5
+ export declare class InsufficientReservesError extends Error {
6
+ readonly isInsufficientReservesError: true;
7
+ constructor();
8
+ }
9
+ /**
10
+ * Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less
11
+ * than the price of a single unit of output after fees.
12
+ */
13
+ export declare class InsufficientInputAmountError extends Error {
14
+ readonly isInsufficientInputAmountError: true;
15
+ constructor();
16
+ }
@@ -0,0 +1,29 @@
1
+ // see https://stackoverflow.com/a/41102306
2
+ const CAN_SET_PROTOTYPE = 'setPrototypeOf' in Object;
3
+ /**
4
+ * Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be
5
+ * obtained by sending any amount of input.
6
+ */
7
+ export class InsufficientReservesError extends Error {
8
+ constructor() {
9
+ super();
10
+ this.isInsufficientReservesError = true;
11
+ this.name = this.constructor.name;
12
+ if (CAN_SET_PROTOTYPE)
13
+ Object.setPrototypeOf(this, new.target.prototype);
14
+ }
15
+ }
16
+ /**
17
+ * Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less
18
+ * than the price of a single unit of output after fees.
19
+ */
20
+ export class InsufficientInputAmountError extends Error {
21
+ constructor() {
22
+ super();
23
+ this.isInsufficientInputAmountError = true;
24
+ this.name = this.constructor.name;
25
+ if (CAN_SET_PROTOTYPE)
26
+ Object.setPrototypeOf(this, new.target.prototype);
27
+ }
28
+ }
29
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/errors.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,MAAM,iBAAiB,GAAG,gBAAgB,IAAI,MAAM,CAAA;AAEpD;;;GAGG;AACH,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAGlD;QACE,KAAK,EAAE,CAAA;QAHO,gCAA2B,GAAS,IAAI,CAAA;QAItD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;QACjC,IAAI,iBAAiB;YAAE,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IAC1E,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,4BAA6B,SAAQ,KAAK;IAGrD;QACE,KAAK,EAAE,CAAA;QAHO,mCAA8B,GAAS,IAAI,CAAA;QAIzD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;QACjC,IAAI,iBAAiB;YAAE,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IAC1E,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ export { FACTORY_ADDRESS_MAP, INIT_CODE_HASH, MINIMUM_LIQUIDITY } from './constants';
2
+ export * from './errors';
3
+ export * from './entities';
4
+ export * from './router';
@@ -0,0 +1,5 @@
1
+ export { FACTORY_ADDRESS_MAP, INIT_CODE_HASH, MINIMUM_LIQUIDITY } from './constants.js';
2
+ export * from './errors.js';
3
+ export * from './entities/index.js';
4
+ export * from './router.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAEpF,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA"}
@@ -0,0 +1,64 @@
1
+ import { Currency, Percent, TradeType } from '@kittycorn-labs/sdk-core';
2
+ import { Trade } from './entities';
3
+ /**
4
+ * Options for producing the arguments to send call to the router.
5
+ */
6
+ export interface TradeOptions {
7
+ /**
8
+ * How much the execution price is allowed to move unfavorably from the trade execution price.
9
+ */
10
+ allowedSlippage: Percent;
11
+ /**
12
+ * How long the swap is valid until it expires, in seconds.
13
+ * This will be used to produce a `deadline` parameter which is computed from when the swap call parameters
14
+ * are generated.
15
+ */
16
+ ttl: number;
17
+ /**
18
+ * The account that should receive the output of the swap.
19
+ */
20
+ recipient: string;
21
+ /**
22
+ * Whether any of the tokens in the path are fee on transfer tokens, which should be handled with special methods
23
+ */
24
+ feeOnTransfer?: boolean;
25
+ }
26
+ export interface TradeOptionsDeadline extends Omit<TradeOptions, 'ttl'> {
27
+ /**
28
+ * When the transaction expires.
29
+ * This is an atlernate to specifying the ttl, for when you do not want to use local time.
30
+ */
31
+ deadline: number;
32
+ }
33
+ /**
34
+ * The parameters to use in the call to the Uniswap V2 Router to execute a trade.
35
+ */
36
+ export interface SwapParameters {
37
+ /**
38
+ * The method to call on the Uniswap V2 Router.
39
+ */
40
+ methodName: string;
41
+ /**
42
+ * The arguments to pass to the method, all hex encoded.
43
+ */
44
+ args: (string | string[])[];
45
+ /**
46
+ * The amount of wei to send in hex.
47
+ */
48
+ value: string;
49
+ }
50
+ /**
51
+ * Represents the Uniswap V2 Router, and has static methods for helping execute trades.
52
+ */
53
+ export declare abstract class Router {
54
+ /**
55
+ * Cannot be constructed.
56
+ */
57
+ private constructor();
58
+ /**
59
+ * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
60
+ * @param trade to produce call parameters for
61
+ * @param options options for the call parameters
62
+ */
63
+ static swapCallParameters(trade: Trade<Currency, Currency, TradeType>, options: TradeOptions | TradeOptionsDeadline): SwapParameters;
64
+ }
@@ -0,0 +1,89 @@
1
+ import { TradeType, validateAndParseAddress } from '@kittycorn-labs/sdk-core';
2
+ import invariant from 'tiny-invariant';
3
+ function toHex(currencyAmount) {
4
+ return `0x${currencyAmount.quotient.toString(16)}`;
5
+ }
6
+ const ZERO_HEX = '0x0';
7
+ /**
8
+ * Represents the Uniswap V2 Router, and has static methods for helping execute trades.
9
+ */
10
+ export class Router {
11
+ /**
12
+ * Cannot be constructed.
13
+ */
14
+ constructor() { }
15
+ /**
16
+ * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
17
+ * @param trade to produce call parameters for
18
+ * @param options options for the call parameters
19
+ */
20
+ static swapCallParameters(trade, options) {
21
+ const etherIn = trade.inputAmount.currency.isNative;
22
+ const etherOut = trade.outputAmount.currency.isNative;
23
+ // the router does not support both ether in and out
24
+ invariant(!(etherIn && etherOut), 'ETHER_IN_OUT');
25
+ invariant(!('ttl' in options) || options.ttl > 0, 'TTL');
26
+ const to = validateAndParseAddress(options.recipient);
27
+ const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
28
+ const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
29
+ const path = trade.route.path.map((token) => token.address);
30
+ const deadline = 'ttl' in options
31
+ ? `0x${(Math.floor(new Date().getTime() / 1000) + options.ttl).toString(16)}`
32
+ : `0x${options.deadline.toString(16)}`;
33
+ const useFeeOnTransfer = Boolean(options.feeOnTransfer);
34
+ let methodName;
35
+ let args;
36
+ let value;
37
+ switch (trade.tradeType) {
38
+ case TradeType.EXACT_INPUT:
39
+ if (etherIn) {
40
+ methodName = useFeeOnTransfer ? 'swapExactETHForTokensSupportingFeeOnTransferTokens' : 'swapExactETHForTokens';
41
+ // (uint amountOutMin, address[] calldata path, address to, uint deadline)
42
+ args = [amountOut, path, to, deadline];
43
+ value = amountIn;
44
+ }
45
+ else if (etherOut) {
46
+ methodName = useFeeOnTransfer ? 'swapExactTokensForETHSupportingFeeOnTransferTokens' : 'swapExactTokensForETH';
47
+ // (uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
48
+ args = [amountIn, amountOut, path, to, deadline];
49
+ value = ZERO_HEX;
50
+ }
51
+ else {
52
+ methodName = useFeeOnTransfer
53
+ ? 'swapExactTokensForTokensSupportingFeeOnTransferTokens'
54
+ : 'swapExactTokensForTokens';
55
+ // (uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
56
+ args = [amountIn, amountOut, path, to, deadline];
57
+ value = ZERO_HEX;
58
+ }
59
+ break;
60
+ case TradeType.EXACT_OUTPUT:
61
+ invariant(!useFeeOnTransfer, 'EXACT_OUT_FOT');
62
+ if (etherIn) {
63
+ methodName = 'swapETHForExactTokens';
64
+ // (uint amountOut, address[] calldata path, address to, uint deadline)
65
+ args = [amountOut, path, to, deadline];
66
+ value = amountIn;
67
+ }
68
+ else if (etherOut) {
69
+ methodName = 'swapTokensForExactETH';
70
+ // (uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
71
+ args = [amountOut, amountIn, path, to, deadline];
72
+ value = ZERO_HEX;
73
+ }
74
+ else {
75
+ methodName = 'swapTokensForExactTokens';
76
+ // (uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
77
+ args = [amountOut, amountIn, path, to, deadline];
78
+ value = ZERO_HEX;
79
+ }
80
+ break;
81
+ }
82
+ return {
83
+ methodName,
84
+ args,
85
+ value,
86
+ };
87
+ }
88
+ }
89
+ //# sourceMappingURL=router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.js","sourceRoot":"","sources":["../../../src/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4C,SAAS,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAA;AAEvH,OAAO,SAAS,MAAM,gBAAgB,CAAA;AAqDtC,SAAS,KAAK,CAAC,cAAwC;IACrD,OAAO,KAAK,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAA;AACpD,CAAC;AAED,MAAM,QAAQ,GAAG,KAAK,CAAA;AAEtB;;GAEG;AACH,MAAM,OAAgB,MAAM;IAC1B;;OAEG;IACH,gBAAuB,CAAC;IACxB;;;;OAIG;IACI,MAAM,CAAC,kBAAkB,CAC9B,KAA2C,EAC3C,OAA4C;QAE5C,MAAM,OAAO,GAAG,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAA;QACnD,MAAM,QAAQ,GAAG,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,QAAQ,CAAA;QACrD,oDAAoD;QACpD,SAAS,CAAC,CAAC,CAAC,OAAO,IAAI,QAAQ,CAAC,EAAE,cAAc,CAAC,CAAA;QACjD,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,OAAO,CA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I;YACJ,KAAK;SACN,CAAA;IACH,CAAC;CACF"}
@@ -0,0 +1,20 @@
1
+ import { Percent } from '@kittycorn-labs/sdk-core';
2
+ import JSBI from 'jsbi';
3
+ /**
4
+ * @deprecated use FACTORY_ADDRESS_MAP instead
5
+ */
6
+ export declare const FACTORY_ADDRESS = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f";
7
+ export declare const FACTORY_ADDRESS_MAP: {
8
+ [chainId: number]: string;
9
+ };
10
+ export declare const INIT_CODE_HASH = "0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f";
11
+ export declare const MINIMUM_LIQUIDITY: JSBI;
12
+ export declare const ZERO: JSBI;
13
+ export declare const ONE: JSBI;
14
+ export declare const FIVE: JSBI;
15
+ export declare const _997: JSBI;
16
+ export declare const _1000: JSBI;
17
+ export declare const BASIS_POINTS: JSBI;
18
+ export declare const ZERO_PERCENT: Percent;
19
+ export declare const ONE_HUNDRED_PERCENT: Percent;
20
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAwB,MAAM,0BAA0B,CAAA;AACxE,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB;;GAEG;AACH,eAAO,MAAM,eAAe,+CAA+C,CAAA;AAE3E,eAAO,MAAM,mBAAmB,EAAE;IAAE,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAA;CAAyB,CAAA;AAEtF,eAAO,MAAM,cAAc,uEAAuE,CAAA;AAElG,eAAO,MAAM,iBAAiB,MAAoB,CAAA;AAGlD,eAAO,MAAM,IAAI,MAAiB,CAAA;AAClC,eAAO,MAAM,GAAG,MAAiB,CAAA;AACjC,eAAO,MAAM,IAAI,MAAiB,CAAA;AAClC,eAAO,MAAM,IAAI,MAAmB,CAAA;AACpC,eAAO,MAAM,KAAK,MAAoB,CAAA;AACtC,eAAO,MAAM,YAAY,MAAqB,CAAA;AAE9C,eAAO,MAAM,YAAY,SAAoB,CAAA;AAC7C,eAAO,MAAM,mBAAmB,SAAmB,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './pair';
2
+ export * from './route';
3
+ export * from './trade';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/entities/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA;AACtB,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}