@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,5 +1,7 @@
1
1
  import { Interface } from '@ethersproject/abi';
2
- import { Currency, Token } from '@uniswap/sdk-core';
2
+ import { Currency, Percent, Token } from '@uniswap/sdk-core';
3
+ import { MintSpecificOptions, IncreaseSpecificOptions, Position } from '@uniswap/v3-sdk';
4
+ export declare type CondensedAddLiquidityOptions = Omit<MintSpecificOptions, 'createPool'> | IncreaseSpecificOptions;
3
5
  export declare enum ApprovalTypes {
4
6
  NOT_REQUIRED = 0,
5
7
  MAX = 1,
@@ -7,6 +9,7 @@ export declare enum ApprovalTypes {
7
9
  ZERO_THEN_MAX = 3,
8
10
  ZERO_THEN_MAX_MINUS_ONE = 4
9
11
  }
12
+ export declare function isMint(options: CondensedAddLiquidityOptions): options is Omit<MintSpecificOptions, 'createPool'>;
10
13
  export declare abstract class ApproveAndCall {
11
14
  static INTERFACE: Interface;
12
15
  /**
@@ -18,5 +21,13 @@ export declare abstract class ApproveAndCall {
18
21
  static encodeApproveZeroThenMax(token: Token): string;
19
22
  static encodeApproveZeroThenMaxMinusOne(token: Token): string;
20
23
  static encodeCallPositionManager(calldatas: string[]): string;
24
+ /**
25
+ * Encode adding liquidity to a position in the nft manager contract
26
+ * @param position Forcasted position with expected amount out from swap
27
+ * @param minimalPosition Forcasted position with custom minimal token amounts
28
+ * @param addLiquidityOptions Options for adding liquidity
29
+ * @param slippageTolerance Defines maximum slippage
30
+ */
31
+ static encodeAddLiquidity(position: Position, minimalPosition: Position, addLiquidityOptions: CondensedAddLiquidityOptions, slippageTolerance: Percent): string;
21
32
  static encodeApprove(token: Currency, approvalType: ApprovalTypes): string;
22
33
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './constants';
2
+ export * from './approveAndCall';
2
3
  export * from './multicallExtended';
3
4
  export * from './paymentsExtended';
4
5
  export * from './swapRouter';
@@ -6,20 +6,136 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
6
6
 
7
7
  var JSBI = _interopDefault(require('jsbi'));
8
8
  var abi = require('@ethersproject/abi');
9
- var IMulticallExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json');
9
+ var invariant = _interopDefault(require('tiny-invariant'));
10
+ var IApproveAndCall_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json');
10
11
  var v3Sdk = require('@uniswap/v3-sdk');
12
+ var IMulticallExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json');
11
13
  var sdkCore = require('@uniswap/sdk-core');
12
14
  var IPeripheryPaymentsWithFeeExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json');
13
15
  var ISwapRouter02_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json');
14
16
  var v2Sdk = require('@uniswap/v2-sdk');
15
- var invariant = _interopDefault(require('tiny-invariant'));
16
- var IApproveAndCall_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json');
17
17
 
18
18
  var MSG_SENDER = '0x0000000000000000000000000000000000000001';
19
19
  var ADDRESS_THIS = '0x0000000000000000000000000000000000000002';
20
20
  var ZERO = /*#__PURE__*/JSBI.BigInt(0);
21
21
  var ONE = /*#__PURE__*/JSBI.BigInt(1);
22
22
 
23
+ (function (ApprovalTypes) {
24
+ ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
25
+ ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
26
+ ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
27
+ ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
28
+ ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
29
+ })(exports.ApprovalTypes || (exports.ApprovalTypes = {})); // type guard
30
+
31
+
32
+ function isMint(options) {
33
+ return Object.keys(options).some(function (k) {
34
+ return k === 'recipient';
35
+ });
36
+ }
37
+ var ApproveAndCall = /*#__PURE__*/function () {
38
+ /**
39
+ * Cannot be constructed.
40
+ */
41
+ function ApproveAndCall() {}
42
+
43
+ ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
44
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
45
+ };
46
+
47
+ ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
48
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
49
+ };
50
+
51
+ ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
52
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
53
+ };
54
+
55
+ ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
56
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
57
+ };
58
+
59
+ ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
60
+ !(calldatas.length > 0) ? invariant(false, 'NULL_CALLDATA') : void 0;
61
+
62
+ if (calldatas.length == 1) {
63
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
64
+ } else {
65
+ var encodedMulticall = v3Sdk.NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
66
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
67
+ }
68
+ }
69
+ /**
70
+ * Encode adding liquidity to a position in the nft manager contract
71
+ * @param position Forcasted position with expected amount out from swap
72
+ * @param minimalPosition Forcasted position with custom minimal token amounts
73
+ * @param addLiquidityOptions Options for adding liquidity
74
+ * @param slippageTolerance Defines maximum slippage
75
+ */
76
+ ;
77
+
78
+ ApproveAndCall.encodeAddLiquidity = function encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, slippageTolerance) {
79
+ var _position$mintAmounts = position.mintAmountsWithSlippage(slippageTolerance),
80
+ amount0Min = _position$mintAmounts.amount0,
81
+ amount1Min = _position$mintAmounts.amount1; // position.mintAmountsWithSlippage() can create amounts not dependenable in scenarios
82
+ // such as range orders. Allow the option to provide a position with custom minimum amounts
83
+ // for these scenarios
84
+
85
+
86
+ if (JSBI.lessThan(minimalPosition.amount0.quotient, amount0Min)) {
87
+ amount0Min = minimalPosition.amount0.quotient;
88
+ }
89
+
90
+ if (JSBI.lessThan(minimalPosition.amount1.quotient, amount1Min)) {
91
+ amount1Min = minimalPosition.amount1.quotient;
92
+ }
93
+
94
+ if (isMint(addLiquidityOptions)) {
95
+ return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [{
96
+ token0: position.pool.token0.address,
97
+ token1: position.pool.token1.address,
98
+ fee: position.pool.fee,
99
+ tickLower: position.tickLower,
100
+ tickUpper: position.tickUpper,
101
+ amount0Min: v3Sdk.toHex(amount0Min),
102
+ amount1Min: v3Sdk.toHex(amount1Min),
103
+ recipient: addLiquidityOptions.recipient
104
+ }]);
105
+ } else {
106
+ return ApproveAndCall.INTERFACE.encodeFunctionData('increaseLiquidity', [{
107
+ token0: position.pool.token0.address,
108
+ token1: position.pool.token1.address,
109
+ amount0Min: v3Sdk.toHex(amount0Min),
110
+ amount1Min: v3Sdk.toHex(amount1Min),
111
+ tokenId: v3Sdk.toHex(addLiquidityOptions.tokenId)
112
+ }]);
113
+ }
114
+ };
115
+
116
+ ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
117
+ switch (approvalType) {
118
+ case exports.ApprovalTypes.MAX:
119
+ return ApproveAndCall.encodeApproveMax(token.wrapped);
120
+
121
+ case exports.ApprovalTypes.MAX_MINUS_ONE:
122
+ return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
123
+
124
+ case exports.ApprovalTypes.ZERO_THEN_MAX:
125
+ return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
126
+
127
+ case exports.ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
128
+ return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
129
+
130
+ default:
131
+ throw 'Error: invalid ApprovalType';
132
+ }
133
+ };
134
+
135
+ return ApproveAndCall;
136
+ }();
137
+ ApproveAndCall.INTERFACE = /*#__PURE__*/new abi.Interface(IApproveAndCall_json.abi);
138
+
23
139
  function validateAndParseBytes32(bytes32) {
24
140
  if (!bytes32.match(/^0x[0-9a-fA-F]{64}$/)) {
25
141
  throw new Error(bytes32 + " is not valid bytes32.");
@@ -217,72 +333,6 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
217
333
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
218
334
  }
219
335
 
220
- var ApprovalTypes;
221
-
222
- (function (ApprovalTypes) {
223
- ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
224
- ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
225
- ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
226
- ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
227
- ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
228
- })(ApprovalTypes || (ApprovalTypes = {}));
229
-
230
- var ApproveAndCall = /*#__PURE__*/function () {
231
- /**
232
- * Cannot be constructed.
233
- */
234
- function ApproveAndCall() {}
235
-
236
- ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
237
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
238
- };
239
-
240
- ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
241
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
242
- };
243
-
244
- ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
245
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
246
- };
247
-
248
- ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
249
- return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
250
- };
251
-
252
- ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
253
- !(calldatas.length > 0) ? invariant(false, 'NULL_CALLDATA') : void 0;
254
-
255
- if (calldatas.length == 1) {
256
- return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
257
- } else {
258
- var encodedMulticall = v3Sdk.NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
259
- return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
260
- }
261
- };
262
-
263
- ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
264
- switch (approvalType) {
265
- case ApprovalTypes.MAX:
266
- return ApproveAndCall.encodeApproveMax(token.wrapped);
267
-
268
- case ApprovalTypes.MAX_MINUS_ONE:
269
- return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
270
-
271
- case ApprovalTypes.ZERO_THEN_MAX:
272
- return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
273
-
274
- case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
275
- return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
276
-
277
- default:
278
- throw 'Error: invalid ApprovalType';
279
- }
280
- };
281
-
282
- return ApproveAndCall;
283
- }();
284
- ApproveAndCall.INTERFACE = /*#__PURE__*/new abi.Interface(IApproveAndCall_json.abi);
285
-
286
336
  function createCommonjsModule(fn, module) {
287
337
  return module = { exports: {} }, fn(module, module.exports), module.exports;
288
338
  }
