@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.
@@ -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
  }
@@ -26,8 +26,14 @@ var ONE = /*#__PURE__*/JSBI.BigInt(1);
26
26
  ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
27
27
  ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
28
28
  ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
29
- })(exports.ApprovalTypes || (exports.ApprovalTypes = {}));
29
+ })(exports.ApprovalTypes || (exports.ApprovalTypes = {})); // type guard
30
30
 
31
+
32
+ function isMint(options) {
33
+ return Object.keys(options).some(function (k) {
34
+ return k === 'recipient';
35
+ });
36
+ }
31
37
  var ApproveAndCall = /*#__PURE__*/function () {
32
38
  /**
33
39
  * Cannot be constructed.
@@ -61,6 +67,33 @@ var ApproveAndCall = /*#__PURE__*/function () {
61
67
  }
62
68
  };
63
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
+
64
97
  ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
65
98
  switch (approvalType) {
66
99
  case exports.ApprovalTypes.MAX:
@@ -1586,9 +1619,12 @@ var SwapRouter = /*#__PURE__*/function () {
1586
1619
 
1587
1620
  var ZERO_IN = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1588
1621
  var ZERO_OUT = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1589
- var totalAmountOut = trades.reduce(function (sum, trade) {
1622
+ var minimumAmountOut = trades.reduce(function (sum, trade) {
1590
1623
  return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1591
1624
  }, ZERO_OUT);
1625
+ var quoteAmountOut = trades.reduce(function (sum, trade) {
1626
+ return sum.add(trade.outputAmount);
1627
+ }, ZERO_OUT);
1592
1628
  var totalAmountIn = trades.reduce(function (sum, trade) {
1593
1629
  return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1594
1630
  }, ZERO_IN);
@@ -1599,7 +1635,8 @@ var SwapRouter = /*#__PURE__*/function () {
1599
1635
  inputIsNative: inputIsNative,
1600
1636
  outputIsNative: outputIsNative,
1601
1637
  totalAmountIn: totalAmountIn,
1602
- totalAmountOut: totalAmountOut
1638
+ minimumAmountOut: minimumAmountOut,
1639
+ quoteAmountOut: quoteAmountOut
1603
1640
  };
1604
1641
  }
1605
1642
  /**
@@ -1617,14 +1654,14 @@ var SwapRouter = /*#__PURE__*/function () {
1617
1654
  inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
1618
1655
  outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
1619
1656
  totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
1620
- totalAmountOut = _SwapRouter$encodeSwa.totalAmountOut; // unwrap or sweep
1657
+ minimumAmountOut = _SwapRouter$encodeSwa.minimumAmountOut; // unwrap or sweep
1621
1658
 
1622
1659
 
1623
1660
  if (routerMustCustody) {
1624
1661
  if (outputIsNative) {
1625
- calldatas.push(PaymentsExtended.encodeUnwrapWETH9(totalAmountOut.quotient, options.recipient, options.fee));
1662
+ calldatas.push(PaymentsExtended.encodeUnwrapWETH9(minimumAmountOut.quotient, options.recipient, options.fee));
1626
1663
  } else {
1627
- 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));
1628
1665
  }
1629
1666
  } // must refund when paying in ETH, but with an uncertain input amount
1630
1667
 
@@ -1652,7 +1689,13 @@ var SwapRouter = /*#__PURE__*/function () {
1652
1689
  outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
1653
1690
  sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
1654
1691
  totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
1655
- 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
+ }
1656
1699
 
1657
1700
  var chainId = sampleTrade.route.chainId;
1658
1701
  var zeroForOne = position.pool.token0 === totalAmountSwapped.currency.wrapped;
@@ -1665,7 +1708,7 @@ var SwapRouter = /*#__PURE__*/function () {
1665
1708
  var tokenIn = inputIsNative ? sdkCore.WETH9[chainId] : positionAmountIn.currency.wrapped;
1666
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
1667
1710
 
1668
- var amountOutRemaining = positionAmountOut.subtract(totalAmountOut.wrapped);
1711
+ var amountOutRemaining = positionAmountOut.subtract(quoteAmountOut.wrapped);
1669
1712
 
1670
1713
  if (amountOutRemaining.greaterThan(sdkCore.CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
1671
1714
  // if output is native, this means the remaining portion is included as native value in the transaction
@@ -1679,7 +1722,7 @@ var SwapRouter = /*#__PURE__*/function () {
1679
1722
  if (tokenInApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
1680
1723
  if (tokenOutApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // encode NFTManager add liquidity
1681
1724
 
1682
- 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
1683
1726
 
1684
1727
  inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
1685
1728
  outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
@@ -1731,4 +1774,5 @@ exports.RouteV3 = RouteV3;
1731
1774
  exports.SwapRouter = SwapRouter;
1732
1775
  exports.Trade = Trade;
1733
1776
  exports.ZERO = ZERO;
1777
+ exports.isMint = isMint;
1734
1778
  //# sourceMappingURL=router-sdk.cjs.development.js.map