@centurion_project/v2-sdk 4.20.0-ctn.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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -0
  3. package/dist/cjs/src/constants.d.ts +19 -0
  4. package/dist/cjs/src/constants.js +23 -0
  5. package/dist/cjs/src/constants.js.map +1 -0
  6. package/dist/cjs/src/entities/index.d.ts +3 -0
  7. package/dist/cjs/src/entities/index.js +7 -0
  8. package/dist/cjs/src/entities/index.js.map +1 -0
  9. package/dist/cjs/src/entities/pair.d.ts +147 -0
  10. package/dist/cjs/src/entities/pair.js +318 -0
  11. package/dist/cjs/src/entities/pair.js.map +1 -0
  12. package/dist/cjs/src/entities/route.d.ts +12 -0
  13. package/dist/cjs/src/entities/route.js +45 -0
  14. package/dist/cjs/src/entities/route.js.map +1 -0
  15. package/dist/cjs/src/entities/trade.d.ts +103 -0
  16. package/dist/cjs/src/entities/trade.js +260 -0
  17. package/dist/cjs/src/entities/trade.js.map +1 -0
  18. package/dist/cjs/src/errors.d.ts +16 -0
  19. package/dist/cjs/src/errors.js +34 -0
  20. package/dist/cjs/src/errors.js.map +1 -0
  21. package/dist/cjs/src/index.d.ts +4 -0
  22. package/dist/cjs/src/index.js +12 -0
  23. package/dist/cjs/src/index.js.map +1 -0
  24. package/dist/cjs/src/router.d.ts +64 -0
  25. package/dist/cjs/src/router.js +94 -0
  26. package/dist/cjs/src/router.js.map +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 +253 -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 +19 -0
  52. package/dist/types/src/entities/index.d.ts +3 -0
  53. package/dist/types/src/entities/pair.d.ts +147 -0
  54. package/dist/types/src/entities/route.d.ts +12 -0
  55. package/dist/types/src/entities/trade.d.ts +103 -0
  56. package/dist/types/src/errors.d.ts +16 -0
  57. package/dist/types/src/index.d.ts +4 -0
  58. package/dist/types/src/router.d.ts +64 -0
  59. package/package.json +68 -0
