@kayenfinance/v3-sdk 1.3.1 → 1.3.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/v3-sdk.esm.js
CHANGED
|
@@ -223,6 +223,7 @@ var _FACTORY_ADDRESS_BY_C, _POOL_INIT_CODE_HASH_, _TICK_SPACINGS;
|
|
|
223
223
|
var FACTORY_ADDRESS = '0x4bC8BDF50843Cc503E196Be4A9560134d358bb3C';
|
|
224
224
|
var FACTORY_ADDRESS_BY_CHAIN_ID = (_FACTORY_ADDRESS_BY_C = {}, _FACTORY_ADDRESS_BY_C[ChainId.CHILIZ] = '0x4bC8BDF50843Cc503E196Be4A9560134d358bb3C', _FACTORY_ADDRESS_BY_C[ChainId.SPICY] = '0x9Fc08909577539c635F04ef924f65c56560c8BD3', _FACTORY_ADDRESS_BY_C);
|
|
225
225
|
var ADDRESS_ZERO = '0x0000000000000000000000000000000000000000';
|
|
226
|
+
|
|
226
227
|
// @deprecated please use poolInitCodeHash(chainId: ChainId)
|
|
227
228
|
var POOL_INIT_CODE_HASH = '0xcb96d536c98123b475b91f2f81bd6d5877fb48561b42d7892f4725df4c8cce7e';
|
|
228
229
|
var POOL_INIT_CODE_HASH_BY_CHAIN_ID = (_POOL_INIT_CODE_HASH_ = {}, _POOL_INIT_CODE_HASH_[ChainId.CHILIZ] = '0xcb96d536c98123b475b91f2f81bd6d5877fb48561b42d7892f4725df4c8cce7e', _POOL_INIT_CODE_HASH_[ChainId.SPICY] = '0xcb96d536c98123b475b91f2f81bd6d5877fb48561b42d7892f4725df4c8cce7e', _POOL_INIT_CODE_HASH_);
|
|
@@ -235,11 +236,11 @@ function poolInitCodeHash(chainId) {
|
|
|
235
236
|
return (_POOL_INIT_CODE_HASH_2 = POOL_INIT_CODE_HASH_BY_CHAIN_ID[chainId]) != null ? _POOL_INIT_CODE_HASH_2 : POOL_INIT_CODE_HASH;
|
|
236
237
|
}
|
|
237
238
|
}
|
|
238
|
-
|
|
239
|
-
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The default factory enabled fee amounts, denominated in hundredths of bips.
|
|
240
242
|
*/
|
|
241
|
-
var FeeAmount
|
|
242
|
-
(function (FeeAmount) {
|
|
243
|
+
var FeeAmount = /*#__PURE__*/function (FeeAmount) {
|
|
243
244
|
FeeAmount[FeeAmount["LOWEST"] = 100] = "LOWEST";
|
|
244
245
|
FeeAmount[FeeAmount["LOW_200"] = 200] = "LOW_200";
|
|
245
246
|
FeeAmount[FeeAmount["LOW_300"] = 300] = "LOW_300";
|
|
@@ -247,9 +248,11 @@ var FeeAmount;
|
|
|
247
248
|
FeeAmount[FeeAmount["LOW"] = 500] = "LOW";
|
|
248
249
|
FeeAmount[FeeAmount["MEDIUM"] = 3000] = "MEDIUM";
|
|
249
250
|
FeeAmount[FeeAmount["HIGH"] = 10000] = "HIGH";
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
251
|
+
return FeeAmount;
|
|
252
|
+
}({});
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The default factory tick spacings by fee amount.
|
|
253
256
|
*/
|
|
254
257
|
var TICK_SPACINGS = (_TICK_SPACINGS = {}, _TICK_SPACINGS[FeeAmount.LOWEST] = 1, _TICK_SPACINGS[FeeAmount.LOW_200] = 4, _TICK_SPACINGS[FeeAmount.LOW_300] = 6, _TICK_SPACINGS[FeeAmount.LOW_400] = 8, _TICK_SPACINGS[FeeAmount.LOW] = 10, _TICK_SPACINGS[FeeAmount.MEDIUM] = 60, _TICK_SPACINGS[FeeAmount.HIGH] = 200, _TICK_SPACINGS);
|
|
255
258
|
|
|
@@ -257,19 +260,20 @@ var TICK_SPACINGS = (_TICK_SPACINGS = {}, _TICK_SPACINGS[FeeAmount.LOWEST] = 1,
|
|
|
257
260
|
var NEGATIVE_ONE = /*#__PURE__*/JSBI.BigInt(-1);
|
|
258
261
|
var ZERO = /*#__PURE__*/JSBI.BigInt(0);
|
|
259
262
|
var ONE = /*#__PURE__*/JSBI.BigInt(1);
|
|
263
|
+
|
|
260
264
|
// used in liquidity amount math
|
|
261
265
|
var Q96 = /*#__PURE__*/JSBI.exponentiate(/*#__PURE__*/JSBI.BigInt(2), /*#__PURE__*/JSBI.BigInt(96));
|
|
262
266
|
var Q192 = /*#__PURE__*/JSBI.exponentiate(Q96, /*#__PURE__*/JSBI.BigInt(2));
|
|
263
267
|
|
|
264
|
-
/**
|
|
265
|
-
* Computes a pool address
|
|
266
|
-
* @param factoryAddress The Uniswap V3 factory address
|
|
267
|
-
* @param tokenA The first token of the pair, irrespective of sort order
|
|
268
|
-
* @param tokenB The second token of the pair, irrespective of sort order
|
|
269
|
-
* @param fee The fee tier of the pool
|
|
270
|
-
* @param initCodeHashManualOverride Override the init code hash used to compute the pool address if necessary
|
|
271
|
-
* @param chainId
|
|
272
|
-
* @returns The pool address
|
|
268
|
+
/**
|
|
269
|
+
* Computes a pool address
|
|
270
|
+
* @param factoryAddress The Uniswap V3 factory address
|
|
271
|
+
* @param tokenA The first token of the pair, irrespective of sort order
|
|
272
|
+
* @param tokenB The second token of the pair, irrespective of sort order
|
|
273
|
+
* @param fee The fee tier of the pool
|
|
274
|
+
* @param initCodeHashManualOverride Override the init code hash used to compute the pool address if necessary
|
|
275
|
+
* @param chainId
|
|
276
|
+
* @returns The pool address
|
|
273
277
|
*/
|
|
274
278
|
function computePoolAddress(_ref) {
|
|
275
279
|
var factoryAddress = _ref.factoryAddress,
|
|
@@ -283,6 +287,7 @@ function computePoolAddress(_ref) {
|
|
|
283
287
|
token1 = _ref2[1]; // does safety checks
|
|
284
288
|
var salt = keccak256(['bytes'], [defaultAbiCoder.encode(['address', 'address', 'uint24'], [token0.address, token1.address, fee])]);
|
|
285
289
|
var initCodeHash = initCodeHashManualOverride != null ? initCodeHashManualOverride : poolInitCodeHash(chainId);
|
|
290
|
+
|
|
286
291
|
// ZKSync uses a different create2 address computation
|
|
287
292
|
// Most likely all ZKEVM chains will use the different computation from standard create2
|
|
288
293
|
switch (chainId) {
|
|
@@ -294,8 +299,8 @@ function computePoolAddress(_ref) {
|
|
|
294
299
|
}
|
|
295
300
|
|
|
296
301
|
var FullMath = /*#__PURE__*/function () {
|
|
297
|
-
/**
|
|
298
|
-
* Cannot be constructed.
|
|
302
|
+
/**
|
|
303
|
+
* Cannot be constructed.
|
|
299
304
|
*/
|
|
300
305
|
function FullMath() {}
|
|
301
306
|
FullMath.mulDivRoundingUp = function mulDivRoundingUp(a, b, denominator) {
|
|
@@ -317,8 +322,8 @@ function addIn256(x, y) {
|
|
|
317
322
|
return JSBI.bitwiseAnd(sum, MaxUint256);
|
|
318
323
|
}
|
|
319
324
|
var SqrtPriceMath = /*#__PURE__*/function () {
|
|
320
|
-
/**
|
|
321
|
-
* Cannot be constructed.
|
|
325
|
+
/**
|
|
326
|
+
* Cannot be constructed.
|
|
322
327
|
*/
|
|
323
328
|
function SqrtPriceMath() {}
|
|
324
329
|
SqrtPriceMath.getAmount0Delta = function getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
@@ -384,8 +389,8 @@ var SqrtPriceMath = /*#__PURE__*/function () {
|
|
|
384
389
|
|
|
385
390
|
var MAX_FEE = /*#__PURE__*/JSBI.exponentiate(/*#__PURE__*/JSBI.BigInt(10), /*#__PURE__*/JSBI.BigInt(6));
|
|
386
391
|
var SwapMath = /*#__PURE__*/function () {
|
|
387
|
-
/**
|
|
388
|
-
* Cannot be constructed.
|
|
392
|
+
/**
|
|
393
|
+
* Cannot be constructed.
|
|
389
394
|
*/
|
|
390
395
|
function SwapMath() {}
|
|
391
396
|
SwapMath.computeSwapStep = function computeSwapStep(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, amountRemaining, feePips) {
|
|
@@ -432,8 +437,8 @@ var SwapMath = /*#__PURE__*/function () {
|
|
|
432
437
|
}();
|
|
433
438
|
|
|
434
439
|
var LiquidityMath = /*#__PURE__*/function () {
|
|
435
|
-
/**
|
|
436
|
-
* Cannot be constructed.
|
|
440
|
+
/**
|
|
441
|
+
* Cannot be constructed.
|
|
437
442
|
*/
|
|
438
443
|
function LiquidityMath() {}
|
|
439
444
|
LiquidityMath.addDelta = function addDelta(x, y) {
|
|
@@ -466,18 +471,23 @@ function mostSignificantBit(x) {
|
|
|
466
471
|
return msb;
|
|
467
472
|
}
|
|
468
473
|
|
|
474
|
+
var _TickMath;
|
|
469
475
|
function mulShift(val, mulBy) {
|
|
470
476
|
return JSBI.signedRightShift(JSBI.multiply(val, JSBI.BigInt(mulBy)), JSBI.BigInt(128));
|
|
471
477
|
}
|
|
472
478
|
var Q32 = /*#__PURE__*/JSBI.exponentiate(/*#__PURE__*/JSBI.BigInt(2), /*#__PURE__*/JSBI.BigInt(32));
|
|
473
479
|
var TickMath = /*#__PURE__*/function () {
|
|
474
|
-
/**
|
|
475
|
-
* Cannot be constructed.
|
|
480
|
+
/**
|
|
481
|
+
* Cannot be constructed.
|
|
476
482
|
*/
|
|
477
483
|
function TickMath() {}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
*
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* The minimum tick that can be used on any pool.
|
|
487
|
+
*/
|
|
488
|
+
/**
|
|
489
|
+
* Returns the sqrt ratio as a Q64.96 for the given tick. The sqrt ratio is computed as sqrt(1.0001)^tick
|
|
490
|
+
* @param tick the tick for which to compute the sqrt ratio
|
|
481
491
|
*/
|
|
482
492
|
TickMath.getSqrtRatioAtTick = function getSqrtRatioAtTick(tick) {
|
|
483
493
|
!(tick >= TickMath.MIN_TICK && tick <= TickMath.MAX_TICK && Number.isInteger(tick)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TICK') : invariant(false) : void 0;
|
|
@@ -503,13 +513,15 @@ var TickMath = /*#__PURE__*/function () {
|
|
|
503
513
|
if ((absTick & 0x40000) !== 0) ratio = mulShift(ratio, '0x2216e584f5fa1ea926041bedfe98');
|
|
504
514
|
if ((absTick & 0x80000) !== 0) ratio = mulShift(ratio, '0x48a170391f7dc42444e8fa2');
|
|
505
515
|
if (tick > 0) ratio = JSBI.divide(MaxUint256, ratio);
|
|
516
|
+
|
|
506
517
|
// back to Q96
|
|
507
518
|
return JSBI.greaterThan(JSBI.remainder(ratio, Q32), ZERO) ? JSBI.add(JSBI.divide(ratio, Q32), ONE) : JSBI.divide(ratio, Q32);
|
|
508
519
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
*
|
|
512
|
-
*
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Returns the tick corresponding to a given sqrt ratio, s.t. #getSqrtRatioAtTick(tick) <= sqrtRatioX96
|
|
523
|
+
* and #getSqrtRatioAtTick(tick + 1) > sqrtRatioX96
|
|
524
|
+
* @param sqrtRatioX96 the sqrt ratio as a Q64.96 for which to compute the tick
|
|
513
525
|
*/;
|
|
514
526
|
TickMath.getTickAtSqrtRatio = function getTickAtSqrtRatio(sqrtRatioX96) {
|
|
515
527
|
!(JSBI.greaterThanOrEqual(sqrtRatioX96, TickMath.MIN_SQRT_RATIO) && JSBI.lessThan(sqrtRatioX96, TickMath.MAX_SQRT_RATIO)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'SQRT_RATIO') : invariant(false) : void 0;
|
|
@@ -535,20 +547,18 @@ var TickMath = /*#__PURE__*/function () {
|
|
|
535
547
|
};
|
|
536
548
|
return TickMath;
|
|
537
549
|
}();
|
|
538
|
-
|
|
539
|
-
* The minimum tick that can be used on any pool.
|
|
540
|
-
*/
|
|
550
|
+
_TickMath = TickMath;
|
|
541
551
|
TickMath.MIN_TICK = -887272;
|
|
542
|
-
/**
|
|
543
|
-
* The maximum tick that can be used on any pool.
|
|
552
|
+
/**
|
|
553
|
+
* The maximum tick that can be used on any pool.
|
|
544
554
|
*/
|
|
545
|
-
TickMath.MAX_TICK = -
|
|
546
|
-
/**
|
|
547
|
-
* The sqrt ratio corresponding to the minimum tick that could be used on any pool.
|
|
555
|
+
TickMath.MAX_TICK = -_TickMath.MIN_TICK;
|
|
556
|
+
/**
|
|
557
|
+
* The sqrt ratio corresponding to the minimum tick that could be used on any pool.
|
|
548
558
|
*/
|
|
549
559
|
TickMath.MIN_SQRT_RATIO = /*#__PURE__*/JSBI.BigInt('4295128739');
|
|
550
|
-
/**
|
|
551
|
-
* The sqrt ratio corresponding to the maximum tick that could be used on any pool.
|
|
560
|
+
/**
|
|
561
|
+
* The sqrt ratio corresponding to the maximum tick that could be used on any pool.
|
|
552
562
|
*/
|
|
553
563
|
TickMath.MAX_SQRT_RATIO = /*#__PURE__*/JSBI.BigInt('1461446703485210103287273052203988822378723970342');
|
|
554
564
|
|
|
@@ -583,7 +593,12 @@ function _v3Swap() {
|
|
|
583
593
|
break;
|
|
584
594
|
}
|
|
585
595
|
step = {};
|
|
586
|
-
step.sqrtPriceStartX96 = state.sqrtPriceX96
|
|
596
|
+
step.sqrtPriceStartX96 = state.sqrtPriceX96
|
|
597
|
+
|
|
598
|
+
// because each iteration of the while loop rounds, we can't optimize this code (relative to the smart contract)
|
|
599
|
+
// by simply traversing to the next available tick, we instead need to exactly replicate
|
|
600
|
+
// tickBitmap.nextInitializedTickWithinOneWord
|
|
601
|
+
;
|
|
587
602
|
_context.n = 2;
|
|
588
603
|
return tickDataProvider.nextInitializedTickWithinOneWord(state.tick, zeroForOne, tickSpacing);
|
|
589
604
|
case 2:
|
|
@@ -608,6 +623,7 @@ function _v3Swap() {
|
|
|
608
623
|
state.amountSpecifiedRemaining = JSBI.add(state.amountSpecifiedRemaining, step.amountOut);
|
|
609
624
|
state.amountCalculated = JSBI.add(state.amountCalculated, JSBI.add(step.amountIn, step.feeAmount));
|
|
610
625
|
}
|
|
626
|
+
|
|
611
627
|
// TODO
|
|
612
628
|
if (!JSBI.equal(state.sqrtPriceX96, step.sqrtPriceNextX96)) {
|
|
613
629
|
_context.n = 5;
|
|
@@ -652,9 +668,13 @@ function _v3Swap() {
|
|
|
652
668
|
return _v3Swap.apply(this, arguments);
|
|
653
669
|
}
|
|
654
670
|
|
|
655
|
-
/**
|
|
656
|
-
*
|
|
657
|
-
|
|
671
|
+
/**
|
|
672
|
+
* Provides information about ticks
|
|
673
|
+
*/
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* This tick data provider does not know how to fetch any tick data. It throws whenever it is required. Useful if you
|
|
677
|
+
* do not need to load tick data for your use case.
|
|
658
678
|
*/
|
|
659
679
|
var NoTickDataProvider = /*#__PURE__*/function () {
|
|
660
680
|
function NoTickDataProvider() {}
|
|
@@ -695,11 +715,11 @@ var NoTickDataProvider = /*#__PURE__*/function () {
|
|
|
695
715
|
}();
|
|
696
716
|
NoTickDataProvider.ERROR_MESSAGE = 'No tick data provider was given';
|
|
697
717
|
|
|
698
|
-
/**
|
|
699
|
-
* Determines if a tick list is sorted
|
|
700
|
-
* @param list The tick list
|
|
701
|
-
* @param comparator The comparator
|
|
702
|
-
* @returns true if sorted
|
|
718
|
+
/**
|
|
719
|
+
* Determines if a tick list is sorted
|
|
720
|
+
* @param list The tick list
|
|
721
|
+
* @param comparator The comparator
|
|
722
|
+
* @returns true if sorted
|
|
703
723
|
*/
|
|
704
724
|
function isSorted(list, comparator) {
|
|
705
725
|
for (var i = 0; i < list.length - 1; i++) {
|
|
@@ -713,12 +733,13 @@ function isSorted(list, comparator) {
|
|
|
713
733
|
function tickComparator(a, b) {
|
|
714
734
|
return a.index - b.index;
|
|
715
735
|
}
|
|
716
|
-
|
|
717
|
-
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* Utility methods for interacting with sorted lists of ticks
|
|
718
739
|
*/
|
|
719
740
|
var TickList = /*#__PURE__*/function () {
|
|
720
|
-
/**
|
|
721
|
-
* Cannot be constructed
|
|
741
|
+
/**
|
|
742
|
+
* Cannot be constructed
|
|
722
743
|
*/
|
|
723
744
|
function TickList() {}
|
|
724
745
|
TickList.validateList = function validateList(ticks, tickSpacing) {
|
|
@@ -728,6 +749,7 @@ var TickList = /*#__PURE__*/function () {
|
|
|
728
749
|
var index = _ref.index;
|
|
729
750
|
return index % tickSpacing === 0;
|
|
730
751
|
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TICK_SPACING') : invariant(false) : void 0;
|
|
752
|
+
|
|
731
753
|
// ensure tick liquidity deltas sum to 0
|
|
732
754
|
!JSBI.equal(ticks.reduce(function (accumulator, _ref2) {
|
|
733
755
|
var liquidityNet = _ref2.liquidityNet;
|
|
@@ -748,11 +770,12 @@ var TickList = /*#__PURE__*/function () {
|
|
|
748
770
|
!(tick.index === index) ? process.env.NODE_ENV !== "production" ? invariant(false, 'NOT_CONTAINED') : invariant(false) : void 0;
|
|
749
771
|
return tick;
|
|
750
772
|
}
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
*
|
|
754
|
-
* @param
|
|
755
|
-
* @
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* Finds the largest tick in the list of ticks that is less than or equal to tick
|
|
776
|
+
* @param ticks list of ticks
|
|
777
|
+
* @param tick tick to find the largest tick that is less than or equal to tick
|
|
778
|
+
* @private
|
|
756
779
|
*/;
|
|
757
780
|
TickList.binarySearch = function binarySearch(ticks, tick) {
|
|
758
781
|
!!this.isBelowSmallest(ticks, tick) ? process.env.NODE_ENV !== "production" ? invariant(false, 'BELOW_SMALLEST') : invariant(false) : void 0;
|
|
@@ -790,6 +813,7 @@ var TickList = /*#__PURE__*/function () {
|
|
|
790
813
|
};
|
|
791
814
|
TickList.nextInitializedTickWithinOneWord = function nextInitializedTickWithinOneWord(ticks, tick, lte, tickSpacing) {
|
|
792
815
|
var compressed = Math.floor(tick / tickSpacing); // matches rounding in the code
|
|
816
|
+
|
|
793
817
|
if (lte) {
|
|
794
818
|
var wordPos = compressed >> 8;
|
|
795
819
|
var minimum = (wordPos << 8) * tickSpacing;
|
|
@@ -813,10 +837,14 @@ var TickList = /*#__PURE__*/function () {
|
|
|
813
837
|
return TickList;
|
|
814
838
|
}();
|
|
815
839
|
|
|
816
|
-
/**
|
|
817
|
-
*
|
|
818
|
-
|
|
819
|
-
|
|
840
|
+
/**
|
|
841
|
+
* Generated method parameters for executing a call.
|
|
842
|
+
*/
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Converts a big int to a hex string
|
|
846
|
+
* @param bigintIsh
|
|
847
|
+
* @returns The hex encoded calldata
|
|
820
848
|
*/
|
|
821
849
|
function toHex(bigintIsh) {
|
|
822
850
|
var bigInt = JSBI.BigInt(bigintIsh);
|
|
@@ -827,10 +855,10 @@ function toHex(bigintIsh) {
|
|
|
827
855
|
return "0x" + hex;
|
|
828
856
|
}
|
|
829
857
|
|
|
830
|
-
/**
|
|
831
|
-
* Converts a route to a hex encoded path
|
|
832
|
-
* @param route the v3 path to convert to an encoded path
|
|
833
|
-
* @param exactOutput whether the route should be encoded in reverse, for making exact output swaps
|
|
858
|
+
/**
|
|
859
|
+
* Converts a route to a hex encoded path
|
|
860
|
+
* @param route the v3 path to convert to an encoded path
|
|
861
|
+
* @param exactOutput whether the route should be encoded in reverse, for making exact output swaps
|
|
834
862
|
*/
|
|
835
863
|
function encodeRouteToPath(route, exactOutput) {
|
|
836
864
|
var firstInputToken = route.input.wrapped;
|
|
@@ -862,12 +890,13 @@ function encodeRouteToPath(route, exactOutput) {
|
|
|
862
890
|
return exactOutput ? pack(types.reverse(), path.reverse()) : pack(types, path);
|
|
863
891
|
}
|
|
864
892
|
|
|
865
|
-
/**
|
|
866
|
-
* Returns the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0
|
|
867
|
-
* @param amount1 The numerator amount i.e., the amount of token1
|
|
868
|
-
* @param amount0 The denominator amount i.e., the amount of token0
|
|
869
|
-
* @returns The sqrt ratio
|
|
893
|
+
/**
|
|
894
|
+
* Returns the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0
|
|
895
|
+
* @param amount1 The numerator amount i.e., the amount of token1
|
|
896
|
+
* @param amount0 The denominator amount i.e., the amount of token0
|
|
897
|
+
* @returns The sqrt ratio
|
|
870
898
|
*/
|
|
899
|
+
|
|
871
900
|
function encodeSqrtRatioX96(amount1, amount0) {
|
|
872
901
|
var numerator = JSBI.leftShift(JSBI.BigInt(amount1), JSBI.BigInt(192));
|
|
873
902
|
var denominator = JSBI.BigInt(amount0);
|
|
@@ -875,16 +904,16 @@ function encodeSqrtRatioX96(amount1, amount0) {
|
|
|
875
904
|
return sqrt(ratioX192);
|
|
876
905
|
}
|
|
877
906
|
|
|
878
|
-
/**
|
|
879
|
-
* Returns an imprecise maximum amount of liquidity received for a given amount of token 0.
|
|
880
|
-
* This function is available to accommodate LiquidityAmounts#getLiquidityForAmount0 in the v3 periphery,
|
|
881
|
-
* which could be more precise by at least 32 bits by dividing by Q64 instead of Q96 in the intermediate step,
|
|
882
|
-
* and shifting the subtracted ratio left by 32 bits. This imprecise calculation will likely be replaced in a future
|
|
883
|
-
* v3 router contract.
|
|
884
|
-
* @param sqrtRatioAX96 The price at the lower boundary
|
|
885
|
-
* @param sqrtRatioBX96 The price at the upper boundary
|
|
886
|
-
* @param amount0 The token0 amount
|
|
887
|
-
* @returns liquidity for amount0, imprecise
|
|
907
|
+
/**
|
|
908
|
+
* Returns an imprecise maximum amount of liquidity received for a given amount of token 0.
|
|
909
|
+
* This function is available to accommodate LiquidityAmounts#getLiquidityForAmount0 in the v3 periphery,
|
|
910
|
+
* which could be more precise by at least 32 bits by dividing by Q64 instead of Q96 in the intermediate step,
|
|
911
|
+
* and shifting the subtracted ratio left by 32 bits. This imprecise calculation will likely be replaced in a future
|
|
912
|
+
* v3 router contract.
|
|
913
|
+
* @param sqrtRatioAX96 The price at the lower boundary
|
|
914
|
+
* @param sqrtRatioBX96 The price at the upper boundary
|
|
915
|
+
* @param amount0 The token0 amount
|
|
916
|
+
* @returns liquidity for amount0, imprecise
|
|
888
917
|
*/
|
|
889
918
|
function maxLiquidityForAmount0Imprecise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
890
919
|
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
@@ -895,13 +924,14 @@ function maxLiquidityForAmount0Imprecise(sqrtRatioAX96, sqrtRatioBX96, amount0)
|
|
|
895
924
|
var intermediate = JSBI.divide(JSBI.multiply(sqrtRatioAX96, sqrtRatioBX96), Q96);
|
|
896
925
|
return JSBI.divide(JSBI.multiply(JSBI.BigInt(amount0), intermediate), JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96));
|
|
897
926
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
*
|
|
901
|
-
*
|
|
902
|
-
* @param
|
|
903
|
-
* @param
|
|
904
|
-
* @
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* Returns a precise maximum amount of liquidity received for a given amount of token 0 by dividing by Q64 instead of Q96 in the intermediate step,
|
|
930
|
+
* and shifting the subtracted ratio left by 32 bits.
|
|
931
|
+
* @param sqrtRatioAX96 The price at the lower boundary
|
|
932
|
+
* @param sqrtRatioBX96 The price at the upper boundary
|
|
933
|
+
* @param amount0 The token0 amount
|
|
934
|
+
* @returns liquidity for amount0, precise
|
|
905
935
|
*/
|
|
906
936
|
function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
907
937
|
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
@@ -913,12 +943,13 @@ function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
|
913
943
|
var denominator = JSBI.multiply(Q96, JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96));
|
|
914
944
|
return JSBI.divide(numerator, denominator);
|
|
915
945
|
}
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
*
|
|
919
|
-
* @param
|
|
920
|
-
* @param
|
|
921
|
-
* @
|
|
946
|
+
|
|
947
|
+
/**
|
|
948
|
+
* Computes the maximum amount of liquidity received for a given amount of token1
|
|
949
|
+
* @param sqrtRatioAX96 The price at the lower tick boundary
|
|
950
|
+
* @param sqrtRatioBX96 The price at the upper tick boundary
|
|
951
|
+
* @param amount1 The token1 amount
|
|
952
|
+
* @returns liquidity for amount1
|
|
922
953
|
*/
|
|
923
954
|
function maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1) {
|
|
924
955
|
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
@@ -928,16 +959,17 @@ function maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1) {
|
|
|
928
959
|
}
|
|
929
960
|
return JSBI.divide(JSBI.multiply(JSBI.BigInt(amount1), Q96), JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96));
|
|
930
961
|
}
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
*
|
|
934
|
-
*
|
|
935
|
-
* @param
|
|
936
|
-
* @param
|
|
937
|
-
* @param
|
|
938
|
-
* @param
|
|
939
|
-
* @param
|
|
940
|
-
*
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* Computes the maximum amount of liquidity received for a given amount of token0, token1,
|
|
965
|
+
* and the prices at the tick boundaries.
|
|
966
|
+
* @param sqrtRatioCurrentX96 the current price
|
|
967
|
+
* @param sqrtRatioAX96 price at lower boundary
|
|
968
|
+
* @param sqrtRatioBX96 price at upper boundary
|
|
969
|
+
* @param amount0 token0 amount
|
|
970
|
+
* @param amount1 token1 amount
|
|
971
|
+
* @param useFullPrecision if false, liquidity will be maximized according to what the router can calculate,
|
|
972
|
+
* not what core can theoretically support
|
|
941
973
|
*/
|
|
942
974
|
function maxLiquidityForAmounts(sqrtRatioCurrentX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1, useFullPrecision) {
|
|
943
975
|
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
@@ -957,10 +989,10 @@ function maxLiquidityForAmounts(sqrtRatioCurrentX96, sqrtRatioAX96, sqrtRatioBX9
|
|
|
957
989
|
}
|
|
958
990
|
}
|
|
959
991
|
|
|
960
|
-
/**
|
|
961
|
-
* Returns the closest tick that is nearest a given tick and usable for the given tick spacing
|
|
962
|
-
* @param tick the target tick
|
|
963
|
-
* @param tickSpacing the spacing of the pool
|
|
992
|
+
/**
|
|
993
|
+
* Returns the closest tick that is nearest a given tick and usable for the given tick spacing
|
|
994
|
+
* @param tick the target tick
|
|
995
|
+
* @param tickSpacing the spacing of the pool
|
|
964
996
|
*/
|
|
965
997
|
function nearestUsableTick(tick, tickSpacing) {
|
|
966
998
|
!(Number.isInteger(tick) && Number.isInteger(tickSpacing)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INTEGERS') : invariant(false) : void 0;
|
|
@@ -972,10 +1004,11 @@ function nearestUsableTick(tick, tickSpacing) {
|
|
|
972
1004
|
|
|
973
1005
|
var Q128 = /*#__PURE__*/JSBI.exponentiate(/*#__PURE__*/JSBI.BigInt(2), /*#__PURE__*/JSBI.BigInt(128));
|
|
974
1006
|
var PositionLibrary = /*#__PURE__*/function () {
|
|
975
|
-
/**
|
|
976
|
-
* Cannot be constructed.
|
|
1007
|
+
/**
|
|
1008
|
+
* Cannot be constructed.
|
|
977
1009
|
*/
|
|
978
1010
|
function PositionLibrary() {}
|
|
1011
|
+
|
|
979
1012
|
// replicates the portions of Position#update required to compute unaccounted fees
|
|
980
1013
|
PositionLibrary.getTokensOwed = function getTokensOwed(feeGrowthInside0LastX128, feeGrowthInside1LastX128, liquidity, feeGrowthInside0X128, feeGrowthInside1X128) {
|
|
981
1014
|
var tokensOwed0 = JSBI.divide(JSBI.multiply(subIn256(feeGrowthInside0X128, feeGrowthInside0LastX128), liquidity), Q128);
|
|
@@ -985,22 +1018,23 @@ var PositionLibrary = /*#__PURE__*/function () {
|
|
|
985
1018
|
return PositionLibrary;
|
|
986
1019
|
}();
|
|
987
1020
|
|
|
988
|
-
/**
|
|
989
|
-
* Returns a price object corresponding to the input tick and the base/quote token
|
|
990
|
-
* Inputs must be tokens because the address order is used to interpret the price represented by the tick
|
|
991
|
-
* @param baseToken the base token of the price
|
|
992
|
-
* @param quoteToken the quote token of the price
|
|
993
|
-
* @param tick the tick for which to return the price
|
|
1021
|
+
/**
|
|
1022
|
+
* Returns a price object corresponding to the input tick and the base/quote token
|
|
1023
|
+
* Inputs must be tokens because the address order is used to interpret the price represented by the tick
|
|
1024
|
+
* @param baseToken the base token of the price
|
|
1025
|
+
* @param quoteToken the quote token of the price
|
|
1026
|
+
* @param tick the tick for which to return the price
|
|
994
1027
|
*/
|
|
995
1028
|
function tickToPrice(baseToken, quoteToken, tick) {
|
|
996
1029
|
var sqrtRatioX96 = TickMath.getSqrtRatioAtTick(tick);
|
|
997
1030
|
var ratioX192 = JSBI.multiply(sqrtRatioX96, sqrtRatioX96);
|
|
998
1031
|
return baseToken.sortsBefore(quoteToken) ? new Price(baseToken, quoteToken, Q192, ratioX192) : new Price(baseToken, quoteToken, ratioX192, Q192);
|
|
999
1032
|
}
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
*
|
|
1003
|
-
*
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Returns the first tick for which the given price is greater than or equal to the tick price
|
|
1036
|
+
* @param price for which to return the closest tick that represents a price less than or equal to the input price,
|
|
1037
|
+
* i.e. the price of the returned tick is less than or equal to the input price
|
|
1004
1038
|
*/
|
|
1005
1039
|
function priceToClosestTick(price) {
|
|
1006
1040
|
var sorted = price.baseCurrency.sortsBefore(price.quoteCurrency);
|
|
@@ -1029,8 +1063,8 @@ function subIn256(x, y) {
|
|
|
1029
1063
|
}
|
|
1030
1064
|
}
|
|
1031
1065
|
var TickLibrary = /*#__PURE__*/function () {
|
|
1032
|
-
/**
|
|
1033
|
-
* Cannot be constructed.
|
|
1066
|
+
/**
|
|
1067
|
+
* Cannot be constructed.
|
|
1034
1068
|
*/
|
|
1035
1069
|
function TickLibrary() {}
|
|
1036
1070
|
TickLibrary.getFeeGrowthInside = function getFeeGrowthInside(feeGrowthOutsideLower, feeGrowthOutsideUpper, tickLower, tickUpper, tickCurrent, feeGrowthGlobal0X128, feeGrowthGlobal1X128) {
|
|
@@ -1061,17 +1095,21 @@ var Tick = function Tick(_ref) {
|
|
|
1061
1095
|
var index = _ref.index,
|
|
1062
1096
|
liquidityGross = _ref.liquidityGross,
|
|
1063
1097
|
liquidityNet = _ref.liquidityNet;
|
|
1098
|
+
this.index = void 0;
|
|
1099
|
+
this.liquidityGross = void 0;
|
|
1100
|
+
this.liquidityNet = void 0;
|
|
1064
1101
|
!(index >= TickMath.MIN_TICK && index <= TickMath.MAX_TICK) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TICK') : invariant(false) : void 0;
|
|
1065
1102
|
this.index = index;
|
|
1066
1103
|
this.liquidityGross = JSBI.BigInt(liquidityGross);
|
|
1067
1104
|
this.liquidityNet = JSBI.BigInt(liquidityNet);
|
|
1068
1105
|
};
|
|
1069
1106
|
|
|
1070
|
-
/**
|
|
1071
|
-
* A data provider for ticks that is backed by an in-memory array of ticks.
|
|
1107
|
+
/**
|
|
1108
|
+
* A data provider for ticks that is backed by an in-memory array of ticks.
|
|
1072
1109
|
*/
|
|
1073
1110
|
var TickListDataProvider = /*#__PURE__*/function () {
|
|
1074
1111
|
function TickListDataProvider(ticks, tickSpacing) {
|
|
1112
|
+
this.ticks = void 0;
|
|
1075
1113
|
var ticksMapped = ticks.map(function (t) {
|
|
1076
1114
|
return t instanceof Tick ? t : new Tick(t);
|
|
1077
1115
|
});
|
|
@@ -1110,32 +1148,43 @@ var TickListDataProvider = /*#__PURE__*/function () {
|
|
|
1110
1148
|
return TickListDataProvider;
|
|
1111
1149
|
}();
|
|
1112
1150
|
|
|
1113
|
-
/**
|
|
1114
|
-
* By default, pools will not allow operations that require ticks.
|
|
1151
|
+
/**
|
|
1152
|
+
* By default, pools will not allow operations that require ticks.
|
|
1115
1153
|
*/
|
|
1116
1154
|
var NO_TICK_DATA_PROVIDER_DEFAULT = /*#__PURE__*/new NoTickDataProvider();
|
|
1117
|
-
|
|
1118
|
-
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* Represents a V3 pool
|
|
1119
1158
|
*/
|
|
1120
1159
|
var Pool = /*#__PURE__*/function () {
|
|
1121
|
-
/**
|
|
1122
|
-
* Construct a pool
|
|
1123
|
-
* @param tokenA One of the tokens in the pool
|
|
1124
|
-
* @param tokenB The other token in the pool
|
|
1125
|
-
* @param fee The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
|
|
1126
|
-
* @param sqrtRatioX96 The sqrt of the current ratio of amounts of token1 to token0
|
|
1127
|
-
* @param liquidity The current value of in range liquidity
|
|
1128
|
-
* @param tickCurrent The current tick of the pool
|
|
1129
|
-
* @param ticks The current state of the pool ticks or a data provider that can return tick data
|
|
1160
|
+
/**
|
|
1161
|
+
* Construct a pool
|
|
1162
|
+
* @param tokenA One of the tokens in the pool
|
|
1163
|
+
* @param tokenB The other token in the pool
|
|
1164
|
+
* @param fee The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
|
|
1165
|
+
* @param sqrtRatioX96 The sqrt of the current ratio of amounts of token1 to token0
|
|
1166
|
+
* @param liquidity The current value of in range liquidity
|
|
1167
|
+
* @param tickCurrent The current tick of the pool
|
|
1168
|
+
* @param ticks The current state of the pool ticks or a data provider that can return tick data
|
|
1130
1169
|
*/
|
|
1131
1170
|
function Pool(tokenA, tokenB, fee, sqrtRatioX96, liquidity, tickCurrent, ticks) {
|
|
1132
1171
|
if (ticks === void 0) {
|
|
1133
1172
|
ticks = NO_TICK_DATA_PROVIDER_DEFAULT;
|
|
1134
1173
|
}
|
|
1174
|
+
this.token0 = void 0;
|
|
1175
|
+
this.token1 = void 0;
|
|
1176
|
+
this.fee = void 0;
|
|
1177
|
+
this.sqrtRatioX96 = void 0;
|
|
1178
|
+
this.liquidity = void 0;
|
|
1179
|
+
this.tickCurrent = void 0;
|
|
1180
|
+
this.tickDataProvider = void 0;
|
|
1181
|
+
this._token0Price = void 0;
|
|
1182
|
+
this._token1Price = void 0;
|
|
1135
1183
|
!(Number.isInteger(fee) && fee < 1000000) ? process.env.NODE_ENV !== "production" ? invariant(false, 'FEE') : invariant(false) : void 0;
|
|
1136
1184
|
var tickCurrentSqrtRatioX96 = TickMath.getSqrtRatioAtTick(tickCurrent);
|
|
1137
1185
|
var nextTickSqrtRatioX96 = TickMath.getSqrtRatioAtTick(tickCurrent + 1);
|
|
1138
|
-
!(JSBI.greaterThanOrEqual(JSBI.BigInt(sqrtRatioX96), tickCurrentSqrtRatioX96) && JSBI.lessThanOrEqual(JSBI.BigInt(sqrtRatioX96), nextTickSqrtRatioX96)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'PRICE_BOUNDS') : invariant(false) : void 0
|
|
1186
|
+
!(JSBI.greaterThanOrEqual(JSBI.BigInt(sqrtRatioX96), tickCurrentSqrtRatioX96) && JSBI.lessThanOrEqual(JSBI.BigInt(sqrtRatioX96), nextTickSqrtRatioX96)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'PRICE_BOUNDS') : invariant(false) : void 0 // always create a copy of the list since we want the pool's tick list to be immutable
|
|
1187
|
+
;
|
|
1139
1188
|
var _ref = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
1140
1189
|
this.token0 = _ref[0];
|
|
1141
1190
|
this.token1 = _ref[1];
|
|
@@ -1145,6 +1194,12 @@ var Pool = /*#__PURE__*/function () {
|
|
|
1145
1194
|
this.tickCurrent = tickCurrent;
|
|
1146
1195
|
this.tickDataProvider = Array.isArray(ticks) ? new TickListDataProvider(ticks, TICK_SPACINGS[fee]) : ticks;
|
|
1147
1196
|
}
|
|
1197
|
+
|
|
1198
|
+
/**
|
|
1199
|
+
* Returns true if the token is either token0 or token1
|
|
1200
|
+
* @param token The token to check
|
|
1201
|
+
* @returns True if token is either token0 or token
|
|
1202
|
+
*/
|
|
1148
1203
|
Pool.getAddress = function getAddress(tokenA, tokenB, fee, initCodeHashManualOverride, factoryAddressOverride) {
|
|
1149
1204
|
return computePoolAddress({
|
|
1150
1205
|
factoryAddress: factoryAddressOverride != null ? factoryAddressOverride : FACTORY_ADDRESS,
|
|
@@ -1153,36 +1208,33 @@ var Pool = /*#__PURE__*/function () {
|
|
|
1153
1208
|
tokenB: tokenB,
|
|
1154
1209
|
initCodeHashManualOverride: initCodeHashManualOverride
|
|
1155
1210
|
});
|
|
1156
|
-
}
|
|
1157
|
-
/**
|
|
1158
|
-
* Returns true if the token is either token0 or token1
|
|
1159
|
-
* @param token The token to check
|
|
1160
|
-
* @returns True if token is either token0 or token
|
|
1161
|
-
*/;
|
|
1211
|
+
};
|
|
1162
1212
|
var _proto = Pool.prototype;
|
|
1163
1213
|
_proto.involvesToken = function involvesToken(token) {
|
|
1164
1214
|
return token.equals(this.token0) || token.equals(this.token1);
|
|
1165
1215
|
}
|
|
1166
|
-
|
|
1167
|
-
|
|
1216
|
+
|
|
1217
|
+
/**
|
|
1218
|
+
* Returns the current mid price of the pool in terms of token0, i.e. the ratio of token1 over token0
|
|
1168
1219
|
*/;
|
|
1169
|
-
/**
|
|
1170
|
-
* Return the price of the given token in terms of the other token in the pool.
|
|
1171
|
-
* @param token The token to return price of
|
|
1172
|
-
* @returns The price of the given token, in terms of the other.
|
|
1220
|
+
/**
|
|
1221
|
+
* Return the price of the given token in terms of the other token in the pool.
|
|
1222
|
+
* @param token The token to return price of
|
|
1223
|
+
* @returns The price of the given token, in terms of the other.
|
|
1173
1224
|
*/
|
|
1174
1225
|
_proto.priceOf = function priceOf(token) {
|
|
1175
1226
|
!this.involvesToken(token) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TOKEN') : invariant(false) : void 0;
|
|
1176
1227
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
1177
1228
|
}
|
|
1178
|
-
|
|
1179
|
-
|
|
1229
|
+
|
|
1230
|
+
/**
|
|
1231
|
+
* Returns the chain ID of the tokens in the pool.
|
|
1180
1232
|
*/;
|
|
1181
|
-
/**
|
|
1182
|
-
* Given an input amount of a token, return the computed output amount, and a pool with state updated after the trade
|
|
1183
|
-
* @param inputAmount The input amount for which to quote the output amount
|
|
1184
|
-
* @param sqrtPriceLimitX96 The Q64.96 sqrt price limit
|
|
1185
|
-
* @returns The output amount and the pool with updated state
|
|
1233
|
+
/**
|
|
1234
|
+
* Given an input amount of a token, return the computed output amount, and a pool with state updated after the trade
|
|
1235
|
+
* @param inputAmount The input amount for which to quote the output amount
|
|
1236
|
+
* @param sqrtPriceLimitX96 The Q64.96 sqrt price limit
|
|
1237
|
+
* @returns The output amount and the pool with updated state
|
|
1186
1238
|
*/
|
|
1187
1239
|
_proto.getOutputAmount =
|
|
1188
1240
|
/*#__PURE__*/
|
|
@@ -1212,11 +1264,11 @@ var Pool = /*#__PURE__*/function () {
|
|
|
1212
1264
|
}
|
|
1213
1265
|
return getOutputAmount;
|
|
1214
1266
|
}()
|
|
1215
|
-
/**
|
|
1216
|
-
* Given a desired output amount of a token, return the computed input amount and a pool with state updated after the trade
|
|
1217
|
-
* @param outputAmount the output amount for which to quote the input amount
|
|
1218
|
-
* @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
|
|
1219
|
-
* @returns The input amount and the pool with updated state
|
|
1267
|
+
/**
|
|
1268
|
+
* Given a desired output amount of a token, return the computed input amount and a pool with state updated after the trade
|
|
1269
|
+
* @param outputAmount the output amount for which to quote the input amount
|
|
1270
|
+
* @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
|
|
1271
|
+
* @returns The input amount and the pool with updated state
|
|
1220
1272
|
*/
|
|
1221
1273
|
;
|
|
1222
1274
|
_proto.getInputAmount =
|
|
@@ -1247,15 +1299,15 @@ var Pool = /*#__PURE__*/function () {
|
|
|
1247
1299
|
}
|
|
1248
1300
|
return getInputAmount;
|
|
1249
1301
|
}()
|
|
1250
|
-
/**
|
|
1251
|
-
* Executes a swap
|
|
1252
|
-
* @param zeroForOne Whether the amount in is token0 or token1
|
|
1253
|
-
* @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
|
|
1254
|
-
* @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
|
|
1255
|
-
* @returns amountCalculated
|
|
1256
|
-
* @returns sqrtRatioX96
|
|
1257
|
-
* @returns liquidity
|
|
1258
|
-
* @returns tickCurrent
|
|
1302
|
+
/**
|
|
1303
|
+
* Executes a swap
|
|
1304
|
+
* @param zeroForOne Whether the amount in is token0 or token1
|
|
1305
|
+
* @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
|
|
1306
|
+
* @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
|
|
1307
|
+
* @returns amountCalculated
|
|
1308
|
+
* @returns sqrtRatioX96
|
|
1309
|
+
* @returns liquidity
|
|
1310
|
+
* @returns tickCurrent
|
|
1259
1311
|
*/
|
|
1260
1312
|
;
|
|
1261
1313
|
_proto.swap =
|
|
@@ -1280,8 +1332,9 @@ var Pool = /*#__PURE__*/function () {
|
|
|
1280
1332
|
var _this$_token0Price;
|
|
1281
1333
|
return (_this$_token0Price = this._token0Price) != null ? _this$_token0Price : this._token0Price = new Price(this.token0, this.token1, Q192, JSBI.multiply(this.sqrtRatioX96, this.sqrtRatioX96));
|
|
1282
1334
|
}
|
|
1283
|
-
|
|
1284
|
-
|
|
1335
|
+
|
|
1336
|
+
/**
|
|
1337
|
+
* Returns the current mid price of the pool in terms of token1, i.e. the ratio of token0 over token1
|
|
1285
1338
|
*/
|
|
1286
1339
|
}, {
|
|
1287
1340
|
key: "token1Price",
|
|
@@ -1302,22 +1355,26 @@ var Pool = /*#__PURE__*/function () {
|
|
|
1302
1355
|
}]);
|
|
1303
1356
|
}();
|
|
1304
1357
|
|
|
1305
|
-
/**
|
|
1306
|
-
* Represents a position on a Uniswap V3 Pool
|
|
1358
|
+
/**
|
|
1359
|
+
* Represents a position on a Uniswap V3 Pool
|
|
1307
1360
|
*/
|
|
1308
1361
|
var Position = /*#__PURE__*/function () {
|
|
1309
|
-
/**
|
|
1310
|
-
* Constructs a position for a given pool with the given liquidity
|
|
1311
|
-
* @param pool For which pool the liquidity is assigned
|
|
1312
|
-
* @param liquidity The amount of liquidity that is in the position
|
|
1313
|
-
* @param tickLower The lower tick of the position
|
|
1314
|
-
* @param tickUpper The upper tick of the position
|
|
1362
|
+
/**
|
|
1363
|
+
* Constructs a position for a given pool with the given liquidity
|
|
1364
|
+
* @param pool For which pool the liquidity is assigned
|
|
1365
|
+
* @param liquidity The amount of liquidity that is in the position
|
|
1366
|
+
* @param tickLower The lower tick of the position
|
|
1367
|
+
* @param tickUpper The upper tick of the position
|
|
1315
1368
|
*/
|
|
1316
1369
|
function Position(_ref) {
|
|
1317
1370
|
var pool = _ref.pool,
|
|
1318
1371
|
liquidity = _ref.liquidity,
|
|
1319
1372
|
tickLower = _ref.tickLower,
|
|
1320
1373
|
tickUpper = _ref.tickUpper;
|
|
1374
|
+
this.pool = void 0;
|
|
1375
|
+
this.tickLower = void 0;
|
|
1376
|
+
this.tickUpper = void 0;
|
|
1377
|
+
this.liquidity = void 0;
|
|
1321
1378
|
// cached resuts for the getters
|
|
1322
1379
|
this._token0Amount = null;
|
|
1323
1380
|
this._token1Amount = null;
|
|
@@ -1330,14 +1387,15 @@ var Position = /*#__PURE__*/function () {
|
|
|
1330
1387
|
this.tickUpper = tickUpper;
|
|
1331
1388
|
this.liquidity = JSBI.BigInt(liquidity);
|
|
1332
1389
|
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1390
|
+
|
|
1391
|
+
/**
|
|
1392
|
+
* Returns the price of token0 at the lower tick
|
|
1335
1393
|
*/
|
|
1336
1394
|
var _proto = Position.prototype;
|
|
1337
|
-
/**
|
|
1338
|
-
* Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
|
|
1339
|
-
* @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
|
|
1340
|
-
* @returns The sqrt ratios after slippage
|
|
1395
|
+
/**
|
|
1396
|
+
* Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
|
|
1397
|
+
* @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
|
|
1398
|
+
* @returns The sqrt ratios after slippage
|
|
1341
1399
|
*/
|
|
1342
1400
|
_proto.ratiosAfterSlippage = function ratiosAfterSlippage(slippageTolerance) {
|
|
1343
1401
|
var priceLower = this.pool.token0Price.asFraction.multiply(new Percent(1).subtract(slippageTolerance));
|
|
@@ -1355,28 +1413,33 @@ var Position = /*#__PURE__*/function () {
|
|
|
1355
1413
|
sqrtRatioX96Upper: sqrtRatioX96Upper
|
|
1356
1414
|
};
|
|
1357
1415
|
}
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
*
|
|
1361
|
-
*
|
|
1362
|
-
* @
|
|
1416
|
+
|
|
1417
|
+
/**
|
|
1418
|
+
* Returns the minimum amounts that must be sent in order to safely mint the amount of liquidity held by the position
|
|
1419
|
+
* with the given slippage tolerance
|
|
1420
|
+
* @param slippageTolerance Tolerance of unfavorable slippage from the current price
|
|
1421
|
+
* @returns The amounts, with slippage
|
|
1363
1422
|
*/;
|
|
1364
1423
|
_proto.mintAmountsWithSlippage = function mintAmountsWithSlippage(slippageTolerance) {
|
|
1365
1424
|
// get lower/upper prices
|
|
1366
1425
|
var _this$ratiosAfterSlip = this.ratiosAfterSlippage(slippageTolerance),
|
|
1367
1426
|
sqrtRatioX96Upper = _this$ratiosAfterSlip.sqrtRatioX96Upper,
|
|
1368
1427
|
sqrtRatioX96Lower = _this$ratiosAfterSlip.sqrtRatioX96Lower;
|
|
1428
|
+
|
|
1369
1429
|
// construct counterfactual pools
|
|
1370
1430
|
var poolLower = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Lower, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Lower));
|
|
1371
1431
|
var poolUpper = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Upper, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Upper));
|
|
1432
|
+
|
|
1372
1433
|
// because the router is imprecise, we need to calculate the position that will be created (assuming no slippage)
|
|
1373
1434
|
var positionThatWillBeCreated = Position.fromAmounts(_extends({
|
|
1374
1435
|
pool: this.pool,
|
|
1375
1436
|
tickLower: this.tickLower,
|
|
1376
1437
|
tickUpper: this.tickUpper
|
|
1377
1438
|
}, this.mintAmounts, {
|
|
1439
|
+
// the mint amounts are what will be passed as calldata
|
|
1378
1440
|
useFullPrecision: false
|
|
1379
1441
|
}));
|
|
1442
|
+
|
|
1380
1443
|
// we want the smaller amounts...
|
|
1381
1444
|
// ...which occurs at the upper price for amount0...
|
|
1382
1445
|
var amount0 = new Position({
|
|
@@ -1397,20 +1460,23 @@ var Position = /*#__PURE__*/function () {
|
|
|
1397
1460
|
amount1: amount1
|
|
1398
1461
|
};
|
|
1399
1462
|
}
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
*
|
|
1403
|
-
*
|
|
1404
|
-
* @
|
|
1463
|
+
|
|
1464
|
+
/**
|
|
1465
|
+
* Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
|
|
1466
|
+
* position with the given slippage tolerance
|
|
1467
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the current price
|
|
1468
|
+
* @returns The amounts, with slippage
|
|
1405
1469
|
*/;
|
|
1406
1470
|
_proto.burnAmountsWithSlippage = function burnAmountsWithSlippage(slippageTolerance) {
|
|
1407
1471
|
// get lower/upper prices
|
|
1408
1472
|
var _this$ratiosAfterSlip2 = this.ratiosAfterSlippage(slippageTolerance),
|
|
1409
1473
|
sqrtRatioX96Upper = _this$ratiosAfterSlip2.sqrtRatioX96Upper,
|
|
1410
1474
|
sqrtRatioX96Lower = _this$ratiosAfterSlip2.sqrtRatioX96Lower;
|
|
1475
|
+
|
|
1411
1476
|
// construct counterfactual pools
|
|
1412
1477
|
var poolLower = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Lower, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Lower));
|
|
1413
1478
|
var poolUpper = new Pool(this.pool.token0, this.pool.token1, this.pool.fee, sqrtRatioX96Upper, 0 /* liquidity doesn't matter */, TickMath.getTickAtSqrtRatio(sqrtRatioX96Upper));
|
|
1479
|
+
|
|
1414
1480
|
// we want the smaller amounts...
|
|
1415
1481
|
// ...which occurs at the upper price for amount0...
|
|
1416
1482
|
var amount0 = new Position({
|
|
@@ -1431,21 +1497,22 @@ var Position = /*#__PURE__*/function () {
|
|
|
1431
1497
|
amount1: amount1.quotient
|
|
1432
1498
|
};
|
|
1433
1499
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
* the
|
|
1500
|
+
|
|
1501
|
+
/**
|
|
1502
|
+
* Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
|
|
1503
|
+
* the current price for the pool
|
|
1437
1504
|
*/;
|
|
1438
|
-
/**
|
|
1439
|
-
* Computes the maximum amount of liquidity received for a given amount of token0, token1,
|
|
1440
|
-
* and the prices at the tick boundaries.
|
|
1441
|
-
* @param pool The pool for which the position should be created
|
|
1442
|
-
* @param tickLower The lower tick of the position
|
|
1443
|
-
* @param tickUpper The upper tick of the position
|
|
1444
|
-
* @param amount0 token0 amount
|
|
1445
|
-
* @param amount1 token1 amount
|
|
1446
|
-
* @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
|
|
1447
|
-
* not what core can theoretically support
|
|
1448
|
-
* @returns The amount of liquidity for the position
|
|
1505
|
+
/**
|
|
1506
|
+
* Computes the maximum amount of liquidity received for a given amount of token0, token1,
|
|
1507
|
+
* and the prices at the tick boundaries.
|
|
1508
|
+
* @param pool The pool for which the position should be created
|
|
1509
|
+
* @param tickLower The lower tick of the position
|
|
1510
|
+
* @param tickUpper The upper tick of the position
|
|
1511
|
+
* @param amount0 token0 amount
|
|
1512
|
+
* @param amount1 token1 amount
|
|
1513
|
+
* @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
|
|
1514
|
+
* not what core can theoretically support
|
|
1515
|
+
* @returns The amount of liquidity for the position
|
|
1449
1516
|
*/
|
|
1450
1517
|
Position.fromAmounts = function fromAmounts(_ref2) {
|
|
1451
1518
|
var pool = _ref2.pool,
|
|
@@ -1463,15 +1530,16 @@ var Position = /*#__PURE__*/function () {
|
|
|
1463
1530
|
liquidity: maxLiquidityForAmounts(pool.sqrtRatioX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1, useFullPrecision)
|
|
1464
1531
|
});
|
|
1465
1532
|
}
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
*
|
|
1469
|
-
* @param
|
|
1470
|
-
* @param
|
|
1471
|
-
* @param
|
|
1472
|
-
* @param
|
|
1473
|
-
*
|
|
1474
|
-
*
|
|
1533
|
+
|
|
1534
|
+
/**
|
|
1535
|
+
* Computes a position with the maximum amount of liquidity received for a given amount of token0, assuming an unlimited amount of token1
|
|
1536
|
+
* @param pool The pool for which the position is created
|
|
1537
|
+
* @param tickLower The lower tick
|
|
1538
|
+
* @param tickUpper The upper tick
|
|
1539
|
+
* @param amount0 The desired amount of token0
|
|
1540
|
+
* @param useFullPrecision If true, liquidity will be maximized according to what the router can calculate,
|
|
1541
|
+
* not what core can theoretically support
|
|
1542
|
+
* @returns The position
|
|
1475
1543
|
*/;
|
|
1476
1544
|
Position.fromAmount0 = function fromAmount0(_ref3) {
|
|
1477
1545
|
var pool = _ref3.pool,
|
|
@@ -1488,13 +1556,14 @@ var Position = /*#__PURE__*/function () {
|
|
|
1488
1556
|
useFullPrecision: useFullPrecision
|
|
1489
1557
|
});
|
|
1490
1558
|
}
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
*
|
|
1494
|
-
* @param
|
|
1495
|
-
* @param
|
|
1496
|
-
* @param
|
|
1497
|
-
* @
|
|
1559
|
+
|
|
1560
|
+
/**
|
|
1561
|
+
* Computes a position with the maximum amount of liquidity received for a given amount of token1, assuming an unlimited amount of token0
|
|
1562
|
+
* @param pool The pool for which the position is created
|
|
1563
|
+
* @param tickLower The lower tick
|
|
1564
|
+
* @param tickUpper The upper tick
|
|
1565
|
+
* @param amount1 The desired amount of token1
|
|
1566
|
+
* @returns The position
|
|
1498
1567
|
*/;
|
|
1499
1568
|
Position.fromAmount1 = function fromAmount1(_ref4) {
|
|
1500
1569
|
var pool = _ref4.pool,
|
|
@@ -1516,16 +1585,18 @@ var Position = /*#__PURE__*/function () {
|
|
|
1516
1585
|
get: function get() {
|
|
1517
1586
|
return tickToPrice(this.pool.token0, this.pool.token1, this.tickLower);
|
|
1518
1587
|
}
|
|
1519
|
-
|
|
1520
|
-
|
|
1588
|
+
|
|
1589
|
+
/**
|
|
1590
|
+
* Returns the price of token0 at the upper tick
|
|
1521
1591
|
*/
|
|
1522
1592
|
}, {
|
|
1523
1593
|
key: "token0PriceUpper",
|
|
1524
1594
|
get: function get() {
|
|
1525
1595
|
return tickToPrice(this.pool.token0, this.pool.token1, this.tickUpper);
|
|
1526
1596
|
}
|
|
1527
|
-
|
|
1528
|
-
|
|
1597
|
+
|
|
1598
|
+
/**
|
|
1599
|
+
* Returns the amount of token0 that this position's liquidity could be burned for at the current pool price
|
|
1529
1600
|
*/
|
|
1530
1601
|
}, {
|
|
1531
1602
|
key: "amount0",
|
|
@@ -1541,8 +1612,9 @@ var Position = /*#__PURE__*/function () {
|
|
|
1541
1612
|
}
|
|
1542
1613
|
return this._token0Amount;
|
|
1543
1614
|
}
|
|
1544
|
-
|
|
1545
|
-
|
|
1615
|
+
|
|
1616
|
+
/**
|
|
1617
|
+
* Returns the amount of token1 that this position's liquidity could be burned for at the current pool price
|
|
1546
1618
|
*/
|
|
1547
1619
|
}, {
|
|
1548
1620
|
key: "amount1",
|
|
@@ -1584,19 +1656,23 @@ var Position = /*#__PURE__*/function () {
|
|
|
1584
1656
|
}]);
|
|
1585
1657
|
}();
|
|
1586
1658
|
|
|
1587
|
-
/**
|
|
1588
|
-
* Represents a list of pools through which a swap can occur
|
|
1589
|
-
* @template TInput The input token
|
|
1590
|
-
* @template TOutput The output token
|
|
1659
|
+
/**
|
|
1660
|
+
* Represents a list of pools through which a swap can occur
|
|
1661
|
+
* @template TInput The input token
|
|
1662
|
+
* @template TOutput The output token
|
|
1591
1663
|
*/
|
|
1592
1664
|
var Route = /*#__PURE__*/function () {
|
|
1593
|
-
/**
|
|
1594
|
-
* Creates an instance of route.
|
|
1595
|
-
* @param pools An array of `Pool` objects, ordered by the route the swap will take
|
|
1596
|
-
* @param input The input token
|
|
1597
|
-
* @param output The output token
|
|
1665
|
+
/**
|
|
1666
|
+
* Creates an instance of route.
|
|
1667
|
+
* @param pools An array of `Pool` objects, ordered by the route the swap will take
|
|
1668
|
+
* @param input The input token
|
|
1669
|
+
* @param output The output token
|
|
1598
1670
|
*/
|
|
1599
1671
|
function Route(pools, input, output) {
|
|
1672
|
+
this.pools = void 0;
|
|
1673
|
+
this.tokenPath = void 0;
|
|
1674
|
+
this.input = void 0;
|
|
1675
|
+
this.output = void 0;
|
|
1600
1676
|
this._midPrice = null;
|
|
1601
1677
|
!(pools.length > 0) ? process.env.NODE_ENV !== "production" ? invariant(false, 'POOLS') : invariant(false) : void 0;
|
|
1602
1678
|
var chainId = pools[0].chainId;
|
|
@@ -1607,8 +1683,9 @@ var Route = /*#__PURE__*/function () {
|
|
|
1607
1683
|
var wrappedInput = input.wrapped;
|
|
1608
1684
|
!pools[0].involvesToken(wrappedInput) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT') : invariant(false) : void 0;
|
|
1609
1685
|
!pools[pools.length - 1].involvesToken(output.wrapped) ? process.env.NODE_ENV !== "production" ? invariant(false, 'OUTPUT') : invariant(false) : void 0;
|
|
1610
|
-
|
|
1611
|
-
|
|
1686
|
+
|
|
1687
|
+
/**
|
|
1688
|
+
* Normalizes token0-token1 order and selects the next token/fee step to add to the path
|
|
1612
1689
|
* */
|
|
1613
1690
|
var tokenPath = [wrappedInput];
|
|
1614
1691
|
for (var _iterator = _createForOfIteratorHelperLoose(pools.entries()), _step; !(_step = _iterator()).done;) {
|
|
@@ -1630,8 +1707,9 @@ var Route = /*#__PURE__*/function () {
|
|
|
1630
1707
|
get: function get() {
|
|
1631
1708
|
return this.pools[0].chainId;
|
|
1632
1709
|
}
|
|
1633
|
-
|
|
1634
|
-
|
|
1710
|
+
|
|
1711
|
+
/**
|
|
1712
|
+
* Returns the mid price of the route
|
|
1635
1713
|
*/
|
|
1636
1714
|
}, {
|
|
1637
1715
|
key: "midPrice",
|
|
@@ -1659,14 +1737,14 @@ var Route = /*#__PURE__*/function () {
|
|
|
1659
1737
|
}]);
|
|
1660
1738
|
}();
|
|
1661
1739
|
|
|
1662
|
-
/**
|
|
1663
|
-
* Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
|
|
1664
|
-
* @template TInput The input token, either Ether or an ERC-20
|
|
1665
|
-
* @template TOutput The output token, either Ether or an ERC-20
|
|
1666
|
-
* @template TTradeType The trade type, either exact input or exact output
|
|
1667
|
-
* @param a The first trade to compare
|
|
1668
|
-
* @param b The second trade to compare
|
|
1669
|
-
* @returns A sorted ordering for two neighboring elements in a trade array
|
|
1740
|
+
/**
|
|
1741
|
+
* Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
|
|
1742
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
1743
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
1744
|
+
* @template TTradeType The trade type, either exact input or exact output
|
|
1745
|
+
* @param a The first trade to compare
|
|
1746
|
+
* @param b The second trade to compare
|
|
1747
|
+
* @returns A sorted ordering for two neighboring elements in a trade array
|
|
1670
1748
|
*/
|
|
1671
1749
|
function tradeComparator(a, b) {
|
|
1672
1750
|
// must have same input and output token for comparison
|
|
@@ -1698,27 +1776,56 @@ function tradeComparator(a, b) {
|
|
|
1698
1776
|
}
|
|
1699
1777
|
}
|
|
1700
1778
|
}
|
|
1701
|
-
/**
|
|
1702
|
-
* Represents a trade executed against a set of routes where some percentage of the input is
|
|
1703
|
-
* split across each route.
|
|
1704
|
-
*
|
|
1705
|
-
* Each route has its own set of pools. Pools can not be re-used across routes.
|
|
1706
|
-
*
|
|
1707
|
-
* Does not account for slippage, i.e., changes in price environment that can occur between
|
|
1708
|
-
* the time the trade is submitted and when it is executed.
|
|
1709
|
-
* @template TInput The input token, either Ether or an ERC-20
|
|
1710
|
-
* @template TOutput The output token, either Ether or an ERC-20
|
|
1711
|
-
* @template TTradeType The trade type, either exact input or exact output
|
|
1779
|
+
/**
|
|
1780
|
+
* Represents a trade executed against a set of routes where some percentage of the input is
|
|
1781
|
+
* split across each route.
|
|
1782
|
+
*
|
|
1783
|
+
* Each route has its own set of pools. Pools can not be re-used across routes.
|
|
1784
|
+
*
|
|
1785
|
+
* Does not account for slippage, i.e., changes in price environment that can occur between
|
|
1786
|
+
* the time the trade is submitted and when it is executed.
|
|
1787
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
1788
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
1789
|
+
* @template TTradeType The trade type, either exact input or exact output
|
|
1712
1790
|
*/
|
|
1713
1791
|
var Trade = /*#__PURE__*/function () {
|
|
1714
|
-
/**
|
|
1715
|
-
* Construct a trade by passing in the pre-computed property values
|
|
1716
|
-
* @param routes The routes through which the trade occurs
|
|
1717
|
-
* @param tradeType The type of trade, exact input or exact output
|
|
1792
|
+
/**
|
|
1793
|
+
* Construct a trade by passing in the pre-computed property values
|
|
1794
|
+
* @param routes The routes through which the trade occurs
|
|
1795
|
+
* @param tradeType The type of trade, exact input or exact output
|
|
1718
1796
|
*/
|
|
1719
1797
|
function Trade(_ref) {
|
|
1720
1798
|
var routes = _ref.routes,
|
|
1721
1799
|
tradeType = _ref.tradeType;
|
|
1800
|
+
/**
|
|
1801
|
+
* The swaps of the trade, i.e. which routes and how much is swapped in each that
|
|
1802
|
+
* make up the trade.
|
|
1803
|
+
*/
|
|
1804
|
+
this.swaps = void 0;
|
|
1805
|
+
/**
|
|
1806
|
+
* The type of the trade, either exact in or exact out.
|
|
1807
|
+
*/
|
|
1808
|
+
this.tradeType = void 0;
|
|
1809
|
+
/**
|
|
1810
|
+
* The cached result of the input amount computation
|
|
1811
|
+
* @private
|
|
1812
|
+
*/
|
|
1813
|
+
this._inputAmount = void 0;
|
|
1814
|
+
/**
|
|
1815
|
+
* The cached result of the output amount computation
|
|
1816
|
+
* @private
|
|
1817
|
+
*/
|
|
1818
|
+
this._outputAmount = void 0;
|
|
1819
|
+
/**
|
|
1820
|
+
* The cached result of the computed execution price
|
|
1821
|
+
* @private
|
|
1822
|
+
*/
|
|
1823
|
+
this._executionPrice = void 0;
|
|
1824
|
+
/**
|
|
1825
|
+
* The cached result of the price impact computation
|
|
1826
|
+
* @private
|
|
1827
|
+
*/
|
|
1828
|
+
this._priceImpact = void 0;
|
|
1722
1829
|
var inputCurrency = routes[0].inputAmount.currency;
|
|
1723
1830
|
var outputCurrency = routes[0].outputAmount.currency;
|
|
1724
1831
|
!routes.every(function (_ref2) {
|
|
@@ -1747,20 +1854,19 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1747
1854
|
this.swaps = routes;
|
|
1748
1855
|
this.tradeType = tradeType;
|
|
1749
1856
|
}
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
* this
|
|
1753
|
-
*
|
|
1754
|
-
*
|
|
1755
|
-
* i.e. which pools the trade goes through.
|
|
1857
|
+
|
|
1858
|
+
/**
|
|
1859
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
1860
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
1861
|
+
* @returns The amount out
|
|
1756
1862
|
*/
|
|
1757
|
-
/**
|
|
1758
|
-
* Constructs an exact in trade with the given amount in and route
|
|
1759
|
-
* @template TInput The input token, either Ether or an ERC-20
|
|
1760
|
-
* @template TOutput The output token, either Ether or an ERC-20
|
|
1761
|
-
* @param route The route of the exact in trade
|
|
1762
|
-
* @param amountIn The amount being passed in
|
|
1763
|
-
* @returns The exact in trade
|
|
1863
|
+
/**
|
|
1864
|
+
* Constructs an exact in trade with the given amount in and route
|
|
1865
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
1866
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
1867
|
+
* @param route The route of the exact in trade
|
|
1868
|
+
* @param amountIn The amount being passed in
|
|
1869
|
+
* @returns The exact in trade
|
|
1764
1870
|
*/
|
|
1765
1871
|
Trade.exactIn =
|
|
1766
1872
|
/*#__PURE__*/
|
|
@@ -1778,13 +1884,13 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1778
1884
|
}
|
|
1779
1885
|
return exactIn;
|
|
1780
1886
|
}()
|
|
1781
|
-
/**
|
|
1782
|
-
* Constructs an exact out trade with the given amount out and route
|
|
1783
|
-
* @template TInput The input token, either Ether or an ERC-20
|
|
1784
|
-
* @template TOutput The output token, either Ether or an ERC-20
|
|
1785
|
-
* @param route The route of the exact out trade
|
|
1786
|
-
* @param amountOut The amount returned by the trade
|
|
1787
|
-
* @returns The exact out trade
|
|
1887
|
+
/**
|
|
1888
|
+
* Constructs an exact out trade with the given amount out and route
|
|
1889
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
1890
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
1891
|
+
* @param route The route of the exact out trade
|
|
1892
|
+
* @param amountOut The amount returned by the trade
|
|
1893
|
+
* @returns The exact out trade
|
|
1788
1894
|
*/
|
|
1789
1895
|
;
|
|
1790
1896
|
Trade.exactOut =
|
|
@@ -1803,15 +1909,15 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1803
1909
|
}
|
|
1804
1910
|
return exactOut;
|
|
1805
1911
|
}()
|
|
1806
|
-
/**
|
|
1807
|
-
* Constructs a trade by simulating swaps through the given route
|
|
1808
|
-
* @template TInput The input token, either Ether or an ERC-20.
|
|
1809
|
-
* @template TOutput The output token, either Ether or an ERC-20.
|
|
1810
|
-
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
1811
|
-
* @param route route to swap through
|
|
1812
|
-
* @param amount the amount specified, either input or output, depending on tradeType
|
|
1813
|
-
* @param tradeType whether the trade is an exact input or exact output swap
|
|
1814
|
-
* @returns The route
|
|
1912
|
+
/**
|
|
1913
|
+
* Constructs a trade by simulating swaps through the given route
|
|
1914
|
+
* @template TInput The input token, either Ether or an ERC-20.
|
|
1915
|
+
* @template TOutput The output token, either Ether or an ERC-20.
|
|
1916
|
+
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
1917
|
+
* @param route route to swap through
|
|
1918
|
+
* @param amount the amount specified, either input or output, depending on tradeType
|
|
1919
|
+
* @param tradeType whether the trade is an exact input or exact output swap
|
|
1920
|
+
* @returns The route
|
|
1815
1921
|
*/
|
|
1816
1922
|
;
|
|
1817
1923
|
Trade.fromRoute =
|
|
@@ -1891,15 +1997,15 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1891
1997
|
}
|
|
1892
1998
|
return fromRoute;
|
|
1893
1999
|
}()
|
|
1894
|
-
/**
|
|
1895
|
-
* Constructs a trade from routes by simulating swaps
|
|
1896
|
-
*
|
|
1897
|
-
* @template TInput The input token, either Ether or an ERC-20.
|
|
1898
|
-
* @template TOutput The output token, either Ether or an ERC-20.
|
|
1899
|
-
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
1900
|
-
* @param routes the routes to swap through and how much of the amount should be routed through each
|
|
1901
|
-
* @param tradeType whether the trade is an exact input or exact output swap
|
|
1902
|
-
* @returns The trade
|
|
2000
|
+
/**
|
|
2001
|
+
* Constructs a trade from routes by simulating swaps
|
|
2002
|
+
*
|
|
2003
|
+
* @template TInput The input token, either Ether or an ERC-20.
|
|
2004
|
+
* @template TOutput The output token, either Ether or an ERC-20.
|
|
2005
|
+
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
2006
|
+
* @param routes the routes to swap through and how much of the amount should be routed through each
|
|
2007
|
+
* @param tradeType whether the trade is an exact input or exact output swap
|
|
2008
|
+
* @returns The trade
|
|
1903
2009
|
*/
|
|
1904
2010
|
;
|
|
1905
2011
|
Trade.fromRoutes =
|
|
@@ -1994,14 +2100,14 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1994
2100
|
}
|
|
1995
2101
|
return fromRoutes;
|
|
1996
2102
|
}()
|
|
1997
|
-
/**
|
|
1998
|
-
* Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
|
|
1999
|
-
* elsewhere and do not have any tick data
|
|
2000
|
-
* @template TInput The input token, either Ether or an ERC-20
|
|
2001
|
-
* @template TOutput The output token, either Ether or an ERC-20
|
|
2002
|
-
* @template TTradeType The type of the trade, either exact in or exact out
|
|
2003
|
-
* @param constructorArguments The arguments passed to the trade constructor
|
|
2004
|
-
* @returns The unchecked trade
|
|
2103
|
+
/**
|
|
2104
|
+
* Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
|
|
2105
|
+
* elsewhere and do not have any tick data
|
|
2106
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
2107
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
2108
|
+
* @template TTradeType The type of the trade, either exact in or exact out
|
|
2109
|
+
* @param constructorArguments The arguments passed to the trade constructor
|
|
2110
|
+
* @returns The unchecked trade
|
|
2005
2111
|
*/
|
|
2006
2112
|
;
|
|
2007
2113
|
Trade.createUncheckedTrade = function createUncheckedTrade(constructorArguments) {
|
|
@@ -2013,23 +2119,19 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2013
2119
|
}]
|
|
2014
2120
|
}));
|
|
2015
2121
|
}
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
*
|
|
2019
|
-
*
|
|
2020
|
-
* @template
|
|
2021
|
-
* @template
|
|
2022
|
-
* @
|
|
2023
|
-
* @
|
|
2122
|
+
|
|
2123
|
+
/**
|
|
2124
|
+
* Creates a trade without computing the result of swapping through the routes. Useful when you have simulated the trade
|
|
2125
|
+
* elsewhere and do not have any tick data
|
|
2126
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
2127
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
2128
|
+
* @template TTradeType The type of the trade, either exact in or exact out
|
|
2129
|
+
* @param constructorArguments The arguments passed to the trade constructor
|
|
2130
|
+
* @returns The unchecked trade
|
|
2024
2131
|
*/;
|
|
2025
2132
|
Trade.createUncheckedTradeWithMultipleRoutes = function createUncheckedTradeWithMultipleRoutes(constructorArguments) {
|
|
2026
2133
|
return new Trade(constructorArguments);
|
|
2027
|
-
}
|
|
2028
|
-
/**
|
|
2029
|
-
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
2030
|
-
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
2031
|
-
* @returns The amount out
|
|
2032
|
-
*/;
|
|
2134
|
+
};
|
|
2033
2135
|
var _proto = Trade.prototype;
|
|
2034
2136
|
_proto.minimumAmountOut = function minimumAmountOut(slippageTolerance, amountOut) {
|
|
2035
2137
|
if (amountOut === void 0) {
|
|
@@ -2043,10 +2145,11 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2043
2145
|
return CurrencyAmount.fromRawAmount(amountOut.currency, slippageAdjustedAmountOut);
|
|
2044
2146
|
}
|
|
2045
2147
|
}
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
*
|
|
2049
|
-
* @
|
|
2148
|
+
|
|
2149
|
+
/**
|
|
2150
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
2151
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
2152
|
+
* @returns The amount in
|
|
2050
2153
|
*/;
|
|
2051
2154
|
_proto.maximumAmountIn = function maximumAmountIn(slippageTolerance, amountIn) {
|
|
2052
2155
|
if (amountIn === void 0) {
|
|
@@ -2060,28 +2163,30 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2060
2163
|
return CurrencyAmount.fromRawAmount(amountIn.currency, slippageAdjustedAmountIn);
|
|
2061
2164
|
}
|
|
2062
2165
|
}
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
*
|
|
2066
|
-
* @
|
|
2166
|
+
|
|
2167
|
+
/**
|
|
2168
|
+
* Return the execution price after accounting for slippage tolerance
|
|
2169
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
2170
|
+
* @returns The execution price
|
|
2067
2171
|
*/;
|
|
2068
2172
|
_proto.worstExecutionPrice = function worstExecutionPrice(slippageTolerance) {
|
|
2069
2173
|
return new Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
2070
2174
|
}
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
*
|
|
2074
|
-
*
|
|
2075
|
-
*
|
|
2076
|
-
*
|
|
2077
|
-
* @param
|
|
2078
|
-
* @param
|
|
2079
|
-
* @param
|
|
2080
|
-
* @param
|
|
2081
|
-
* @param
|
|
2082
|
-
* @param
|
|
2083
|
-
* @param
|
|
2084
|
-
* @
|
|
2175
|
+
|
|
2176
|
+
/**
|
|
2177
|
+
* Given a list of pools, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
2178
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
2179
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
2180
|
+
* the amount in among multiple routes.
|
|
2181
|
+
* @param pools the pools to consider in finding the best trade
|
|
2182
|
+
* @param nextAmountIn exact amount of input currency to spend
|
|
2183
|
+
* @param currencyOut the desired currency out
|
|
2184
|
+
* @param maxNumResults maximum number of results to return
|
|
2185
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
|
|
2186
|
+
* @param currentPools used in recursion; the current list of pools
|
|
2187
|
+
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
2188
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
2189
|
+
* @returns The exact in trade
|
|
2085
2190
|
*/;
|
|
2086
2191
|
Trade.bestTradeExactIn =
|
|
2087
2192
|
/*#__PURE__*/
|
|
@@ -2178,21 +2283,21 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2178
2283
|
}
|
|
2179
2284
|
return bestTradeExactIn;
|
|
2180
2285
|
}()
|
|
2181
|
-
/**
|
|
2182
|
-
* similar to the above method but instead targets a fixed output amount
|
|
2183
|
-
* given a list of pools, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
2184
|
-
* to an output token amount, making at most `maxHops` hops
|
|
2185
|
-
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
2186
|
-
* the amount in among multiple routes.
|
|
2187
|
-
* @param pools the pools to consider in finding the best trade
|
|
2188
|
-
* @param currencyIn the currency to spend
|
|
2189
|
-
* @param currencyAmountOut the desired currency amount out
|
|
2190
|
-
* @param nextAmountOut the exact amount of currency out
|
|
2191
|
-
* @param maxNumResults maximum number of results to return
|
|
2192
|
-
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
|
|
2193
|
-
* @param currentPools used in recursion; the current list of pools
|
|
2194
|
-
* @param bestTrades used in recursion; the current list of best trades
|
|
2195
|
-
* @returns The exact out trade
|
|
2286
|
+
/**
|
|
2287
|
+
* similar to the above method but instead targets a fixed output amount
|
|
2288
|
+
* given a list of pools, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
2289
|
+
* to an output token amount, making at most `maxHops` hops
|
|
2290
|
+
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
2291
|
+
* the amount in among multiple routes.
|
|
2292
|
+
* @param pools the pools to consider in finding the best trade
|
|
2293
|
+
* @param currencyIn the currency to spend
|
|
2294
|
+
* @param currencyAmountOut the desired currency amount out
|
|
2295
|
+
* @param nextAmountOut the exact amount of currency out
|
|
2296
|
+
* @param maxNumResults maximum number of results to return
|
|
2297
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
|
|
2298
|
+
* @param currentPools used in recursion; the current list of pools
|
|
2299
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
2300
|
+
* @returns The exact out trade
|
|
2196
2301
|
*/
|
|
2197
2302
|
;
|
|
2198
2303
|
Trade.bestTradeExactOut =
|
|
@@ -2292,16 +2397,25 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2292
2397
|
}();
|
|
2293
2398
|
return _createClass(Trade, [{
|
|
2294
2399
|
key: "route",
|
|
2295
|
-
get:
|
|
2400
|
+
get:
|
|
2401
|
+
/**
|
|
2402
|
+
* @deprecated Deprecated in favor of 'swaps' property. If the trade consists of multiple routes
|
|
2403
|
+
* this will return an error.
|
|
2404
|
+
*
|
|
2405
|
+
* When the trade consists of just a single route, this returns the route of the trade,
|
|
2406
|
+
* i.e. which pools the trade goes through.
|
|
2407
|
+
*/
|
|
2408
|
+
function get() {
|
|
2296
2409
|
!(this.swaps.length === 1) ? process.env.NODE_ENV !== "production" ? invariant(false, 'MULTIPLE_ROUTES') : invariant(false) : void 0;
|
|
2297
2410
|
return this.swaps[0].route;
|
|
2298
2411
|
}
|
|
2299
|
-
/**
|
|
2300
|
-
* The input amount for the trade assuming no slippage.
|
|
2301
|
-
*/
|
|
2302
2412
|
}, {
|
|
2303
2413
|
key: "inputAmount",
|
|
2304
|
-
get:
|
|
2414
|
+
get:
|
|
2415
|
+
/**
|
|
2416
|
+
* The input amount for the trade assuming no slippage.
|
|
2417
|
+
*/
|
|
2418
|
+
function get() {
|
|
2305
2419
|
if (this._inputAmount) {
|
|
2306
2420
|
return this._inputAmount;
|
|
2307
2421
|
}
|
|
@@ -2315,12 +2429,13 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2315
2429
|
this._inputAmount = totalInputFromRoutes;
|
|
2316
2430
|
return this._inputAmount;
|
|
2317
2431
|
}
|
|
2318
|
-
/**
|
|
2319
|
-
* The output amount for the trade assuming no slippage.
|
|
2320
|
-
*/
|
|
2321
2432
|
}, {
|
|
2322
2433
|
key: "outputAmount",
|
|
2323
|
-
get:
|
|
2434
|
+
get:
|
|
2435
|
+
/**
|
|
2436
|
+
* The output amount for the trade assuming no slippage.
|
|
2437
|
+
*/
|
|
2438
|
+
function get() {
|
|
2324
2439
|
if (this._outputAmount) {
|
|
2325
2440
|
return this._outputAmount;
|
|
2326
2441
|
}
|
|
@@ -2334,21 +2449,23 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2334
2449
|
this._outputAmount = totalOutputFromRoutes;
|
|
2335
2450
|
return this._outputAmount;
|
|
2336
2451
|
}
|
|
2337
|
-
/**
|
|
2338
|
-
* The price expressed in terms of output amount/input amount.
|
|
2339
|
-
*/
|
|
2340
2452
|
}, {
|
|
2341
2453
|
key: "executionPrice",
|
|
2342
|
-
get:
|
|
2454
|
+
get:
|
|
2455
|
+
/**
|
|
2456
|
+
* The price expressed in terms of output amount/input amount.
|
|
2457
|
+
*/
|
|
2458
|
+
function get() {
|
|
2343
2459
|
var _this$_executionPrice;
|
|
2344
2460
|
return (_this$_executionPrice = this._executionPrice) != null ? _this$_executionPrice : this._executionPrice = new Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
2345
2461
|
}
|
|
2346
|
-
/**
|
|
2347
|
-
* Returns the percent difference between the route's mid price and the price impact
|
|
2348
|
-
*/
|
|
2349
2462
|
}, {
|
|
2350
2463
|
key: "priceImpact",
|
|
2351
|
-
get:
|
|
2464
|
+
get:
|
|
2465
|
+
/**
|
|
2466
|
+
* Returns the percent difference between the route's mid price and the price impact
|
|
2467
|
+
*/
|
|
2468
|
+
function get() {
|
|
2352
2469
|
if (this._priceImpact) {
|
|
2353
2470
|
return this._priceImpact;
|
|
2354
2471
|
}
|
|
@@ -2368,8 +2485,8 @@ var Trade = /*#__PURE__*/function () {
|
|
|
2368
2485
|
}();
|
|
2369
2486
|
|
|
2370
2487
|
var Multicall = /*#__PURE__*/function () {
|
|
2371
|
-
/**
|
|
2372
|
-
* Cannot be constructed.
|
|
2488
|
+
/**
|
|
2489
|
+
* Cannot be constructed.
|
|
2373
2490
|
*/
|
|
2374
2491
|
function Multicall() {}
|
|
2375
2492
|
Multicall.encodeMulticall = function encodeMulticall(calldatas) {
|
|
@@ -2386,8 +2503,8 @@ function isAllowedPermit(permitOptions) {
|
|
|
2386
2503
|
return 'nonce' in permitOptions;
|
|
2387
2504
|
}
|
|
2388
2505
|
var SelfPermit = /*#__PURE__*/function () {
|
|
2389
|
-
/**
|
|
2390
|
-
* Cannot be constructed.
|
|
2506
|
+
/**
|
|
2507
|
+
* Cannot be constructed.
|
|
2391
2508
|
*/
|
|
2392
2509
|
function SelfPermit() {}
|
|
2393
2510
|
SelfPermit.encodePermit = function encodePermit(token, options) {
|
|
@@ -2398,8 +2515,8 @@ var SelfPermit = /*#__PURE__*/function () {
|
|
|
2398
2515
|
SelfPermit.INTERFACE = /*#__PURE__*/new Interface(ISelfPermit.abi);
|
|
2399
2516
|
|
|
2400
2517
|
var Payments = /*#__PURE__*/function () {
|
|
2401
|
-
/**
|
|
2402
|
-
* Cannot be constructed.
|
|
2518
|
+
/**
|
|
2519
|
+
* Cannot be constructed.
|
|
2403
2520
|
*/
|
|
2404
2521
|
function Payments() {}
|
|
2405
2522
|
Payments.encodeFeeBips = function encodeFeeBips(fee) {
|
|
@@ -2434,15 +2551,25 @@ Payments.INTERFACE = /*#__PURE__*/new Interface(IPeripheryPaymentsWithFee.abi);
|
|
|
2434
2551
|
|
|
2435
2552
|
var _excluded = ["expectedCurrencyOwed0", "expectedCurrencyOwed1"];
|
|
2436
2553
|
var MaxUint128 = /*#__PURE__*/toHex(/*#__PURE__*/JSBI.subtract(/*#__PURE__*/JSBI.exponentiate(/*#__PURE__*/JSBI.BigInt(2), /*#__PURE__*/JSBI.BigInt(128)), /*#__PURE__*/JSBI.BigInt(1)));
|
|
2554
|
+
|
|
2555
|
+
/**
|
|
2556
|
+
* Options for producing the calldata to add liquidity.
|
|
2557
|
+
*/
|
|
2558
|
+
|
|
2437
2559
|
// type guard
|
|
2438
2560
|
function isMint(options) {
|
|
2439
2561
|
return Object.keys(options).some(function (k) {
|
|
2440
2562
|
return k === 'recipient';
|
|
2441
2563
|
});
|
|
2442
2564
|
}
|
|
2565
|
+
|
|
2566
|
+
/**
|
|
2567
|
+
* Options for producing the calldata to exit a position.
|
|
2568
|
+
*/
|
|
2569
|
+
|
|
2443
2570
|
var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
2444
|
-
/**
|
|
2445
|
-
* Cannot be constructed.
|
|
2571
|
+
/**
|
|
2572
|
+
* Cannot be constructed.
|
|
2446
2573
|
*/
|
|
2447
2574
|
function NonfungiblePositionManager() {}
|
|
2448
2575
|
NonfungiblePositionManager.encodeCreate = function encodeCreate(pool) {
|
|
@@ -2457,19 +2584,23 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2457
2584
|
NonfungiblePositionManager.addCallParameters = function addCallParameters(position, options) {
|
|
2458
2585
|
!JSBI.greaterThan(position.liquidity, ZERO) ? process.env.NODE_ENV !== "production" ? invariant(false, 'ZERO_LIQUIDITY') : invariant(false) : void 0;
|
|
2459
2586
|
var calldatas = [];
|
|
2587
|
+
|
|
2460
2588
|
// get amounts
|
|
2461
2589
|
var _position$mintAmounts = position.mintAmounts,
|
|
2462
2590
|
amount0Desired = _position$mintAmounts.amount0,
|
|
2463
2591
|
amount1Desired = _position$mintAmounts.amount1;
|
|
2592
|
+
|
|
2464
2593
|
// adjust for slippage
|
|
2465
2594
|
var minimumAmounts = position.mintAmountsWithSlippage(options.slippageTolerance);
|
|
2466
2595
|
var amount0Min = toHex(minimumAmounts.amount0);
|
|
2467
2596
|
var amount1Min = toHex(minimumAmounts.amount1);
|
|
2468
2597
|
var deadline = toHex(options.deadline);
|
|
2598
|
+
|
|
2469
2599
|
// create pool if needed
|
|
2470
2600
|
if (isMint(options) && options.createPool) {
|
|
2471
2601
|
calldatas.push(this.encodeCreate(position.pool));
|
|
2472
2602
|
}
|
|
2603
|
+
|
|
2473
2604
|
// permits if necessary
|
|
2474
2605
|
if (options.token0Permit) {
|
|
2475
2606
|
calldatas.push(SelfPermit.encodePermit(position.pool.token0, options.token0Permit));
|
|
@@ -2477,6 +2608,7 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2477
2608
|
if (options.token1Permit) {
|
|
2478
2609
|
calldatas.push(SelfPermit.encodePermit(position.pool.token1, options.token1Permit));
|
|
2479
2610
|
}
|
|
2611
|
+
|
|
2480
2612
|
// mint
|
|
2481
2613
|
if (isMint(options)) {
|
|
2482
2614
|
var recipient = validateAndParseAddress(options.recipient);
|
|
@@ -2509,6 +2641,7 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2509
2641
|
var wrapped = options.useNative.wrapped;
|
|
2510
2642
|
!(position.pool.token0.equals(wrapped) || position.pool.token1.equals(wrapped)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'NO_WETH') : invariant(false) : void 0;
|
|
2511
2643
|
var wrappedValue = position.pool.token0.equals(wrapped) ? amount0Desired : amount1Desired;
|
|
2644
|
+
|
|
2512
2645
|
// we only need to refund if we're actually sending ETH
|
|
2513
2646
|
if (JSBI.greaterThan(wrappedValue, ZERO)) {
|
|
2514
2647
|
calldatas.push(Payments.encodeRefundETH());
|
|
@@ -2525,6 +2658,7 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2525
2658
|
var tokenId = toHex(options.tokenId);
|
|
2526
2659
|
var involvesETH = options.expectedCurrencyOwed0.currency.isNative || options.expectedCurrencyOwed1.currency.isNative;
|
|
2527
2660
|
var recipient = validateAndParseAddress(options.recipient);
|
|
2661
|
+
|
|
2528
2662
|
// collect
|
|
2529
2663
|
calldatas.push(NonfungiblePositionManager.INTERFACE.encodeFunctionData('collect', [{
|
|
2530
2664
|
tokenId: tokenId,
|
|
@@ -2548,16 +2682,18 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2548
2682
|
value: toHex(0)
|
|
2549
2683
|
};
|
|
2550
2684
|
}
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
*
|
|
2554
|
-
* @param
|
|
2555
|
-
* @
|
|
2685
|
+
|
|
2686
|
+
/**
|
|
2687
|
+
* Produces the calldata for completely or partially exiting a position
|
|
2688
|
+
* @param position The position to exit
|
|
2689
|
+
* @param options Additional information necessary for generating the calldata
|
|
2690
|
+
* @returns The call parameters
|
|
2556
2691
|
*/;
|
|
2557
2692
|
NonfungiblePositionManager.removeCallParameters = function removeCallParameters(position, options) {
|
|
2558
2693
|
var calldatas = [];
|
|
2559
2694
|
var deadline = toHex(options.deadline);
|
|
2560
2695
|
var tokenId = toHex(options.tokenId);
|
|
2696
|
+
|
|
2561
2697
|
// construct a partial position with a percentage of liquidity
|
|
2562
2698
|
var partialPosition = new Position({
|
|
2563
2699
|
pool: position.pool,
|
|
@@ -2566,6 +2702,7 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2566
2702
|
tickUpper: position.tickUpper
|
|
2567
2703
|
});
|
|
2568
2704
|
!JSBI.greaterThan(partialPosition.liquidity, ZERO) ? process.env.NODE_ENV !== "production" ? invariant(false, 'ZERO_LIQUIDITY') : invariant(false) : void 0;
|
|
2705
|
+
|
|
2569
2706
|
// slippage-adjusted underlying amounts
|
|
2570
2707
|
var _partialPosition$burn = partialPosition.burnAmountsWithSlippage(options.slippageTolerance),
|
|
2571
2708
|
amount0Min = _partialPosition$burn.amount0,
|
|
@@ -2573,6 +2710,7 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2573
2710
|
if (options.permit) {
|
|
2574
2711
|
calldatas.push(NonfungiblePositionManager.INTERFACE.encodeFunctionData('permit', [validateAndParseAddress(options.permit.spender), tokenId, toHex(options.permit.deadline), options.permit.v, options.permit.r, options.permit.s]));
|
|
2575
2712
|
}
|
|
2713
|
+
|
|
2576
2714
|
// remove liquidity
|
|
2577
2715
|
calldatas.push(NonfungiblePositionManager.INTERFACE.encodeFunctionData('decreaseLiquidity', [{
|
|
2578
2716
|
tokenId: tokenId,
|
|
@@ -2621,22 +2759,26 @@ var NonfungiblePositionManager = /*#__PURE__*/function () {
|
|
|
2621
2759
|
}();
|
|
2622
2760
|
NonfungiblePositionManager.INTERFACE = /*#__PURE__*/new Interface(INonfungiblePositionManager.abi);
|
|
2623
2761
|
|
|
2624
|
-
/**
|
|
2625
|
-
*
|
|
2626
|
-
|
|
2762
|
+
/**
|
|
2763
|
+
* Optional arguments to send to the quoter.
|
|
2764
|
+
*/
|
|
2765
|
+
|
|
2766
|
+
/**
|
|
2767
|
+
* Represents the Uniswap V3 QuoterV1 contract with a method for returning the formatted
|
|
2768
|
+
* calldata needed to call the quoter contract.
|
|
2627
2769
|
*/
|
|
2628
2770
|
var SwapQuoter = /*#__PURE__*/function () {
|
|
2629
2771
|
function SwapQuoter() {}
|
|
2630
|
-
/**
|
|
2631
|
-
* Produces the on-chain method name of the appropriate function within QuoterV2,
|
|
2632
|
-
* and the relevant hex encoded parameters.
|
|
2633
|
-
* @template TInput The input token, either Ether or an ERC-20
|
|
2634
|
-
* @template TOutput The output token, either Ether or an ERC-20
|
|
2635
|
-
* @param route The swap route, a list of pools through which a swap can occur
|
|
2636
|
-
* @param amount The amount of the quote, either an amount in, or an amount out
|
|
2637
|
-
* @param tradeType The trade type, either exact input or exact output
|
|
2638
|
-
* @param options The optional params including price limit and Quoter contract switch
|
|
2639
|
-
* @returns The formatted calldata
|
|
2772
|
+
/**
|
|
2773
|
+
* Produces the on-chain method name of the appropriate function within QuoterV2,
|
|
2774
|
+
* and the relevant hex encoded parameters.
|
|
2775
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
2776
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
2777
|
+
* @param route The swap route, a list of pools through which a swap can occur
|
|
2778
|
+
* @param amount The amount of the quote, either an amount in, or an amount out
|
|
2779
|
+
* @param tradeType The trade type, either exact input or exact output
|
|
2780
|
+
* @param options The optional params including price limit and Quoter contract switch
|
|
2781
|
+
* @returns The formatted calldata
|
|
2640
2782
|
*/
|
|
2641
2783
|
SwapQuoter.quoteCallParameters = function quoteCallParameters(route, amount, tradeType, options) {
|
|
2642
2784
|
if (options === void 0) {
|
|
@@ -2679,13 +2821,25 @@ var SwapQuoter = /*#__PURE__*/function () {
|
|
|
2679
2821
|
SwapQuoter.V1INTERFACE = /*#__PURE__*/new Interface(IQuoter.abi);
|
|
2680
2822
|
SwapQuoter.V2INTERFACE = /*#__PURE__*/new Interface(IQuoterV2.abi);
|
|
2681
2823
|
|
|
2824
|
+
/**
|
|
2825
|
+
* Represents a unique staking program.
|
|
2826
|
+
*/
|
|
2827
|
+
|
|
2828
|
+
/**
|
|
2829
|
+
* Options to specify when claiming rewards.
|
|
2830
|
+
*/
|
|
2831
|
+
|
|
2832
|
+
/**
|
|
2833
|
+
* Options to specify when withdrawing a position.
|
|
2834
|
+
*/
|
|
2835
|
+
|
|
2682
2836
|
var Staker = /*#__PURE__*/function () {
|
|
2683
2837
|
function Staker() {}
|
|
2684
|
-
/**
|
|
2685
|
-
* To claim rewards, must unstake and then claim.
|
|
2686
|
-
* @param incentiveKey The unique identifier of a staking program.
|
|
2687
|
-
* @param options Options for producing the calldata to claim. Can't claim unless you unstake.
|
|
2688
|
-
* @returns The calldatas for 'unstakeToken' and 'claimReward'.
|
|
2838
|
+
/**
|
|
2839
|
+
* To claim rewards, must unstake and then claim.
|
|
2840
|
+
* @param incentiveKey The unique identifier of a staking program.
|
|
2841
|
+
* @param options Options for producing the calldata to claim. Can't claim unless you unstake.
|
|
2842
|
+
* @returns The calldatas for 'unstakeToken' and 'claimReward'.
|
|
2689
2843
|
*/
|
|
2690
2844
|
Staker.encodeClaim = function encodeClaim(incentiveKey, options) {
|
|
2691
2845
|
var _options$amount;
|
|
@@ -2696,14 +2850,15 @@ var Staker = /*#__PURE__*/function () {
|
|
|
2696
2850
|
calldatas.push(Staker.INTERFACE.encodeFunctionData('claimReward', [incentiveKey.rewardToken.address, recipient, toHex(amount)]));
|
|
2697
2851
|
return calldatas;
|
|
2698
2852
|
}
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
*
|
|
2702
|
-
*
|
|
2703
|
-
*
|
|
2704
|
-
*
|
|
2705
|
-
*
|
|
2706
|
-
*
|
|
2853
|
+
|
|
2854
|
+
/**
|
|
2855
|
+
*
|
|
2856
|
+
* Note: A `tokenId` can be staked in many programs but to claim rewards and continue the program you must unstake, claim, and then restake.
|
|
2857
|
+
* @param incentiveKeys An IncentiveKey or array of IncentiveKeys that `tokenId` is staked in.
|
|
2858
|
+
* Input an array of IncentiveKeys to claim rewards for each program.
|
|
2859
|
+
* @param options ClaimOptions to specify tokenId, recipient, and amount wanting to collect.
|
|
2860
|
+
* Note that you can only specify one amount and one recipient across the various programs if you are collecting from multiple programs at once.
|
|
2861
|
+
* @returns
|
|
2707
2862
|
*/;
|
|
2708
2863
|
Staker.collectRewards = function collectRewards(incentiveKeys, options) {
|
|
2709
2864
|
incentiveKeys = Array.isArray(incentiveKeys) ? incentiveKeys : [incentiveKeys];
|
|
@@ -2721,11 +2876,12 @@ var Staker = /*#__PURE__*/function () {
|
|
|
2721
2876
|
value: toHex(0)
|
|
2722
2877
|
};
|
|
2723
2878
|
}
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
*
|
|
2727
|
-
* @param
|
|
2728
|
-
* @
|
|
2879
|
+
|
|
2880
|
+
/**
|
|
2881
|
+
*
|
|
2882
|
+
* @param incentiveKeys A list of incentiveKeys to unstake from. Should include all incentiveKeys (unique staking programs) that `options.tokenId` is staked in.
|
|
2883
|
+
* @param withdrawOptions Options for producing claim calldata and withdraw calldata. Can't withdraw without unstaking all programs for `tokenId`.
|
|
2884
|
+
* @returns Calldata for unstaking, claiming, and withdrawing.
|
|
2729
2885
|
*/;
|
|
2730
2886
|
Staker.withdrawToken = function withdrawToken(incentiveKeys, withdrawOptions) {
|
|
2731
2887
|
var calldatas = [];
|
|
@@ -2746,10 +2902,11 @@ var Staker = /*#__PURE__*/function () {
|
|
|
2746
2902
|
value: toHex(0)
|
|
2747
2903
|
};
|
|
2748
2904
|
}
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
*
|
|
2752
|
-
* @
|
|
2905
|
+
|
|
2906
|
+
/**
|
|
2907
|
+
*
|
|
2908
|
+
* @param incentiveKeys A single IncentiveKey or array of IncentiveKeys to be encoded and used in the data parameter in `safeTransferFrom`
|
|
2909
|
+
* @returns An IncentiveKey as a string
|
|
2753
2910
|
*/;
|
|
2754
2911
|
Staker.encodeDeposit = function encodeDeposit(incentiveKeys) {
|
|
2755
2912
|
incentiveKeys = Array.isArray(incentiveKeys) ? incentiveKeys : [incentiveKeys];
|
|
@@ -2766,10 +2923,10 @@ var Staker = /*#__PURE__*/function () {
|
|
|
2766
2923
|
}
|
|
2767
2924
|
return data;
|
|
2768
2925
|
}
|
|
2769
|
-
/**
|
|
2770
|
-
*
|
|
2771
|
-
* @param incentiveKey An `IncentiveKey` which represents a unique staking program.
|
|
2772
|
-
* @returns An encoded IncentiveKey to be read by ethers
|
|
2926
|
+
/**
|
|
2927
|
+
*
|
|
2928
|
+
* @param incentiveKey An `IncentiveKey` which represents a unique staking program.
|
|
2929
|
+
* @returns An encoded IncentiveKey to be read by ethers
|
|
2773
2930
|
*/;
|
|
2774
2931
|
Staker._encodeIncentiveKey = function _encodeIncentiveKey(incentiveKey) {
|
|
2775
2932
|
var _incentiveKey$pool = incentiveKey.pool,
|
|
@@ -2790,18 +2947,23 @@ var Staker = /*#__PURE__*/function () {
|
|
|
2790
2947
|
Staker.INTERFACE = /*#__PURE__*/new Interface(IUniswapV3Staker.abi);
|
|
2791
2948
|
Staker.INCENTIVE_KEY_ABI = 'tuple(address rewardToken, address pool, uint256 startTime, uint256 endTime, address refundee)';
|
|
2792
2949
|
|
|
2793
|
-
/**
|
|
2794
|
-
*
|
|
2950
|
+
/**
|
|
2951
|
+
* Options for producing the arguments to send calls to the router.
|
|
2952
|
+
*/
|
|
2953
|
+
|
|
2954
|
+
/**
|
|
2955
|
+
* Represents the Uniswap V3 SwapRouter, and has static methods for helping execute trades.
|
|
2795
2956
|
*/
|
|
2796
2957
|
var SwapRouter = /*#__PURE__*/function () {
|
|
2797
|
-
/**
|
|
2798
|
-
* Cannot be constructed.
|
|
2958
|
+
/**
|
|
2959
|
+
* Cannot be constructed.
|
|
2799
2960
|
*/
|
|
2800
2961
|
function SwapRouter() {}
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
*
|
|
2804
|
-
* @param
|
|
2962
|
+
|
|
2963
|
+
/**
|
|
2964
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
2965
|
+
* @param trade to produce call parameters for
|
|
2966
|
+
* @param options options for the call parameters
|
|
2805
2967
|
*/
|
|
2806
2968
|
SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
|
|
2807
2969
|
if (!Array.isArray(trades)) {
|
|
@@ -2810,6 +2972,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
2810
2972
|
var sampleTrade = trades[0];
|
|
2811
2973
|
var tokenIn = sampleTrade.inputAmount.currency.wrapped;
|
|
2812
2974
|
var tokenOut = sampleTrade.outputAmount.currency.wrapped;
|
|
2975
|
+
|
|
2813
2976
|
// All trades should have the same starting and ending token.
|
|
2814
2977
|
!trades.every(function (trade) {
|
|
2815
2978
|
return trade.inputAmount.currency.wrapped.equals(tokenIn);
|
|
@@ -2823,6 +2986,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
2823
2986
|
var totalAmountOut = trades.reduce(function (sum, trade) {
|
|
2824
2987
|
return sum.add(trade.minimumAmountOut(options.slippageTolerance));
|
|
2825
2988
|
}, ZERO_OUT);
|
|
2989
|
+
|
|
2826
2990
|
// flag for whether a refund needs to happen
|
|
2827
2991
|
var mustRefund = sampleTrade.inputAmount.currency.isNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT;
|
|
2828
2992
|
var inputIsNative = sampleTrade.inputAmount.currency.isNative;
|
|
@@ -2832,6 +2996,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
2832
2996
|
var totalValue = inputIsNative ? trades.reduce(function (sum, trade) {
|
|
2833
2997
|
return sum.add(trade.maximumAmountIn(options.slippageTolerance));
|
|
2834
2998
|
}, ZERO_IN) : ZERO_IN;
|
|
2999
|
+
|
|
2835
3000
|
// encode permit if necessary
|
|
2836
3001
|
if (options.inputTokenPermit) {
|
|
2837
3002
|
!sampleTrade.inputAmount.currency.isToken ? process.env.NODE_ENV !== "production" ? invariant(false, 'NON_TOKEN_PERMIT') : invariant(false) : void 0;
|
|
@@ -2848,6 +3013,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
2848
3013
|
outputAmount = _step2$value.outputAmount;
|
|
2849
3014
|
var amountIn = toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
2850
3015
|
var amountOut = toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient);
|
|
3016
|
+
|
|
2851
3017
|
// flag for whether the trade is single hop or not
|
|
2852
3018
|
var singleHop = route.pools.length === 1;
|
|
2853
3019
|
if (singleHop) {
|
|
@@ -2903,6 +3069,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
2903
3069
|
}
|
|
2904
3070
|
}
|
|
2905
3071
|
}
|
|
3072
|
+
|
|
2906
3073
|
// unwrap
|
|
2907
3074
|
if (routerMustCustody) {
|
|
2908
3075
|
if (!!options.fee) {
|
|
@@ -2915,6 +3082,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
2915
3082
|
calldatas.push(Payments.encodeUnwrapWETH9(totalAmountOut.quotient, recipient));
|
|
2916
3083
|
}
|
|
2917
3084
|
}
|
|
3085
|
+
|
|
2918
3086
|
// refund
|
|
2919
3087
|
if (mustRefund) {
|
|
2920
3088
|
calldatas.push(Payments.encodeRefundETH());
|