@@ -1588,9 +1638,12 @@ var SwapRouter = /*#__PURE__*/function () {
1588
1638
 
1589
1639
  var ZERO_IN = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1590
1640
  var ZERO_OUT = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1591
- var totalAmountOut = trades.reduce(function (sum, trade) {
1641
+ var minimumAmountOut = trades.reduce(function (sum, trade) {
1592
1642
  return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1593
1643
  }, ZERO_OUT);
1644
+ var quoteAmountOut = trades.reduce(function (sum, trade) {
1645
+ return sum.add(trade.outputAmount);
1646
+ }, ZERO_OUT);
1594
1647
  var totalAmountIn = trades.reduce(function (sum, trade) {
1595
1648
  return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1596
1649
  }, ZERO_IN);
@@ -1601,7 +1654,8 @@ var SwapRouter = /*#__PURE__*/function () {
1601
1654
  inputIsNative: inputIsNative,
1602
1655
  outputIsNative: outputIsNative,
1603
1656
  totalAmountIn: totalAmountIn,
1604
- totalAmountOut: totalAmountOut
1657
+ minimumAmountOut: minimumAmountOut,
1658
+ quoteAmountOut: quoteAmountOut
1605
1659
  };
1606
1660
  }
1607
1661
  /**
@@ -1619,14 +1673,14 @@ var SwapRouter = /*#__PURE__*/function () {
1619
1673
  inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
1620
1674
  outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
1621
1675
  totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
1622
- totalAmountOut = _SwapRouter$encodeSwa.totalAmountOut; // unwrap or sweep
1676
+ minimumAmountOut = _SwapRouter$encodeSwa.minimumAmountOut; // unwrap or sweep
1623
1677
 
1624
1678
 
1625
1679
  if (routerMustCustody) {
1626
1680
  if (outputIsNative) {
1627
- calldatas.push(PaymentsExtended.encodeUnwrapWETH9(totalAmountOut.quotient, options.recipient, options.fee));
1681
+ calldatas.push(PaymentsExtended.encodeUnwrapWETH9(minimumAmountOut.quotient, options.recipient, options.fee));
1628
1682
  } else {
1629
- calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, totalAmountOut.quotient, options.recipient, options.fee));
1683
+ calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, minimumAmountOut.quotient, options.recipient, options.fee));
1630
1684
  }
