@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/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Alpha software
|
|
2
|
+
|
|
3
|
+
The latest version of the SDK is used in production in the Uniswap Interface,
|
|
4
|
+
but it is considered Alpha software and may contain bugs or change significantly between patch versions.
|
|
5
|
+
If you have questions about how to use the SDK, please reach out in the `#dev-chat` channel of the Discord.
|
|
6
|
+
Pull requests welcome!
|
|
7
|
+
|
|
8
|
+
# Uniswap Swap Router SDK
|
|
9
|
+
|
|
10
|
+
This SDK is meant to facilitate interactions with the contracts in [swap-router-contracts](https://github.com/Uniswap/swap-router-contracts).
|
package/dist/entities/route.d.ts
CHANGED
|
@@ -2,18 +2,18 @@ import { Route as V2RouteSDK, Pair } from '@uniswap/v2-sdk';
|
|
|
2
2
|
import { Route as V3RouteSDK, Pool } from '@uniswap/v3-sdk';
|
|
3
3
|
import { Protocol } from './protocol';
|
|
4
4
|
import { Currency, Price, Token } from '@uniswap/sdk-core';
|
|
5
|
-
export interface IRoute<TInput extends Currency, TOutput extends Currency> {
|
|
5
|
+
export interface IRoute<TInput extends Currency, TOutput extends Currency, TPool extends Pool | Pair> {
|
|
6
6
|
protocol: Protocol;
|
|
7
|
-
pools:
|
|
7
|
+
pools: TPool[];
|
|
8
8
|
path: Token[];
|
|
9
9
|
midPrice: Price<TInput, TOutput>;
|
|
10
10
|
}
|
|
11
|
-
export declare class RouteV2<TInput extends Currency, TOutput extends Currency> extends V2RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput> {
|
|
11
|
+
export declare class RouteV2<TInput extends Currency, TOutput extends Currency> extends V2RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, Pair> {
|
|
12
12
|
readonly protocol: Protocol;
|
|
13
13
|
readonly pools: Pair[];
|
|
14
14
|
constructor(v2Route: V2RouteSDK<TInput, TOutput>);
|
|
15
15
|
}
|
|
16
|
-
export declare class RouteV3<TInput extends Currency, TOutput extends Currency> extends V3RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput> {
|
|
16
|
+
export declare class RouteV3<TInput extends Currency, TOutput extends Currency> extends V3RouteSDK<TInput, TOutput> implements IRoute<TInput, TOutput, Pool> {
|
|
17
17
|
readonly protocol: Protocol;
|
|
18
18
|
readonly path: Token[];
|
|
19
19
|
constructor(v3Route: V3RouteSDK<TInput, TOutput>);
|
package/dist/entities/trade.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Currency, CurrencyAmount, Percent, Price, TradeType } from '@uniswap/sdk-core';
|
|
2
2
|
import { IRoute } from './route';
|
|
3
|
-
import { Route as V2RouteSDK } from '@uniswap/v2-sdk';
|
|
4
|
-
import { Route as V3RouteSDK } from '@uniswap/v3-sdk';
|
|
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
|
-
readonly routes: IRoute<TInput, TOutput>[];
|
|
6
|
+
readonly routes: IRoute<TInput, TOutput, Pair | Pool>[];
|
|
7
7
|
readonly tradeType: TTradeType;
|
|
8
8
|
private _outputAmount;
|
|
9
9
|
private _inputAmount;
|
|
@@ -12,7 +12,7 @@ export declare class Trade<TInput extends Currency, TOutput extends Currency, TT
|
|
|
12
12
|
* make up the trade. May consist of swaps in v2 or v3.
|
|
13
13
|
*/
|
|
14
14
|
readonly swaps: {
|
|
15
|
-
route: IRoute<TInput, TOutput>;
|
|
15
|
+
route: IRoute<TInput, TOutput, Pair | Pool>;
|
|
16
16
|
inputAmount: CurrencyAmount<TInput>;
|
|
17
17
|
outputAmount: CurrencyAmount<TOutput>;
|
|
18
18
|
}[];
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import JSBI from 'jsbi';
|
|
2
1
|
import { Interface } from '@ethersproject/abi';
|
|
3
|
-
import { FeeOptions } from '@uniswap/v3-sdk';
|
|
4
2
|
import { Token } from '@uniswap/sdk-core';
|
|
3
|
+
import { FeeOptions } from '@uniswap/v3-sdk';
|
|
4
|
+
import JSBI from 'jsbi';
|
|
5
5
|
export declare abstract class PaymentsExtended {
|
|
6
6
|
static INTERFACE: Interface;
|
|
7
7
|
/**
|
|
8
8
|
* Cannot be constructed.
|
|
9
9
|
*/
|
|
10
10
|
private constructor();
|
|
11
|
-
private static encodeFeeBips;
|
|
12
11
|
static encodeUnwrapWETH9(amountMinimum: JSBI, recipient?: string, feeOptions?: FeeOptions): string;
|
|
13
12
|
static encodeSweepToken(token: Token, amountMinimum: JSBI, recipient?: string, feeOptions?: FeeOptions): string;
|
|
14
13
|
static encodePull(token: Token, amount: JSBI): string;
|
|
14
|
+
static encodeWrapETH(amount: JSBI): string;
|
|
15
15
|
}
|
|
@@ -8,11 +8,11 @@ var JSBI = _interopDefault(require('jsbi'));
|
|
|
8
8
|
var abi = require('@ethersproject/abi');
|
|
9
9
|
var v3Sdk = require('@uniswap/v3-sdk');
|
|
10
10
|
var IMulticallExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json');
|
|
11
|
-
var IPeripheryPaymentsWithFeeExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json');
|
|
12
11
|
var sdkCore = require('@uniswap/sdk-core');
|
|
13
|
-
var
|
|
12
|
+
var IPeripheryPaymentsWithFeeExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json');
|
|
14
13
|
var ISwapRouter02_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json');
|
|
15
14
|
var v2Sdk = require('@uniswap/v2-sdk');
|
|
15
|
+
var invariant = _interopDefault(require('tiny-invariant'));
|
|
16
16
|
|
|
17
17
|
var MSG_SENDER = '0x0000000000000000000000000000000000000000';
|
|
18
18
|
var ADDRESS_THIS = '0x0000000000000000000000000000000000000001';
|
|
@@ -45,7 +45,7 @@ var MulticallExtended = /*#__PURE__*/function () {
|
|
|
45
45
|
} // this means the validation value should be a previousBlockhash
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
if (typeof validation === 'string') {
|
|
48
|
+
if (typeof validation === 'string' && validation.startsWith('0x')) {
|
|
49
49
|
var previousBlockhash = validateAndParseBytes32(validation);
|
|
50
50
|
return MulticallExtended.INTERFACE.encodeFunctionData('multicall(bytes32,bytes[])', [previousBlockhash, calldatas]);
|
|
51
51
|
} else {
|
|
@@ -58,16 +58,16 @@ var MulticallExtended = /*#__PURE__*/function () {
|
|
|
58
58
|
}();
|
|
59
59
|
MulticallExtended.INTERFACE = /*#__PURE__*/new abi.Interface(IMulticallExtended_json.abi);
|
|
60
60
|
|
|
61
|
+
function encodeFeeBips(fee) {
|
|
62
|
+
return v3Sdk.toHex(fee.multiply(10000).quotient);
|
|
63
|
+
}
|
|
64
|
+
|
|
61
65
|
var PaymentsExtended = /*#__PURE__*/function () {
|
|
62
66
|
/**
|
|
63
67
|
* Cannot be constructed.
|
|
64
68
|
*/
|
|
65
69
|
function PaymentsExtended() {}
|
|
66
70
|
|
|
67
|
-
PaymentsExtended.encodeFeeBips = function encodeFeeBips(fee) {
|
|
68
|
-
return v3Sdk.toHex(fee.multiply(10000).quotient);
|
|
69
|
-
};
|
|
70
|
-
|
|
71
71
|
PaymentsExtended.encodeUnwrapWETH9 = function encodeUnwrapWETH9(amountMinimum, recipient, feeOptions) {
|
|
72
72
|
// if there's a recipient, just pass it along
|
|
73
73
|
if (typeof recipient === 'string') {
|
|
@@ -75,7 +75,7 @@ var PaymentsExtended = /*#__PURE__*/function () {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
if (!!feeOptions) {
|
|
78
|
-
var feeBips =
|
|
78
|
+
var feeBips = encodeFeeBips(feeOptions.fee);
|
|
79
79
|
var feeRecipient = sdkCore.validateAndParseAddress(feeOptions.recipient);
|
|
80
80
|
return PaymentsExtended.INTERFACE.encodeFunctionData('unwrapWETH9WithFee(uint256,uint256,address)', [v3Sdk.toHex(amountMinimum), feeBips, feeRecipient]);
|
|
81
81
|
} else {
|
|
@@ -90,7 +90,7 @@ var PaymentsExtended = /*#__PURE__*/function () {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
if (!!feeOptions) {
|
|
93
|
-
var feeBips =
|
|
93
|
+
var feeBips = encodeFeeBips(feeOptions.fee);
|
|
94
94
|
var feeRecipient = sdkCore.validateAndParseAddress(feeOptions.recipient);
|
|
95
95
|
return PaymentsExtended.INTERFACE.encodeFunctionData('sweepTokenWithFee(address,uint256,uint256,address)', [token.address, v3Sdk.toHex(amountMinimum), feeBips, feeRecipient]);
|
|
96
96
|
} else {
|
|
@@ -102,6 +102,10 @@ var PaymentsExtended = /*#__PURE__*/function () {
|
|
|
102
102
|
return PaymentsExtended.INTERFACE.encodeFunctionData('pull', [token.address, v3Sdk.toHex(amount)]);
|
|
103
103
|
};
|
|
104
104
|
|
|
105
|
+
PaymentsExtended.encodeWrapETH = function encodeWrapETH(amount) {
|
|
106
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('wrapETH', [v3Sdk.toHex(amount)]);
|
|
107
|
+
};
|
|
108
|
+
|
|
105
109
|
return PaymentsExtended;
|
|
106
110
|
}();
|
|
107
111
|
PaymentsExtended.INTERFACE = /*#__PURE__*/new abi.Interface(IPeripheryPaymentsWithFeeExtended_json.abi);
|
|
@@ -212,188 +216,10 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
|
212
216
|
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
213
217
|
}
|
|
214
218
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
var SwapRouter = /*#__PURE__*/function () {
|
|
221
|
-
/**
|
|
222
|
-
* Cannot be constructed.
|
|
223
|
-
*/
|
|
224
|
-
function SwapRouter() {}
|
|
225
|
-
|
|
226
|
-
SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody) {
|
|
227
|
-
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance).quotient);
|
|
228
|
-
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance).quotient);
|
|
229
|
-
var path = trade.route.path.map(function (token) {
|
|
230
|
-
return token.address;
|
|
231
|
-
});
|
|
232
|
-
var recipient = routerMustCustody ? ADDRESS_THIS : options.recipient ? sdkCore.validateAndParseAddress(options.recipient) : MSG_SENDER;
|
|
233
|
-
|
|
234
|
-
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
235
|
-
var exactInputParams = [amountIn, // save gas by only passing slippage check if we can't check it later
|
|
236
|
-
// not a pure win, as sometimes this will cost us more when it would have caused an earlier failure
|
|
237
|
-
routerMustCustody ? 0 : amountOut, path, recipient];
|
|
238
|
-
return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
|
|
239
|
-
} else {
|
|
240
|
-
var exactOutputParams = [amountOut, amountIn, path, recipient];
|
|
241
|
-
return SwapRouter.INTERFACE.encodeFunctionData('swapTokensForExactTokens', exactOutputParams);
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody) {
|
|
246
|
-
var calldatas = [];
|
|
247
|
-
|
|
248
|
-
for (var _iterator = _createForOfIteratorHelperLoose(trade.swaps), _step; !(_step = _iterator()).done;) {
|
|
249
|
-
var _step$value = _step.value,
|
|
250
|
-
route = _step$value.route,
|
|
251
|
-
inputAmount = _step$value.inputAmount,
|
|
252
|
-
outputAmount = _step$value.outputAmount;
|
|
253
|
-
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
254
|
-
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient); // flag for whether the trade is single hop or not
|
|
255
|
-
|
|
256
|
-
var singleHop = route.pools.length === 1;
|
|
257
|
-
var recipient = routerMustCustody ? ADDRESS_THIS : options.recipient ? sdkCore.validateAndParseAddress(options.recipient) : MSG_SENDER;
|
|
258
|
-
|
|
259
|
-
if (singleHop) {
|
|
260
|
-
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
261
|
-
var exactInputSingleParams = {
|
|
262
|
-
tokenIn: route.tokenPath[0].address,
|
|
263
|
-
tokenOut: route.tokenPath[1].address,
|
|
264
|
-
fee: route.pools[0].fee,
|
|
265
|
-
recipient: recipient,
|
|
266
|
-
amountIn: amountIn,
|
|
267
|
-
amountOutMinimum: amountOut,
|
|
268
|
-
sqrtPriceLimitX96: 0
|
|
269
|
-
};
|
|
270
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
271
|
-
} else {
|
|
272
|
-
var exactOutputSingleParams = {
|
|
273
|
-
tokenIn: route.tokenPath[0].address,
|
|
274
|
-
tokenOut: route.tokenPath[1].address,
|
|
275
|
-
fee: route.pools[0].fee,
|
|
276
|
-
recipient: recipient,
|
|
277
|
-
amountOut: amountOut,
|
|
278
|
-
amountInMaximum: amountIn,
|
|
279
|
-
sqrtPriceLimitX96: 0
|
|
280
|
-
};
|
|
281
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutputSingle', [exactOutputSingleParams]));
|
|
282
|
-
}
|
|
283
|
-
} else {
|
|
284
|
-
var path = v3Sdk.encodeRouteToPath(route, trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT);
|
|
285
|
-
|
|
286
|
-
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
287
|
-
var exactInputParams = {
|
|
288
|
-
path: path,
|
|
289
|
-
recipient: recipient,
|
|
290
|
-
amountIn: amountIn,
|
|
291
|
-
amountOutMinimum: amountOut
|
|
292
|
-
};
|
|
293
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
294
|
-
} else {
|
|
295
|
-
var exactOutputParams = {
|
|
296
|
-
path: path,
|
|
297
|
-
recipient: recipient,
|
|
298
|
-
amountOut: amountOut,
|
|
299
|
-
amountInMaximum: amountIn
|
|
300
|
-
};
|
|
301
|
-
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutput', [exactOutputParams]));
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
return calldatas;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
310
|
-
* @param trade to produce call parameters for
|
|
311
|
-
* @param options options for the call parameters
|
|
312
|
-
*/
|
|
313
|
-
;
|
|
314
|
-
|
|
315
|
-
SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
|
|
316
|
-
if (!Array.isArray(trades)) {
|
|
317
|
-
trades = [trades];
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
var sampleTrade = trades[0]; // All trades should have the same starting/ending currency and trade type
|
|
321
|
-
|
|
322
|
-
!trades.every(function (trade) {
|
|
323
|
-
return trade.inputAmount.currency.equals(sampleTrade.inputAmount.currency);
|
|
324
|
-
}) ? invariant(false, 'TOKEN_IN_DIFF') : void 0;
|
|
325
|
-
!trades.every(function (trade) {
|
|
326
|
-
return trade.outputAmount.currency.equals(sampleTrade.outputAmount.currency);
|
|
327
|
-
}) ? invariant(false, 'TOKEN_OUT_DIFF') : void 0;
|
|
328
|
-
!trades.every(function (trade) {
|
|
329
|
-
return trade.tradeType === sampleTrade.tradeType;
|
|
330
|
-
}) ? invariant(false, 'TRADE_TYPE_DIFF') : void 0;
|
|
331
|
-
var calldatas = [];
|
|
332
|
-
var ZERO_IN = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
|
|
333
|
-
var ZERO_OUT = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
|
|
334
|
-
var totalAmountOut = trades.reduce(function (sum, trade) {
|
|
335
|
-
return sum.add(trade.minimumAmountOut(options.slippageTolerance));
|
|
336
|
-
}, ZERO_OUT);
|
|
337
|
-
var inputIsNative = sampleTrade.inputAmount.currency.isNative;
|
|
338
|
-
var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether a refund needs to happen
|
|
339
|
-
// 1. when paying in ETH, but with an uncertain input amount
|
|
340
|
-
|
|
341
|
-
var mustRefund = inputIsNative && sampleTrade.tradeType === sdkCore.TradeType.EXACT_OUTPUT; // flag for whether funds should be send first to the router
|
|
342
|
-
// 1. when receiving ETH (which much be unwrapped from WETH)
|
|
343
|
-
// 2. when a fee on the output is being taken
|
|
344
|
-
// 3. when there are >1 exact input trades. this one isn't strictly necessary,
|
|
345
|
-
// but typically we want to perform an aggregated slippage check
|
|
346
|
-
|
|
347
|
-
var routerMustCustody = outputIsNative || !!options.fee || trades.length > 1 && sampleTrade.tradeType === sdkCore.TradeType.EXACT_INPUT;
|
|
348
|
-
var totalValue = inputIsNative ? trades.reduce(function (sum, trade) {
|
|
349
|
-
return sum.add(trade.maximumAmountIn(options.slippageTolerance));
|
|
350
|
-
}, ZERO_IN) : ZERO_IN; // encode permit if necessary
|
|
351
|
-
|
|
352
|
-
if (options.inputTokenPermit) {
|
|
353
|
-
!sampleTrade.inputAmount.currency.isToken ? invariant(false, 'NON_TOKEN_PERMIT') : void 0;
|
|
354
|
-
calldatas.push(v3Sdk.SelfPermit.encodePermit(sampleTrade.inputAmount.currency, options.inputTokenPermit));
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
for (var _iterator2 = _createForOfIteratorHelperLoose(trades), _step2; !(_step2 = _iterator2()).done;) {
|
|
358
|
-
var trade = _step2.value;
|
|
359
|
-
|
|
360
|
-
if (trade instanceof v2Sdk.Trade) {
|
|
361
|
-
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody));
|
|
362
|
-
} else {
|
|
363
|
-
for (var _iterator3 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody)), _step3; !(_step3 = _iterator3()).done;) {
|
|
364
|
-
var calldata = _step3.value;
|
|
365
|
-
calldatas.push(calldata);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
} // unwrap
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
if (routerMustCustody) {
|
|
372
|
-
// if all trades are exact output, we can save gas by not passing a slippage check
|
|
373
|
-
var canOmitSlippageCheck = sampleTrade.tradeType === sdkCore.TradeType.EXACT_OUTPUT;
|
|
374
|
-
var amountNecessary = canOmitSlippageCheck ? ZERO$1 : totalAmountOut.quotient;
|
|
375
|
-
|
|
376
|
-
if (outputIsNative) {
|
|
377
|
-
calldatas.push(PaymentsExtended.encodeUnwrapWETH9(amountNecessary, options.recipient, options.fee));
|
|
378
|
-
} else {
|
|
379
|
-
calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, amountNecessary, options.recipient, options.fee));
|
|
380
|
-
}
|
|
381
|
-
} // refund
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
if (mustRefund) {
|
|
385
|
-
calldatas.push(v3Sdk.Payments.encodeRefundETH());
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
return {
|
|
389
|
-
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
390
|
-
value: v3Sdk.toHex(totalValue.quotient)
|
|
391
|
-
};
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
return SwapRouter;
|
|
395
|
-
}();
|
|
396
|
-
SwapRouter.INTERFACE = /*#__PURE__*/new abi.Interface(ISwapRouter02_json.abi);
|
|
219
|
+
(function (Protocol) {
|
|
220
|
+
Protocol["V2"] = "V2";
|
|
221
|
+
Protocol["V3"] = "V3";
|
|
222
|
+
})(exports.Protocol || (exports.Protocol = {}));
|
|
397
223
|
|
|
398
224
|
function createCommonjsModule(fn, module) {
|
|
399
225
|
return module = { exports: {} }, fn(module, module.exports), module.exports;
|
|
@@ -1155,11 +981,6 @@ try {
|
|
|
1155
981
|
}
|
|
1156
982
|
});
|
|
1157
983
|
|
|
1158
|
-
(function (Protocol) {
|
|
1159
|
-
Protocol["V2"] = "V2";
|
|
1160
|
-
Protocol["V3"] = "V3";
|
|
1161
|
-
})(exports.Protocol || (exports.Protocol = {}));
|
|
1162
|
-
|
|
1163
984
|
var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
|
|
1164
985
|
_inheritsLoose(RouteV2, _V2RouteSDK);
|
|
1165
986
|
|
|
@@ -1167,8 +988,8 @@ var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
|
|
|
1167
988
|
var _this;
|
|
1168
989
|
|
|
1169
990
|
_this = _V2RouteSDK.call(this, v2Route.pairs, v2Route.input, v2Route.output) || this;
|
|
1170
|
-
_this.pools = _this.pairs;
|
|
1171
991
|
_this.protocol = exports.Protocol.V2;
|
|
992
|
+
_this.pools = _this.pairs;
|
|
1172
993
|
return _this;
|
|
1173
994
|
}
|
|
1174
995
|
|
|
@@ -1490,6 +1311,219 @@ var Trade = /*#__PURE__*/function () {
|
|
|
1490
1311
|
return Trade;
|
|
1491
1312
|
}();
|
|
1492
1313
|
|
|
1314
|
+
var ZERO$1 = /*#__PURE__*/JSBI.BigInt(0);
|
|
1315
|
+
/**
|
|
1316
|
+
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
|
|
1317
|
+
*/
|
|
1318
|
+
|
|
1319
|
+
var SwapRouter = /*#__PURE__*/function () {
|
|
1320
|
+
/**
|
|
1321
|
+
* Cannot be constructed.
|
|
1322
|
+
*/
|
|
1323
|
+
function SwapRouter() {}
|
|
1324
|
+
|
|
1325
|
+
SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody) {
|
|
1326
|
+
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance).quotient);
|
|
1327
|
+
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance).quotient);
|
|
1328
|
+
var path = trade.route.path.map(function (token) {
|
|
1329
|
+
return token.address;
|
|
1330
|
+
});
|
|
1331
|
+
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : sdkCore.validateAndParseAddress(options.recipient);
|
|
1332
|
+
|
|
1333
|
+
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1334
|
+
var exactInputParams = [amountIn, // save gas by only passing slippage check if we can't check it later
|
|
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];
|
|
1337
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
|
|
1338
|
+
} else {
|
|
1339
|
+
var exactOutputParams = [amountOut, amountIn, path, recipient];
|
|
1340
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapTokensForExactTokens', exactOutputParams);
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody) {
|
|
1345
|
+
var calldatas = [];
|
|
1346
|
+
|
|
1347
|
+
for (var _iterator = _createForOfIteratorHelperLoose(trade.swaps), _step; !(_step = _iterator()).done;) {
|
|
1348
|
+
var _step$value = _step.value,
|
|
1349
|
+
route = _step$value.route,
|
|
1350
|
+
inputAmount = _step$value.inputAmount,
|
|
1351
|
+
outputAmount = _step$value.outputAmount;
|
|
1352
|
+
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
1353
|
+
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient); // flag for whether the trade is single hop or not
|
|
1354
|
+
|
|
1355
|
+
var singleHop = route.pools.length === 1;
|
|
1356
|
+
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : sdkCore.validateAndParseAddress(options.recipient);
|
|
1357
|
+
|
|
1358
|
+
if (singleHop) {
|
|
1359
|
+
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1360
|
+
var exactInputSingleParams = {
|
|
1361
|
+
tokenIn: route.tokenPath[0].address,
|
|
1362
|
+
tokenOut: route.tokenPath[1].address,
|
|
1363
|
+
fee: route.pools[0].fee,
|
|
1364
|
+
recipient: recipient,
|
|
1365
|
+
amountIn: amountIn,
|
|
1366
|
+
amountOutMinimum: amountOut,
|
|
1367
|
+
sqrtPriceLimitX96: 0,
|
|
1368
|
+
hasAlreadyPaid: false
|
|
1369
|
+
};
|
|
1370
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
1371
|
+
} else {
|
|
1372
|
+
var exactOutputSingleParams = {
|
|
1373
|
+
tokenIn: route.tokenPath[0].address,
|
|
1374
|
+
tokenOut: route.tokenPath[1].address,
|
|
1375
|
+
fee: route.pools[0].fee,
|
|
1376
|
+
recipient: recipient,
|
|
1377
|
+
amountOut: amountOut,
|
|
1378
|
+
amountInMaximum: amountIn,
|
|
1379
|
+
sqrtPriceLimitX96: 0
|
|
1380
|
+
};
|
|
1381
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutputSingle', [exactOutputSingleParams]));
|
|
1382
|
+
}
|
|
1383
|
+
} else {
|
|
1384
|
+
var path = v3Sdk.encodeRouteToPath(route, trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT);
|
|
1385
|
+
|
|
1386
|
+
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1387
|
+
var exactInputParams = {
|
|
1388
|
+
path: path,
|
|
1389
|
+
recipient: recipient,
|
|
1390
|
+
amountIn: amountIn,
|
|
1391
|
+
amountOutMinimum: amountOut,
|
|
1392
|
+
hasAlreadyPaid: false
|
|
1393
|
+
};
|
|
1394
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
1395
|
+
} else {
|
|
1396
|
+
var exactOutputParams = {
|
|
1397
|
+
path: path,
|
|
1398
|
+
recipient: recipient,
|
|
1399
|
+
amountOut: amountOut,
|
|
1400
|
+
amountInMaximum: amountIn
|
|
1401
|
+
};
|
|
1402
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutput', [exactOutputParams]));
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
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
|
+
;
|
|
1415
|
+
|
|
1416
|
+
SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
|
|
1417
|
+
// If dealing with an instance of the aggregated Trade object, unbundle it to individual V2Trade and V3Trade objects.
|
|
1418
|
+
if (trades instanceof Trade) {
|
|
1419
|
+
!trades.swaps.every(function (swap) {
|
|
1420
|
+
return swap.route.protocol == exports.Protocol.V3 || swap.route.protocol == exports.Protocol.V2;
|
|
1421
|
+
}) ? invariant(false, 'UNSUPPORTED_PROTOCOL') : void 0;
|
|
1422
|
+
var v2Andv3Trades = [];
|
|
1423
|
+
|
|
1424
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(trades.swaps), _step2; !(_step2 = _iterator2()).done;) {
|
|
1425
|
+
var _step2$value = _step2.value,
|
|
1426
|
+
route = _step2$value.route,
|
|
1427
|
+
inputAmount = _step2$value.inputAmount,
|
|
1428
|
+
outputAmount = _step2$value.outputAmount;
|
|
1429
|
+
|
|
1430
|
+
if (route.protocol == exports.Protocol.V2) {
|
|
1431
|
+
v2Andv3Trades.push(new v2Sdk.Trade(route, trades.tradeType == sdkCore.TradeType.EXACT_INPUT ? inputAmount : outputAmount, trades.tradeType));
|
|
1432
|
+
} else if (route.protocol == exports.Protocol.V3) {
|
|
1433
|
+
v2Andv3Trades.push(v3Sdk.Trade.createUncheckedTrade({
|
|
1434
|
+
route: route,
|
|
1435
|
+
inputAmount: inputAmount,
|
|
1436
|
+
outputAmount: outputAmount,
|
|
1437
|
+
tradeType: trades.tradeType
|
|
1438
|
+
}));
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
trades = v2Andv3Trades;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
if (!Array.isArray(trades)) {
|
|
1446
|
+
trades = [trades];
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
var sampleTrade = trades[0]; // All trades should have the same starting/ending currency and trade type
|
|
1450
|
+
|
|
1451
|
+
!trades.every(function (trade) {
|
|
1452
|
+
return trade.inputAmount.currency.equals(sampleTrade.inputAmount.currency);
|
|
1453
|
+
}) ? invariant(false, 'TOKEN_IN_DIFF') : void 0;
|
|
1454
|
+
!trades.every(function (trade) {
|
|
1455
|
+
return trade.outputAmount.currency.equals(sampleTrade.outputAmount.currency);
|
|
1456
|
+
}) ? invariant(false, 'TOKEN_OUT_DIFF') : void 0;
|
|
1457
|
+
!trades.every(function (trade) {
|
|
1458
|
+
return trade.tradeType === sampleTrade.tradeType;
|
|
1459
|
+
}) ? invariant(false, 'TRADE_TYPE_DIFF') : void 0;
|
|
1460
|
+
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
|
+
var inputIsNative = sampleTrade.inputAmount.currency.isNative;
|
|
1467
|
+
var outputIsNative = sampleTrade.outputAmount.currency.isNative; // flag for whether a refund needs to happen
|
|
1468
|
+
// 1. when paying in ETH, but with an uncertain input amount
|
|
1469
|
+
|
|
1470
|
+
var mustRefund = inputIsNative && sampleTrade.tradeType === sdkCore.TradeType.EXACT_OUTPUT; // flag for whether funds should be send first to the router
|
|
1471
|
+
// 1. when receiving ETH (which much be unwrapped from WETH)
|
|
1472
|
+
// 2. when a fee on the output is being taken
|
|
1473
|
+
// 3. when there are >1 exact input trades. this one isn't strictly necessary,
|
|
1474
|
+
// but typically we want to perform an aggregated slippage check
|
|
1475
|
+
|
|
1476
|
+
var routerMustCustody = outputIsNative || !!options.fee || trades.length > 1 && sampleTrade.tradeType === sdkCore.TradeType.EXACT_INPUT;
|
|
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
|
|
1480
|
+
|
|
1481
|
+
if (options.inputTokenPermit) {
|
|
1482
|
+
!sampleTrade.inputAmount.currency.isToken ? invariant(false, 'NON_TOKEN_PERMIT') : void 0;
|
|
1483
|
+
calldatas.push(v3Sdk.SelfPermit.encodePermit(sampleTrade.inputAmount.currency, options.inputTokenPermit));
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(trades), _step3; !(_step3 = _iterator3()).done;) {
|
|
1487
|
+
var trade = _step3.value;
|
|
1488
|
+
|
|
1489
|
+
if (trade instanceof v2Sdk.Trade) {
|
|
1490
|
+
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody));
|
|
1491
|
+
} else {
|
|
1492
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody)), _step4; !(_step4 = _iterator4()).done;) {
|
|
1493
|
+
var calldata = _step4.value;
|
|
1494
|
+
calldatas.push(calldata);
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
} // unwrap
|
|
1498
|
+
|
|
1499
|
+
|
|
1500
|
+
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
|
+
if (outputIsNative) {
|
|
1506
|
+
calldatas.push(PaymentsExtended.encodeUnwrapWETH9(amountNecessary, options.recipient, options.fee));
|
|
1507
|
+
} else {
|
|
1508
|
+
calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, amountNecessary, options.recipient, options.fee));
|
|
1509
|
+
}
|
|
1510
|
+
} // refund
|
|
1511
|
+
|
|
1512
|
+
|
|
1513
|
+
if (mustRefund) {
|
|
1514
|
+
calldatas.push(v3Sdk.Payments.encodeRefundETH());
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
return {
|
|
1518
|
+
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
1519
|
+
value: v3Sdk.toHex(totalValue.quotient)
|
|
1520
|
+
};
|
|
1521
|
+
};
|
|
1522
|
+
|
|
1523
|
+
return SwapRouter;
|
|
1524
|
+
}();
|
|
1525
|
+
SwapRouter.INTERFACE = /*#__PURE__*/new abi.Interface(ISwapRouter02_json.abi);
|
|
1526
|
+
|
|
1493
1527
|
exports.ADDRESS_THIS = ADDRESS_THIS;
|
|
1494
1528
|
exports.MSG_SENDER = MSG_SENDER;
|
|
1495
1529
|
exports.MulticallExtended = MulticallExtended;
|