@uniswap/router-sdk 1.0.0-beta.7 → 1.0.2

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,18 +1,85 @@
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, Route as Route$1, Pool, Trade as Trade$1, encodeRouteToPath, SelfPermit } from '@uniswap/v3-sdk';
5
- import { validateAndParseAddress, TradeType, Fraction, CurrencyAmount, Price, Percent } from '@uniswap/sdk-core';
6
- import { abi as abi$1 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json';
7
- import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json';
8
- import { Route, Pair, Trade as Trade$2 } from '@uniswap/v2-sdk';
9
3
  import invariant from 'tiny-invariant';
4
+ import { abi } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json';
5
+ import { NonfungiblePositionManager, Multicall, toHex, Payments, Route as Route$1, Pool, Trade as Trade$1, encodeRouteToPath, SelfPermit } from '@uniswap/v3-sdk';
6
+ import { abi as abi$1 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json';
7
+ import { validateAndParseAddress, TradeType, Fraction, CurrencyAmount, Price, Percent, WETH9 } from '@uniswap/sdk-core';
8
+ import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json';
9
+ import { abi as abi$3 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json';
10
+ import { Route, Pair, Trade as Trade$2 } from '@uniswap/v2-sdk';
10
11
 
11
- var MSG_SENDER = '0x0000000000000000000000000000000000000000';
12
- var ADDRESS_THIS = '0x0000000000000000000000000000000000000001';
12
+ var MSG_SENDER = '0x0000000000000000000000000000000000000001';
13
+ var ADDRESS_THIS = '0x0000000000000000000000000000000000000002';
13
14
  var ZERO = /*#__PURE__*/JSBI.BigInt(0);
14
15
  var ONE = /*#__PURE__*/JSBI.BigInt(1);
15
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 = {}));
26
+
27
+ var ApproveAndCall = /*#__PURE__*/function () {
28
+ /**
29
+ * Cannot be constructed.
30
+ */
31
+ function ApproveAndCall() {}
32
+
33
+ ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
34
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
35
+ };
36
+
37
+ ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
38
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
39
+ };
40
+
41
+ ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
42
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
43
+ };
44
+
45
+ ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
46
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
47
+ };
48
+
49
+ ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
50
+ !(calldatas.length > 0) ? process.env.NODE_ENV !== "production" ? invariant(false, 'NULL_CALLDATA') : invariant(false) : void 0;
51
+
52
+ if (calldatas.length == 1) {
53
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
54
+ } else {
55
+ var encodedMulticall = NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
56
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
57
+ }
58
+ };
59
+
60
+ ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
61
+ switch (approvalType) {
62
+ case ApprovalTypes.MAX:
63
+ return ApproveAndCall.encodeApproveMax(token.wrapped);
64
+
65
+ case ApprovalTypes.MAX_MINUS_ONE:
66
+ return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
67
+
68
+ case ApprovalTypes.ZERO_THEN_MAX:
69
+ return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
70
+
71
+ case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
72
+ return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
73
+
74
+ default:
75
+ throw 'Error: invalid ApprovalType';
76
+ }
77
+ };
78
+
79
+ return ApproveAndCall;
80
+ }();
81
+ ApproveAndCall.INTERFACE = /*#__PURE__*/new Interface(abi);
82
+
16
83
  function validateAndParseBytes32(bytes32) {
17
84
  if (!bytes32.match(/^0x[0-9a-fA-F]{64}$/)) {
18
85
  throw new Error(bytes32 + " is not valid bytes32.");
@@ -50,7 +117,7 @@ var MulticallExtended = /*#__PURE__*/function () {
50
117
 
51
118
  return MulticallExtended;
52
119
  }();
53
- MulticallExtended.INTERFACE = /*#__PURE__*/new Interface(abi);
120
+ MulticallExtended.INTERFACE = /*#__PURE__*/new Interface(abi$1);
54
121
 
55
122
  function encodeFeeBips(fee) {
56
123
  return toHex(fee.multiply(10000).quotient);
@@ -102,7 +169,7 @@ var PaymentsExtended = /*#__PURE__*/function () {
102
169
 
103
170
  return PaymentsExtended;
104
171
  }();
105
- PaymentsExtended.INTERFACE = /*#__PURE__*/new Interface(abi$1);
172
+ PaymentsExtended.INTERFACE = /*#__PURE__*/new Interface(abi$2);
106
173
 
107
174
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
108
175
  try {
@@ -210,13 +277,6 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
210
277
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
211
278
  }
212
279
 
213
- var Protocol;
214
-
215
- (function (Protocol) {
216
- Protocol["V2"] = "V2";
217
- Protocol["V3"] = "V3";
218
- })(Protocol || (Protocol = {}));
219
-
220
280
  function createCommonjsModule(fn, module) {
221
281
  return module = { exports: {} }, fn(module, module.exports), module.exports;
222
282
  }
@@ -977,6 +1037,13 @@ try {
977
1037
  }
978
1038
  });
979
1039
 
1040
+ var Protocol;
1041
+
1042
+ (function (Protocol) {
1043
+ Protocol["V2"] = "V2";
1044
+ Protocol["V3"] = "V3";
1045
+ })(Protocol || (Protocol = {}));
1046
+
980
1047
  var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
981
1048
  _inheritsLoose(RouteV2, _V2RouteSDK);
982
1049
 
@@ -1343,6 +1410,7 @@ var Trade = /*#__PURE__*/function () {
1343
1410
  return Trade;
1344
1411
  }();
1345
1412
 
1413
+ var ZERO$1 = /*#__PURE__*/JSBI.BigInt(0);
1346
1414
  /**
1347
1415
  * Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
1348
1416
  */
@@ -1432,15 +1500,9 @@ var SwapRouter = /*#__PURE__*/function () {
1432
1500
  }
1433
1501
 
1434
1502
  return calldatas;
1435
- }
1436
- /**
1437
- * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
1438
- * @param trade to produce call parameters for
1439
- * @param options options for the call parameters
1440
- */
1441
- ;
1503
+ };
1442
1504
 
