@kayenfinance/v2-sdk 1.2.1 → 1.2.3
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.
package/dist/v2-sdk.esm.js
CHANGED
|
@@ -6,8 +6,9 @@ import { keccak256, pack } from '@ethersproject/solidity';
|
|
|
6
6
|
import invariant from 'tiny-invariant';
|
|
7
7
|
|
|
8
8
|
var _FACTORY_ADDRESS_BY_C, _INIT_CODE_HASH_BY_CH;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated use FACTORY_ADDRESS_MAP instead
|
|
11
12
|
*/
|
|
12
13
|
var FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f';
|
|
13
14
|
var FACTORY_ADDRESS_BY_CHAIN_ID = (_FACTORY_ADDRESS_BY_C = {}, _FACTORY_ADDRESS_BY_C[ChainId.CHILIZ] = '0xE2918AA38088878546c1A18F2F9b1BC83297fdD3', _FACTORY_ADDRESS_BY_C[ChainId.SPICY] = '0xfc1924E20d64AD4daA3A4947b4bAE6cDE77d2dBC', _FACTORY_ADDRESS_BY_C);
|
|
@@ -15,6 +16,7 @@ var FACTORY_ADDRESS_MAP = V2_FACTORY_ADDRESSES;
|
|
|
15
16
|
var INIT_CODE_HASH = '0x4fb91ee014f2c198fe03fa69aa007f8e04f2fe96fe4444492a517fdec1063a17';
|
|
16
17
|
var INIT_CODE_HASH_BY_CHAIN_ID = (_INIT_CODE_HASH_BY_CH = {}, _INIT_CODE_HASH_BY_CH[ChainId.CHILIZ] = '0x4fb91ee014f2c198fe03fa69aa007f8e04f2fe96fe4444492a517fdec1063a17', _INIT_CODE_HASH_BY_CH[ChainId.SPICY] = '0x4fb91ee014f2c198fe03fa69aa007f8e04f2fe96fe4444492a517fdec1063a17', _INIT_CODE_HASH_BY_CH);
|
|
17
18
|
var MINIMUM_LIQUIDITY = /*#__PURE__*/JSBI.BigInt(1000);
|
|
19
|
+
|
|
18
20
|
// exports for internal consumption
|
|
19
21
|
var ZERO = /*#__PURE__*/JSBI.BigInt(0);
|
|
20
22
|
var ONE = /*#__PURE__*/JSBI.BigInt(1);
|
|
@@ -139,9 +141,10 @@ function _wrapNativeSuper(t) {
|
|
|
139
141
|
|
|
140
142
|
// see https://stackoverflow.com/a/41102306
|
|
141
143
|
var CAN_SET_PROTOTYPE = 'setPrototypeOf' in Object;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be
|
|
147
|
+
* obtained by sending any amount of input.
|
|
145
148
|
*/
|
|
146
149
|
var InsufficientReservesError = /*#__PURE__*/function (_Error) {
|
|
147
150
|
function InsufficientReservesError() {
|
|
@@ -155,9 +158,10 @@ var InsufficientReservesError = /*#__PURE__*/function (_Error) {
|
|
|
155
158
|
_inheritsLoose(InsufficientReservesError, _Error);
|
|
156
159
|
return InsufficientReservesError;
|
|
157
160
|
}(/*#__PURE__*/_wrapNativeSuper(Error));
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
*
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less
|
|
164
|
+
* than the price of a single unit of output after fees.
|
|
161
165
|
*/
|
|
162
166
|
var InsufficientInputAmountError = /*#__PURE__*/function (_Error2) {
|
|
163
167
|
function InsufficientInputAmountError() {
|
|
@@ -183,11 +187,18 @@ var computePairAddress = function computePairAddress(_ref) {
|
|
|
183
187
|
};
|
|
184
188
|
var Pair = /*#__PURE__*/function () {
|
|
185
189
|
function Pair(currencyAmountA, tokenAmountB) {
|
|
190
|
+
this.liquidityToken = void 0;
|
|
191
|
+
this.tokenAmounts = void 0;
|
|
186
192
|
var tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks
|
|
187
193
|
? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
188
194
|
this.liquidityToken = new Token(tokenAmounts[0].currency.chainId, Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), 18, 'UNI-V2', 'Uniswap V2');
|
|
189
195
|
this.tokenAmounts = tokenAmounts;
|
|
190
196
|
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns true if the token is either token0 or token1
|
|
200
|
+
* @param token to check
|
|
201
|
+
*/
|
|
191
202
|
Pair.getAddress = function getAddress(tokenA, tokenB) {
|
|
192
203
|
var _FACTORY_ADDRESS_MAP$;
|
|
193
204
|
var factoryAddress = (_FACTORY_ADDRESS_MAP$ = FACTORY_ADDRESS_MAP[tokenA.chainId]) != null ? _FACTORY_ADDRESS_MAP$ : FACTORY_ADDRESS;
|
|
@@ -196,92 +207,91 @@ var Pair = /*#__PURE__*/function () {
|
|
|
196
207
|
tokenA: tokenA,
|
|
197
208
|
tokenB: tokenB
|
|
198
209
|
});
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Returns true if the token is either token0 or token1
|
|
202
|
-
* @param token to check
|
|
203
|
-
*/;
|
|
210
|
+
};
|
|
204
211
|
var _proto = Pair.prototype;
|
|
205
212
|
_proto.involvesToken = function involvesToken(token) {
|
|
206
213
|
return token.equals(this.token0) || token.equals(this.token1);
|
|
207
214
|
}
|
|
208
|
-
|
|
209
|
-
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
210
218
|
*/;
|
|
211
|
-
/**
|
|
212
|
-
* Return the price of the given token in terms of the other token in the pair.
|
|
213
|
-
* @param token token to return price of
|
|
219
|
+
/**
|
|
220
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
221
|
+
* @param token token to return price of
|
|
214
222
|
*/
|
|
215
223
|
_proto.priceOf = function priceOf(token) {
|
|
216
224
|
!this.involvesToken(token) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TOKEN') : invariant(false) : void 0;
|
|
217
225
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
218
226
|
}
|
|
219
|
-
|
|
220
|
-
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Returns the chain ID of the tokens in the pair.
|
|
221
230
|
*/;
|
|
222
231
|
_proto.reserveOf = function reserveOf(token) {
|
|
223
232
|
!this.involvesToken(token) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TOKEN') : invariant(false) : void 0;
|
|
224
233
|
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
225
234
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
* /
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
* @param
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* getAmountOut is the linear algebra of reserve ratio against amountIn:amountOut.
|
|
238
|
+
* https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
|
|
239
|
+
* has the math deduction for the reserve calculation without fee-on-transfer fees.
|
|
240
|
+
*
|
|
241
|
+
* With fee-on-transfer tax, intuitively it's just:
|
|
242
|
+
* inputAmountWithFeeAndTax = 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
243
|
+
* = (1 - amountIn.sellFeesBips / 10000) * amountInWithFee
|
|
244
|
+
* where amountInWithFee is the amountIn after taking out the LP fees
|
|
245
|
+
* outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
246
|
+
*
|
|
247
|
+
* But we are illustrating the math deduction below to ensure that's the case.
|
|
248
|
+
*
|
|
249
|
+
* before swap A * B = K where A = reserveIn B = reserveOut
|
|
250
|
+
*
|
|
251
|
+
* after swap A' * B' = K where only k is a constant value
|
|
252
|
+
*
|
|
253
|
+
* getAmountOut
|
|
254
|
+
*
|
|
255
|
+
* A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
|
|
256
|
+
* B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
257
|
+
* amountOut = (B - B') / (1 - amountOut.buyFeesBips / 10000) # where A' * B' still is k
|
|
258
|
+
* = (B - K/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
259
|
+
* /
|
|
260
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
261
|
+
* = (B - AB/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
262
|
+
* /
|
|
263
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
264
|
+
* = ((BA + B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn - AB)/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
|
|
265
|
+
* /
|
|
266
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
267
|
+
* = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn / (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
268
|
+
* /
|
|
269
|
+
* (1 - amountOut.buyFeesBips / 10000)
|
|
270
|
+
* amountOut * (1 - amountOut.buyFeesBips / 10000) = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
271
|
+
* /
|
|
272
|
+
* (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
273
|
+
*
|
|
274
|
+
* outputAmountWithTax = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
|
|
275
|
+
* /
|
|
276
|
+
* (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
|
|
277
|
+
* = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn * 1000
|
|
278
|
+
* /
|
|
279
|
+
* ((A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn) * 1000)
|
|
280
|
+
* = (B * (1 - amountIn.sellFeesBips / 10000) 997 * * amountIn
|
|
281
|
+
* /
|
|
282
|
+
* (1000 * A + (1 - amountIn.sellFeesBips / 10000) * 997 * amountIn)
|
|
283
|
+
* = (B * (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
|
|
284
|
+
* /
|
|
285
|
+
* (1000 * A + (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
|
|
286
|
+
* = (B * inputAmountWithFeeAndTax)
|
|
287
|
+
* /
|
|
288
|
+
* (1000 * A + inputAmountWithFeeAndTax)
|
|
289
|
+
*
|
|
290
|
+
* inputAmountWithFeeAndTax = (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee
|
|
291
|
+
* outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
292
|
+
*
|
|
293
|
+
* @param inputAmount
|
|
294
|
+
* @param calculateFotFees
|
|
285
295
|
*/;
|
|
286
296
|
_proto.getOutputAmount = function getOutputAmount(inputAmount, calculateFotFees) {
|
|
287
297
|
if (calculateFotFees === void 0) {
|
|
@@ -312,47 +322,48 @@ var Pair = /*#__PURE__*/function () {
|
|
|
312
322
|
}
|
|
313
323
|
return [outputAmountAfterTax, new Pair(inputReserve.add(inputAmountAfterTax), outputReserve.subtract(outputAmountAfterTax))];
|
|
314
324
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* getAmountIn is the linear algebra of reserve ratio against amountIn:amountOut.
|
|
328
|
+
* https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
|
|
329
|
+
* has the math deduction for the reserve calculation without fee-on-transfer fees.
|
|
330
|
+
*
|
|
331
|
+
* With fee-on-transfer fees, intuitively it's just:
|
|
332
|
+
* outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
|
|
333
|
+
* inputAmountWithTax = amountIn / (1 - amountIn.sellFeesBips / 10000) / 0.997
|
|
334
|
+
*
|
|
335
|
+
* But we are illustrating the math deduction below to ensure that's the case.
|
|
336
|
+
*
|
|
337
|
+
* before swap A * B = K where A = reserveIn B = reserveOut
|
|
338
|
+
*
|
|
339
|
+
* after swap A' * B' = K where only k is a constant value
|
|
340
|
+
*
|
|
341
|
+
* getAmountIn
|
|
342
|
+
*
|
|
343
|
+
* B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
|
|
344
|
+
* A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
|
|
345
|
+
* amountIn = (A' - A) / (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
346
|
+
* = (K / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
|
|
347
|
+
* /
|
|
348
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
349
|
+
* = (AB / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
|
|
350
|
+
* /
|
|
351
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
352
|
+
* = ((AB - AB + A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
353
|
+
* /
|
|
354
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
355
|
+
* = ((A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
356
|
+
* /
|
|
357
|
+
* (0.997 * (1 - amountIn.sellFeesBips / 10000))
|
|
358
|
+
* = ((A * 1000 * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
|
|
359
|
+
* /
|
|
360
|
+
* (997 * (1 - amountIn.sellFeesBips / 10000))
|
|
361
|
+
*
|
|
362
|
+
* outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
|
|
363
|
+
* inputAmountWithTax = amountIn / (997 * (1 - amountIn.sellFeesBips / 10000))
|
|
364
|
+
* = (A * outputAmountWithTax * 1000) / ((B - outputAmountWithTax) * 997)
|
|
365
|
+
*
|
|
366
|
+
* @param outputAmount
|
|
356
367
|
*/;
|
|
357
368
|
_proto.getInputAmount = function getInputAmount(outputAmount, calculateFotFees) {
|
|
358
369
|
if (calculateFotFees === void 0) {
|
|
@@ -447,8 +458,9 @@ var Pair = /*#__PURE__*/function () {
|
|
|
447
458
|
var result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
448
459
|
return new Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
449
460
|
}
|
|
450
|
-
|
|
451
|
-
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
452
464
|
*/
|
|
453
465
|
}, {
|
|
454
466
|
key: "token1Price",
|
|
@@ -486,6 +498,10 @@ var Pair = /*#__PURE__*/function () {
|
|
|
486
498
|
|
|
487
499
|
var Route = /*#__PURE__*/function () {
|
|
488
500
|
function Route(pairs, input, output) {
|
|
501
|
+
this.pairs = void 0;
|
|
502
|
+
this.path = void 0;
|
|
503
|
+
this.input = void 0;
|
|
504
|
+
this.output = void 0;
|
|
489
505
|
this._midPrice = null;
|
|
490
506
|
!(pairs.length > 0) ? process.env.NODE_ENV !== "production" ? invariant(false, 'PAIRS') : invariant(false) : void 0;
|
|
491
507
|
var chainId = pairs[0].chainId;
|
|
@@ -534,6 +550,8 @@ var Route = /*#__PURE__*/function () {
|
|
|
534
550
|
}]);
|
|
535
551
|
}();
|
|
536
552
|
|
|
553
|
+
// minimal interface so the input output comparator may be shared across types
|
|
554
|
+
|
|
537
555
|
// comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts
|
|
538
556
|
// in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first
|
|
539
557
|
function inputOutputComparator(a, b) {
|
|
@@ -559,27 +577,54 @@ function inputOutputComparator(a, b) {
|
|
|
559
577
|
}
|
|
560
578
|
}
|
|
561
579
|
}
|
|
580
|
+
|
|
562
581
|
// extension of the input output comparator that also considers other dimensions of the trade in ranking them
|
|
563
582
|
function tradeComparator(a, b) {
|
|
564
583
|
var ioComp = inputOutputComparator(a, b);
|
|
565
584
|
if (ioComp !== 0) {
|
|
566
585
|
return ioComp;
|
|
567
586
|
}
|
|
587
|
+
|
|
568
588
|
// consider lowest slippage next, since these are less likely to fail
|
|
569
589
|
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
570
590
|
return -1;
|
|
571
591
|
} else if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
572
592
|
return 1;
|
|
573
593
|
}
|
|
594
|
+
|
|
574
595
|
// finally consider the number of hops since each hop costs gas
|
|
575
596
|
return a.route.path.length - b.route.path.length;
|
|
576
597
|
}
|
|
577
|
-
/**
|
|
578
|
-
* Represents a trade executed against a list of pairs.
|
|
579
|
-
* Does not account for slippage, i.e. trades that front run this trade and move the price.
|
|
598
|
+
/**
|
|
599
|
+
* Represents a trade executed against a list of pairs.
|
|
600
|
+
* Does not account for slippage, i.e. trades that front run this trade and move the price.
|
|
580
601
|
*/
|
|
581
602
|
var Trade = /*#__PURE__*/function () {
|
|
582
603
|
function Trade(route, amount, tradeType) {
|
|
604
|
+
/**
|
|
605
|
+
* The route of the trade, i.e. which pairs the trade goes through and the input/output currencies.
|
|
606
|
+
*/
|
|
607
|
+
this.route = void 0;
|
|
608
|
+
/**
|
|
609
|
+
* The type of the trade, either exact in or exact out.
|
|
610
|
+
*/
|
|
611
|
+
this.tradeType = void 0;
|
|
612
|
+
/**
|
|
613
|
+
* The input amount for the trade assuming no slippage.
|
|
614
|
+
*/
|
|
615
|
+
this.inputAmount = void 0;
|
|
616
|
+
/**
|
|
617
|
+
* The output amount for the trade assuming no slippage.
|
|
618
|
+
*/
|
|
619
|
+
this.outputAmount = void 0;
|
|
620
|
+
/**
|
|
621
|
+
* The price expressed in terms of output amount/input amount.
|
|
622
|
+
*/
|
|
623
|
+
this.executionPrice = void 0;
|
|
624
|
+
/**
|
|
625
|
+
* The percent difference between the mid price before the trade and the trade execution price.
|
|
626
|
+
*/
|
|
627
|
+
this.priceImpact = void 0;
|
|
583
628
|
this.route = route;
|
|
584
629
|
this.tradeType = tradeType;
|
|
585
630
|
var tokenAmounts = new Array(route.path.length);
|
|
@@ -609,26 +654,28 @@ var Trade = /*#__PURE__*/function () {
|
|
|
609
654
|
this.executionPrice = new Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
610
655
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
611
656
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
*
|
|
615
|
-
* @param
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
660
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
661
|
+
*/
|
|
662
|
+
/**
|
|
663
|
+
* Constructs an exact in trade with the given amount in and route
|
|
664
|
+
* @param route route of the exact in trade
|
|
665
|
+
* @param amountIn the amount being passed in
|
|
616
666
|
*/
|
|
617
667
|
Trade.exactIn = function exactIn(route, amountIn) {
|
|
618
668
|
return new Trade(route, amountIn, TradeType.EXACT_INPUT);
|
|
619
669
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
*
|
|
623
|
-
* @param
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Constructs an exact out trade with the given amount out and route
|
|
673
|
+
* @param route route of the exact out trade
|
|
674
|
+
* @param amountOut the amount returned by the trade
|
|
624
675
|
*/;
|
|
625
676
|
Trade.exactOut = function exactOut(route, amountOut) {
|
|
626
677
|
return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
630
|
-
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
631
|
-
*/;
|
|
678
|
+
};
|
|
632
679
|
var _proto = Trade.prototype;
|
|
633
680
|
_proto.minimumAmountOut = function minimumAmountOut(slippageTolerance) {
|
|
634
681
|
!!slippageTolerance.lessThan(ZERO) ? process.env.NODE_ENV !== "production" ? invariant(false, 'SLIPPAGE_TOLERANCE') : invariant(false) : void 0;
|
|
@@ -639,9 +686,10 @@ var Trade = /*#__PURE__*/function () {
|
|
|
639
686
|
return CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
640
687
|
}
|
|
641
688
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
*
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
692
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
645
693
|
*/;
|
|
646
694
|
_proto.maximumAmountIn = function maximumAmountIn(slippageTolerance) {
|
|
647
695
|
!!slippageTolerance.lessThan(ZERO) ? process.env.NODE_ENV !== "production" ? invariant(false, 'SLIPPAGE_TOLERANCE') : invariant(false) : void 0;
|
|
@@ -652,19 +700,20 @@ var Trade = /*#__PURE__*/function () {
|
|
|
652
700
|
return CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
653
701
|
}
|
|
654
702
|
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
*
|
|
658
|
-
*
|
|
659
|
-
*
|
|
660
|
-
*
|
|
661
|
-
* @param
|
|
662
|
-
* @param
|
|
663
|
-
* @param
|
|
664
|
-
* @param
|
|
665
|
-
* @param
|
|
666
|
-
* @param
|
|
667
|
-
* @param
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
706
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
707
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
708
|
+
* the amount in among multiple routes.
|
|
709
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
710
|
+
* @param nextAmountIn exact amount of input currency to spend
|
|
711
|
+
* @param currencyOut the desired currency out
|
|
712
|
+
* @param maxNumResults maximum number of results to return
|
|
713
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
714
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
715
|
+
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
716
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
668
717
|
*/;
|
|
669
718
|
Trade.bestTradeExactIn = function bestTradeExactIn(pairs, currencyAmountIn, currencyOut, _temp,
|
|
670
719
|
// used in recursion.
|
|
@@ -710,6 +759,7 @@ var Trade = /*#__PURE__*/function () {
|
|
|
710
759
|
sortedInsert(bestTrades, new Trade(new Route([].concat(currentPairs, [pair]), currencyAmountIn.currency, currencyOut), currencyAmountIn, TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
711
760
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
712
761
|
var pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
762
|
+
|
|
713
763
|
// otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
|
|
714
764
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
715
765
|
maxNumResults: maxNumResults,
|
|
@@ -719,27 +769,29 @@ var Trade = /*#__PURE__*/function () {
|
|
|
719
769
|
}
|
|
720
770
|
return bestTrades;
|
|
721
771
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
*
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* Return the execution price after accounting for slippage tolerance
|
|
775
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
725
776
|
*/;
|
|
726
777
|
_proto.worstExecutionPrice = function worstExecutionPrice(slippageTolerance) {
|
|
727
778
|
return new Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
728
779
|
}
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
*
|
|
734
|
-
*
|
|
735
|
-
*
|
|
736
|
-
* @param
|
|
737
|
-
* @param
|
|
738
|
-
* @param
|
|
739
|
-
* @param
|
|
740
|
-
* @param
|
|
741
|
-
* @param
|
|
742
|
-
* @param
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* similar to the above method but instead targets a fixed output amount
|
|
783
|
+
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
784
|
+
* to an output token amount, making at most `maxHops` hops
|
|
785
|
+
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
786
|
+
* the amount in among multiple routes.
|
|
787
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
788
|
+
* @param currencyIn the currency to spend
|
|
789
|
+
* @param nextAmountOut the exact amount of currency out
|
|
790
|
+
* @param maxNumResults maximum number of results to return
|
|
791
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
792
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
793
|
+
* @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
|
|
794
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
743
795
|
*/;
|
|
744
796
|
Trade.bestTradeExactOut = function bestTradeExactOut(pairs, currencyIn, currencyAmountOut, _temp2,
|
|
745
797
|
// used in recursion.
|
|
@@ -785,6 +837,7 @@ var Trade = /*#__PURE__*/function () {
|
|
|
785
837
|
sortedInsert(bestTrades, new Trade(new Route([pair].concat(currentPairs), currencyIn, currencyAmountOut.currency), currencyAmountOut, TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
786
838
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
787
839
|
var pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
840
|
+
|
|
788
841
|
// otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
|
|
789
842
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
790
843
|
maxNumResults: maxNumResults,
|
|
@@ -797,22 +850,31 @@ var Trade = /*#__PURE__*/function () {
|
|
|
797
850
|
return Trade;
|
|
798
851
|
}();
|
|
799
852
|
|
|
853
|
+
/**
|
|
854
|
+
* Options for producing the arguments to send call to the router.
|
|
855
|
+
*/
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* The parameters to use in the call to the Uniswap V2 Router to execute a trade.
|
|
859
|
+
*/
|
|
860
|
+
|
|
800
861
|
function toHex(currencyAmount) {
|
|
801
862
|
return "0x" + currencyAmount.quotient.toString(16);
|
|
802
863
|
}
|
|
803
864
|
var ZERO_HEX = '0x0';
|
|
804
|
-
|
|
805
|
-
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Represents the Uniswap V2 Router, and has static methods for helping execute trades.
|
|
806
868
|
*/
|
|
807
869
|
var Router = /*#__PURE__*/function () {
|
|
808
|
-
/**
|
|
809
|
-
* Cannot be constructed.
|
|
870
|
+
/**
|
|
871
|
+
* Cannot be constructed.
|
|
810
872
|
*/
|
|
811
873
|
function Router() {}
|
|
812
|
-
/**
|
|
813
|
-
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
814
|
-
* @param trade to produce call parameters for
|
|
815
|
-
* @param options options for the call parameters
|
|
874
|
+
/**
|
|
875
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
876
|
+
* @param trade to produce call parameters for
|
|
877
|
+
* @param options options for the call parameters
|
|
816
878
|
*/
|
|
817
879
|
Router.swapCallParameters = function swapCallParameters(trade, options) {
|
|
818
880
|
var etherIn = trade.inputAmount.currency.isNative;
|