1631
1685
  } // must refund when paying in ETH, but with an uncertain input amount
1632
1686
 
@@ -1654,10 +1708,17 @@ var SwapRouter = /*#__PURE__*/function () {
1654
1708
  outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
1655
1709
  sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
1656
1710
  totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
1657
- totalAmountOut = _SwapRouter$encodeSwa2.totalAmountOut;
1711
+ quoteAmountOut = _SwapRouter$encodeSwa2.quoteAmountOut,
1712
+ minimumAmountOut = _SwapRouter$encodeSwa2.minimumAmountOut; // encode output token permit if necessary
1713
+
1714
+
1715
+ if (options.outputTokenPermit) {
1716
+ !quoteAmountOut.currency.isToken ? invariant(false, 'NON_TOKEN_PERMIT_OUTPUT') : void 0;
1717
+ calldatas.push(v3Sdk.SelfPermit.encodePermit(quoteAmountOut.currency, options.outputTokenPermit));
1718
+ }
1658
1719
 
1659
1720
  var chainId = sampleTrade.route.chainId;
1660
- var zeroForOne = position.pool.token0 === totalAmountSwapped.currency.wrapped;
1721
+ var zeroForOne = position.pool.token0.wrapped.address === totalAmountSwapped.currency.wrapped.address;
1661
1722
 
1662
1723
  var _SwapRouter$getPositi = SwapRouter.getPositionAmounts(position, zeroForOne),
1663
1724
  positionAmountIn = _SwapRouter$getPositi.positionAmountIn,
@@ -1667,7 +1728,7 @@ var SwapRouter = /*#__PURE__*/function () {
1667
1728
  var tokenIn = inputIsNative ? sdkCore.WETH9[chainId] : positionAmountIn.currency.wrapped;
1668
1729
  var tokenOut = outputIsNative ? sdkCore.WETH9[chainId] : positionAmountOut.currency.wrapped; // if swap output does not make up whole outputTokenBalanceDesired, pull in remaining tokens for adding liquidity
1669
1730
 
1670
- var amountOutRemaining = positionAmountOut.subtract(totalAmountOut.wrapped);
1731
+ var amountOutRemaining = positionAmountOut.subtract(quoteAmountOut.wrapped);
1671
1732
 
1672
1733
  if (amountOutRemaining.greaterThan(sdkCore.CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
1673
1734
  // if output is native, this means the remaining portion is included as native value in the transaction
@@ -1678,10 +1739,20 @@ var SwapRouter = /*#__PURE__*/function () {
1678
1739
 
1679
1740
  inputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(positionAmountIn.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenIn, positionAmountIn.quotient)); // approve token balances to NFTManager
1680
1741
 
1681
- if (tokenInApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
1682
- if (tokenOutApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // encode NFTManager add liquidity
1742
+ if (tokenInApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
1743
+ if (tokenOutApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // represents a position with token amounts resulting from a swap with maximum slippage
1744
+ // hence the minimal amount out possible.
1745
+
1746
+ var minimalPosition = v3Sdk.Position.fromAmounts({
1747
+ pool: position.pool,
1748
+ tickLower: position.tickLower,
1749
+ tickUpper: position.tickUpper,
1750
+ amount0: zeroForOne ? position.amount0.quotient.toString() : minimumAmountOut.quotient.toString(),
1751
+ amount1: zeroForOne ? minimumAmountOut.quotient.toString() : position.amount1.quotient.toString(),
1752
+ useFullPrecision: false
1753
+ }); // encode NFTManager add liquidity
1683
1754
 
1684
- calldatas.push(ApproveAndCall.encodeCallPositionManager([v3Sdk.NonfungiblePositionManager.addCallParameters(position, addLiquidityOptions).calldata])); // sweep remaining tokens
1755
+ calldatas.push(ApproveAndCall.encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, options.slippageTolerance)); // sweep remaining tokens
1685
1756
 
1686
1757
  inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
1687
1758
  outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
@@ -1723,6 +1794,7 @@ var SwapRouter = /*#__PURE__*/function () {
1723
1794
  SwapRouter.INTERFACE = /*#__PURE__*/new abi.Interface(ISwapRouter02_json.abi);
1724
1795
 
1725
1796
  exports.ADDRESS_THIS = ADDRESS_THIS;
1797
+ exports.ApproveAndCall = ApproveAndCall;
1726
1798
  exports.MSG_SENDER = MSG_SENDER;
1727
1799
  exports.MulticallExtended = MulticallExtended;
1728
1800
  exports.ONE = ONE;
@@ -1732,4 +1804,5 @@ exports.RouteV3 = RouteV3;
1732
1804
  exports.SwapRouter = SwapRouter;
1733
1805
  exports.Trade = Trade;
1734
1806
  exports.ZERO = ZERO;
1807
+ exports.isMint = isMint;
1735
1808
  //# sourceMappingURL=router-sdk.cjs.development.js.map