@uniswap/router-sdk 1.0.0-beta.6 → 1.0.1

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,15 +1,16 @@
1
1
  import JSBI from 'jsbi';
2
2
  import { Interface } from '@ethersproject/abi';
3
- import { Multicall, toHex, Payments, Route as Route$1, Trade as Trade$1, encodeRouteToPath, SelfPermit } from '@uniswap/v3-sdk';
4
3
  import { abi } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json';
5
- import { validateAndParseAddress, TradeType, Fraction, CurrencyAmount, Price, Percent } from '@uniswap/sdk-core';
4
+ import { Multicall, toHex, Payments, NonfungiblePositionManager, Route as Route$1, Pool, Trade as Trade$1, encodeRouteToPath, SelfPermit } from '@uniswap/v3-sdk';
5
+ import { validateAndParseAddress, TradeType, Fraction, CurrencyAmount, Price, Percent, WETH9 } from '@uniswap/sdk-core';
6
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, Trade as Trade$2 } from '@uniswap/v2-sdk';
7
+ import { abi as abi$3 } 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
9
  import invariant from 'tiny-invariant';
10
+ import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json';
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
 
@@ -210,12 +211,71 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
210
211
  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
212
  }
212
213
 
213
- var Protocol;
214
+ var ApprovalTypes;
214
215
 
215
- (function (Protocol) {
216
- Protocol["V2"] = "V2";
217
- Protocol["V3"] = "V3";
218
- })(Protocol || (Protocol = {}));
216
+ (function (ApprovalTypes) {
217
+ ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
218
+ ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
219
+ ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
220
+ ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
221
+ ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
222
+ })(ApprovalTypes || (ApprovalTypes = {}));
223
+
224
+ var ApproveAndCall = /*#__PURE__*/function () {
225
+ /**
226
+ * Cannot be constructed.
227
+ */
228
+ function ApproveAndCall() {}
229
+
230
+ ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
231
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
232
+ };
233
+
234
+ ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
235
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
236
+ };
237
+
238
+ ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
239
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
240
+ };
241
+
242
+ ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
243
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
244
+ };
245
+
246
+ ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
247
+ !(calldatas.length > 0) ? process.env.NODE_ENV !== "production" ? invariant(false, 'NULL_CALLDATA') : invariant(false) : void 0;
248
+
249
+ if (calldatas.length == 1) {
250
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
251
+ } else {
252
+ var encodedMulticall = NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
253
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
254
+ }
255
+ };
256
+
257
+ ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
258
+ switch (approvalType) {
259
+ case ApprovalTypes.MAX:
260
+ return ApproveAndCall.encodeApproveMax(token.wrapped);
261
+
262
+ case ApprovalTypes.MAX_MINUS_ONE:
263
+ return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
264
+
265
+ case ApprovalTypes.ZERO_THEN_MAX:
266
+ return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
267
+
268
+ case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
269
+ return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
270
+
271
+ default:
272
+ throw 'Error: invalid ApprovalType';
273
+ }
274
+ };
275
+
276
+ return ApproveAndCall;
277
+ }();
278
+ ApproveAndCall.INTERFACE = /*#__PURE__*/new Interface(abi$2);
219
279
 
220
280
  function createCommonjsModule(fn, module) {
221
281
  return module = { exports: {} }, fn(module, module.exports), module.exports;
@@ -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
 
@@ -1047,7 +1114,43 @@ var Trade = /*#__PURE__*/function () {
1047
1114
  });
1048
1115
  }
1049
1116
 
