@uniswap/router-sdk 1.0.0-beta.8 → 1.0.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.
@@ -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,6 @@ 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
+ static encodeAddLiquidity(position: Position, addLiquidityOptions: CondensedAddLiquidityOptions, slippageTolerance: Percent): string;
21
25
  static encodeApprove(token: Currency, approvalType: ApprovalTypes): string;
22
26
  }
@@ -1,5 +1,5 @@
1
1
  import JSBI from 'jsbi';
2
- export declare const MSG_SENDER = "0x0000000000000000000000000000000000000000";
3
- export declare const ADDRESS_THIS = "0x0000000000000000000000000000000000000001";
2
+ export declare const MSG_SENDER = "0x0000000000000000000000000000000000000001";
3
+ export declare const ADDRESS_THIS = "0x0000000000000000000000000000000000000002";
4
4
  export declare const ZERO: JSBI;
5
5
  export declare const ONE: JSBI;
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,117 @@ 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
- var MSG_SENDER = '0x0000000000000000000000000000000000000000';
19
- var ADDRESS_THIS = '0x0000000000000000000000000000000000000001';
18
+ var MSG_SENDER = '0x0000000000000000000000000000000000000001';
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
+ ApproveAndCall.encodeAddLiquidity = function encodeAddLiquidity(position, addLiquidityOptions, slippageTolerance) {
71
+ var _position$mintAmounts = position.mintAmountsWithSlippage(slippageTolerance),
72
+ amount0Min = _position$mintAmounts.amount0,
73
+ amount1Min = _position$mintAmounts.amount1;
74
+
75
+ if (isMint(addLiquidityOptions)) {
76
+ return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [{
77
+ token0: position.pool.token0.address,
78
+ token1: position.pool.token1.address,
79
+ fee: position.pool.fee,
80
+ tickLower: position.tickLower,
81
+ tickUpper: position.tickUpper,
82
+ amount0Min: v3Sdk.toHex(amount0Min),
83
+ amount1Min: v3Sdk.toHex(amount1Min),
84
+ recipient: addLiquidityOptions.recipient
85
+ }]);
86
+ } else {
87
+ return ApproveAndCall.INTERFACE.encodeFunctionData('increaseLiquidity', [{
88
+ token0: position.pool.token0.address,
89
+ token1: position.pool.token1.address,
90
+ amount0Min: v3Sdk.toHex(amount0Min),
91
+ amount1Min: v3Sdk.toHex(amount1Min),
92
+ tokenId: v3Sdk.toHex(addLiquidityOptions.tokenId)
93
+ }]);
94
+ }
95
+ };
96
+
97
+ ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
98
+ switch (approvalType) {
99
+ case exports.ApprovalTypes.MAX:
100
+ return ApproveAndCall.encodeApproveMax(token.wrapped);
101
+
102
+ case exports.ApprovalTypes.MAX_MINUS_ONE:
103
+ return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
104
+
105
+ case exports.ApprovalTypes.ZERO_THEN_MAX:
106
+ return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
107
+
108
+ case exports.ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
109
+ return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
110
+
111
+ default:
112
+ throw 'Error: invalid ApprovalType';
113
+ }
114
+ };
115
+
116
+ return ApproveAndCall;
117
+ }();
118
+ ApproveAndCall.INTERFACE = /*#__PURE__*/new abi.Interface(IApproveAndCall_json.abi);
119
+
23
120
  function validateAndParseBytes32(bytes32) {
24
121
  if (!bytes32.match(/^0x[0-9a-fA-F]{64}$/)) {
25
122
  throw new Error(bytes32 + " is not valid bytes32.");
@@ -217,72 +314,6 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
217
314
  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
315
  }
219
316
 
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
317
  function createCommonjsModule(fn, module) {
287
318
  return module = { exports: {} }, fn(module, module.exports), module.exports;
288
319
  }
@@ -1588,9 +1619,12 @@ var SwapRouter = /*#__PURE__*/function () {
1588
1619
 
1589
1620
  var ZERO_IN = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1590
1621
  var ZERO_OUT = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1591
- var totalAmountOut = trades.reduce(function (sum, trade) {
1622
+ var minimumAmountOut = trades.reduce(function (sum, trade) {
1592
1623
  return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1593
1624
  }, ZERO_OUT);
1625
+ var quoteAmountOut = trades.reduce(function (sum, trade) {
1626
+ return sum.add(trade.outputAmount);
1627
+ }, ZERO_OUT);
1594
1628
  var totalAmountIn = trades.reduce(function (sum, trade) {
1595
1629
  return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1596
1630
  }, ZERO_IN);
@@ -1601,7 +1635,8 @@ var SwapRouter = /*#__PURE__*/function () {
1601
1635
  inputIsNative: inputIsNative,
1602
1636
  outputIsNative: outputIsNative,
1603
1637
  totalAmountIn: totalAmountIn,
1604
- totalAmountOut: totalAmountOut
1638
+ minimumAmountOut: minimumAmountOut,
1639
+ quoteAmountOut: quoteAmountOut
1605
1640
  };
1606
1641
  }
1607
1642
  /**
@@ -1619,14 +1654,14 @@ var SwapRouter = /*#__PURE__*/function () {
1619
1654
  inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
1620
1655
  outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
1621
1656
  totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
1622
- totalAmountOut = _SwapRouter$encodeSwa.totalAmountOut; // unwrap or sweep
1657
+ minimumAmountOut = _SwapRouter$encodeSwa.minimumAmountOut; // unwrap or sweep
1623
1658
 
1624
1659
 
1625
1660
  if (routerMustCustody) {
1626
1661
  if (outputIsNative) {
1627
- calldatas.push(PaymentsExtended.encodeUnwrapWETH9(totalAmountOut.quotient, options.recipient, options.fee));
1662
+ calldatas.push(PaymentsExtended.encodeUnwrapWETH9(minimumAmountOut.quotient, options.recipient, options.fee));
1628
1663
  } else {
1629
- calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, totalAmountOut.quotient, options.recipient, options.fee));
1664
+ calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, minimumAmountOut.quotient, options.recipient, options.fee));
1630
1665
  }
1631
1666
  } // must refund when paying in ETH, but with an uncertain input amount