@@ -0,0 +1,103 @@
1
+ import { Currency, CurrencyAmount, Percent, Price, TradeType } from '@centurion_project/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,253 @@
1
+ import { computePriceImpact, CurrencyAmount, Fraction, Price, sortedInsert, TradeType, } from '@centurion_project/sdk-core';
2
+ import { ONE, ZERO } from '../constants';
3
+ import invariant from 'tiny-invariant';
4
+ import { Route } from './route';
5
+ // comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts
6
+ // in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first
7
+ export function inputOutputComparator(a, b) {
8
+ // must have same input and output token for comparison
9
+ invariant(a.inputAmount.currency.equals(b.inputAmount.currency), 'INPUT_CURRENCY');
10
+ invariant(a.outputAmount.currency.equals(b.outputAmount.currency), 'OUTPUT_CURRENCY');
11
+ if (a.outputAmount.equalTo(b.outputAmount)) {
12
+ if (a.inputAmount.equalTo(b.inputAmount)) {
13
+ return 0;
14
+ }
15
+ // trade A requires less input than trade B, so A should come first
16
+ if (a.inputAmount.lessThan(b.inputAmount)) {
17
+ return -1;
18
+ }
19
+ else {
20
+ return 1;
21
+ }
22
+ }
23
+ else {
24
+ // tradeA has less output than trade B, so should come second
25
+ if (a.outputAmount.lessThan(b.outputAmount)) {
26
+ return 1;
27
+ }
28
+ else {
29
+ return -1;
30
+ }
31
+ }
32
+ }
33
+ // extension of the input output comparator that also considers other dimensions of the trade in ranking them
34
+ export function tradeComparator(a, b) {
35
+ const ioComp = inputOutputComparator(a, b);
36
+ if (ioComp !== 0) {
37
+ return ioComp;
38
+ }
39
+ // consider lowest slippage next, since these are less likely to fail
40
+ if (a.priceImpact.lessThan(b.priceImpact)) {
41
+ return -1;
42
+ }
43
+ else if (a.priceImpact.greaterThan(b.priceImpact)) {
44
+ return 1;
45
+ }
46
+ // finally consider the number of hops since each hop costs gas
47
+ return a.route.path.length - b.route.path.length;
48
+ }
49
+ /**
50
+ * Represents a trade executed against a list of pairs.
51
+ * Does not account for slippage, i.e. trades that front run this trade and move the price.
52
+ */
53
+ export class Trade {
54
+ /**
55
+ * Constructs an exact in trade with the given amount in and route
56
+ * @param route route of the exact in trade
57
+ * @param amountIn the amount being passed in
58
+ */
59
+ static exactIn(route, amountIn) {
60
+ return new Trade(route, amountIn, TradeType.EXACT_INPUT);
61
+ }
62
+ /**
63
+ * Constructs an exact out trade with the given amount out and route
64
+ * @param route route of the exact out trade
65
+ * @param amountOut the amount returned by the trade
66
+ */
67
+ static exactOut(route, amountOut) {
68
+ return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
69
+ }
70
+ constructor(route, amount, tradeType) {
71
+ this.route = route;
72
+ this.tradeType = tradeType;
73
+ const tokenAmounts = new Array(route.path.length);
74
+ if (tradeType === TradeType.EXACT_INPUT) {
75
+ invariant(amount.currency.equals(route.input), 'INPUT');
76
+ tokenAmounts[0] = amount.wrapped;
77
+ for (let i = 0; i < route.path.length - 1; i++) {
78
+ const pair = route.pairs[i];
79
+ const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
80
+ tokenAmounts[i + 1] = outputAmount;
81
+ }
82
+ this.inputAmount = CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
83
+ this.outputAmount = CurrencyAmount.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
84
+ }
85
+ else {
86
+ invariant(amount.currency.equals(route.output), 'OUTPUT');
87
+ tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
88
+ for (let i = route.path.length - 1; i > 0; i--) {
89
+ const pair = route.pairs[i - 1];
90
+ const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
91
+ tokenAmounts[i - 1] = inputAmount;
92
+ }
93
+ this.inputAmount = CurrencyAmount.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
94
+ this.outputAmount = CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
95
+ }
96
+ this.executionPrice = new Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
97
+ this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
98
+ }
99
+ /**
100
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
101
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
102
+ */
103
+ minimumAmountOut(slippageTolerance) {
104
+ invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE');
105
+ if (this.tradeType === TradeType.EXACT_OUTPUT) {
106
+ return this.outputAmount;
107
+ }
108
+ else {
109
+ const slippageAdjustedAmountOut = new Fraction(ONE)
110
+ .add(slippageTolerance)
111
+ .invert()
112
+ .multiply(this.outputAmount.quotient).quotient;
113
+ return CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
114
+ }
115
+ }
116
+ /**
117
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
118
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
119
+ */
120
+ maximumAmountIn(slippageTolerance) {
121
+ invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE');
122
+ if (this.tradeType === TradeType.EXACT_INPUT) {
123
+ return this.inputAmount;
124
+ }
125
+ else {
126
+ const slippageAdjustedAmountIn = new Fraction(ONE)
127
+ .add(slippageTolerance)
128
+ .multiply(this.inputAmount.quotient).quotient;
129
+ return CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
130
+ }
131
+ }
132
+ /**
133
+ * Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
134
+ * amount to an output token, making at most `maxHops` hops.
135
+ * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
136
+ * the amount in among multiple routes.
137
+ * @param pairs the pairs to consider in finding the best trade
138
+ * @param nextAmountIn exact amount of input currency to spend
139
+ * @param currencyOut the desired currency out
140
+ * @param maxNumResults maximum number of results to return
141
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
142
+ * @param currentPairs used in recursion; the current list of pairs
143
+ * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
144
+ * @param bestTrades used in recursion; the current list of best trades
145
+ */
146
+ static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {},
147
+ // used in recursion.
148
+ currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
149
+ invariant(pairs.length > 0, 'PAIRS');
150
+ invariant(maxHops > 0, 'MAX_HOPS');
151
+ invariant(currencyAmountIn === nextAmountIn || currentPairs.length > 0, 'INVALID_RECURSION');
152
+ const amountIn = nextAmountIn.wrapped;
153
+ const tokenOut = currencyOut.wrapped;
154
+ for (let i = 0; i < pairs.length; i++) {
155
+ const pair = pairs[i];
156
+ // pair irrelevant
157
+ if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
158
+ continue;
159
+ if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
160
+ continue;
161
+ let amountOut;
162
+ try {
163
+ ;
164
+ [amountOut] = pair.getOutputAmount(amountIn);
165
+ }
166
+ catch (error) {
167
+ // input too low
168
+ if (error.isInsufficientInputAmountError) {
169
+ continue;
170
+ }
171
+ throw error;
172
+ }
173
+ // we have arrived at the output token, so this is the final trade of one of the paths
174
+ if (amountOut.currency.equals(tokenOut)) {
175
+ sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
176
+ }
177
+ else if (maxHops > 1 && pairs.length > 1) {
178
+ const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
179
+ // otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
180
+ Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
181
+ maxNumResults,
182
+ maxHops: maxHops - 1,
183
+ }, [...currentPairs, pair], amountOut, bestTrades);
184
+ }
185
+ }
186
+ return bestTrades;
187
+ }
188
+ /**
189
+ * Return the execution price after accounting for slippage tolerance
190
+ * @param slippageTolerance the allowed tolerated slippage
191
+ */
192
+ worstExecutionPrice(slippageTolerance) {
193
+ return new Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
194
+ }
195
+ /**
196
+ * similar to the above method but instead targets a fixed output amount
197
+ * given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
198
+ * to an output token amount, making at most `maxHops` hops
199
+ * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
200
+ * the amount in among multiple routes.
201
+ * @param pairs the pairs to consider in finding the best trade
202
+ * @param currencyIn the currency to spend
203
+ * @param nextAmountOut the exact amount of currency out
204
+ * @param maxNumResults maximum number of results to return
205
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
206
+ * @param currentPairs used in recursion; the current list of pairs
207
+ * @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
208
+ * @param bestTrades used in recursion; the current list of best trades
209
+ */
210
+ static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {},
211
+ // used in recursion.
212
+ currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
213
+ invariant(pairs.length > 0, 'PAIRS');
214
+ invariant(maxHops > 0, 'MAX_HOPS');
215
+ invariant(currencyAmountOut === nextAmountOut || currentPairs.length > 0, 'INVALID_RECURSION');
216
+ const amountOut = nextAmountOut.wrapped;
217
+ const tokenIn = currencyIn.wrapped;
218
+ for (let i = 0; i < pairs.length; i++) {
219
+ const pair = pairs[i];
220
+ // pair irrelevant
221
+ if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
222
+ continue;
223
+ if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
224
+ continue;
225
+ let amountIn;
226
+ try {
227
+ ;
228
+ [amountIn] = pair.getInputAmount(amountOut);
229
+ }
230
+ catch (error) {
231
+ // not enough liquidity in this pair
232
+ if (error.isInsufficientReservesError) {
233
+ continue;
234
+ }
235
+ throw error;
236
+ }
237
+ // we have arrived at the input token, so this is the first trade of one of the paths
238
+ if (amountIn.currency.equals(tokenIn)) {
239
+ sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
240
+ }
241
+ else if (maxHops > 1 && pairs.length > 1) {
242
+ const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
243
+ // otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
244
+ Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
245
+ maxNumResults,
246
+ maxHops: maxHops - 1,
247
+ }, [pair, ...currentPairs], amountIn, bestTrades);
248
+ }
249
+ }
250
+ return bestTrades;
251
+ }
252
+ }
253
+ //# 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,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AAY,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';
2
+ export * from './errors';
3
+ export * from './entities';
4
+ export * from './router';
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 '@centurion_project/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 '@centurion_project/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,6BAA6B,CAAA;AAE1H,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,OA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AE,QAAQ,CAAC,CAAA;oBAChD,KAAK,GAAG,QAAQ,CAAA;iBACjB;qBAAM;oBACL,UAAU,GAAG,0BAA0B,CAAA;oBACvC,yFAAyF;oBACzF,IAAI,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAE,QAAQ,CAAC,CAAA;oBAChD,KAAK,GAAG,QAAQ,CAAA;iBACjB;gBACD,MAAK;SACR;QACD,OAAO;YACL,UAAU;YACV,IAAI;YACJ,KAAK;SACN,CAAA;IACH,CAAC;CACF"}
@@ -0,0 +1,19 @@
1
+ import { Percent } from '@centurion_project/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;
@@ -0,0 +1,3 @@
1
+ export * from './pair';
2
+ export * from './route';
3
+ export * from './trade';