1050
- this.tradeType = tradeType;
1117
+ this.tradeType = tradeType; // each route must have the same input and output currency
1118
+
1119
+ var inputCurrency = this.swaps[0].inputAmount.currency;
1120
+ var outputCurrency = this.swaps[0].outputAmount.currency;
1121
+ !this.swaps.every(function (_ref2) {
1122
+ var route = _ref2.route;
1123
+ return inputCurrency.wrapped.equals(route.input.wrapped);
1124
+ }) ? process.env.NODE_ENV !== "production" ? invariant(false, 'INPUT_CURRENCY_MATCH') : invariant(false) : void 0;
1125
+ !this.swaps.every(function (_ref3) {
1126
+ var route = _ref3.route;
1127
+ return outputCurrency.wrapped.equals(route.output.wrapped);
1128
+ }) ? process.env.NODE_ENV !== "production" ? invariant(false, 'OUTPUT_CURRENCY_MATCH') : invariant(false) : void 0; // pools must be unique inter protocols
1129
+
1130
+ var numPools = this.swaps.map(function (_ref4) {
1131
+ var route = _ref4.route;
1132
+ return route.pools.length;
1133
+ }).reduce(function (total, cur) {
1134
+ return total + cur;
1135
+ }, 0);
1136
+ var poolAddressSet = new Set();
1137
+
1138
+ for (var _iterator3 = _createForOfIteratorHelperLoose(this.swaps), _step3; !(_step3 = _iterator3()).done;) {
1139
+ var _route2 = _step3.value.route;
1140
+
1141
+ for (var _iterator4 = _createForOfIteratorHelperLoose(_route2.pools), _step4; !(_step4 = _iterator4()).done;) {
1142
+ var pool = _step4.value;
1143
+
1144
+ if (_route2.protocol == Protocol.V3) {
1145
+ poolAddressSet.add(Pool.getAddress(pool.token0, pool.token1, pool.fee));
1146
+ } else {
1147
+ var pair = pool;
1148
+ poolAddressSet.add(Pair.getAddress(pair.token0, pair.token1));
1149
+ }
1150
+ }
1151
+ }
1152
+
1153
+ !(numPools == poolAddressSet.size) ? process.env.NODE_ENV !== "production" ? invariant(false, 'POOLS_DUPLICATED') : invariant(false) : void 0;
1051
1154
  }
1052
1155
 
1053
1156
  var _proto = Trade.prototype;
