@uniswap/router-sdk 1.0.2 → 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.
@@ -2,7 +2,7 @@ import JSBI from 'jsbi';
2
2
  import { Interface } from '@ethersproject/abi';
3
3
  import invariant from 'tiny-invariant';
4
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';
5
+ import { NonfungiblePositionManager, toHex, Multicall, Payments, Route as Route$1, Pool, Trade as Trade$1, encodeRouteToPath, SelfPermit } from '@uniswap/v3-sdk';
6
6
  import { abi as abi$1 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json';
7
7
  import { validateAndParseAddress, TradeType, Fraction, CurrencyAmount, Price, Percent, WETH9 } from '@uniswap/sdk-core';
8
8
  import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json';
@@ -22,8 +22,14 @@ var ApprovalTypes;
22
22
  ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
23
23
  ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
24
24
  ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
25
- })(ApprovalTypes || (ApprovalTypes = {}));
25
+ })(ApprovalTypes || (ApprovalTypes = {})); // type guard
26
26
 
27
+
28
+ function isMint(options) {
29
+ return Object.keys(options).some(function (k) {
30
+ return k === 'recipient';
31
+ });
32
+ }
27
33
  var ApproveAndCall = /*#__PURE__*/function () {
28
34
  /**
29
35
  * Cannot be constructed.
@@ -57,6 +63,33 @@ var ApproveAndCall = /*#__PURE__*/function () {
57
63
  }
58
64
  };
59
65
 
66
+ ApproveAndCall.encodeAddLiquidity = function encodeAddLiquidity(position, addLiquidityOptions, slippageTolerance) {
67
+ var _position$mintAmounts = position.mintAmountsWithSlippage(slippageTolerance),
68
+ amount0Min = _position$mintAmounts.amount0,
69
+ amount1Min = _position$mintAmounts.amount1;
70
+
71
+ if (isMint(addLiquidityOptions)) {
72
+ return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [{
73
+ token0: position.pool.token0.address,
74
+ token1: position.pool.token1.address,
75
+ fee: position.pool.fee,
76
+ tickLower: position.tickLower,
77
+ tickUpper: position.tickUpper,
78
+ amount0Min: toHex(amount0Min),
79
+ amount1Min: toHex(amount1Min),
80
+ recipient: addLiquidityOptions.recipient
81
+ }]);
82
+ } else {
83
+ return ApproveAndCall.INTERFACE.encodeFunctionData('increaseLiquidity', [{
84
+ token0: position.pool.token0.address,
85
+ token1: position.pool.token1.address,
86
+ amount0Min: toHex(amount0Min),
87
+ amount1Min: toHex(amount1Min),
88
+ tokenId: toHex(addLiquidityOptions.tokenId)
89
+ }]);
90
+ }
91
+ };
92
+
60
93
  ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
61
94
  switch (approvalType) {
62
95
  case ApprovalTypes.MAX:
@@ -1584,9 +1617,12 @@ var SwapRouter = /*#__PURE__*/function () {
1584
1617
 
1585
1618
  var ZERO_IN = CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1586
1619
  var ZERO_OUT = CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1587
- var totalAmountOut = trades.reduce(function (sum, trade) {
1620
+ var minimumAmountOut = trades.reduce(function (sum, trade) {
1588
1621
  return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1589
1622
  }, ZERO_OUT);
1623
+ var quoteAmountOut = trades.reduce(function (sum, trade) {
1624
+ return sum.add(trade.outputAmount);
1625
+ }, ZERO_OUT);
1590
1626
  var totalAmountIn = trades.reduce(function (sum, trade) {
1591
1627
  return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1592
1628
  }, ZERO_IN);
@@ -1597,7 +1633,8 @@ var SwapRouter = /*#__PURE__*/function () {
1597
1633
  inputIsNative: inputIsNative,
1598
1634
  outputIsNative: outputIsNative,
1599
1635
  totalAmountIn: totalAmountIn,
1600
- totalAmountOut: totalAmountOut
1636
+ minimumAmountOut: minimumAmountOut,
1637
+ quoteAmountOut: quoteAmountOut
1601
1638
  };
1602
1639
  }
1603
1640
  /**
@@ -1615,14 +1652,14 @@ var SwapRouter = /*#__PURE__*/function () {
1615
1652
  inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
1616
1653
  outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
1617
1654
  totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
1618
- totalAmountOut = _SwapRouter$encodeSwa.totalAmountOut; // unwrap or sweep
1655
+ minimumAmountOut = _SwapRouter$encodeSwa.minimumAmountOut; // unwrap or sweep
1619
1656
 
1620
1657
 
1621
1658
  if (routerMustCustody) {
1622
1659
  if (outputIsNative) {
1623
- calldatas.push(PaymentsExtended.encodeUnwrapWETH9(totalAmountOut.quotient, options.recipient, options.fee));
1660
+ calldatas.push(PaymentsExtended.encodeUnwrapWETH9(minimumAmountOut.quotient, options.recipient, options.fee));
1624
1661
  } else {
1625
- calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, totalAmountOut.quotient, options.recipient, options.fee));
1662
+ calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, minimumAmountOut.quotient, options.recipient, options.fee));
1626
1663
  }
1627
1664
  } // must refund when paying in ETH, but with an uncertain input amount
