@uniswap/router-sdk 1.0.1 → 1.0.5

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.
@@ -1,19 +1,137 @@
1
1
  import JSBI from 'jsbi';
2
2
  import { Interface } from '@ethersproject/abi';
3
- import { abi } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json';
4
- import { Multicall, toHex, Payments, NonfungiblePositionManager, Route as Route$1, Pool, Trade as Trade$1, encodeRouteToPath, SelfPermit } from '@uniswap/v3-sdk';
3
+ import invariant from 'tiny-invariant';
4
+ import { abi } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json';
5
+ import { NonfungiblePositionManager, toHex, Multicall, Payments, Route as Route$1, Pool, Trade as Trade$1, encodeRouteToPath, SelfPermit, Position } from '@uniswap/v3-sdk';
6
+ import { abi as abi$1 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json';
5
7
  import { validateAndParseAddress, TradeType, Fraction, CurrencyAmount, Price, Percent, WETH9 } from '@uniswap/sdk-core';
6
- import { abi as abi$1 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json';
8
+ import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json';
7
9
  import { abi as abi$3 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json';
8
10
  import { Route, Pair, Trade as Trade$2 } from '@uniswap/v2-sdk';
9
- import invariant from 'tiny-invariant';
10
- import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json';
11
11
 
12
12
  var MSG_SENDER = '0x0000000000000000000000000000000000000001';
13
13
  var ADDRESS_THIS = '0x0000000000000000000000000000000000000002';
14
14
  var ZERO = /*#__PURE__*/JSBI.BigInt(0);
15
15
  var ONE = /*#__PURE__*/JSBI.BigInt(1);
16
16
 
17
+ var ApprovalTypes;
18
+
19
+ (function (ApprovalTypes) {
20
+ ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
21
+ ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
22
+ ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
23
+ ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
24
+ ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
25
+ })(ApprovalTypes || (ApprovalTypes = {})); // type guard
26
+
27
+
28
+ function isMint(options) {
29
+ return Object.keys(options).some(function (k) {
30
+ return k === 'recipient';
31
+ });
32
+ }
33
+ var ApproveAndCall = /*#__PURE__*/function () {
34
+ /**
35
+ * Cannot be constructed.
36
+ */
37
+ function ApproveAndCall() {}
38
+
39
+ ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
40
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
41
+ };
42
+
43
+ ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
44
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
45
+ };
46
+
47
+ ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
48
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
49
+ };
50
+
51
+ ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
52
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
53
+ };
54
+
55
+ ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
56
+ !(calldatas.length > 0) ? process.env.NODE_ENV !== "production" ? invariant(false, 'NULL_CALLDATA') : invariant(false) : void 0;
57
+
58
+ if (calldatas.length == 1) {
59
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
60
+ } else {
61
+ var encodedMulticall = NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
62
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
63
+ }
64
+ }
65
+ /**
66
+ * Encode adding liquidity to a position in the nft manager contract
67
+ * @param position Forcasted position with expected amount out from swap
68
+ * @param minimalPosition Forcasted position with custom minimal token amounts
69
+ * @param addLiquidityOptions Options for adding liquidity
70
+ * @param slippageTolerance Defines maximum slippage
71
+ */
72
+ ;
73
+
74
+ ApproveAndCall.encodeAddLiquidity = function encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, slippageTolerance) {
75
+ var _position$mintAmounts = position.mintAmountsWithSlippage(slippageTolerance),
76
+ amount0Min = _position$mintAmounts.amount0,
77
+ amount1Min = _position$mintAmounts.amount1; // position.mintAmountsWithSlippage() can create amounts not dependenable in scenarios
78
+ // such as range orders. Allow the option to provide a position with custom minimum amounts
79
+ // for these scenarios
80
+
81
+
82
+ if (JSBI.lessThan(minimalPosition.amount0.quotient, amount0Min)) {
83
+ amount0Min = minimalPosition.amount0.quotient;
84
+ }
85
+
86
+ if (JSBI.lessThan(minimalPosition.amount1.quotient, amount1Min)) {
87
+ amount1Min = minimalPosition.amount1.quotient;
88
+ }
89
+
90
+ if (isMint(addLiquidityOptions)) {
91
+ return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [{
92
+ token0: position.pool.token0.address,
93
+ token1: position.pool.token1.address,
94
+ fee: position.pool.fee,
95
+ tickLower: position.tickLower,
96
+ tickUpper: position.tickUpper,
97
+ amount0Min: toHex(amount0Min),
98
+ amount1Min: toHex(amount1Min),
99
+ recipient: addLiquidityOptions.recipient
100
+ }]);
101
+ } else {
102
+ return ApproveAndCall.INTERFACE.encodeFunctionData('increaseLiquidity', [{
103
+ token0: position.pool.token0.address,
104
+ token1: position.pool.token1.address,
105
+ amount0Min: toHex(amount0Min),
106
+ amount1Min: toHex(amount1Min),
107
+ tokenId: toHex(addLiquidityOptions.tokenId)
108
+ }]);
109
+ }
110
+ };
111
+
112
+ ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
113
+ switch (approvalType) {
114
+ case ApprovalTypes.MAX:
115
+ return ApproveAndCall.encodeApproveMax(token.wrapped);
116
+
117
+ case ApprovalTypes.MAX_MINUS_ONE:
118
+ return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
119
+
120
+ case ApprovalTypes.ZERO_THEN_MAX:
121
+ return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
122
+
123
+ case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
124
+ return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
125
+
126
+ default:
127
+ throw 'Error: invalid ApprovalType';
128
+ }
129
+ };
130
+
131
+ return ApproveAndCall;
132
+ }();
133
+ ApproveAndCall.INTERFACE = /*#__PURE__*/new Interface(abi);
134
+
17
135
  function validateAndParseBytes32(bytes32) {
18
136
  if (!bytes32.match(/^0x[0-9a-fA-F]{64}$/)) {
19
137
  throw new Error(bytes32 + " is not valid bytes32.");
@@ -51,7 +169,7 @@ var MulticallExtended = /*#__PURE__*/function () {
51
169
 
52
170
  return MulticallExtended;
53
171
  }();
54
- MulticallExtended.INTERFACE = /*#__PURE__*/new Interface(abi);
172
+ MulticallExtended.INTERFACE = /*#__PURE__*/new Interface(abi$1);
55
173
 
56
174
  function encodeFeeBips(fee) {
57
175
  return toHex(fee.multiply(10000).quotient);
@@ -103,7 +221,7 @@ var PaymentsExtended = /*#__PURE__*/function () {
103
221
 
104
222
  return PaymentsExtended;
105
223
  }();
106
- PaymentsExtended.INTERFACE = /*#__PURE__*/new Interface(abi$1);
224
+ PaymentsExtended.INTERFACE = /*#__PURE__*/new Interface(abi$2);
107
225
 
108
226
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
109
227
  try {
@@ -211,72 +329,6 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
211
329
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
212
330
  }
213
331
 
214
- var ApprovalTypes;
215
-
216
- (function (ApprovalTypes) {
217
- ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
218
- ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
219
- ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
220
- ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
221
- ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
222
- })(ApprovalTypes || (ApprovalTypes = {}));
223
-
224
- var ApproveAndCall = /*#__PURE__*/function () {
225
- /**
226
- * Cannot be constructed.
227
- */
228
- function ApproveAndCall() {}
229
-
230
- ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
231
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
232
- };
233
-
234
- ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
235
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
236
- };
237
-
238
- ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
239
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
240
- };
241
-
242
- ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
243
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
244
- };
245
-
246
- ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
247
- !(calldatas.length > 0) ? process.env.NODE_ENV !== "production" ? invariant(false, 'NULL_CALLDATA') : invariant(false) : void 0;
248
-
249
- if (calldatas.length == 1) {
250
- return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
251
- } else {
252
- var encodedMulticall = NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
253
- return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
254
- }
255
- };
256
-
257
- ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
258
- switch (approvalType) {
259
- case ApprovalTypes.MAX:
260
- return ApproveAndCall.encodeApproveMax(token.wrapped);
261
-
262
- case ApprovalTypes.MAX_MINUS_ONE:
263
- return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
264
-
265
- case ApprovalTypes.ZERO_THEN_MAX:
266
- return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
267
-
268
- case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
269
- return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
270
-
271
- default:
272
- throw 'Error: invalid ApprovalType';
273
- }
274
- };
275
-
276
- return ApproveAndCall;
277
- }();
278
- ApproveAndCall.INTERFACE = /*#__PURE__*/new Interface(abi$2);
279
-
280
332
  function createCommonjsModule(fn, module) {
281
333
  return module = { exports: {} }, fn(module, module.exports), module.exports;
282
334
  }