1443
- SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
1505
+ SwapRouter.encodeSwaps = function encodeSwaps(trades, options, isSwapAndAdd) {
1444
1506
  // If dealing with an instance of the aggregated Trade object, unbundle it to individual V2Trade and V3Trade objects.
1445
1507
  if (trades instanceof Trade) {
1446
1508
  !trades.swaps.every(function (swap) {
@@ -1488,16 +1550,8 @@ var SwapRouter = /*#__PURE__*/function () {
1488
1550
  return trade.tradeType === sampleTrade.tradeType;
1489
1551
  }) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TRADE_TYPE_DIFF') : invariant(false) : void 0;
1490
1552
  var calldatas = [];
1491
- var ZERO_IN = CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1492
- var ZERO_OUT = CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1493
- var totalAmountOut = trades.reduce(function (sum, trade) {
1494
- return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1495
- }, ZERO_OUT);
1496
1553
  var inputIsNative = sampleTrade.inputAmount.currency.isNative;
1497
- var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether a refund needs to happen
1498
- // 1. when paying in ETH, but with an uncertain input amount
1499
-
1500
- var mustRefund = inputIsNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT; // flag for whether we want to perform an aggregated slippage check
1554
+ var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether we want to perform an aggregated slippage check
1501
1555
  // 1. when there are >2 exact input trades. this is only a heuristic,
1502
1556
  // as it's still more gas-expensive even in this case, but has benefits
1503
1557
  // in that the reversion probability is lower
@@ -1505,12 +1559,10 @@ var SwapRouter = /*#__PURE__*/function () {
1505
1559
  var performAggregatedSlippageCheck = sampleTrade.tradeType === TradeType.EXACT_INPUT && numberOfTrades > 2; // flag for whether funds should be send first to the router
1506
1560
  // 1. when receiving ETH (which much be unwrapped from WETH)
1507
1561
  // 2. when a fee on the output is being taken
1508
- // 3. when performing an aggregated slippage check
1562
+ // 3. when performing swap and add
1563
+ // 4. when performing an aggregated slippage check
1509
1564
 
1510
- var routerMustCustody = outputIsNative || !!options.fee || performAggregatedSlippageCheck;
1511
- var totalValue = inputIsNative ? trades.reduce(function (sum, trade) {
1512
- return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1513
- }, ZERO_IN) : ZERO_IN; // encode permit if necessary
1565
+ var routerMustCustody = outputIsNative || !!options.fee || !!isSwapAndAdd || performAggregatedSlippageCheck; // encode permit if necessary
1514
1566
 
1515
1567
  if (options.inputTokenPermit) {
1516
1568
  !sampleTrade.inputAmount.currency.isToken ? process.env.NODE_ENV !== "production" ? invariant(false, 'NON_TOKEN_PERMIT') : invariant(false) : void 0;
@@ -1528,7 +1580,42 @@ var SwapRouter = /*#__PURE__*/function () {
1528
1580
  calldatas.push(calldata);
1529
1581
  }
1530
1582
  }
1531
- } // unwrap or sweep
1583
+ }
1584
+
1585
+ var ZERO_IN = CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1586
+ var ZERO_OUT = CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1587
+ var totalAmountOut = trades.reduce(function (sum, trade) {
1588
+ return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1589
+ }, ZERO_OUT);
1590
+ var totalAmountIn = trades.reduce(function (sum, trade) {
1591
+ return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1592
+ }, ZERO_IN);
1593
+ return {
1594
+ calldatas: calldatas,
1595
+ sampleTrade: sampleTrade,
1596
+ routerMustCustody: routerMustCustody,
1597
+ inputIsNative: inputIsNative,
1598
+ outputIsNative: outputIsNative,
1599
+ totalAmountIn: totalAmountIn,
1600
+ totalAmountOut: totalAmountOut
1601
+ };
1602
+ }
1603
+ /**
1604
+ * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
1605
+ * @param trade to produce call parameters for
1606
+ * @param options options for the call parameters
1607
+ */
1608
+ ;
1609
+
1610
+ SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
1611
+ var _SwapRouter$encodeSwa = SwapRouter.encodeSwaps(trades, options),
1612
+ calldatas = _SwapRouter$encodeSwa.calldatas,
1613
+ sampleTrade = _SwapRouter$encodeSwa.sampleTrade,
1614
+ routerMustCustody = _SwapRouter$encodeSwa.routerMustCustody,
1615
+ inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
1616
+ outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
1617
+ totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
1618
+ totalAmountOut = _SwapRouter$encodeSwa.totalAmountOut; // unwrap or sweep
1532
1619
 
1533
1620
 
1534
1621
  if (routerMustCustody) {
@@ -1537,22 +1624,99 @@ var SwapRouter = /*#__PURE__*/function () {
1537
1624
  } else {
1538
1625
  calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, totalAmountOut.quotient, options.recipient, options.fee));
1539
1626
  }