1628
1665
 
@@ -1650,7 +1687,13 @@ var SwapRouter = /*#__PURE__*/function () {
1650
1687
  outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
1651
1688
  sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
1652
1689
  totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
1653
- totalAmountOut = _SwapRouter$encodeSwa2.totalAmountOut;
1690
+ quoteAmountOut = _SwapRouter$encodeSwa2.quoteAmountOut; // encode output token permit if necessary
1691
+
1692
+
1693
+ if (options.outputTokenPermit) {
1694
+ !quoteAmountOut.currency.isToken ? process.env.NODE_ENV !== "production" ? invariant(false, 'NON_TOKEN_PERMIT_OUTPUT') : invariant(false) : void 0;
1695
+ calldatas.push(SelfPermit.encodePermit(quoteAmountOut.currency, options.outputTokenPermit));
1696
+ }
1654
1697
 
1655
1698
  var chainId = sampleTrade.route.chainId;
1656
1699
  var zeroForOne = position.pool.token0 === totalAmountSwapped.currency.wrapped;
@@ -1663,7 +1706,7 @@ var SwapRouter = /*#__PURE__*/function () {
1663
1706
  var tokenIn = inputIsNative ? WETH9[chainId] : positionAmountIn.currency.wrapped;
1664
1707
  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
1708
 
1666
- var amountOutRemaining = positionAmountOut.subtract(totalAmountOut.wrapped);
1709
+ var amountOutRemaining = positionAmountOut.subtract(quoteAmountOut.wrapped);
1667
1710
 
1668
1711
  if (amountOutRemaining.greaterThan(CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
1669
1712
  // if output is native, this means the remaining portion is included as native value in the transaction
@@ -1677,7 +1720,7 @@ var SwapRouter = /*#__PURE__*/function () {
1677
1720
  if (tokenInApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
1678
1721
  if (tokenOutApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // encode NFTManager add liquidity
1679
1722
 
1680
- calldatas.push(ApproveAndCall.encodeCallPositionManager([NonfungiblePositionManager.addCallParameters(position, addLiquidityOptions).calldata])); // sweep remaining tokens
1723
+ calldatas.push(ApproveAndCall.encodeAddLiquidity(position, addLiquidityOptions, options.slippageTolerance)); // sweep remaining tokens
1681
1724
 
1682
1725
  inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
1683
1726
  outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
@@ -1718,5 +1761,5 @@ var SwapRouter = /*#__PURE__*/function () {
1718
1761
  }();
1719
1762
  SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$3);
1720
1763
 
1721
- export { ADDRESS_THIS, ApprovalTypes, ApproveAndCall, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO };
1764
+ export { ADDRESS_THIS, ApprovalTypes, ApproveAndCall, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO, isMint };
1722
1765
  //# sourceMappingURL=router-sdk.esm.js.map