@@ -1584,9 +1636,12 @@ var SwapRouter = /*#__PURE__*/function () {
1584
1636
 
1585
1637
  var ZERO_IN = CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1586
1638
  var ZERO_OUT = CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1587
- var totalAmountOut = trades.reduce(function (sum, trade) {
1639
+ var minimumAmountOut = trades.reduce(function (sum, trade) {
1588
1640
  return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1589
1641
  }, ZERO_OUT);
1642
+ var quoteAmountOut = trades.reduce(function (sum, trade) {
1643
+ return sum.add(trade.outputAmount);
1644
+ }, ZERO_OUT);
1590
1645
  var totalAmountIn = trades.reduce(function (sum, trade) {
1591
1646
  return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1592
1647
  }, ZERO_IN);
@@ -1597,7 +1652,8 @@ var SwapRouter = /*#__PURE__*/function () {
1597
1652
  inputIsNative: inputIsNative,
1598
1653
  outputIsNative: outputIsNative,
1599
1654
  totalAmountIn: totalAmountIn,
1600
- totalAmountOut: totalAmountOut
1655
+ minimumAmountOut: minimumAmountOut,
1656
+ quoteAmountOut: quoteAmountOut
1601
1657
  };
1602
1658
  }
1603
1659
  /**
@@ -1615,14 +1671,14 @@ var SwapRouter = /*#__PURE__*/function () {
1615
1671
  inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
1616
1672
  outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
1617
1673
  totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
1618
- totalAmountOut = _SwapRouter$encodeSwa.totalAmountOut; // unwrap or sweep
1674
+ minimumAmountOut = _SwapRouter$encodeSwa.minimumAmountOut; // unwrap or sweep
1619
1675
 
1620
1676
 
1621
1677
  if (routerMustCustody) {
1622
1678
  if (outputIsNative) {
1623
- calldatas.push(PaymentsExtended.encodeUnwrapWETH9(totalAmountOut.quotient, options.recipient, options.fee));
1679
+ calldatas.push(PaymentsExtended.encodeUnwrapWETH9(minimumAmountOut.quotient, options.recipient, options.fee));
1624
1680
  } else {
1625
- calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, totalAmountOut.quotient, options.recipient, options.fee));
1681
+ calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, minimumAmountOut.quotient, options.recipient, options.fee));
1626
1682
  }