1632
1667
 
@@ -1654,7 +1689,13 @@ var SwapRouter = /*#__PURE__*/function () {
1654
1689
  outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
1655
1690
  sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
1656
1691
  totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
1657
- totalAmountOut = _SwapRouter$encodeSwa2.totalAmountOut;
1692
+ quoteAmountOut = _SwapRouter$encodeSwa2.quoteAmountOut; // encode output token permit if necessary
1693
+
1694
+
1695
+ if (options.outputTokenPermit) {
1696
+ !quoteAmountOut.currency.isToken ? invariant(false, 'NON_TOKEN_PERMIT_OUTPUT') : void 0;
1697
+ calldatas.push(v3Sdk.SelfPermit.encodePermit(quoteAmountOut.currency, options.outputTokenPermit));
1698
+ }
1658
1699
 
1659
1700
  var chainId = sampleTrade.route.chainId;
1660
1701
  var zeroForOne = position.pool.token0 === totalAmountSwapped.currency.wrapped;
@@ -1667,7 +1708,7 @@ var SwapRouter = /*#__PURE__*/function () {
1667
1708
  var tokenIn = inputIsNative ? sdkCore.WETH9[chainId] : positionAmountIn.currency.wrapped;
1668
1709
  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
1710
 
1670
- var amountOutRemaining = positionAmountOut.subtract(totalAmountOut.wrapped);
1711
+ var amountOutRemaining = positionAmountOut.subtract(quoteAmountOut.wrapped);
1671
1712
 
1672
1713
  if (amountOutRemaining.greaterThan(sdkCore.CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
1673
1714
  // if output is native, this means the remaining portion is included as native value in the transaction
@@ -1678,10 +1719,10 @@ var SwapRouter = /*#__PURE__*/function () {
1678
1719
 
1679
1720
  inputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(positionAmountIn.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenIn, positionAmountIn.quotient)); // approve token balances to NFTManager
1680
1721
 
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
1722
+ if (tokenInApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
1723
+ if (tokenOutApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // encode NFTManager add liquidity
1683
1724
 
1684
- calldatas.push(ApproveAndCall.encodeCallPositionManager([v3Sdk.NonfungiblePositionManager.addCallParameters(position, addLiquidityOptions).calldata])); // sweep remaining tokens
1725
+ calldatas.push(ApproveAndCall.encodeAddLiquidity(position, addLiquidityOptions, options.slippageTolerance)); // sweep remaining tokens
1685
1726
 
1686
1727
  inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
1687
1728
  outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
@@ -1723,6 +1764,7 @@ var SwapRouter = /*#__PURE__*/function () {
1723
1764
  SwapRouter.INTERFACE = /*#__PURE__*/new abi.Interface(ISwapRouter02_json.abi);
1724
1765
 
1725
1766
  exports.ADDRESS_THIS = ADDRESS_THIS;
1767
+ exports.ApproveAndCall = ApproveAndCall;
1726
1768
  exports.MSG_SENDER = MSG_SENDER;
1727
1769
  exports.MulticallExtended = MulticallExtended;
1728
1770
  exports.ONE = ONE;
@@ -1732,4 +1774,5 @@ exports.RouteV3 = RouteV3;
1732
1774
  exports.SwapRouter = SwapRouter;
1733
1775
  exports.Trade = Trade;
1734
1776
  exports.ZERO = ZERO;
1777
+ exports.isMint = isMint;
1735
1778
  //# sourceMappingURL=router-sdk.cjs.development.js.map