@@ -1105,7 +1208,7 @@ var Trade = /*#__PURE__*/function () {
1105
1208
 
1106
1209
  Trade.fromRoutes = /*#__PURE__*/function () {
1107
1210
  var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(v2Routes, v3Routes, tradeType) {
1108
- var populatedV2Routes, populatedV3Routes, _iterator3, _step3, _step3$value, routev2, amount, v2Trade, inputAmount, outputAmount, _iterator4, _step4, _step4$value, routev3, _amount, v3Trade, _inputAmount2, _outputAmount2;
1211
+ var populatedV2Routes, populatedV3Routes, _iterator5, _step5, _step5$value, routev2, amount, v2Trade, inputAmount, outputAmount, _iterator6, _step6, _step6$value, routev3, _amount, v3Trade, _inputAmount2, _outputAmount2;
1109
1212
 
1110
1213
  return runtime_1.wrap(function _callee$(_context) {
1111
1214
  while (1) {
@@ -1114,8 +1217,8 @@ var Trade = /*#__PURE__*/function () {
1114
1217
  populatedV2Routes = [];
1115
1218
  populatedV3Routes = [];
1116
1219
 
1117
- for (_iterator3 = _createForOfIteratorHelperLoose(v2Routes); !(_step3 = _iterator3()).done;) {
1118
- _step3$value = _step3.value, routev2 = _step3$value.routev2, amount = _step3$value.amount;
1220
+ for (_iterator5 = _createForOfIteratorHelperLoose(v2Routes); !(_step5 = _iterator5()).done;) {
1221
+ _step5$value = _step5.value, routev2 = _step5$value.routev2, amount = _step5$value.amount;
1119
1222
  v2Trade = new Trade$2(routev2, amount, tradeType);
1120
1223
  inputAmount = v2Trade.inputAmount, outputAmount = v2Trade.outputAmount;
1121
1224
  populatedV2Routes.push({
@@ -1125,15 +1228,15 @@ var Trade = /*#__PURE__*/function () {
1125
1228
  });
1126
1229
  }
1127
1230
 
1128
- _iterator4 = _createForOfIteratorHelperLoose(v3Routes);
1231
+ _iterator6 = _createForOfIteratorHelperLoose(v3Routes);
1129
1232
 
1130
1233
  case 4:
1131
- if ((_step4 = _iterator4()).done) {
1234
+ if ((_step6 = _iterator6()).done) {
1132
1235
  _context.next = 13;
1133
1236
  break;
1134
1237
  }
1135
1238
 
1136
- _step4$value = _step4.value, routev3 = _step4$value.routev3, _amount = _step4$value.amount;
1239
+ _step6$value = _step6.value, routev3 = _step6$value.routev3, _amount = _step6$value.amount;
1137
1240
  _context.next = 8;
1138
1241
  return Trade$1.fromRoute(routev3, _amount, tradeType);
1139
1242
 
@@ -1240,8 +1343,8 @@ var Trade = /*#__PURE__*/function () {
1240
1343
  }
1241
1344
 
1242
1345
  var inputCurrency = this.swaps[0].inputAmount.currency;
1243
- var totalInputFromRoutes = this.swaps.map(function (_ref2) {
1244
- var inputAmount = _ref2.inputAmount;
1346
+ var totalInputFromRoutes = this.swaps.map(function (_ref5) {
1347
+ var inputAmount = _ref5.inputAmount;
1245
1348
  return inputAmount;
1246
1349
  }).reduce(function (total, cur) {
1247
1350
  return total.add(cur);
@@ -1257,8 +1360,8 @@ var Trade = /*#__PURE__*/function () {
1257
1360
  }
1258
1361
 
1259
1362
  var outputCurrency = this.swaps[0].outputAmount.currency;
1260
- var totalOutputFromRoutes = this.swaps.map(function (_ref3) {
1261
- var outputAmount = _ref3.outputAmount;
1363
+ var totalOutputFromRoutes = this.swaps.map(function (_ref6) {
1364
+ var outputAmount = _ref6.outputAmount;
1262
1365
  return outputAmount;
1263
1366
  }).reduce(function (total, cur) {
1264
1367
  return total.add(cur);
@@ -1290,10 +1393,10 @@ var Trade = /*#__PURE__*/function () {
1290
1393
 
1291
1394
  var spotOutputAmount = CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
1292
1395
 
1293
- for (var _iterator5 = _createForOfIteratorHelperLoose(this.swaps), _step5; !(_step5 = _iterator5()).done;) {
1294
- var _step5$value = _step5.value,
1295
- route = _step5$value.route,
1296
- inputAmount = _step5$value.inputAmount;
1396
+ for (var _iterator7 = _createForOfIteratorHelperLoose(this.swaps), _step7; !(_step7 = _iterator7()).done;) {
1397
+ var _step7$value = _step7.value,
1398
+ route = _step7$value.route,
1399
+ inputAmount = _step7$value.inputAmount;
1297
1400
  var midPrice = route.midPrice;
1298
1401
  spotOutputAmount = spotOutputAmount.add(midPrice.quote(inputAmount));
1299
1402
  }
@@ -1318,7 +1421,7 @@ var SwapRouter = /*#__PURE__*/function () {
1318
1421
  */
1319
1422
  function SwapRouter() {}
1320
1423
 
1321
- SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody) {
1424
+ SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
1322
1425
  var amountIn = toHex(trade.maximumAmountIn(options.slippageTolerance).quotient);
1323
1426
  var amountOut = toHex(trade.minimumAmountOut(options.slippageTolerance).quotient);
1324
1427
  var path = trade.route.path.map(function (token) {
@@ -1327,9 +1430,7 @@ var SwapRouter = /*#__PURE__*/function () {
1327
1430
  var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : validateAndParseAddress(options.recipient);
1328
1431
 
1329
1432
  if (trade.tradeType === TradeType.EXACT_INPUT) {
1330
- var exactInputParams = [amountIn, // save gas by only passing slippage check if we can't check it later
1331
- // not a pure win, as sometimes this will cost us more when it would have caused an earlier failure
1332
- routerMustCustody ? 0 : amountOut, path, recipient, false];
1433
+ var exactInputParams = [amountIn, performAggregatedSlippageCheck ? 0 : amountOut, path, recipient];
1333
1434
  return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
1334
1435
  } else {
1335
1436
  var exactOutputParams = [amountOut, amountIn, path, recipient];
@@ -1337,7 +1438,7 @@ var SwapRouter = /*#__PURE__*/function () {
1337
1438
  }
1338
1439
  };
1339
1440
 
1340
- SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody) {
1441
+ SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
1341
1442
  var calldatas = [];
1342
1443
 
1343
1444
  for (var _iterator = _createForOfIteratorHelperLoose(trade.swaps), _step; !(_step = _iterator()).done;) {
@@ -1359,9 +1460,8 @@ var SwapRouter = /*#__PURE__*/function () {
1359
1460
  fee: route.pools[0].fee,
1360
1461
  recipient: recipient,
1361
1462
  amountIn: amountIn,
1362
- amountOutMinimum: amountOut,
1363
- sqrtPriceLimitX96: 0,
1364
- hasAlreadyPaid: false
1463
+ amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut,
1464
+ sqrtPriceLimitX96: 0
1365
1465
  };
1366
1466
  calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
1367
1467
  } else {
@@ -1384,8 +1484,7 @@ var SwapRouter = /*#__PURE__*/function () {
1384
1484
  path: path,
1385
1485
  recipient: recipient,
1386
1486
  amountIn: amountIn,
1387
- amountOutMinimum: amountOut,
1388
- hasAlreadyPaid: false
1487
+ amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut
1389
1488
  };
1390
1489
  calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
1391
1490
  } else {
@@ -1401,15 +1500,9 @@ var SwapRouter = /*#__PURE__*/function () {
1401
1500
  }
1402
1501
 
1403
1502
  return calldatas;
1404
- }
1405
- /**
1406
- * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
1407
- * @param trade to produce call parameters for
1408
- * @param options options for the call parameters
1409
- */
1410
- ;
1503
+ };
1411
1504
 
1412
- SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
1505
+ SwapRouter.encodeSwaps = function encodeSwaps(trades, options, isSwapAndAdd) {
1413
1506
  // If dealing with an instance of the aggregated Trade object, unbundle it to individual V2Trade and V3Trade objects.
1414
1507
  if (trades instanceof Trade) {
1415
1508
  !trades.swaps.every(function (swap) {
@@ -1442,6 +1535,9 @@ var SwapRouter = /*#__PURE__*/function () {
1442
1535
  trades = [trades];
1443
1536
  }
1444
1537
 
1538
+ var numberOfTrades = trades.reduce(function (numberOfTrades, trade) {
1539
+ return numberOfTrades + (trade instanceof Trade$1 ? trade.swaps.length : 1);
1540
+ }, 0);
1445
1541
  var sampleTrade = trades[0]; // All trades should have the same starting/ending currency and trade type
1446
1542
 
1447
1543
  !trades.every(function (trade) {
@@ -1454,25 +1550,19 @@ var SwapRouter = /*#__PURE__*/function () {
1454
1550
  return trade.tradeType === sampleTrade.tradeType;
1455
1551
  }) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TRADE_TYPE_DIFF') : invariant(false) : void 0;
1456
1552
  var calldatas = [];
1457
- var ZERO_IN = CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
1458
- var ZERO_OUT = CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
1459
- var totalAmountOut = trades.reduce(function (sum, trade) {
1460
- return sum.add(trade.minimumAmountOut(options.slippageTolerance));
1461
- }, ZERO_OUT);
1462
1553
  var inputIsNative = sampleTrade.inputAmount.currency.isNative;
1463
- var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether a refund needs to happen
1464
- // 1. when paying in ETH, but with an uncertain input amount
1554
+ var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether we want to perform an aggregated slippage check
1555
+ // 1. when there are >2 exact input trades. this is only a heuristic,
1556
+ // as it's still more gas-expensive even in this case, but has benefits
1557
+ // in that the reversion probability is lower
1465
1558
 
1466
- var mustRefund = inputIsNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT; // flag for whether funds should be send first to the router
1559
+ var performAggregatedSlippageCheck = sampleTrade.tradeType === TradeType.EXACT_INPUT && numberOfTrades > 2; // flag for whether funds should be send first to the router
1467
1560
  // 1. when receiving ETH (which much be unwrapped from WETH)
1468
1561
  // 2. when a fee on the output is being taken
1469
- // 3. when there are >1 exact input trades. this one isn't strictly necessary,
1470
- // but typically we want to perform an aggregated slippage check
1562
+ // 3. when performing swap and add
1563
+ // 4. when performing an aggregated slippage check
1471
1564
 
1472
- var routerMustCustody = outputIsNative || !!options.fee || trades.length > 1 && sampleTrade.tradeType === TradeType.EXACT_INPUT;
1473
- var totalValue = inputIsNative ? trades.reduce(function (sum, trade) {
1474
- return sum.add(trade.maximumAmountIn(options.slippageTolerance));
1475
- }, ZERO_IN) : ZERO_IN; // encode permit if necessary
1565
+ var routerMustCustody = outputIsNative || !!options.fee || !!isSwapAndAdd || performAggregatedSlippageCheck; // encode permit if necessary
1476
1566
 
1477
1567
  if (options.inputTokenPermit) {
1478
1568
  !sampleTrade.inputAmount.currency.isToken ? process.env.NODE_ENV !== "production" ? invariant(false, 'NON_TOKEN_PERMIT') : invariant(false) : void 0;
@@ -1483,42 +1573,150 @@ var SwapRouter = /*#__PURE__*/function () {
1483
1573
  var trade = _step3.value;
1484
1574
 
1485
1575
  if (trade instanceof Trade$2) {
1486
- calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody));
1576
+ calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck));
1487
1577
  } else {
1488
- for (var _iterator4 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody)), _step4; !(_step4 = _iterator4()).done;) {
1578
+ for (var _iterator4 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck)), _step4; !(_step4 = _iterator4()).done;) {
1489
1579
  var calldata = _step4.value;
1490
1580
  calldatas.push(calldata);
1491
1581
  }
1492
1582
  }
1493
- } // unwrap
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
1494
1619
 
1495
1620
 
1496
1621
  if (routerMustCustody) {
1497
- // if all trades are exact output, we can save gas by not passing a slippage check
1498
- var canOmitSlippageCheck = sampleTrade.tradeType === TradeType.EXACT_OUTPUT;
1499
- var amountNecessary = canOmitSlippageCheck ? ZERO$1 : totalAmountOut.quotient;
1500
-
1501
1622
  if (outputIsNative) {
1502
- calldatas.push(PaymentsExtended.encodeUnwrapWETH9(amountNecessary, options.recipient, options.fee));
1623
+ calldatas.push(PaymentsExtended.encodeUnwrapWETH9(totalAmountOut.quotient, options.recipient, options.fee));
1503
1624
  } else {
1504
- calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, amountNecessary, options.recipient, options.fee));
1625
+ calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, totalAmountOut.quotient, options.recipient, options.fee));
1505
1626
  }
1506
- } // refund
1627
+ } // must refund when paying in ETH, but with an uncertain input amount
1507
1628
 
1508
1629
 
1509
- if (mustRefund) {
1630
+ if (inputIsNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT) {
1510
1631
  calldatas.push(Payments.encodeRefundETH());
1511
1632
  }
1512
1633
 
1513
1634
  return {
1514
1635
  calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
1515
- 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
1516
1714
  };
1517
1715
  };
1518
1716
 
1519
1717
  return SwapRouter;
1520
1718
  }();
1521
- SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$2);
1719
+ SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$3);
1522
1720
 
1523
1721
  export { ADDRESS_THIS, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO };
1524
1722
  //# sourceMappingURL=router-sdk.esm.js.map