1540
- } // refund
1627
+ } // must refund when paying in ETH, but with an uncertain input amount
1541
1628
 
1542
1629
 
1543
- if (mustRefund) {
1630
+ if (inputIsNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT) {
1544
1631
  calldatas.push(Payments.encodeRefundETH());
1545
1632
  }
1546
1633
 
1547
1634
  return {
1548
1635
  calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
1549
- value: toHex(totalValue.quotient)
1636
+ value: toHex(inputIsNative ? totalAmountIn.quotient : ZERO$1)
1637
+ };
1638
+ }
1639
+ /**
1640
+ * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
1641
+ * @param trade to produce call parameters for
1642
+ * @param options options for the call parameters
1643
+ */
1644
+ ;
1645
+
1646
+ SwapRouter.swapAndAddCallParameters = function swapAndAddCallParameters(trades, options, position, addLiquidityOptions, tokenInApprovalType, tokenOutApprovalType) {
1647
+ var _SwapRouter$encodeSwa2 = SwapRouter.encodeSwaps(trades, options, true),
1648
+ calldatas = _SwapRouter$encodeSwa2.calldatas,
1649
+ inputIsNative = _SwapRouter$encodeSwa2.inputIsNative,
1650
+ outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
1651
+ sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
1652
+ totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
1653
+ totalAmountOut = _SwapRouter$encodeSwa2.totalAmountOut;
1654
+
1655
+ var chainId = sampleTrade.route.chainId;
1656
+ var zeroForOne = position.pool.token0 === totalAmountSwapped.currency.wrapped;
1657
+
1658
+ var _SwapRouter$getPositi = SwapRouter.getPositionAmounts(position, zeroForOne),
1659
+ positionAmountIn = _SwapRouter$getPositi.positionAmountIn,
1660
+ positionAmountOut = _SwapRouter$getPositi.positionAmountOut; // if tokens are native they will be converted to WETH9
1661
+
1662
+
1663
+ var tokenIn = inputIsNative ? WETH9[chainId] : positionAmountIn.currency.wrapped;
1664
+ 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
+
1666
+ var amountOutRemaining = positionAmountOut.subtract(totalAmountOut.wrapped);
1667
+
1668
+ if (amountOutRemaining.greaterThan(CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
1669
+ // if output is native, this means the remaining portion is included as native value in the transaction
1670
+ // and must be wrapped. Otherwise, pull in remaining ERC20 token.
1671
+ outputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(amountOutRemaining.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenOut, amountOutRemaining.quotient));
1672
+ } // if input is native, convert to WETH9, else pull ERC20 token
1673
+
1674
+
1675
+ inputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(positionAmountIn.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenIn, positionAmountIn.quotient)); // approve token balances to NFTManager
1676
+
1677
+ 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
1679
+
1680
+ calldatas.push(ApproveAndCall.encodeCallPositionManager([NonfungiblePositionManager.addCallParameters(position, addLiquidityOptions).calldata])); // sweep remaining tokens
1681
+
1682
+ inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
1683
+ outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
1684
+ var value;
1685
+
1686
+ if (inputIsNative) {
1687
+ value = totalAmountSwapped.wrapped.add(positionAmountIn.wrapped).quotient;
1688
+ } else if (outputIsNative) {
1689
+ value = amountOutRemaining.quotient;
1690
+ } else {
1691
+ value = ZERO$1;
1692
+ }
1693
+
1694
+ return {
1695
+ calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
1696
+ value: value.toString()
1697
+ };
1698
+ };
1699
+
1700
+ SwapRouter.getPositionAmounts = function getPositionAmounts(position, zeroForOne) {
1701
+ var _position$mintAmounts = position.mintAmounts,
1702
+ amount0 = _position$mintAmounts.amount0,
1703
+ amount1 = _position$mintAmounts.amount1;
1704
+ var currencyAmount0 = CurrencyAmount.fromRawAmount(position.pool.token0, amount0);
1705
+ var currencyAmount1 = CurrencyAmount.fromRawAmount(position.pool.token1, amount1);
1706
+
1707
+ var _ref = zeroForOne ? [currencyAmount0, currencyAmount1] : [currencyAmount1, currencyAmount0],
1708
+ positionAmountIn = _ref[0],
1709
+ positionAmountOut = _ref[1];
1710
+
1711
+ return {
1712
+ positionAmountIn: positionAmountIn,
1713
+ positionAmountOut: positionAmountOut
1550
1714
  };
1551
1715
  };
1552
1716
 
1553
1717
  return SwapRouter;
1554
1718
  }();
1555
- SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$2);
1719
+ SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$3);
1556
1720
 
1557
- export { ADDRESS_THIS, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO };
1721
+ export { ADDRESS_THIS, ApprovalTypes, ApproveAndCall, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO };
1558
1722
  //# sourceMappingURL=router-sdk.esm.js.map