@uniswap/router-sdk 1.0.0-beta.2 → 1.0.0-beta.6
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.
- package/README.md +10 -0
- package/dist/entities/route.d.ts +4 -4
- package/dist/entities/trade.d.ts +4 -4
- package/dist/paymentsExtended.d.ts +3 -3
- package/dist/router-sdk.cjs.development.js +231 -197
- package/dist/router-sdk.cjs.development.js.map +1 -1
- package/dist/router-sdk.cjs.production.min.js +1 -1
- package/dist/router-sdk.cjs.production.min.js.map +1 -1
- package/dist/router-sdk.esm.js +238 -204
- package/dist/router-sdk.esm.js.map +1 -1
- package/dist/swapRouter.d.ts +3 -3
- package/package.json +6 -3
package/dist/router-sdk.esm.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import JSBI from 'jsbi';
|
|
2
2
|
import { Interface } from '@ethersproject/abi';
|
|
3
|
-
import { Multicall, toHex, Payments,
|
|
3
|
+
import { Multicall, toHex, Payments, Route as Route$1, Trade as Trade$1, encodeRouteToPath, SelfPermit } from '@uniswap/v3-sdk';
|
|
4
4
|
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';
|
|
5
6
|
import { abi as abi$1 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json';
|
|
6
|
-
import { validateAndParseAddress, TradeType, CurrencyAmount, Fraction, Price, Percent } from '@uniswap/sdk-core';
|
|
7
|
-
import invariant from 'tiny-invariant';
|
|
8
7
|
import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json';
|
|
9
|
-
import { Trade as Trade$
|
|
8
|
+
import { Route, Trade as Trade$2 } from '@uniswap/v2-sdk';
|
|
9
|
+
import invariant from 'tiny-invariant';
|
|
10
10
|
|
|
11
11
|
var MSG_SENDER = '0x0000000000000000000000000000000000000000';
|
|
12
12
|
var ADDRESS_THIS = '0x0000000000000000000000000000000000000001';
|
|
@@ -39,7 +39,7 @@ var MulticallExtended = /*#__PURE__*/function () {
|
|
|
39
39
|
} // this means the validation value should be a previousBlockhash
|
|
40
40
|
|
|
41
41
|
|
|
42
|
-
if (typeof validation === 'string') {
|
|
42
|
+
if (typeof validation === 'string' && validation.startsWith('0x')) {
|
|
43
43
|
var previousBlockhash = validateAndParseBytes32(validation);
|
|
44
44
|
return MulticallExtended.INTERFACE.encodeFunctionData('multicall(bytes32,bytes[])', [previousBlockhash, calldatas]);
|
|
45
45
|
} else {
|
|
@@ -52,16 +52,16 @@ var MulticallExtended = /*#__PURE__*/function () {
|
|
|
52
52
|
}();
|
|
53
53
|
MulticallExtended.INTERFACE = /*#__PURE__*/new Interface(abi);
|
|
54
54
|
|
|
55
|
+
function encodeFeeBips(fee) {
|
|
56
|
+
return toHex(fee.multiply(10000).quotient);
|
|
57
|
+
}
|
|
58
|
+
|
|
55
59
|
var PaymentsExtended = /*#__PURE__*/function () {
|
|
56
60
|
/**
|
|
57
61
|
* Cannot be constructed.
|
|
58
62
|
*/
|
|
59
63
|
function PaymentsExtended() {}
|
|
60
64
|
|
|
61
|
-
PaymentsExtended.encodeFeeBips = function encodeFeeBips(fee) {
|
|
62
|
-
return toHex(fee.multiply(10000).quotient);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
65
|
PaymentsExtended.encodeUnwrapWETH9 = function encodeUnwrapWETH9(amountMinimum, recipient, feeOptions) {
|
|
66
66
|
// if there's a recipient, just pass it along
|
|
67
67
|
if (typeof recipient === 'string') {
|
|
@@ -69,7 +69,7 @@ var PaymentsExtended = /*#__PURE__*/function () {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
if (!!feeOptions) {
|
|
72
|
-
var feeBips =
|
|
72
|
+
var feeBips = encodeFeeBips(feeOptions.fee);
|
|
73
73
|
var feeRecipient = validateAndParseAddress(feeOptions.recipient);
|
|
74
74
|
return PaymentsExtended.INTERFACE.encodeFunctionData('unwrapWETH9WithFee(uint256,uint256,address)', [toHex(amountMinimum), feeBips, feeRecipient]);
|
|
75
75
|
} else {
|
|
@@ -84,7 +84,7 @@ var PaymentsExtended = /*#__PURE__*/function () {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
if (!!feeOptions) {
|
|
87
|
-
var feeBips =
|
|
87
|
+
var feeBips = encodeFeeBips(feeOptions.fee);
|
|
88
88
|
var feeRecipient = validateAndParseAddress(feeOptions.recipient);
|
|
89
89
|
return PaymentsExtended.INTERFACE.encodeFunctionData('sweepTokenWithFee(address,uint256,uint256,address)', [token.address, toHex(amountMinimum), feeBips, feeRecipient]);
|
|
90
90
|
} else {
|
|
@@ -96,6 +96,10 @@ var PaymentsExtended = /*#__PURE__*/function () {
|
|
|
96
96
|
return PaymentsExtended.INTERFACE.encodeFunctionData('pull', [token.address, toHex(amount)]);
|
|
97
97
|
};
|
|
98
98
|
|
|
99
|
+
PaymentsExtended.encodeWrapETH = function encodeWrapETH(amount) {
|
|
100
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('wrapETH', [toHex(amount)]);
|
|
101
|
+
};
|
|
102
|
+
|
|
99
103
|
return PaymentsExtended;
|
|
100
104
|
}();
|
|
101
105
|
PaymentsExtended.INTERFACE = /*#__PURE__*/new Interface(abi$1);
|
|
@@ -206,188 +210,12 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
|
206
210
|
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
207
211
|
}
|
|
208
212
|
|
|
209
|
-
var
|
|
210
|
-
/**
|
|
211
|
-
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
|
|
212
|
-
*/
|
|
213
|
-
|
|
214
|
-
var SwapRouter = /*#__PURE__*/function () {
|
|
215
|
-
/**
|
|
216
|
-
* Cannot be constructed.
|
|
217
|
-
*/
|
|
218
|
-
function SwapRouter() {}
|
|
219
|
-
|
|
220
|
-
SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody) {
|
|
221
|
-
var amountIn = toHex(trade.maximumAmountIn(options.slippageTolerance).quotient);
|
|
222
|
-
var amountOut = toHex(trade.minimumAmountOut(options.slippageTolerance).quotient);
|
|
223
|
-
var path = trade.route.path.map(function (token) {
|
|
224
|
-
return token.address;
|
|
225
|
-
});
|
|
226
|
-
var recipient = routerMustCustody ? ADDRESS_THIS : options.recipient ? validateAndParseAddress(options.recipient) : MSG_SENDER;
|
|
227
|
-
|
|
228
|
-
if (trade.tradeType === TradeType.EXACT_INPUT) {
|
|
229
|
-
var exactInputParams = [amountIn, // save gas by only passing slippage check if we can't check it later
|
|
230
|
-
// not a pure win, as sometimes this will cost us more when it would have caused an earlier failure
|
|
231
|
-
routerMustCustody ? 0 : amountOut, path, recipient];
|
|
232
|
-
return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
|
|
233
|
-
} else {
|
|
234
|
-
var exactOutputParams = [amountOut, amountIn, path, recipient];
|
|
235
|
-
return SwapRouter.INTERFACE.encodeFunctionData('swapTokensForExactTokens', exactOutputParams);
|
|
236
|
-
}
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody) {
|
|
240
|
-
var calldatas = [];
|
|
241
|
-
|
|
242
|
-
for (var _iterator = _createForOfIteratorHelperLoose(trade.swaps), _step; !(_step = _iterator()).done;) {
|
|
243
|
-
var _step$value = _step.value,
|
|
244
|
-
route = _step$value.route,
|
|
245
|
-
inputAmount = _step$value.inputAmount,
|
|
246
|
-
outputAmount = _step$value.outputAmount;
|
|
247
|
-
var amountIn = toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
248
|
-
var amountOut = toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient); // flag for whether the trade is single hop or not
|
|
249
|
-
|
|
250
|
-
var singleHop = route.pools.length === 1;
|
|
251
|
-
var recipient = routerMustCustody ? ADDRESS_THIS : options.recipient ? validateAndParseAddress(options.recipient) : MSG_SENDER;
|
|
252
|
-
|
|
253
|
-
if (singleHop) {
|
|
254
|
-
if (trade.tradeType === TradeType.EXACT_INPUT) {
|
|
255
|
-
var exactInputSingleParams = {
|
|
256
|
-
tokenIn: route.tokenPath[0].address,
|
|
257
|
-
tokenOut: route.tokenPath[1].address,
|
|
258
|
-
fee: route.pools[0].fee,
|
|
259
|
-
recipient: recipient,
|
|
260
|
-
amountIn: amountIn,
|
|
261
|
-
amountOutMinimum: amountOut,
|
|
262
|
-
sqrtPriceLimitX96: 0
|
|
263
|
-
};
|
|
264
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
265
|
-
} else {
|
|
266
|
-
var exactOutputSingleParams = {
|
|
267
|
-
tokenIn: route.tokenPath[0].address,
|
|
268
|
-
tokenOut: route.tokenPath[1].address,
|
|
269
|
-
fee: route.pools[0].fee,
|
|
270
|
-
recipient: recipient,
|
|
271
|
-
amountOut: amountOut,
|
|
272
|
-
amountInMaximum: amountIn,
|
|
273
|
-
sqrtPriceLimitX96: 0
|
|
274
|
-
};
|
|
275
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutputSingle', [exactOutputSingleParams]));
|
|
276
|
-
}
|
|
277
|
-
} else {
|
|
278
|
-
var path = encodeRouteToPath(route, trade.tradeType === TradeType.EXACT_OUTPUT);
|
|
279
|
-
|
|
280
|
-
if (trade.tradeType === TradeType.EXACT_INPUT) {
|
|
281
|
-
var exactInputParams = {
|
|
282
|
-
path: path,
|
|
283
|
-
recipient: recipient,
|
|
284
|
-
amountIn: amountIn,
|
|
285
|
-
amountOutMinimum: amountOut
|
|
286
|
-
};
|
|
287
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
288
|
-
} else {
|
|
289
|
-
var exactOutputParams = {
|
|
290
|
-
path: path,
|
|
291
|
-
recipient: recipient,
|
|
292
|
-
amountOut: amountOut,
|
|
293
|
-
amountInMaximum: amountIn
|
|
294
|
-
};
|
|
295
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutput', [exactOutputParams]));
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return calldatas;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
304
|
-
* @param trade to produce call parameters for
|
|
305
|
-
* @param options options for the call parameters
|
|
306
|
-
*/
|
|
307
|
-
;
|
|
308
|
-
|
|
309
|
-
SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
|
|
310
|
-
if (!Array.isArray(trades)) {
|
|
311
|
-
trades = [trades];
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
var sampleTrade = trades[0]; // All trades should have the same starting/ending currency and trade type
|
|
315
|
-
|
|
316
|
-
!trades.every(function (trade) {
|
|
317
|
-
return trade.inputAmount.currency.equals(sampleTrade.inputAmount.currency);
|
|
318
|
-
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TOKEN_IN_DIFF') : invariant(false) : void 0;
|
|
319
|
-
!trades.every(function (trade) {
|
|
320
|
-
return trade.outputAmount.currency.equals(sampleTrade.outputAmount.currency);
|
|
321
|
-
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TOKEN_OUT_DIFF') : invariant(false) : void 0;
|
|
322
|
-
!trades.every(function (trade) {
|
|
323
|
-
return trade.tradeType === sampleTrade.tradeType;
|
|
324
|
-
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TRADE_TYPE_DIFF') : invariant(false) : void 0;
|
|
325
|
-
var calldatas = [];
|
|
326
|
-
var ZERO_IN = CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
|
|
327
|
-
var ZERO_OUT = CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
|
|
328
|
-
var totalAmountOut = trades.reduce(function (sum, trade) {
|
|
329
|
-
return sum.add(trade.minimumAmountOut(options.slippageTolerance));
|
|
330
|
-
}, ZERO_OUT);
|
|
331
|
-
var inputIsNative = sampleTrade.inputAmount.currency.isNative;
|
|
332
|
-
var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether a refund needs to happen
|
|
333
|
-
// 1. when paying in ETH, but with an uncertain input amount
|
|
334
|
-
|
|
335
|
-
var mustRefund = inputIsNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT; // flag for whether funds should be send first to the router
|
|
336
|
-
// 1. when receiving ETH (which much be unwrapped from WETH)
|
|
337
|
-
// 2. when a fee on the output is being taken
|
|
338
|
-
// 3. when there are >1 exact input trades. this one isn't strictly necessary,
|
|
339
|
-
// but typically we want to perform an aggregated slippage check
|
|
340
|
-
|
|
341
|
-
var routerMustCustody = outputIsNative || !!options.fee || trades.length > 1 && sampleTrade.tradeType === TradeType.EXACT_INPUT;
|
|
342
|
-
var totalValue = inputIsNative ? trades.reduce(function (sum, trade) {
|
|
343
|
-
return sum.add(trade.maximumAmountIn(options.slippageTolerance));
|
|
344
|
-
}, ZERO_IN) : ZERO_IN; // encode permit if necessary
|
|
345
|
-
|
|
346
|
-
if (options.inputTokenPermit) {
|
|
347
|
-
!sampleTrade.inputAmount.currency.isToken ? process.env.NODE_ENV !== "production" ? invariant(false, 'NON_TOKEN_PERMIT') : invariant(false) : void 0;
|
|
348
|
-
calldatas.push(SelfPermit.encodePermit(sampleTrade.inputAmount.currency, options.inputTokenPermit));
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
for (var _iterator2 = _createForOfIteratorHelperLoose(trades), _step2; !(_step2 = _iterator2()).done;) {
|
|
352
|
-
var trade = _step2.value;
|
|
353
|
-
|
|
354
|
-
if (trade instanceof Trade$1) {
|
|
355
|
-
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody));
|
|
356
|
-
} else {
|
|
357
|
-
for (var _iterator3 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody)), _step3; !(_step3 = _iterator3()).done;) {
|
|
358
|
-
var calldata = _step3.value;
|
|
359
|
-
calldatas.push(calldata);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
} // unwrap
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
if (routerMustCustody) {
|
|
366
|
-
// if all trades are exact output, we can save gas by not passing a slippage check
|
|
367
|
-
var canOmitSlippageCheck = sampleTrade.tradeType === TradeType.EXACT_OUTPUT;
|
|
368
|
-
var amountNecessary = canOmitSlippageCheck ? ZERO$1 : totalAmountOut.quotient;
|
|
369
|
-
|
|
370
|
-
if (outputIsNative) {
|
|
371
|
-
calldatas.push(PaymentsExtended.encodeUnwrapWETH9(amountNecessary, options.recipient, options.fee));
|
|
372
|
-
} else {
|
|
373
|
-
calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, amountNecessary, options.recipient, options.fee));
|
|
374
|
-
}
|
|
375
|
-
} // refund
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
if (mustRefund) {
|
|
379
|
-
calldatas.push(Payments.encodeRefundETH());
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
return {
|
|
383
|
-
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
384
|
-
value: toHex(totalValue.quotient)
|
|
385
|
-
};
|
|
386
|
-
};
|
|
213
|
+
var Protocol;
|
|
387
214
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
215
|
+
(function (Protocol) {
|
|
216
|
+
Protocol["V2"] = "V2";
|
|
217
|
+
Protocol["V3"] = "V3";
|
|
218
|
+
})(Protocol || (Protocol = {}));
|
|
391
219
|
|
|
392
220
|
function createCommonjsModule(fn, module) {
|
|
393
221
|
return module = { exports: {} }, fn(module, module.exports), module.exports;
|
|
@@ -1149,13 +977,6 @@ try {
|
|
|
1149
977
|
}
|
|
1150
978
|
});
|
|
1151
979
|
|
|
1152
|
-
var Protocol;
|
|
1153
|
-
|
|
1154
|
-
(function (Protocol) {
|
|
1155
|
-
Protocol["V2"] = "V2";
|
|
1156
|
-
Protocol["V3"] = "V3";
|
|
1157
|
-
})(Protocol || (Protocol = {}));
|
|
1158
|
-
|
|
1159
980
|
var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
|
|
1160
981
|
_inheritsLoose(RouteV2, _V2RouteSDK);
|
|
1161
982
|
|
|
@@ -1163,8 +984,8 @@ var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
|
|
|
1163
984
|
var _this;
|
|
1164
985
|
|
|
1165
986
|
_this = _V2RouteSDK.call(this, v2Route.pairs, v2Route.input, v2Route.output) || this;
|
|
1166
|
-
_this.pools = _this.pairs;
|
|
1167
987
|
_this.protocol = Protocol.V2;
|
|
988
|
+
_this.pools = _this.pairs;
|
|
1168
989
|
return _this;
|
|
1169
990
|
}
|
|
1170
991
|
|
|
@@ -1295,7 +1116,7 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1295
1116
|
|
|
1296
1117
|
for (_iterator3 = _createForOfIteratorHelperLoose(v2Routes); !(_step3 = _iterator3()).done;) {
|
|
1297
1118
|
_step3$value = _step3.value, routev2 = _step3$value.routev2, amount = _step3$value.amount;
|
|
1298
|
-
v2Trade = new Trade$
|
|
1119
|
+
v2Trade = new Trade$2(routev2, amount, tradeType);
|
|
1299
1120
|
inputAmount = v2Trade.inputAmount, outputAmount = v2Trade.outputAmount;
|
|
1300
1121
|
populatedV2Routes.push({
|
|
1301
1122
|
routev2: routev2,
|
|
@@ -1314,7 +1135,7 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1314
1135
|
|
|
1315
1136
|
_step4$value = _step4.value, routev3 = _step4$value.routev3, _amount = _step4$value.amount;
|
|
1316
1137
|
_context.next = 8;
|
|
1317
|
-
return Trade$
|
|
1138
|
+
return Trade$1.fromRoute(routev3, _amount, tradeType);
|
|
1318
1139
|
|
|
1319
1140
|
case 8:
|
|
1320
1141
|
v3Trade = _context.sent;
|
|
@@ -1364,7 +1185,7 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1364
1185
|
break;
|
|
1365
1186
|
}
|
|
1366
1187
|
|
|
1367
|
-
v2Trade = new Trade$
|
|
1188
|
+
v2Trade = new Trade$2(route, amount, tradeType);
|
|
1368
1189
|
inputAmount = v2Trade.inputAmount, outputAmount = v2Trade.outputAmount;
|
|
1369
1190
|
v2Routes = [{
|
|
1370
1191
|
routev2: route,
|
|
@@ -1377,7 +1198,7 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1377
1198
|
|
|
1378
1199
|
case 7:
|
|
1379
1200
|
_context2.next = 9;
|
|
1380
|
-
return Trade$
|
|
1201
|
+
return Trade$1.fromRoute(route, amount, tradeType);
|
|
1381
1202
|
|
|
1382
1203
|
case 9:
|
|
1383
1204
|
v3Trade = _context2.sent;
|
|
@@ -1486,5 +1307,218 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1486
1307
|
return Trade;
|
|
1487
1308
|
}();
|
|
1488
1309
|
|
|
1310
|
+
var ZERO$1 = /*#__PURE__*/JSBI.BigInt(0);
|
|
1311
|
+
/**
|
|
1312
|
+
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
|
|
1313
|
+
*/
|
|
1314
|
+
|
|
1315
|
+
var SwapRouter = /*#__PURE__*/function () {
|
|
1316
|
+
/**
|
|
1317
|
+
* Cannot be constructed.
|
|
1318
|
+
*/
|
|
1319
|
+
function SwapRouter() {}
|
|
1320
|
+
|
|
1321
|
+
SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody) {
|
|
1322
|
+
var amountIn = toHex(trade.maximumAmountIn(options.slippageTolerance).quotient);
|
|
1323
|
+
var amountOut = toHex(trade.minimumAmountOut(options.slippageTolerance).quotient);
|
|
1324
|
+
var path = trade.route.path.map(function (token) {
|
|
1325
|
+
return token.address;
|
|
1326
|
+
});
|
|
1327
|
+
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : validateAndParseAddress(options.recipient);
|
|
1328
|
+
|
|
1329
|
+
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];
|
|
1333
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
|
|
1334
|
+
} else {
|
|
1335
|
+
var exactOutputParams = [amountOut, amountIn, path, recipient];
|
|
1336
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapTokensForExactTokens', exactOutputParams);
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
|
|
1340
|
+
SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody) {
|
|
1341
|
+
var calldatas = [];
|
|
1342
|
+
|
|
1343
|
+
for (var _iterator = _createForOfIteratorHelperLoose(trade.swaps), _step; !(_step = _iterator()).done;) {
|
|
1344
|
+
var _step$value = _step.value,
|
|
1345
|
+
route = _step$value.route,
|
|
1346
|
+
inputAmount = _step$value.inputAmount,
|
|
1347
|
+
outputAmount = _step$value.outputAmount;
|
|
1348
|
+
var amountIn = toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
1349
|
+
var amountOut = toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient); // flag for whether the trade is single hop or not
|
|
1350
|
+
|
|
1351
|
+
var singleHop = route.pools.length === 1;
|
|
1352
|
+
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : validateAndParseAddress(options.recipient);
|
|
1353
|
+
|
|
1354
|
+
if (singleHop) {
|
|
1355
|
+
if (trade.tradeType === TradeType.EXACT_INPUT) {
|
|
1356
|
+
var exactInputSingleParams = {
|
|
1357
|
+
tokenIn: route.tokenPath[0].address,
|
|
1358
|
+
tokenOut: route.tokenPath[1].address,
|
|
1359
|
+
fee: route.pools[0].fee,
|
|
1360
|
+
recipient: recipient,
|
|
1361
|
+
amountIn: amountIn,
|
|
1362
|
+
amountOutMinimum: amountOut,
|
|
1363
|
+
sqrtPriceLimitX96: 0,
|
|
1364
|
+
hasAlreadyPaid: false
|
|
1365
|
+
};
|
|
1366
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
1367
|
+
} else {
|
|
1368
|
+
var exactOutputSingleParams = {
|
|
1369
|
+
tokenIn: route.tokenPath[0].address,
|
|
1370
|
+
tokenOut: route.tokenPath[1].address,
|
|
1371
|
+
fee: route.pools[0].fee,
|
|
1372
|
+
recipient: recipient,
|
|
1373
|
+
amountOut: amountOut,
|
|
1374
|
+
amountInMaximum: amountIn,
|
|
1375
|
+
sqrtPriceLimitX96: 0
|
|
1376
|
+
};
|
|
1377
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutputSingle', [exactOutputSingleParams]));
|
|
1378
|
+
}
|
|
1379
|
+
} else {
|
|
1380
|
+
var path = encodeRouteToPath(route, trade.tradeType === TradeType.EXACT_OUTPUT);
|
|
1381
|
+
|
|
1382
|
+
if (trade.tradeType === TradeType.EXACT_INPUT) {
|
|
1383
|
+
var exactInputParams = {
|
|
1384
|
+
path: path,
|
|
1385
|
+
recipient: recipient,
|
|
1386
|
+
amountIn: amountIn,
|
|
1387
|
+
amountOutMinimum: amountOut,
|
|
1388
|
+
hasAlreadyPaid: false
|
|
1389
|
+
};
|
|
1390
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
1391
|
+
} else {
|
|
1392
|
+
var exactOutputParams = {
|
|
1393
|
+
path: path,
|
|
1394
|
+
recipient: recipient,
|
|
1395
|
+
amountOut: amountOut,
|
|
1396
|
+
amountInMaximum: amountIn
|
|
1397
|
+
};
|
|
1398
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutput', [exactOutputParams]));
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
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
|
+
;
|
|
1411
|
+
|
|
1412
|
+
SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
|
|
1413
|
+
// If dealing with an instance of the aggregated Trade object, unbundle it to individual V2Trade and V3Trade objects.
|
|
1414
|
+
if (trades instanceof Trade) {
|
|
1415
|
+
!trades.swaps.every(function (swap) {
|
|
1416
|
+
return swap.route.protocol == Protocol.V3 || swap.route.protocol == Protocol.V2;
|
|
1417
|
+
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'UNSUPPORTED_PROTOCOL') : invariant(false) : void 0;
|
|
1418
|
+
var v2Andv3Trades = [];
|
|
1419
|
+
|
|
1420
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(trades.swaps), _step2; !(_step2 = _iterator2()).done;) {
|
|
1421
|
+
var _step2$value = _step2.value,
|
|
1422
|
+
route = _step2$value.route,
|
|
1423
|
+
inputAmount = _step2$value.inputAmount,
|
|
1424
|
+
outputAmount = _step2$value.outputAmount;
|
|
1425
|
+
|
|
1426
|
+
if (route.protocol == Protocol.V2) {
|
|
1427
|
+
v2Andv3Trades.push(new Trade$2(route, trades.tradeType == TradeType.EXACT_INPUT ? inputAmount : outputAmount, trades.tradeType));
|
|
1428
|
+
} else if (route.protocol == Protocol.V3) {
|
|
1429
|
+
v2Andv3Trades.push(Trade$1.createUncheckedTrade({
|
|
1430
|
+
route: route,
|
|
1431
|
+
inputAmount: inputAmount,
|
|
1432
|
+
outputAmount: outputAmount,
|
|
1433
|
+
tradeType: trades.tradeType
|
|
1434
|
+
}));
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
trades = v2Andv3Trades;
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
if (!Array.isArray(trades)) {
|
|
1442
|
+
trades = [trades];
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
var sampleTrade = trades[0]; // All trades should have the same starting/ending currency and trade type
|
|
1446
|
+
|
|
1447
|
+
!trades.every(function (trade) {
|
|
1448
|
+
return trade.inputAmount.currency.equals(sampleTrade.inputAmount.currency);
|
|
1449
|
+
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TOKEN_IN_DIFF') : invariant(false) : void 0;
|
|
1450
|
+
!trades.every(function (trade) {
|
|
1451
|
+
return trade.outputAmount.currency.equals(sampleTrade.outputAmount.currency);
|
|
1452
|
+
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TOKEN_OUT_DIFF') : invariant(false) : void 0;
|
|
1453
|
+
!trades.every(function (trade) {
|
|
1454
|
+
return trade.tradeType === sampleTrade.tradeType;
|
|
1455
|
+
}) ? process.env.NODE_ENV !== "production" ? invariant(false, 'TRADE_TYPE_DIFF') : invariant(false) : void 0;
|
|
1456
|
+
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
|
+
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
|
|
1465
|
+
|
|
1466
|
+
var mustRefund = inputIsNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT; // flag for whether funds should be send first to the router
|
|
1467
|
+
// 1. when receiving ETH (which much be unwrapped from WETH)
|
|
1468
|
+
// 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
|
|
1471
|
+
|
|
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
|
|
1476
|
+
|
|
1477
|
+
if (options.inputTokenPermit) {
|
|
1478
|
+
!sampleTrade.inputAmount.currency.isToken ? process.env.NODE_ENV !== "production" ? invariant(false, 'NON_TOKEN_PERMIT') : invariant(false) : void 0;
|
|
1479
|
+
calldatas.push(SelfPermit.encodePermit(sampleTrade.inputAmount.currency, options.inputTokenPermit));
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(trades), _step3; !(_step3 = _iterator3()).done;) {
|
|
1483
|
+
var trade = _step3.value;
|
|
1484
|
+
|
|
1485
|
+
if (trade instanceof Trade$2) {
|
|
1486
|
+
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody));
|
|
1487
|
+
} else {
|
|
1488
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody)), _step4; !(_step4 = _iterator4()).done;) {
|
|
1489
|
+
var calldata = _step4.value;
|
|
1490
|
+
calldatas.push(calldata);
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
} // unwrap
|
|
1494
|
+
|
|
1495
|
+
|
|
1496
|
+
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
|
+
if (outputIsNative) {
|
|
1502
|
+
calldatas.push(PaymentsExtended.encodeUnwrapWETH9(amountNecessary, options.recipient, options.fee));
|
|
1503
|
+
} else {
|
|
1504
|
+
calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, amountNecessary, options.recipient, options.fee));
|
|
1505
|
+
}
|
|
1506
|
+
} // refund
|
|
1507
|
+
|
|
1508
|
+
|
|
1509
|
+
if (mustRefund) {
|
|
1510
|
+
calldatas.push(Payments.encodeRefundETH());
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
return {
|
|
1514
|
+
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
1515
|
+
value: toHex(totalValue.quotient)
|
|
1516
|
+
};
|
|
1517
|
+
};
|
|
1518
|
+
|
|
1519
|
+
return SwapRouter;
|
|
1520
|
+
}();
|
|
1521
|
+
SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$2);
|
|
1522
|
+
|
|
1489
1523
|
export { ADDRESS_THIS, MSG_SENDER, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, ZERO };
|
|
1490
1524
|
//# sourceMappingURL=router-sdk.esm.js.map
|