1627
1683
  } // must refund when paying in ETH, but with an uncertain input amount
1628
1684
 
@@ -1650,10 +1706,17 @@ var SwapRouter = /*#__PURE__*/function () {
1650
1706
  outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
1651
1707
  sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
1652
1708
  totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
1653
- totalAmountOut = _SwapRouter$encodeSwa2.totalAmountOut;
1709
+ quoteAmountOut = _SwapRouter$encodeSwa2.quoteAmountOut,
1710
+ minimumAmountOut = _SwapRouter$encodeSwa2.minimumAmountOut; // encode output token permit if necessary
1711
+
1712
+
1713
+ if (options.outputTokenPermit) {
1714
+ !quoteAmountOut.currency.isToken ? process.env.NODE_ENV !== "production" ? invariant(false, 'NON_TOKEN_PERMIT_OUTPUT') : invariant(false) : void 0;
1715
+ calldatas.push(SelfPermit.encodePermit(quoteAmountOut.currency, options.outputTokenPermit));
1716
+ }
1654
1717
 
1655
1718
  var chainId = sampleTrade.route.chainId;
1656
- var zeroForOne = position.pool.token0 === totalAmountSwapped.currency.wrapped;
1719
+ var zeroForOne = position.pool.token0.wrapped.address === totalAmountSwapped.currency.wrapped.address;
1657
1720
 
1658
1721
  var _SwapRouter$getPositi = SwapRouter.getPositionAmounts(position, zeroForOne),
1659
1722
  positionAmountIn = _SwapRouter$getPositi.positionAmountIn,
@@ -1663,7 +1726,7 @@ var SwapRouter = /*#__PURE__*/function () {
1663
1726
  var tokenIn = inputIsNative ? WETH9[chainId] : positionAmountIn.currency.wrapped;
1664
1727
  var tokenOut = outputIsNative ? WETH9[chainId] : positionAmountOut.currency.wrapped; // if swap output does not make up whole outputTokenBalanceDesired, pull in remaining tokens for adding liquidity
1665
1728
 
1666
- var amountOutRemaining = positionAmountOut.subtract(totalAmountOut.wrapped);
1729
+ var amountOutRemaining = positionAmountOut.subtract(quoteAmountOut.wrapped);
1667
1730
 
1668
1731
  if (amountOutRemaining.greaterThan(CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
1669
1732
  // if output is native, this means the remaining portion is included as native value in the transaction
@@ -1675,9 +1738,19 @@ var SwapRouter = /*#__PURE__*/function () {
1675
1738
  inputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(positionAmountIn.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenIn, positionAmountIn.quotient)); // approve token balances to NFTManager
1676
1739
 
1677
1740
  if (tokenInApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
1678
- if (tokenOutApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // encode NFTManager add liquidity
1741
+ if (tokenOutApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // represents a position with token amounts resulting from a swap with maximum slippage
1742
+ // hence the minimal amount out possible.
1743
+
1744
+ var minimalPosition = Position.fromAmounts({
1745
+ pool: position.pool,
1746
+ tickLower: position.tickLower,
1747
+ tickUpper: position.tickUpper,
1748
+ amount0: zeroForOne ? position.amount0.quotient.toString() : minimumAmountOut.quotient.toString(),
1749
+ amount1: zeroForOne ? minimumAmountOut.quotient.toString() : position.amount1.quotient.toString(),
1750
+ useFullPrecision: false
1751
+ }); // encode NFTManager add liquidity
1679
1752
 
1680
- calldatas.push(ApproveAndCall.encodeCallPositionManager([NonfungiblePositionManager.addCallParameters(position, addLiquidityOptions).calldata])); // sweep remaining tokens
1753
+ calldatas.push(ApproveAndCall.encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, options.slippageTolerance)); // sweep remaining tokens
1681
1754
 
1682
1755
  inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
1683
1756
  outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
@@ -1718,5 +1791,5 @@ var SwapRouter = /*#__PURE__*/function () {
1718
1791
  }();
1719
1792
  SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$3);
1720
1793
 
1721
- export { ADDRESS_THIS, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO };
1794
+ export { ADDRESS_THIS, ApprovalTypes, ApproveAndCall, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO, isMint };
1722
1795
  //# sourceMappingURL=router-sdk.esm.js.map