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