@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.
- package/dist/approveAndCall.d.ts +22 -0
- package/dist/constants.d.ts +2 -2
- package/dist/entities/route.d.ts +2 -0
- package/dist/entities/trade.d.ts +2 -2
- package/dist/router-sdk.cjs.development.js +264 -66
- 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 +269 -71
- package/dist/router-sdk.esm.js.map +1 -1
- package/dist/swapRouter.d.ts +10 -1
- package/package.json +2 -2
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import { Currency, Token } from '@uniswap/sdk-core';
|
|
3
|
+
export declare enum ApprovalTypes {
|
|
4
|
+
NOT_REQUIRED = 0,
|
|
5
|
+
MAX = 1,
|
|
6
|
+
MAX_MINUS_ONE = 2,
|
|
7
|
+
ZERO_THEN_MAX = 3,
|
|
8
|
+
ZERO_THEN_MAX_MINUS_ONE = 4
|
|
9
|
+
}
|
|
10
|
+
export declare abstract class ApproveAndCall {
|
|
11
|
+
static INTERFACE: Interface;
|
|
12
|
+
/**
|
|
13
|
+
* Cannot be constructed.
|
|
14
|
+
*/
|
|
15
|
+
private constructor();
|
|
16
|
+
static encodeApproveMax(token: Token): string;
|
|
17
|
+
static encodeApproveMaxMinusOne(token: Token): string;
|
|
18
|
+
static encodeApproveZeroThenMax(token: Token): string;
|
|
19
|
+
static encodeApproveZeroThenMaxMinusOne(token: Token): string;
|
|
20
|
+
static encodeCallPositionManager(calldatas: string[]): string;
|
|
21
|
+
static encodeApprove(token: Currency, approvalType: ApprovalTypes): string;
|
|
22
|
+
}
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import JSBI from 'jsbi';
|
|
2
|
-
export declare const MSG_SENDER = "
|
|
3
|
-
export declare const ADDRESS_THIS = "
|
|
2
|
+
export declare const MSG_SENDER = "0x0000000000000000000000000000000000000001";
|
|
3
|
+
export declare const ADDRESS_THIS = "0x0000000000000000000000000000000000000002";
|
|
4
4
|
export declare const ZERO: JSBI;
|
|
5
5
|
export declare const ONE: JSBI;
|
package/dist/entities/route.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export interface IRoute<TInput extends Currency, TOutput extends Currency, TPool
|
|
|
7
7
|
pools: TPool[];
|
|
8
8
|
path: Token[];
|
|
9
9
|
midPrice: Price<TInput, TOutput>;
|
|
10
|
+
input: TInput;
|
|
11
|
+
output: TOutput;
|
|
10
12
|
}
|
|
11
13
|
export declare class RouteV2<TInput extends Currency, TOutput extends Currency> extends V2RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, Pair> {
|
|
12
14
|
readonly protocol: Protocol;
|
package/dist/entities/trade.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Currency, CurrencyAmount, Percent, Price, TradeType } from '@uniswap/sdk-core';
|
|
2
|
+
import { Pair, Route as V2RouteSDK } from '@uniswap/v2-sdk';
|
|
3
|
+
import { Pool, Route as V3RouteSDK } from '@uniswap/v3-sdk';
|
|
2
4
|
import { IRoute } from './route';
|
|
3
|
-
import { Route as V2RouteSDK, Pair } from '@uniswap/v2-sdk';
|
|
4
|
-
import { Route as V3RouteSDK, Pool } from '@uniswap/v3-sdk';
|
|
5
5
|
export declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
|
|
6
6
|
readonly routes: IRoute<TInput, TOutput, Pair | Pool>[];
|
|
7
7
|
readonly tradeType: TTradeType;
|
|
@@ -6,16 +6,17 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
|
|
|
6
6
|
|
|
7
7
|
var JSBI = _interopDefault(require('jsbi'));
|
|
8
8
|
var abi = require('@ethersproject/abi');
|
|
9
|
-
var v3Sdk = require('@uniswap/v3-sdk');
|
|
10
9
|
var IMulticallExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json');
|
|
10
|
+
var v3Sdk = require('@uniswap/v3-sdk');
|
|
11
11
|
var sdkCore = require('@uniswap/sdk-core');
|
|
12
12
|
var IPeripheryPaymentsWithFeeExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json');
|
|
13
13
|
var ISwapRouter02_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json');
|
|
14
14
|
var v2Sdk = require('@uniswap/v2-sdk');
|
|
15
15
|
var invariant = _interopDefault(require('tiny-invariant'));
|
|
16
|
+
var IApproveAndCall_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json');
|
|
16
17
|
|
|
17
|
-
var MSG_SENDER = '
|
|
18
|
-
var ADDRESS_THIS = '
|
|
18
|
+
var MSG_SENDER = '0x0000000000000000000000000000000000000001';
|
|
19
|
+
var ADDRESS_THIS = '0x0000000000000000000000000000000000000002';
|
|
19
20
|
var ZERO = /*#__PURE__*/JSBI.BigInt(0);
|
|
20
21
|
var ONE = /*#__PURE__*/JSBI.BigInt(1);
|
|
21
22
|
|
|
@@ -216,10 +217,71 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
|
216
217
|
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
217
218
|
}
|
|
218
219
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
220
|
+
var ApprovalTypes;
|
|
221
|
+
|
|
222
|
+
(function (ApprovalTypes) {
|
|
223
|
+
ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
|
|
224
|
+
ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
|
|
225
|
+
ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
|
|
226
|
+
ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
|
|
227
|
+
ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
|
|
228
|
+
})(ApprovalTypes || (ApprovalTypes = {}));
|
|
229
|
+
|
|
230
|
+
var ApproveAndCall = /*#__PURE__*/function () {
|
|
231
|
+
/**
|
|
232
|
+
* Cannot be constructed.
|
|
233
|
+
*/
|
|
234
|
+
function ApproveAndCall() {}
|
|
235
|
+
|
|
236
|
+
ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
|
|
237
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
|
|
241
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
|
|
245
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
|
|
249
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
|
|
253
|
+
!(calldatas.length > 0) ? invariant(false, 'NULL_CALLDATA') : void 0;
|
|
254
|
+
|
|
255
|
+
if (calldatas.length == 1) {
|
|
256
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
|
|
257
|
+
} else {
|
|
258
|
+
var encodedMulticall = v3Sdk.NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
|
|
259
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
|
|
264
|
+
switch (approvalType) {
|
|
265
|
+
case ApprovalTypes.MAX:
|
|
266
|
+
return ApproveAndCall.encodeApproveMax(token.wrapped);
|
|
267
|
+
|
|
268
|
+
case ApprovalTypes.MAX_MINUS_ONE:
|
|
269
|
+
return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
|
|
270
|
+
|
|
271
|
+
case ApprovalTypes.ZERO_THEN_MAX:
|
|
272
|
+
return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
|
|
273
|
+
|
|
274
|
+
case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
|
|
275
|
+
return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
|
|
276
|
+
|
|
277
|
+
default:
|
|
278
|
+
throw 'Error: invalid ApprovalType';
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
return ApproveAndCall;
|
|
283
|
+
}();
|
|
284
|
+
ApproveAndCall.INTERFACE = /*#__PURE__*/new abi.Interface(IApproveAndCall_json.abi);
|
|
223
285
|
|
|
224
286
|
function createCommonjsModule(fn, module) {
|
|
225
287
|
return module = { exports: {} }, fn(module, module.exports), module.exports;
|
|
@@ -981,6 +1043,11 @@ try {
|
|
|
981
1043
|
}
|
|
982
1044
|
});
|
|
983
1045
|
|
|
1046
|
+
(function (Protocol) {
|
|
1047
|
+
Protocol["V2"] = "V2";
|
|
1048
|
+
Protocol["V3"] = "V3";
|
|
1049
|
+
})(exports.Protocol || (exports.Protocol = {}));
|
|
1050
|
+
|
|
984
1051
|
var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
|
|
985
1052
|
_inheritsLoose(RouteV2, _V2RouteSDK);
|
|
986
1053
|
|
|
@@ -1051,7 +1118,43 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1051
1118
|
});
|
|
1052
1119
|
}
|
|
1053
1120
|
|
|
1054
|
-
this.tradeType = tradeType;
|
|
1121
|
+
this.tradeType = tradeType; // each route must have the same input and output currency
|
|
1122
|
+
|
|
1123
|
+
var inputCurrency = this.swaps[0].inputAmount.currency;
|
|
1124
|
+
var outputCurrency = this.swaps[0].outputAmount.currency;
|
|
1125
|
+
!this.swaps.every(function (_ref2) {
|
|
1126
|
+
var route = _ref2.route;
|
|
1127
|
+
return inputCurrency.wrapped.equals(route.input.wrapped);
|
|
1128
|
+
}) ? invariant(false, 'INPUT_CURRENCY_MATCH') : void 0;
|
|
1129
|
+
!this.swaps.every(function (_ref3) {
|
|
1130
|
+
var route = _ref3.route;
|
|
1131
|
+
return outputCurrency.wrapped.equals(route.output.wrapped);
|
|
1132
|
+
}) ? invariant(false, 'OUTPUT_CURRENCY_MATCH') : void 0; // pools must be unique inter protocols
|
|
1133
|
+
|
|
1134
|
+
var numPools = this.swaps.map(function (_ref4) {
|
|
1135
|
+
var route = _ref4.route;
|
|
1136
|
+
return route.pools.length;
|
|
1137
|
+
}).reduce(function (total, cur) {
|
|
1138
|
+
return total + cur;
|
|
1139
|
+
}, 0);
|
|
1140
|
+
var poolAddressSet = new Set();
|
|
1141
|
+
|
|
1142
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(this.swaps), _step3; !(_step3 = _iterator3()).done;) {
|
|
1143
|
+
var _route2 = _step3.value.route;
|
|
1144
|
+
|
|
1145
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(_route2.pools), _step4; !(_step4 = _iterator4()).done;) {
|
|
1146
|
+
var pool = _step4.value;
|
|
1147
|
+
|
|
1148
|
+
if (_route2.protocol == exports.Protocol.V3) {
|
|
1149
|
+
poolAddressSet.add(v3Sdk.Pool.getAddress(pool.token0, pool.token1, pool.fee));
|
|
1150
|
+
} else {
|
|
1151
|
+
var pair = pool;
|
|
1152
|
+
poolAddressSet.add(v2Sdk.Pair.getAddress(pair.token0, pair.token1));
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
!(numPools == poolAddressSet.size) ? invariant(false, 'POOLS_DUPLICATED') : void 0;
|
|
1055
1158
|
}
|
|
1056
1159
|
|
|
1057
1160
|
var _proto = Trade.prototype;
|
|
@@ -1109,7 +1212,7 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1109
1212
|
|
|
1110
1213
|
Trade.fromRoutes = /*#__PURE__*/function () {
|
|
1111
1214
|
var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(v2Routes, v3Routes, tradeType) {
|
|
1112
|
-
var populatedV2Routes, populatedV3Routes,
|
|
1215
|
+
var populatedV2Routes, populatedV3Routes, _iterator5, _step5, _step5$value, routev2, amount, v2Trade, inputAmount, outputAmount, _iterator6, _step6, _step6$value, routev3, _amount, v3Trade, _inputAmount2, _outputAmount2;
|
|
1113
1216
|
|
|
1114
1217
|
return runtime_1.wrap(function _callee$(_context) {
|
|
1115
1218
|
while (1) {
|
|
@@ -1118,8 +1221,8 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1118
1221
|
populatedV2Routes = [];
|
|
1119
1222
|
populatedV3Routes = [];
|
|
1120
1223
|
|
|
1121
|
-
for (
|
|
1122
|
-
|
|
1224
|
+
for (_iterator5 = _createForOfIteratorHelperLoose(v2Routes); !(_step5 = _iterator5()).done;) {
|
|
1225
|
+
_step5$value = _step5.value, routev2 = _step5$value.routev2, amount = _step5$value.amount;
|
|
1123
1226
|
v2Trade = new v2Sdk.Trade(routev2, amount, tradeType);
|
|
1124
1227
|
inputAmount = v2Trade.inputAmount, outputAmount = v2Trade.outputAmount;
|
|
1125
1228
|
populatedV2Routes.push({
|
|
@@ -1129,15 +1232,15 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1129
1232
|
});
|
|
1130
1233
|
}
|
|
1131
1234
|
|
|
1132
|
-
|
|
1235
|
+
_iterator6 = _createForOfIteratorHelperLoose(v3Routes);
|
|
1133
1236
|
|
|
1134
1237
|
case 4:
|
|
1135
|
-
if ((
|
|
1238
|
+
if ((_step6 = _iterator6()).done) {
|
|
1136
1239
|
_context.next = 13;
|
|
1137
1240
|
break;
|
|
1138
1241
|
}
|
|
1139
1242
|
|
|
1140
|
-
|
|
1243
|
+
_step6$value = _step6.value, routev3 = _step6$value.routev3, _amount = _step6$value.amount;
|
|
1141
1244
|
_context.next = 8;
|
|
1142
1245
|
return v3Sdk.Trade.fromRoute(routev3, _amount, tradeType);
|
|
1143
1246
|
|
|
@@ -1244,8 +1347,8 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1244
1347
|
}
|
|
1245
1348
|
|
|
1246
1349
|
var inputCurrency = this.swaps[0].inputAmount.currency;
|
|
1247
|
-
var totalInputFromRoutes = this.swaps.map(function (
|
|
1248
|
-
var inputAmount =
|
|
1350
|
+
var totalInputFromRoutes = this.swaps.map(function (_ref5) {
|
|
1351
|
+
var inputAmount = _ref5.inputAmount;
|
|
1249
1352
|
return inputAmount;
|
|
1250
1353
|
}).reduce(function (total, cur) {
|
|
1251
1354
|
return total.add(cur);
|
|
@@ -1261,8 +1364,8 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1261
1364
|
}
|
|
1262
1365
|
|
|
1263
1366
|
var outputCurrency = this.swaps[0].outputAmount.currency;
|
|
1264
|
-
var totalOutputFromRoutes = this.swaps.map(function (
|
|
1265
|
-
var outputAmount =
|
|
1367
|
+
var totalOutputFromRoutes = this.swaps.map(function (_ref6) {
|
|
1368
|
+
var outputAmount = _ref6.outputAmount;
|
|
1266
1369
|
return outputAmount;
|
|
1267
1370
|
}).reduce(function (total, cur) {
|
|
1268
1371
|
return total.add(cur);
|
|
@@ -1294,10 +1397,10 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1294
1397
|
|
|
1295
1398
|
var spotOutputAmount = sdkCore.CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
|
|
1296
1399
|
|
|
1297
|
-
for (var
|
|
1298
|
-
var
|
|
1299
|
-
route =
|
|
1300
|
-
inputAmount =
|
|
1400
|
+
for (var _iterator7 = _createForOfIteratorHelperLoose(this.swaps), _step7; !(_step7 = _iterator7()).done;) {
|
|
1401
|
+
var _step7$value = _step7.value,
|
|
1402
|
+
route = _step7$value.route,
|
|
1403
|
+
inputAmount = _step7$value.inputAmount;
|
|
1301
1404
|
var midPrice = route.midPrice;
|
|
1302
1405
|
spotOutputAmount = spotOutputAmount.add(midPrice.quote(inputAmount));
|
|
1303
1406
|
}
|
|
@@ -1322,7 +1425,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1322
1425
|
*/
|
|
1323
1426
|
function SwapRouter() {}
|
|
1324
1427
|
|
|
1325
|
-
SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody) {
|
|
1428
|
+
SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
1326
1429
|
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance).quotient);
|
|
1327
1430
|
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance).quotient);
|
|
1328
1431
|
var path = trade.route.path.map(function (token) {
|
|
@@ -1331,9 +1434,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1331
1434
|
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : sdkCore.validateAndParseAddress(options.recipient);
|
|
1332
1435
|
|
|
1333
1436
|
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1334
|
-
var exactInputParams = [amountIn,
|
|
1335
|
-
// not a pure win, as sometimes this will cost us more when it would have caused an earlier failure
|
|
1336
|
-
routerMustCustody ? 0 : amountOut, path, recipient, false];
|
|
1437
|
+
var exactInputParams = [amountIn, performAggregatedSlippageCheck ? 0 : amountOut, path, recipient];
|
|
1337
1438
|
return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
|
|
1338
1439
|
} else {
|
|
1339
1440
|
var exactOutputParams = [amountOut, amountIn, path, recipient];
|
|
@@ -1341,7 +1442,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1341
1442
|
}
|
|
1342
1443
|
};
|
|
1343
1444
|
|
|
1344
|
-
SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody) {
|
|
1445
|
+
SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
1345
1446
|
var calldatas = [];
|
|
1346
1447
|
|
|
1347
1448
|
for (var _iterator = _createForOfIteratorHelperLoose(trade.swaps), _step; !(_step = _iterator()).done;) {
|
|
@@ -1363,9 +1464,8 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1363
1464
|
fee: route.pools[0].fee,
|
|
1364
1465
|
recipient: recipient,
|
|
1365
1466
|
amountIn: amountIn,
|
|
1366
|
-
amountOutMinimum: amountOut,
|
|
1367
|
-
sqrtPriceLimitX96: 0
|
|
1368
|
-
hasAlreadyPaid: false
|
|
1467
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut,
|
|
1468
|
+
sqrtPriceLimitX96: 0
|
|
1369
1469
|
};
|
|
1370
1470
|
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
1371
1471
|
} else {
|
|
@@ -1388,8 +1488,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1388
1488
|
path: path,
|
|
1389
1489
|
recipient: recipient,
|
|
1390
1490
|
amountIn: amountIn,
|
|
1391
|
-
amountOutMinimum: amountOut
|
|
1392
|
-
hasAlreadyPaid: false
|
|
1491
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut
|
|
1393
1492
|
};
|
|
1394
1493
|
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
1395
1494
|
} else {
|
|
@@ -1405,15 +1504,9 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1405
1504
|
}
|
|
1406
1505
|
|
|
1407
1506
|
return calldatas;
|
|
1408
|
-
}
|
|
1409
|
-
/**
|
|
1410
|
-
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
1411
|
-
* @param trade to produce call parameters for
|
|
1412
|
-
* @param options options for the call parameters
|
|
1413
|
-
*/
|
|
1414
|
-
;
|
|
1507
|
+
};
|
|
1415
1508
|
|
|
1416
|
-
SwapRouter.
|
|
1509
|
+
SwapRouter.encodeSwaps = function encodeSwaps(trades, options, isSwapAndAdd) {
|
|
1417
1510
|
// If dealing with an instance of the aggregated Trade object, unbundle it to individual V2Trade and V3Trade objects.
|
|
1418
1511
|
if (trades instanceof Trade) {
|
|
1419
1512
|
!trades.swaps.every(function (swap) {
|
|
@@ -1446,6 +1539,9 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1446
1539
|
trades = [trades];
|
|
1447
1540
|
}
|
|
1448
1541
|
|
|
1542
|
+
var numberOfTrades = trades.reduce(function (numberOfTrades, trade) {
|
|
1543
|
+
return numberOfTrades + (trade instanceof v3Sdk.Trade ? trade.swaps.length : 1);
|
|
1544
|
+
}, 0);
|
|
1449
1545
|
var sampleTrade = trades[0]; // All trades should have the same starting/ending currency and trade type
|
|
1450
1546
|
|
|
1451
1547
|
!trades.every(function (trade) {
|
|
@@ -1458,25 +1554,19 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1458
1554
|
return trade.tradeType === sampleTrade.tradeType;
|
|
1459
1555
|
}) ? invariant(false, 'TRADE_TYPE_DIFF') : void 0;
|
|
1460
1556
|
var calldatas = [];
|
|
1461
|
-
var ZERO_IN = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
|
|
1462
|
-
var ZERO_OUT = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
|
|
1463
|
-
var totalAmountOut = trades.reduce(function (sum, trade) {
|
|
1464
|
-
return sum.add(trade.minimumAmountOut(options.slippageTolerance));
|
|
1465
|
-
}, ZERO_OUT);
|
|
1466
1557
|
var inputIsNative = sampleTrade.inputAmount.currency.isNative;
|
|
1467
|
-
var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether
|
|
1468
|
-
// 1. when
|
|
1558
|
+
var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether we want to perform an aggregated slippage check
|
|
1559
|
+
// 1. when there are >2 exact input trades. this is only a heuristic,
|
|
1560
|
+
// as it's still more gas-expensive even in this case, but has benefits
|
|
1561
|
+
// in that the reversion probability is lower
|
|
1469
1562
|
|
|
1470
|
-
var
|
|
1563
|
+
var performAggregatedSlippageCheck = sampleTrade.tradeType === sdkCore.TradeType.EXACT_INPUT && numberOfTrades > 2; // flag for whether funds should be send first to the router
|
|
1471
1564
|
// 1. when receiving ETH (which much be unwrapped from WETH)
|
|
1472
1565
|
// 2. when a fee on the output is being taken
|
|
1473
|
-
// 3. when
|
|
1474
|
-
//
|
|
1566
|
+
// 3. when performing swap and add
|
|
1567
|
+
// 4. when performing an aggregated slippage check
|
|
1475
1568
|
|
|
1476
|
-
var routerMustCustody = outputIsNative || !!options.fee ||
|
|
1477
|
-
var totalValue = inputIsNative ? trades.reduce(function (sum, trade) {
|
|
1478
|
-
return sum.add(trade.maximumAmountIn(options.slippageTolerance));
|
|
1479
|
-
}, ZERO_IN) : ZERO_IN; // encode permit if necessary
|
|
1569
|
+
var routerMustCustody = outputIsNative || !!options.fee || !!isSwapAndAdd || performAggregatedSlippageCheck; // encode permit if necessary
|
|
1480
1570
|
|
|
1481
1571
|
if (options.inputTokenPermit) {
|
|
1482
1572
|
!sampleTrade.inputAmount.currency.isToken ? invariant(false, 'NON_TOKEN_PERMIT') : void 0;
|
|
@@ -1487,36 +1577,144 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
1487
1577
|
var trade = _step3.value;
|
|
1488
1578
|
|
|
1489
1579
|
if (trade instanceof v2Sdk.Trade) {
|
|
1490
|
-
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody));
|
|
1580
|
+
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck));
|
|
1491
1581
|
} else {
|
|
1492
|
-
for (var _iterator4 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody)), _step4; !(_step4 = _iterator4()).done;) {
|
|
1582
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck)), _step4; !(_step4 = _iterator4()).done;) {
|
|
1493
1583
|
var calldata = _step4.value;
|
|
1494
1584
|
calldatas.push(calldata);
|
|
1495
1585
|
}
|
|
1496
1586
|
}
|
|
1497
|
-
}
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
var ZERO_IN = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
|
|
1590
|
+
var ZERO_OUT = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
|
|
1591
|
+
var totalAmountOut = trades.reduce(function (sum, trade) {
|
|
1592
|
+
return sum.add(trade.minimumAmountOut(options.slippageTolerance));
|
|
1593
|
+
}, ZERO_OUT);
|
|
1594
|
+
var totalAmountIn = trades.reduce(function (sum, trade) {
|
|
1595
|
+
return sum.add(trade.maximumAmountIn(options.slippageTolerance));
|
|
1596
|
+
}, ZERO_IN);
|
|
1597
|
+
return {
|
|
1598
|
+
calldatas: calldatas,
|
|
1599
|
+
sampleTrade: sampleTrade,
|
|
1600
|
+
routerMustCustody: routerMustCustody,
|
|
1601
|
+
inputIsNative: inputIsNative,
|
|
1602
|
+
outputIsNative: outputIsNative,
|
|
1603
|
+
totalAmountIn: totalAmountIn,
|
|
1604
|
+
totalAmountOut: totalAmountOut
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
/**
|
|
1608
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
1609
|
+
* @param trade to produce call parameters for
|
|
1610
|
+
* @param options options for the call parameters
|
|
1611
|
+
*/
|
|
1612
|
+
;
|
|
1613
|
+
|
|
1614
|
+
SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
|
|
1615
|
+
var _SwapRouter$encodeSwa = SwapRouter.encodeSwaps(trades, options),
|
|
1616
|
+
calldatas = _SwapRouter$encodeSwa.calldatas,
|
|
1617
|
+
sampleTrade = _SwapRouter$encodeSwa.sampleTrade,
|
|
1618
|
+
routerMustCustody = _SwapRouter$encodeSwa.routerMustCustody,
|
|
1619
|
+
inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
|
|
1620
|
+
outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
|
|
1621
|
+
totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
|
|
1622
|
+
totalAmountOut = _SwapRouter$encodeSwa.totalAmountOut; // unwrap or sweep
|
|
1498
1623
|
|
|
1499
1624
|
|
|
1500
1625
|
if (routerMustCustody) {
|
|
1501
|
-
// if all trades are exact output, we can save gas by not passing a slippage check
|
|
1502
|
-
var canOmitSlippageCheck = sampleTrade.tradeType === sdkCore.TradeType.EXACT_OUTPUT;
|
|
1503
|
-
var amountNecessary = canOmitSlippageCheck ? ZERO$1 : totalAmountOut.quotient;
|
|
1504
|
-
|
|
1505
1626
|
if (outputIsNative) {
|
|
1506
|
-
calldatas.push(PaymentsExtended.encodeUnwrapWETH9(
|
|
1627
|
+
calldatas.push(PaymentsExtended.encodeUnwrapWETH9(totalAmountOut.quotient, options.recipient, options.fee));
|
|
1507
1628
|
} else {
|
|
1508
|
-
calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped,
|
|
1629
|
+
calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, totalAmountOut.quotient, options.recipient, options.fee));
|
|
1509
1630
|
}
|
|
1510
|
-
} // refund
|
|
1631
|
+
} // must refund when paying in ETH, but with an uncertain input amount
|
|
1511
1632
|
|
|
1512
1633
|
|
|
1513
|
-
if (
|
|
1634
|
+
if (inputIsNative && sampleTrade.tradeType === sdkCore.TradeType.EXACT_OUTPUT) {
|
|
1514
1635
|
calldatas.push(v3Sdk.Payments.encodeRefundETH());
|
|
1515
1636
|
}
|
|
1516
1637
|
|
|
1517
1638
|
return {
|
|
1518
1639
|
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
1519
|
-
value: v3Sdk.toHex(
|
|
1640
|
+
value: v3Sdk.toHex(inputIsNative ? totalAmountIn.quotient : ZERO$1)
|
|
1641
|
+
};
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
1645
|
+
* @param trade to produce call parameters for
|
|
1646
|
+
* @param options options for the call parameters
|
|
1647
|
+
*/
|
|
1648
|
+
;
|
|
1649
|
+
|
|
1650
|
+
SwapRouter.swapAndAddCallParameters = function swapAndAddCallParameters(trades, options, position, addLiquidityOptions, tokenInApprovalType, tokenOutApprovalType) {
|
|
1651
|
+
var _SwapRouter$encodeSwa2 = SwapRouter.encodeSwaps(trades, options, true),
|
|
1652
|
+
calldatas = _SwapRouter$encodeSwa2.calldatas,
|
|
1653
|
+
inputIsNative = _SwapRouter$encodeSwa2.inputIsNative,
|
|
1654
|
+
outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
|
|
1655
|
+
sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
|
|
1656
|
+
totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
|
|
1657
|
+
totalAmountOut = _SwapRouter$encodeSwa2.totalAmountOut;
|
|
1658
|
+
|
|
1659
|
+
var chainId = sampleTrade.route.chainId;
|
|
1660
|
+
var zeroForOne = position.pool.token0 === totalAmountSwapped.currency.wrapped;
|
|
1661
|
+
|
|
1662
|
+
var _SwapRouter$getPositi = SwapRouter.getPositionAmounts(position, zeroForOne),
|
|
1663
|
+
positionAmountIn = _SwapRouter$getPositi.positionAmountIn,
|
|
1664
|
+
positionAmountOut = _SwapRouter$getPositi.positionAmountOut; // if tokens are native they will be converted to WETH9
|
|
1665
|
+
|
|
1666
|
+
|
|
1667
|
+
var tokenIn = inputIsNative ? sdkCore.WETH9[chainId] : positionAmountIn.currency.wrapped;
|
|
1668
|
+
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
|
|
1669
|
+
|
|
1670
|
+
var amountOutRemaining = positionAmountOut.subtract(totalAmountOut.wrapped);
|
|
1671
|
+
|
|
1672
|
+
if (amountOutRemaining.greaterThan(sdkCore.CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
|
|
1673
|
+
// if output is native, this means the remaining portion is included as native value in the transaction
|
|
1674
|
+
// and must be wrapped. Otherwise, pull in remaining ERC20 token.
|
|
1675
|
+
outputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(amountOutRemaining.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenOut, amountOutRemaining.quotient));
|
|
1676
|
+
} // if input is native, convert to WETH9, else pull ERC20 token
|
|
1677
|
+
|
|
1678
|
+
|
|
1679
|
+
inputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(positionAmountIn.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenIn, positionAmountIn.quotient)); // approve token balances to NFTManager
|
|
1680
|
+
|
|
1681
|
+
if (tokenInApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
|
|
1682
|
+
if (tokenOutApprovalType !== ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType)); // encode NFTManager add liquidity
|
|
1683
|
+
|
|
1684
|
+
calldatas.push(ApproveAndCall.encodeCallPositionManager([v3Sdk.NonfungiblePositionManager.addCallParameters(position, addLiquidityOptions).calldata])); // sweep remaining tokens
|
|
1685
|
+
|
|
1686
|
+
inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
|
|
1687
|
+
outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
|
|
1688
|
+
var value;
|
|
1689
|
+
|
|
1690
|
+
if (inputIsNative) {
|
|
1691
|
+
value = totalAmountSwapped.wrapped.add(positionAmountIn.wrapped).quotient;
|
|
1692
|
+
} else if (outputIsNative) {
|
|
1693
|
+
value = amountOutRemaining.quotient;
|
|
1694
|
+
} else {
|
|
1695
|
+
value = ZERO$1;
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
return {
|
|
1699
|
+
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
1700
|
+
value: value.toString()
|
|
1701
|
+
};
|
|
1702
|
+
};
|
|
1703
|
+
|
|
1704
|
+
SwapRouter.getPositionAmounts = function getPositionAmounts(position, zeroForOne) {
|
|
1705
|
+
var _position$mintAmounts = position.mintAmounts,
|
|
1706
|
+
amount0 = _position$mintAmounts.amount0,
|
|
1707
|
+
amount1 = _position$mintAmounts.amount1;
|
|
1708
|
+
var currencyAmount0 = sdkCore.CurrencyAmount.fromRawAmount(position.pool.token0, amount0);
|
|
1709
|
+
var currencyAmount1 = sdkCore.CurrencyAmount.fromRawAmount(position.pool.token1, amount1);
|
|
1710
|
+
|
|
1711
|
+
var _ref = zeroForOne ? [currencyAmount0, currencyAmount1] : [currencyAmount1, currencyAmount0],
|
|
1712
|
+
positionAmountIn = _ref[0],
|
|
1713
|
+
positionAmountOut = _ref[1];
|
|
1714
|
+
|
|
1715
|
+
return {
|
|
1716
|
+
positionAmountIn: positionAmountIn,
|
|
1717
|
+
positionAmountOut: positionAmountOut
|
|
1520
1718
|
};
|
|
1521
1719
|
};
|
|
1522
1720
|
|