@kayenfi/router-sdk 0.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/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/approveAndCall.d.ts +33 -0
- package/dist/constants.d.ts +12 -0
- package/dist/entities/mixedRoute/route.d.ts +27 -0
- package/dist/entities/mixedRoute/trade.d.ts +183 -0
- package/dist/entities/protocol.d.ts +5 -0
- package/dist/entities/route.d.ts +27 -0
- package/dist/entities/trade.d.ts +93 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +8 -0
- package/dist/multicallExtended.d.ts +11 -0
- package/dist/paymentsExtended.d.ts +15 -0
- package/dist/router-sdk.cjs.development.js +2316 -0
- package/dist/router-sdk.cjs.development.js.map +1 -0
- package/dist/router-sdk.cjs.production.min.js +2 -0
- package/dist/router-sdk.cjs.production.min.js.map +1 -0
- package/dist/router-sdk.esm.js +2288 -0
- package/dist/router-sdk.esm.js.map +1 -0
- package/dist/swapRouter.d.ts +95 -0
- package/dist/utils/TPool.d.ts +3 -0
- package/dist/utils/encodeMixedRouteToPath.d.ts +9 -0
- package/dist/utils/getOutputAmount.d.ts +3 -0
- package/dist/utils/index.d.ts +16 -0
- package/dist/utils/isValidTokenPath.d.ts +3 -0
- package/package.json +43 -0
|
@@ -0,0 +1,2316 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
+
|
|
7
|
+
var sdkCore = require('@kayenfi/sdk-core');
|
|
8
|
+
var JSBI = _interopDefault(require('jsbi'));
|
|
9
|
+
var abi = require('@ethersproject/abi');
|
|
10
|
+
var invariant = _interopDefault(require('tiny-invariant'));
|
|
11
|
+
var IApproveAndCall_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json');
|
|
12
|
+
var v3Sdk = require('@kayenfi/v3-sdk');
|
|
13
|
+
var IMulticallExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json');
|
|
14
|
+
var IPeripheryPaymentsWithFeeExtended_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json');
|
|
15
|
+
var ISwapRouter02_json = require('@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json');
|
|
16
|
+
var v2Sdk = require('@kayenfi/v2-sdk');
|
|
17
|
+
var solidity = require('@ethersproject/solidity');
|
|
18
|
+
|
|
19
|
+
var ADDRESS_ZERO = '0x0000000000000000000000000000000000000000';
|
|
20
|
+
var MSG_SENDER = '0x0000000000000000000000000000000000000001';
|
|
21
|
+
var ADDRESS_THIS = '0x0000000000000000000000000000000000000002';
|
|
22
|
+
var ZERO = /*#__PURE__*/JSBI.BigInt(0);
|
|
23
|
+
var ONE = /*#__PURE__*/JSBI.BigInt(1);
|
|
24
|
+
// = 1 << 23 or 0b0100000000000000000000000
|
|
25
|
+
var MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER = 1 << 23;
|
|
26
|
+
// = 10 << 4 or 0b00100000
|
|
27
|
+
var MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER = 2 << 4;
|
|
28
|
+
// = 11 << 20 or 0b001100000000000000000000
|
|
29
|
+
var MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER = 3 << 20;
|
|
30
|
+
// = 100 << 20 or 0b010000000000000000000000
|
|
31
|
+
// export const MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER = 4 << 20
|
|
32
|
+
var ZERO_PERCENT = /*#__PURE__*/new sdkCore.Percent(ZERO);
|
|
33
|
+
var ONE_HUNDRED_PERCENT = /*#__PURE__*/new sdkCore.Percent(100, 100);
|
|
34
|
+
|
|
35
|
+
(function (ApprovalTypes) {
|
|
36
|
+
ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
|
|
37
|
+
ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
|
|
38
|
+
ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
|
|
39
|
+
ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
|
|
40
|
+
ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
|
|
41
|
+
})(exports.ApprovalTypes || (exports.ApprovalTypes = {}));
|
|
42
|
+
// type guard
|
|
43
|
+
function isMint(options) {
|
|
44
|
+
return Object.keys(options).some(function (k) {
|
|
45
|
+
return k === 'recipient';
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
var ApproveAndCall = /*#__PURE__*/function () {
|
|
49
|
+
/**
|
|
50
|
+
* Cannot be constructed.
|
|
51
|
+
*/
|
|
52
|
+
function ApproveAndCall() {}
|
|
53
|
+
ApproveAndCall.encodeApproveMax = function encodeApproveMax(token) {
|
|
54
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
|
|
55
|
+
};
|
|
56
|
+
ApproveAndCall.encodeApproveMaxMinusOne = function encodeApproveMaxMinusOne(token) {
|
|
57
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
|
|
58
|
+
};
|
|
59
|
+
ApproveAndCall.encodeApproveZeroThenMax = function encodeApproveZeroThenMax(token) {
|
|
60
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
|
|
61
|
+
};
|
|
62
|
+
ApproveAndCall.encodeApproveZeroThenMaxMinusOne = function encodeApproveZeroThenMaxMinusOne(token) {
|
|
63
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
|
|
64
|
+
};
|
|
65
|
+
ApproveAndCall.encodeCallPositionManager = function encodeCallPositionManager(calldatas) {
|
|
66
|
+
!(calldatas.length > 0) ? invariant(false, 'NULL_CALLDATA') : void 0;
|
|
67
|
+
if (calldatas.length === 1) {
|
|
68
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
|
|
69
|
+
} else {
|
|
70
|
+
var encodedMulticall = v3Sdk.NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
|
|
71
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Encode adding liquidity to a position in the nft manager contract
|
|
76
|
+
* @param position Forcasted position with expected amount out from swap
|
|
77
|
+
* @param minimalPosition Forcasted position with custom minimal token amounts
|
|
78
|
+
* @param addLiquidityOptions Options for adding liquidity
|
|
79
|
+
* @param slippageTolerance Defines maximum slippage
|
|
80
|
+
*/;
|
|
81
|
+
ApproveAndCall.encodeAddLiquidity = function encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, slippageTolerance) {
|
|
82
|
+
var _position$mintAmounts = position.mintAmountsWithSlippage(slippageTolerance),
|
|
83
|
+
amount0Min = _position$mintAmounts.amount0,
|
|
84
|
+
amount1Min = _position$mintAmounts.amount1;
|
|
85
|
+
// position.mintAmountsWithSlippage() can create amounts not dependenable in scenarios
|
|
86
|
+
// such as range orders. Allow the option to provide a position with custom minimum amounts
|
|
87
|
+
// for these scenarios
|
|
88
|
+
if (JSBI.lessThan(minimalPosition.amount0.quotient, amount0Min)) {
|
|
89
|
+
amount0Min = minimalPosition.amount0.quotient;
|
|
90
|
+
}
|
|
91
|
+
if (JSBI.lessThan(minimalPosition.amount1.quotient, amount1Min)) {
|
|
92
|
+
amount1Min = minimalPosition.amount1.quotient;
|
|
93
|
+
}
|
|
94
|
+
if (isMint(addLiquidityOptions)) {
|
|
95
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [{
|
|
96
|
+
token0: position.pool.token0.address,
|
|
97
|
+
token1: position.pool.token1.address,
|
|
98
|
+
fee: position.pool.fee,
|
|
99
|
+
tickLower: position.tickLower,
|
|
100
|
+
tickUpper: position.tickUpper,
|
|
101
|
+
amount0Min: v3Sdk.toHex(amount0Min),
|
|
102
|
+
amount1Min: v3Sdk.toHex(amount1Min),
|
|
103
|
+
recipient: addLiquidityOptions.recipient
|
|
104
|
+
}]);
|
|
105
|
+
} else {
|
|
106
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('increaseLiquidity', [{
|
|
107
|
+
token0: position.pool.token0.address,
|
|
108
|
+
token1: position.pool.token1.address,
|
|
109
|
+
amount0Min: v3Sdk.toHex(amount0Min),
|
|
110
|
+
amount1Min: v3Sdk.toHex(amount1Min),
|
|
111
|
+
tokenId: v3Sdk.toHex(addLiquidityOptions.tokenId)
|
|
112
|
+
}]);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
ApproveAndCall.encodeApprove = function encodeApprove(token, approvalType) {
|
|
116
|
+
switch (approvalType) {
|
|
117
|
+
case exports.ApprovalTypes.MAX:
|
|
118
|
+
return ApproveAndCall.encodeApproveMax(token.wrapped);
|
|
119
|
+
case exports.ApprovalTypes.MAX_MINUS_ONE:
|
|
120
|
+
return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
|
|
121
|
+
case exports.ApprovalTypes.ZERO_THEN_MAX:
|
|
122
|
+
return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
|
|
123
|
+
case exports.ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
|
|
124
|
+
return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
|
|
125
|
+
default:
|
|
126
|
+
throw new Error('Error: invalid ApprovalType');
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
return ApproveAndCall;
|
|
130
|
+
}();
|
|
131
|
+
ApproveAndCall.INTERFACE = /*#__PURE__*/new abi.Interface(IApproveAndCall_json.abi);
|
|
132
|
+
|
|
133
|
+
function validateAndParseBytes32(bytes32) {
|
|
134
|
+
if (!bytes32.match(/^0x[0-9a-fA-F]{64}$/)) {
|
|
135
|
+
throw new Error(bytes32 + " is not valid bytes32.");
|
|
136
|
+
}
|
|
137
|
+
return bytes32.toLowerCase();
|
|
138
|
+
}
|
|
139
|
+
var MulticallExtended = /*#__PURE__*/function () {
|
|
140
|
+
/**
|
|
141
|
+
* Cannot be constructed.
|
|
142
|
+
*/
|
|
143
|
+
function MulticallExtended() {}
|
|
144
|
+
MulticallExtended.encodeMulticall = function encodeMulticall(calldatas, validation) {
|
|
145
|
+
// if there's no validation, we can just fall back to regular multicall
|
|
146
|
+
if (typeof validation === 'undefined') {
|
|
147
|
+
return v3Sdk.Multicall.encodeMulticall(calldatas);
|
|
148
|
+
}
|
|
149
|
+
// if there is validation, we have to normalize calldatas
|
|
150
|
+
if (!Array.isArray(calldatas)) {
|
|
151
|
+
calldatas = [calldatas];
|
|
152
|
+
}
|
|
153
|
+
// this means the validation value should be a previousBlockhash
|
|
154
|
+
if (typeof validation === 'string' && validation.startsWith('0x')) {
|
|
155
|
+
var previousBlockhash = validateAndParseBytes32(validation);
|
|
156
|
+
return MulticallExtended.INTERFACE.encodeFunctionData('multicall(bytes32,bytes[])', [previousBlockhash, calldatas]);
|
|
157
|
+
} else {
|
|
158
|
+
var deadline = v3Sdk.toHex(validation);
|
|
159
|
+
return MulticallExtended.INTERFACE.encodeFunctionData('multicall(uint256,bytes[])', [deadline, calldatas]);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
return MulticallExtended;
|
|
163
|
+
}();
|
|
164
|
+
MulticallExtended.INTERFACE = /*#__PURE__*/new abi.Interface(IMulticallExtended_json.abi);
|
|
165
|
+
|
|
166
|
+
function encodeFeeBips(fee) {
|
|
167
|
+
return v3Sdk.toHex(fee.multiply(10000).quotient);
|
|
168
|
+
}
|
|
169
|
+
var PaymentsExtended = /*#__PURE__*/function () {
|
|
170
|
+
/**
|
|
171
|
+
* Cannot be constructed.
|
|
172
|
+
*/
|
|
173
|
+
function PaymentsExtended() {}
|
|
174
|
+
PaymentsExtended.encodeUnwrapWETH9 = function encodeUnwrapWETH9(amountMinimum, recipient, feeOptions) {
|
|
175
|
+
// if there's a recipient, just pass it along
|
|
176
|
+
if (typeof recipient === 'string') {
|
|
177
|
+
return v3Sdk.Payments.encodeUnwrapWETH9(amountMinimum, recipient, feeOptions);
|
|
178
|
+
}
|
|
179
|
+
if (!!feeOptions) {
|
|
180
|
+
var feeBips = encodeFeeBips(feeOptions.fee);
|
|
181
|
+
var feeRecipient = sdkCore.validateAndParseAddress(feeOptions.recipient);
|
|
182
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('unwrapWETH9WithFee(uint256,uint256,address)', [v3Sdk.toHex(amountMinimum), feeBips, feeRecipient]);
|
|
183
|
+
} else {
|
|
184
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('unwrapWETH9(uint256)', [v3Sdk.toHex(amountMinimum)]);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
PaymentsExtended.encodeSweepToken = function encodeSweepToken(token, amountMinimum, recipient, feeOptions) {
|
|
188
|
+
// if there's a recipient, just pass it along
|
|
189
|
+
if (typeof recipient === 'string') {
|
|
190
|
+
return v3Sdk.Payments.encodeSweepToken(token, amountMinimum, recipient, feeOptions);
|
|
191
|
+
}
|
|
192
|
+
if (!!feeOptions) {
|
|
193
|
+
var feeBips = encodeFeeBips(feeOptions.fee);
|
|
194
|
+
var feeRecipient = sdkCore.validateAndParseAddress(feeOptions.recipient);
|
|
195
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('sweepTokenWithFee(address,uint256,uint256,address)', [token.address, v3Sdk.toHex(amountMinimum), feeBips, feeRecipient]);
|
|
196
|
+
} else {
|
|
197
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('sweepToken(address,uint256)', [token.address, v3Sdk.toHex(amountMinimum)]);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
PaymentsExtended.encodePull = function encodePull(token, amount) {
|
|
201
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('pull', [token.address, v3Sdk.toHex(amount)]);
|
|
202
|
+
};
|
|
203
|
+
PaymentsExtended.encodeWrapETH = function encodeWrapETH(amount) {
|
|
204
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('wrapETH', [v3Sdk.toHex(amount)]);
|
|
205
|
+
};
|
|
206
|
+
return PaymentsExtended;
|
|
207
|
+
}();
|
|
208
|
+
PaymentsExtended.INTERFACE = /*#__PURE__*/new abi.Interface(IPeripheryPaymentsWithFeeExtended_json.abi);
|
|
209
|
+
|
|
210
|
+
function _regeneratorRuntime() {
|
|
211
|
+
_regeneratorRuntime = function () {
|
|
212
|
+
return e;
|
|
213
|
+
};
|
|
214
|
+
var t,
|
|
215
|
+
e = {},
|
|
216
|
+
r = Object.prototype,
|
|
217
|
+
n = r.hasOwnProperty,
|
|
218
|
+
o = Object.defineProperty || function (t, e, r) {
|
|
219
|
+
t[e] = r.value;
|
|
220
|
+
},
|
|
221
|
+
i = "function" == typeof Symbol ? Symbol : {},
|
|
222
|
+
a = i.iterator || "@@iterator",
|
|
223
|
+
c = i.asyncIterator || "@@asyncIterator",
|
|
224
|
+
u = i.toStringTag || "@@toStringTag";
|
|
225
|
+
function define(t, e, r) {
|
|
226
|
+
return Object.defineProperty(t, e, {
|
|
227
|
+
value: r,
|
|
228
|
+
enumerable: !0,
|
|
229
|
+
configurable: !0,
|
|
230
|
+
writable: !0
|
|
231
|
+
}), t[e];
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
define({}, "");
|
|
235
|
+
} catch (t) {
|
|
236
|
+
define = function (t, e, r) {
|
|
237
|
+
return t[e] = r;
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function wrap(t, e, r, n) {
|
|
241
|
+
var i = e && e.prototype instanceof Generator ? e : Generator,
|
|
242
|
+
a = Object.create(i.prototype),
|
|
243
|
+
c = new Context(n || []);
|
|
244
|
+
return o(a, "_invoke", {
|
|
245
|
+
value: makeInvokeMethod(t, r, c)
|
|
246
|
+
}), a;
|
|
247
|
+
}
|
|
248
|
+
function tryCatch(t, e, r) {
|
|
249
|
+
try {
|
|
250
|
+
return {
|
|
251
|
+
type: "normal",
|
|
252
|
+
arg: t.call(e, r)
|
|
253
|
+
};
|
|
254
|
+
} catch (t) {
|
|
255
|
+
return {
|
|
256
|
+
type: "throw",
|
|
257
|
+
arg: t
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
e.wrap = wrap;
|
|
262
|
+
var h = "suspendedStart",
|
|
263
|
+
l = "suspendedYield",
|
|
264
|
+
f = "executing",
|
|
265
|
+
s = "completed",
|
|
266
|
+
y = {};
|
|
267
|
+
function Generator() {}
|
|
268
|
+
function GeneratorFunction() {}
|
|
269
|
+
function GeneratorFunctionPrototype() {}
|
|
270
|
+
var p = {};
|
|
271
|
+
define(p, a, function () {
|
|
272
|
+
return this;
|
|
273
|
+
});
|
|
274
|
+
var d = Object.getPrototypeOf,
|
|
275
|
+
v = d && d(d(values([])));
|
|
276
|
+
v && v !== r && n.call(v, a) && (p = v);
|
|
277
|
+
var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
|
|
278
|
+
function defineIteratorMethods(t) {
|
|
279
|
+
["next", "throw", "return"].forEach(function (e) {
|
|
280
|
+
define(t, e, function (t) {
|
|
281
|
+
return this._invoke(e, t);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
function AsyncIterator(t, e) {
|
|
286
|
+
function invoke(r, o, i, a) {
|
|
287
|
+
var c = tryCatch(t[r], t, o);
|
|
288
|
+
if ("throw" !== c.type) {
|
|
289
|
+
var u = c.arg,
|
|
290
|
+
h = u.value;
|
|
291
|
+
return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
|
|
292
|
+
invoke("next", t, i, a);
|
|
293
|
+
}, function (t) {
|
|
294
|
+
invoke("throw", t, i, a);
|
|
295
|
+
}) : e.resolve(h).then(function (t) {
|
|
296
|
+
u.value = t, i(u);
|
|
297
|
+
}, function (t) {
|
|
298
|
+
return invoke("throw", t, i, a);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
a(c.arg);
|
|
302
|
+
}
|
|
303
|
+
var r;
|
|
304
|
+
o(this, "_invoke", {
|
|
305
|
+
value: function (t, n) {
|
|
306
|
+
function callInvokeWithMethodAndArg() {
|
|
307
|
+
return new e(function (e, r) {
|
|
308
|
+
invoke(t, n, e, r);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
function makeInvokeMethod(e, r, n) {
|
|
316
|
+
var o = h;
|
|
317
|
+
return function (i, a) {
|
|
318
|
+
if (o === f) throw new Error("Generator is already running");
|
|
319
|
+
if (o === s) {
|
|
320
|
+
if ("throw" === i) throw a;
|
|
321
|
+
return {
|
|
322
|
+
value: t,
|
|
323
|
+
done: !0
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
for (n.method = i, n.arg = a;;) {
|
|
327
|
+
var c = n.delegate;
|
|
328
|
+
if (c) {
|
|
329
|
+
var u = maybeInvokeDelegate(c, n);
|
|
330
|
+
if (u) {
|
|
331
|
+
if (u === y) continue;
|
|
332
|
+
return u;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
|
|
336
|
+
if (o === h) throw o = s, n.arg;
|
|
337
|
+
n.dispatchException(n.arg);
|
|
338
|
+
} else "return" === n.method && n.abrupt("return", n.arg);
|
|
339
|
+
o = f;
|
|
340
|
+
var p = tryCatch(e, r, n);
|
|
341
|
+
if ("normal" === p.type) {
|
|
342
|
+
if (o = n.done ? s : l, p.arg === y) continue;
|
|
343
|
+
return {
|
|
344
|
+
value: p.arg,
|
|
345
|
+
done: n.done
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
"throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function maybeInvokeDelegate(e, r) {
|
|
353
|
+
var n = r.method,
|
|
354
|
+
o = e.iterator[n];
|
|
355
|
+
if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
|
|
356
|
+
var i = tryCatch(o, e.iterator, r.arg);
|
|
357
|
+
if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
|
|
358
|
+
var a = i.arg;
|
|
359
|
+
return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
|
|
360
|
+
}
|
|
361
|
+
function pushTryEntry(t) {
|
|
362
|
+
var e = {
|
|
363
|
+
tryLoc: t[0]
|
|
364
|
+
};
|
|
365
|
+
1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
|
|
366
|
+
}
|
|
367
|
+
function resetTryEntry(t) {
|
|
368
|
+
var e = t.completion || {};
|
|
369
|
+
e.type = "normal", delete e.arg, t.completion = e;
|
|
370
|
+
}
|
|
371
|
+
function Context(t) {
|
|
372
|
+
this.tryEntries = [{
|
|
373
|
+
tryLoc: "root"
|
|
374
|
+
}], t.forEach(pushTryEntry, this), this.reset(!0);
|
|
375
|
+
}
|
|
376
|
+
function values(e) {
|
|
377
|
+
if (e || "" === e) {
|
|
378
|
+
var r = e[a];
|
|
379
|
+
if (r) return r.call(e);
|
|
380
|
+
if ("function" == typeof e.next) return e;
|
|
381
|
+
if (!isNaN(e.length)) {
|
|
382
|
+
var o = -1,
|
|
383
|
+
i = function next() {
|
|
384
|
+
for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
|
|
385
|
+
return next.value = t, next.done = !0, next;
|
|
386
|
+
};
|
|
387
|
+
return i.next = i;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
throw new TypeError(typeof e + " is not iterable");
|
|
391
|
+
}
|
|
392
|
+
return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
|
|
393
|
+
value: GeneratorFunctionPrototype,
|
|
394
|
+
configurable: !0
|
|
395
|
+
}), o(GeneratorFunctionPrototype, "constructor", {
|
|
396
|
+
value: GeneratorFunction,
|
|
397
|
+
configurable: !0
|
|
398
|
+
}), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
|
|
399
|
+
var e = "function" == typeof t && t.constructor;
|
|
400
|
+
return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
|
|
401
|
+
}, e.mark = function (t) {
|
|
402
|
+
return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
|
|
403
|
+
}, e.awrap = function (t) {
|
|
404
|
+
return {
|
|
405
|
+
__await: t
|
|
406
|
+
};
|
|
407
|
+
}, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
|
|
408
|
+
return this;
|
|
409
|
+
}), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
|
|
410
|
+
void 0 === i && (i = Promise);
|
|
411
|
+
var a = new AsyncIterator(wrap(t, r, n, o), i);
|
|
412
|
+
return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
|
|
413
|
+
return t.done ? t.value : a.next();
|
|
414
|
+
});
|
|
415
|
+
}, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
|
|
416
|
+
return this;
|
|
417
|
+
}), define(g, "toString", function () {
|
|
418
|
+
return "[object Generator]";
|
|
419
|
+
}), e.keys = function (t) {
|
|
420
|
+
var e = Object(t),
|
|
421
|
+
r = [];
|
|
422
|
+
for (var n in e) r.push(n);
|
|
423
|
+
return r.reverse(), function next() {
|
|
424
|
+
for (; r.length;) {
|
|
425
|
+
var t = r.pop();
|
|
426
|
+
if (t in e) return next.value = t, next.done = !1, next;
|
|
427
|
+
}
|
|
428
|
+
return next.done = !0, next;
|
|
429
|
+
};
|
|
430
|
+
}, e.values = values, Context.prototype = {
|
|
431
|
+
constructor: Context,
|
|
432
|
+
reset: function (e) {
|
|
433
|
+
if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
|
|
434
|
+
},
|
|
435
|
+
stop: function () {
|
|
436
|
+
this.done = !0;
|
|
437
|
+
var t = this.tryEntries[0].completion;
|
|
438
|
+
if ("throw" === t.type) throw t.arg;
|
|
439
|
+
return this.rval;
|
|
440
|
+
},
|
|
441
|
+
dispatchException: function (e) {
|
|
442
|
+
if (this.done) throw e;
|
|
443
|
+
var r = this;
|
|
444
|
+
function handle(n, o) {
|
|
445
|
+
return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
|
|
446
|
+
}
|
|
447
|
+
for (var o = this.tryEntries.length - 1; o >= 0; --o) {
|
|
448
|
+
var i = this.tryEntries[o],
|
|
449
|
+
a = i.completion;
|
|
450
|
+
if ("root" === i.tryLoc) return handle("end");
|
|
451
|
+
if (i.tryLoc <= this.prev) {
|
|
452
|
+
var c = n.call(i, "catchLoc"),
|
|
453
|
+
u = n.call(i, "finallyLoc");
|
|
454
|
+
if (c && u) {
|
|
455
|
+
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
456
|
+
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
457
|
+
} else if (c) {
|
|
458
|
+
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
459
|
+
} else {
|
|
460
|
+
if (!u) throw new Error("try statement without catch or finally");
|
|
461
|
+
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
},
|
|
466
|
+
abrupt: function (t, e) {
|
|
467
|
+
for (var r = this.tryEntries.length - 1; r >= 0; --r) {
|
|
468
|
+
var o = this.tryEntries[r];
|
|
469
|
+
if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
|
|
470
|
+
var i = o;
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
|
|
475
|
+
var a = i ? i.completion : {};
|
|
476
|
+
return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
|
|
477
|
+
},
|
|
478
|
+
complete: function (t, e) {
|
|
479
|
+
if ("throw" === t.type) throw t.arg;
|
|
480
|
+
return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
|
|
481
|
+
},
|
|
482
|
+
finish: function (t) {
|
|
483
|
+
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
484
|
+
var r = this.tryEntries[e];
|
|
485
|
+
if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
catch: function (t) {
|
|
489
|
+
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
490
|
+
var r = this.tryEntries[e];
|
|
491
|
+
if (r.tryLoc === t) {
|
|
492
|
+
var n = r.completion;
|
|
493
|
+
if ("throw" === n.type) {
|
|
494
|
+
var o = n.arg;
|
|
495
|
+
resetTryEntry(r);
|
|
496
|
+
}
|
|
497
|
+
return o;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
throw new Error("illegal catch attempt");
|
|
501
|
+
},
|
|
502
|
+
delegateYield: function (e, r, n) {
|
|
503
|
+
return this.delegate = {
|
|
504
|
+
iterator: values(e),
|
|
505
|
+
resultName: r,
|
|
506
|
+
nextLoc: n
|
|
507
|
+
}, "next" === this.method && (this.arg = t), y;
|
|
508
|
+
}
|
|
509
|
+
}, e;
|
|
510
|
+
}
|
|
511
|
+
function _toPrimitive(t, r) {
|
|
512
|
+
if ("object" != typeof t || !t) return t;
|
|
513
|
+
var e = t[Symbol.toPrimitive];
|
|
514
|
+
if (void 0 !== e) {
|
|
515
|
+
var i = e.call(t, r || "default");
|
|
516
|
+
if ("object" != typeof i) return i;
|
|
517
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
518
|
+
}
|
|
519
|
+
return ("string" === r ? String : Number)(t);
|
|
520
|
+
}
|
|
521
|
+
function _toPropertyKey(t) {
|
|
522
|
+
var i = _toPrimitive(t, "string");
|
|
523
|
+
return "symbol" == typeof i ? i : String(i);
|
|
524
|
+
}
|
|
525
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
526
|
+
try {
|
|
527
|
+
var info = gen[key](arg);
|
|
528
|
+
var value = info.value;
|
|
529
|
+
} catch (error) {
|
|
530
|
+
reject(error);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
if (info.done) {
|
|
534
|
+
resolve(value);
|
|
535
|
+
} else {
|
|
536
|
+
Promise.resolve(value).then(_next, _throw);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function _asyncToGenerator(fn) {
|
|
540
|
+
return function () {
|
|
541
|
+
var self = this,
|
|
542
|
+
args = arguments;
|
|
543
|
+
return new Promise(function (resolve, reject) {
|
|
544
|
+
var gen = fn.apply(self, args);
|
|
545
|
+
function _next(value) {
|
|
546
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
547
|
+
}
|
|
548
|
+
function _throw(err) {
|
|
549
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
550
|
+
}
|
|
551
|
+
_next(undefined);
|
|
552
|
+
});
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
function _defineProperties(target, props) {
|
|
556
|
+
for (var i = 0; i < props.length; i++) {
|
|
557
|
+
var descriptor = props[i];
|
|
558
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
559
|
+
descriptor.configurable = true;
|
|
560
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
561
|
+
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
565
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
566
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
567
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
568
|
+
writable: false
|
|
569
|
+
});
|
|
570
|
+
return Constructor;
|
|
571
|
+
}
|
|
572
|
+
function _extends() {
|
|
573
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
574
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
575
|
+
var source = arguments[i];
|
|
576
|
+
for (var key in source) {
|
|
577
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
578
|
+
target[key] = source[key];
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return target;
|
|
583
|
+
};
|
|
584
|
+
return _extends.apply(this, arguments);
|
|
585
|
+
}
|
|
586
|
+
function _inheritsLoose(subClass, superClass) {
|
|
587
|
+
subClass.prototype = Object.create(superClass.prototype);
|
|
588
|
+
subClass.prototype.constructor = subClass;
|
|
589
|
+
_setPrototypeOf(subClass, superClass);
|
|
590
|
+
}
|
|
591
|
+
function _setPrototypeOf(o, p) {
|
|
592
|
+
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
|
593
|
+
o.__proto__ = p;
|
|
594
|
+
return o;
|
|
595
|
+
};
|
|
596
|
+
return _setPrototypeOf(o, p);
|
|
597
|
+
}
|
|
598
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
599
|
+
if (!o) return;
|
|
600
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
601
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
602
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
603
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
604
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
605
|
+
}
|
|
606
|
+
function _arrayLikeToArray(arr, len) {
|
|
607
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
608
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
609
|
+
return arr2;
|
|
610
|
+
}
|
|
611
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
612
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
613
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
614
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
615
|
+
if (it) o = it;
|
|
616
|
+
var i = 0;
|
|
617
|
+
return function () {
|
|
618
|
+
if (i >= o.length) return {
|
|
619
|
+
done: true
|
|
620
|
+
};
|
|
621
|
+
return {
|
|
622
|
+
done: false,
|
|
623
|
+
value: o[i++]
|
|
624
|
+
};
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function isValidTokenPath(_prevPool, currentPool, inputToken) {
|
|
631
|
+
if (currentPool.involvesToken(inputToken)) return true;
|
|
632
|
+
// throw if both v4 pools, native/wrapped tokens not interchangeable in v4
|
|
633
|
+
// if (prevPool instanceof V4Pool && currentPool instanceof V4Pool) return false
|
|
634
|
+
// v2/v3 --> v4 valid if v2/v3 output is the wrapped version of the v4 pool native currency
|
|
635
|
+
// if (currentPool instanceof V4Pool) {
|
|
636
|
+
// if (currentPool.token0.wrapped.equals(inputToken) || currentPool.token1.wrapped.equals(inputToken)) return true
|
|
637
|
+
// }
|
|
638
|
+
// v4 --> v2/v3 valid if v4 output is the native version of the v2/v3 wrapped token
|
|
639
|
+
// if (prevPool instanceof V4Pool) {
|
|
640
|
+
// if (currentPool.involvesToken(inputToken.wrapped)) return true
|
|
641
|
+
// }
|
|
642
|
+
return false;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Represents a list of pools or pairs through which a swap can occur
|
|
647
|
+
* @template TInput The input token
|
|
648
|
+
* @template TOutput The output token
|
|
649
|
+
*/
|
|
650
|
+
var MixedRouteSDK = /*#__PURE__*/function () {
|
|
651
|
+
/**
|
|
652
|
+
* Creates an instance of route.
|
|
653
|
+
* @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
|
|
654
|
+
* @param input The input token
|
|
655
|
+
* @param output The output token
|
|
656
|
+
*/
|
|
657
|
+
function MixedRouteSDK(pools, input, output) {
|
|
658
|
+
this._midPrice = null;
|
|
659
|
+
!(pools.length > 0) ? invariant(false, 'POOLS') : void 0;
|
|
660
|
+
var chainId = pools[0].chainId;
|
|
661
|
+
var allOnSameChain = pools.every(function (pool) {
|
|
662
|
+
return pool.chainId === chainId;
|
|
663
|
+
});
|
|
664
|
+
!allOnSameChain ? invariant(false, 'CHAIN_IDS') : void 0;
|
|
665
|
+
// if (pools[0] instanceof V4Pool) {
|
|
666
|
+
// this.adjustedInput = pools[0].involvesToken(input) ? input : input.wrapped
|
|
667
|
+
// } else {
|
|
668
|
+
this.adjustedInput = input.wrapped; // no native currencies in v2/v3
|
|
669
|
+
// }
|
|
670
|
+
!pools[0].involvesToken(this.adjustedInput) ? invariant(false, 'INPUT') : void 0;
|
|
671
|
+
var lastPool = pools[pools.length - 1];
|
|
672
|
+
// if (lastPool instanceof V4Pool) {
|
|
673
|
+
// invariant(lastPool.involvesToken(output) || lastPool.involvesToken(output.wrapped), 'OUTPUT')
|
|
674
|
+
// } else {
|
|
675
|
+
!lastPool.involvesToken(output.wrapped) ? invariant(false, 'OUTPUT') : void 0;
|
|
676
|
+
// }
|
|
677
|
+
/**
|
|
678
|
+
* Normalizes token0-token1 order and selects the next token/fee step to add to the path
|
|
679
|
+
* */
|
|
680
|
+
var tokenPath = [this.adjustedInput];
|
|
681
|
+
pools[0].token0.equals(this.adjustedInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
|
|
682
|
+
for (var i = 1; i < pools.length; i++) {
|
|
683
|
+
var prevPool = pools[i - 1];
|
|
684
|
+
var pool = pools[i];
|
|
685
|
+
var inputToken = tokenPath[i];
|
|
686
|
+
var outputToken = pool.token0.wrapped.equals(inputToken.wrapped) ? pool.token1 : pool.token0;
|
|
687
|
+
!isValidTokenPath(prevPool, pool, inputToken) ? invariant(false, 'PATH') : void 0;
|
|
688
|
+
tokenPath.push(outputToken);
|
|
689
|
+
}
|
|
690
|
+
this.pools = pools;
|
|
691
|
+
this.path = tokenPath;
|
|
692
|
+
this.input = input;
|
|
693
|
+
this.output = output != null ? output : tokenPath[tokenPath.length - 1];
|
|
694
|
+
}
|
|
695
|
+
_createClass(MixedRouteSDK, [{
|
|
696
|
+
key: "chainId",
|
|
697
|
+
get: function get() {
|
|
698
|
+
return this.pools[0].chainId;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Returns the mid price of the route
|
|
702
|
+
*/
|
|
703
|
+
}, {
|
|
704
|
+
key: "midPrice",
|
|
705
|
+
get: function get() {
|
|
706
|
+
if (this._midPrice !== null) return this._midPrice;
|
|
707
|
+
var price = this.pools.slice(1).reduce(function (_ref, pool) {
|
|
708
|
+
var nextInput = _ref.nextInput,
|
|
709
|
+
price = _ref.price;
|
|
710
|
+
return nextInput.equals(pool.token0) ? {
|
|
711
|
+
nextInput: pool.token1,
|
|
712
|
+
price: price.multiply(pool.token0Price.asFraction)
|
|
713
|
+
} : {
|
|
714
|
+
nextInput: pool.token0,
|
|
715
|
+
price: price.multiply(pool.token1Price.asFraction)
|
|
716
|
+
};
|
|
717
|
+
}, this.pools[0].token0.equals(this.adjustedInput) ? {
|
|
718
|
+
nextInput: this.pools[0].token1,
|
|
719
|
+
price: this.pools[0].token0Price.asFraction
|
|
720
|
+
} : {
|
|
721
|
+
nextInput: this.pools[0].token0,
|
|
722
|
+
price: this.pools[0].token1Price.asFraction
|
|
723
|
+
}).price;
|
|
724
|
+
return this._midPrice = new sdkCore.Price(this.input, this.output, price.denominator, price.numerator);
|
|
725
|
+
}
|
|
726
|
+
}]);
|
|
727
|
+
return MixedRouteSDK;
|
|
728
|
+
}();
|
|
729
|
+
|
|
730
|
+
function getOutputAmount(_x, _x2) {
|
|
731
|
+
return _getOutputAmount.apply(this, arguments);
|
|
732
|
+
}
|
|
733
|
+
function _getOutputAmount() {
|
|
734
|
+
_getOutputAmount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pool, amountIn) {
|
|
735
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
736
|
+
while (1) switch (_context.prev = _context.next) {
|
|
737
|
+
case 0:
|
|
738
|
+
_context.next = 2;
|
|
739
|
+
return pool.getOutputAmount(amountIn.wrapped);
|
|
740
|
+
case 2:
|
|
741
|
+
return _context.abrupt("return", _context.sent);
|
|
742
|
+
case 3:
|
|
743
|
+
case "end":
|
|
744
|
+
return _context.stop();
|
|
745
|
+
}
|
|
746
|
+
}, _callee);
|
|
747
|
+
}));
|
|
748
|
+
return _getOutputAmount.apply(this, arguments);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
|
|
753
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
754
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
755
|
+
* @template TTradeType The trade type, either exact input or exact output
|
|
756
|
+
* @param a The first trade to compare
|
|
757
|
+
* @param b The second trade to compare
|
|
758
|
+
* @returns A sorted ordering for two neighboring elements in a trade array
|
|
759
|
+
*/
|
|
760
|
+
function tradeComparator(a, b) {
|
|
761
|
+
// must have same input and output token for comparison
|
|
762
|
+
!a.inputAmount.currency.equals(b.inputAmount.currency) ? invariant(false, 'INPUT_CURRENCY') : void 0;
|
|
763
|
+
!a.outputAmount.currency.equals(b.outputAmount.currency) ? invariant(false, 'OUTPUT_CURRENCY') : void 0;
|
|
764
|
+
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
765
|
+
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
766
|
+
// consider the number of hops since each hop costs gas
|
|
767
|
+
var aHops = a.swaps.reduce(function (total, cur) {
|
|
768
|
+
return total + cur.route.path.length;
|
|
769
|
+
}, 0);
|
|
770
|
+
var bHops = b.swaps.reduce(function (total, cur) {
|
|
771
|
+
return total + cur.route.path.length;
|
|
772
|
+
}, 0);
|
|
773
|
+
return aHops - bHops;
|
|
774
|
+
}
|
|
775
|
+
// trade A requires less input than trade B, so A should come first
|
|
776
|
+
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
777
|
+
return -1;
|
|
778
|
+
} else {
|
|
779
|
+
return 1;
|
|
780
|
+
}
|
|
781
|
+
} else {
|
|
782
|
+
// tradeA has less output than trade B, so should come second
|
|
783
|
+
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
784
|
+
return 1;
|
|
785
|
+
} else {
|
|
786
|
+
return -1;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Represents a trade executed against a set of routes where some percentage of the input is
|
|
792
|
+
* split across each route.
|
|
793
|
+
*
|
|
794
|
+
* Each route has its own set of pools. Pools can not be re-used across routes.
|
|
795
|
+
*
|
|
796
|
+
* Does not account for slippage, i.e., changes in price environment that can occur between
|
|
797
|
+
* the time the trade is submitted and when it is executed.
|
|
798
|
+
* @notice This class is functionally the same as the `Trade` class in the `@kayenfi/v3-sdk` package, aside from typing and some input validation.
|
|
799
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
800
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
801
|
+
* @template TTradeType The trade type, either exact input or exact output
|
|
802
|
+
*/
|
|
803
|
+
var MixedRouteTrade = /*#__PURE__*/function () {
|
|
804
|
+
/**
|
|
805
|
+
* Construct a trade by passing in the pre-computed property values
|
|
806
|
+
* @param routes The routes through which the trade occurs
|
|
807
|
+
* @param tradeType The type of trade, exact input or exact output
|
|
808
|
+
*/
|
|
809
|
+
function MixedRouteTrade(_ref) {
|
|
810
|
+
var routes = _ref.routes,
|
|
811
|
+
tradeType = _ref.tradeType;
|
|
812
|
+
var inputCurrency = routes[0].inputAmount.currency;
|
|
813
|
+
var outputCurrency = routes[0].outputAmount.currency;
|
|
814
|
+
!routes.every(function (_ref2) {
|
|
815
|
+
var route = _ref2.route;
|
|
816
|
+
return inputCurrency.wrapped.equals(route.input.wrapped);
|
|
817
|
+
}) ? invariant(false, 'INPUT_CURRENCY_MATCH') : void 0;
|
|
818
|
+
!routes.every(function (_ref3) {
|
|
819
|
+
var route = _ref3.route;
|
|
820
|
+
return outputCurrency.wrapped.equals(route.output.wrapped);
|
|
821
|
+
}) ? invariant(false, 'OUTPUT_CURRENCY_MATCH') : void 0;
|
|
822
|
+
var numPools = routes.map(function (_ref4) {
|
|
823
|
+
var route = _ref4.route;
|
|
824
|
+
return route.pools.length;
|
|
825
|
+
}).reduce(function (total, cur) {
|
|
826
|
+
return total + cur;
|
|
827
|
+
}, 0);
|
|
828
|
+
var poolIdentifierSet = new Set();
|
|
829
|
+
for (var _iterator = _createForOfIteratorHelperLoose(routes), _step; !(_step = _iterator()).done;) {
|
|
830
|
+
var route = _step.value.route;
|
|
831
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(route.pools), _step2; !(_step2 = _iterator2()).done;) {
|
|
832
|
+
var pool = _step2.value;
|
|
833
|
+
// if (pool instanceof V4Pool) {
|
|
834
|
+
// poolIdentifierSet.add(pool.poolId)
|
|
835
|
+
// } else
|
|
836
|
+
if (pool instanceof v3Sdk.Pool) {
|
|
837
|
+
poolIdentifierSet.add(v3Sdk.Pool.getAddress(pool.token0, pool.token1, pool.fee));
|
|
838
|
+
} else if (pool instanceof v2Sdk.Pair) {
|
|
839
|
+
var pair = pool;
|
|
840
|
+
poolIdentifierSet.add(v2Sdk.Pair.getAddress(pair.token0, pair.token1));
|
|
841
|
+
} else {
|
|
842
|
+
throw new Error('Unexpected pool type in route when constructing trade object');
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
!(numPools === poolIdentifierSet.size) ? invariant(false, 'POOLS_DUPLICATED') : void 0;
|
|
847
|
+
!(tradeType === sdkCore.TradeType.EXACT_INPUT) ? invariant(false, 'TRADE_TYPE') : void 0;
|
|
848
|
+
this.swaps = routes;
|
|
849
|
+
this.tradeType = tradeType;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* @deprecated Deprecated in favor of 'swaps' property. If the trade consists of multiple routes
|
|
853
|
+
* this will return an error.
|
|
854
|
+
*
|
|
855
|
+
* When the trade consists of just a single route, this returns the route of the trade,
|
|
856
|
+
* i.e. which pools the trade goes through.
|
|
857
|
+
*/
|
|
858
|
+
/**
|
|
859
|
+
* Constructs a trade by simulating swaps through the given route
|
|
860
|
+
* @template TInput The input token, either Ether or an ERC-20.
|
|
861
|
+
* @template TOutput The output token, either Ether or an ERC-20.
|
|
862
|
+
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
863
|
+
* @param route route to swap through
|
|
864
|
+
* @param amount the amount specified, either input or output, depending on tradeType
|
|
865
|
+
* @param tradeType whether the trade is an exact input or exact output swap
|
|
866
|
+
* @returns The route
|
|
867
|
+
*/
|
|
868
|
+
MixedRouteTrade.fromRoute =
|
|
869
|
+
/*#__PURE__*/
|
|
870
|
+
function () {
|
|
871
|
+
var _fromRoute = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(route, amount, tradeType) {
|
|
872
|
+
var amounts, inputAmount, outputAmount, i, pool, _yield$getOutputAmoun, _outputAmount;
|
|
873
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
874
|
+
while (1) switch (_context.prev = _context.next) {
|
|
875
|
+
case 0:
|
|
876
|
+
amounts = new Array(route.path.length);
|
|
877
|
+
!(tradeType === sdkCore.TradeType.EXACT_INPUT) ? invariant(false, 'TRADE_TYPE') : void 0;
|
|
878
|
+
!amount.currency.equals(route.input) ? invariant(false, 'INPUT') : void 0;
|
|
879
|
+
amounts[0] = /* route.pools[0] instanceof V4Pool ? amount : */amount.wrapped;
|
|
880
|
+
i = 0;
|
|
881
|
+
case 5:
|
|
882
|
+
if (!(i < route.path.length - 1)) {
|
|
883
|
+
_context.next = 15;
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
pool = route.pools[i];
|
|
887
|
+
_context.next = 9;
|
|
888
|
+
return getOutputAmount(pool, amounts[i]);
|
|
889
|
+
case 9:
|
|
890
|
+
_yield$getOutputAmoun = _context.sent;
|
|
891
|
+
_outputAmount = _yield$getOutputAmoun[0];
|
|
892
|
+
amounts[i + 1] = _outputAmount;
|
|
893
|
+
case 12:
|
|
894
|
+
i++;
|
|
895
|
+
_context.next = 5;
|
|
896
|
+
break;
|
|
897
|
+
case 15:
|
|
898
|
+
inputAmount = sdkCore.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
899
|
+
outputAmount = sdkCore.CurrencyAmount.fromFractionalAmount(route.output, amounts[amounts.length - 1].numerator, amounts[amounts.length - 1].denominator);
|
|
900
|
+
return _context.abrupt("return", new MixedRouteTrade({
|
|
901
|
+
routes: [{
|
|
902
|
+
inputAmount: inputAmount,
|
|
903
|
+
outputAmount: outputAmount,
|
|
904
|
+
route: route
|
|
905
|
+
}],
|
|
906
|
+
tradeType: tradeType
|
|
907
|
+
}));
|
|
908
|
+
case 18:
|
|
909
|
+
case "end":
|
|
910
|
+
return _context.stop();
|
|
911
|
+
}
|
|
912
|
+
}, _callee);
|
|
913
|
+
}));
|
|
914
|
+
function fromRoute(_x, _x2, _x3) {
|
|
915
|
+
return _fromRoute.apply(this, arguments);
|
|
916
|
+
}
|
|
917
|
+
return fromRoute;
|
|
918
|
+
}()
|
|
919
|
+
/**
|
|
920
|
+
* Constructs a trade from routes by simulating swaps
|
|
921
|
+
*
|
|
922
|
+
* @template TInput The input token, either Ether or an ERC-20.
|
|
923
|
+
* @template TOutput The output token, either Ether or an ERC-20.
|
|
924
|
+
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
925
|
+
* @param routes the routes to swap through and how much of the amount should be routed through each
|
|
926
|
+
* @param tradeType whether the trade is an exact input or exact output swap
|
|
927
|
+
* @returns The trade
|
|
928
|
+
*/
|
|
929
|
+
;
|
|
930
|
+
MixedRouteTrade.fromRoutes =
|
|
931
|
+
/*#__PURE__*/
|
|
932
|
+
function () {
|
|
933
|
+
var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(routes, tradeType) {
|
|
934
|
+
var populatedRoutes, _iterator3, _step3, _step3$value, route, amount, amounts, inputAmount, outputAmount, i, pool, _yield$getOutputAmoun2, _outputAmount2;
|
|
935
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
936
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
937
|
+
case 0:
|
|
938
|
+
populatedRoutes = [];
|
|
939
|
+
!(tradeType === sdkCore.TradeType.EXACT_INPUT) ? invariant(false, 'TRADE_TYPE') : void 0;
|
|
940
|
+
_iterator3 = _createForOfIteratorHelperLoose(routes);
|
|
941
|
+
case 3:
|
|
942
|
+
if ((_step3 = _iterator3()).done) {
|
|
943
|
+
_context2.next = 26;
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
_step3$value = _step3.value, route = _step3$value.route, amount = _step3$value.amount;
|
|
947
|
+
amounts = new Array(route.path.length);
|
|
948
|
+
inputAmount = void 0;
|
|
949
|
+
outputAmount = void 0;
|
|
950
|
+
!amount.currency.equals(route.input) ? invariant(false, 'INPUT') : void 0;
|
|
951
|
+
inputAmount = sdkCore.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
952
|
+
amounts[0] = sdkCore.CurrencyAmount.fromFractionalAmount(route.adjustedInput, amount.numerator, amount.denominator);
|
|
953
|
+
i = 0;
|
|
954
|
+
case 12:
|
|
955
|
+
if (!(i < route.path.length - 1)) {
|
|
956
|
+
_context2.next = 22;
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
pool = route.pools[i];
|
|
960
|
+
_context2.next = 16;
|
|
961
|
+
return getOutputAmount(pool, amounts[i]);
|
|
962
|
+
case 16:
|
|
963
|
+
_yield$getOutputAmoun2 = _context2.sent;
|
|
964
|
+
_outputAmount2 = _yield$getOutputAmoun2[0];
|
|
965
|
+
amounts[i + 1] = _outputAmount2;
|
|
966
|
+
case 19:
|
|
967
|
+
i++;
|
|
968
|
+
_context2.next = 12;
|
|
969
|
+
break;
|
|
970
|
+
case 22:
|
|
971
|
+
outputAmount = sdkCore.CurrencyAmount.fromFractionalAmount(route.output, amounts[amounts.length - 1].numerator, amounts[amounts.length - 1].denominator);
|
|
972
|
+
populatedRoutes.push({
|
|
973
|
+
route: route,
|
|
974
|
+
inputAmount: inputAmount,
|
|
975
|
+
outputAmount: outputAmount
|
|
976
|
+
});
|
|
977
|
+
case 24:
|
|
978
|
+
_context2.next = 3;
|
|
979
|
+
break;
|
|
980
|
+
case 26:
|
|
981
|
+
return _context2.abrupt("return", new MixedRouteTrade({
|
|
982
|
+
routes: populatedRoutes,
|
|
983
|
+
tradeType: tradeType
|
|
984
|
+
}));
|
|
985
|
+
case 27:
|
|
986
|
+
case "end":
|
|
987
|
+
return _context2.stop();
|
|
988
|
+
}
|
|
989
|
+
}, _callee2);
|
|
990
|
+
}));
|
|
991
|
+
function fromRoutes(_x4, _x5) {
|
|
992
|
+
return _fromRoutes.apply(this, arguments);
|
|
993
|
+
}
|
|
994
|
+
return fromRoutes;
|
|
995
|
+
}()
|
|
996
|
+
/**
|
|
997
|
+
* Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
|
|
998
|
+
* elsewhere and do not have any tick data
|
|
999
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
1000
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
1001
|
+
* @template TTradeType The type of the trade, either exact in or exact out
|
|
1002
|
+
* @param constructorArguments The arguments passed to the trade constructor
|
|
1003
|
+
* @returns The unchecked trade
|
|
1004
|
+
*/
|
|
1005
|
+
;
|
|
1006
|
+
MixedRouteTrade.createUncheckedTrade = function createUncheckedTrade(constructorArguments) {
|
|
1007
|
+
return new MixedRouteTrade(_extends({}, constructorArguments, {
|
|
1008
|
+
routes: [{
|
|
1009
|
+
inputAmount: constructorArguments.inputAmount,
|
|
1010
|
+
outputAmount: constructorArguments.outputAmount,
|
|
1011
|
+
route: constructorArguments.route
|
|
1012
|
+
}]
|
|
1013
|
+
}));
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Creates a trade without computing the result of swapping through the routes. Useful when you have simulated the trade
|
|
1017
|
+
* elsewhere and do not have any tick data
|
|
1018
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
1019
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
1020
|
+
* @template TTradeType The type of the trade, either exact in or exact out
|
|
1021
|
+
* @param constructorArguments The arguments passed to the trade constructor
|
|
1022
|
+
* @returns The unchecked trade
|
|
1023
|
+
*/;
|
|
1024
|
+
MixedRouteTrade.createUncheckedTradeWithMultipleRoutes = function createUncheckedTradeWithMultipleRoutes(constructorArguments) {
|
|
1025
|
+
return new MixedRouteTrade(constructorArguments);
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
1029
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
1030
|
+
* @returns The amount out
|
|
1031
|
+
*/;
|
|
1032
|
+
var _proto = MixedRouteTrade.prototype;
|
|
1033
|
+
_proto.minimumAmountOut = function minimumAmountOut(slippageTolerance, amountOut) {
|
|
1034
|
+
if (amountOut === void 0) {
|
|
1035
|
+
amountOut = this.outputAmount;
|
|
1036
|
+
}
|
|
1037
|
+
!!slippageTolerance.lessThan(ZERO) ? invariant(false, 'SLIPPAGE_TOLERANCE') : void 0;
|
|
1038
|
+
/// does not support exactOutput, as enforced in the constructor
|
|
1039
|
+
var slippageAdjustedAmountOut = new sdkCore.Fraction(ONE).add(slippageTolerance).invert().multiply(amountOut.quotient).quotient;
|
|
1040
|
+
return sdkCore.CurrencyAmount.fromRawAmount(amountOut.currency, slippageAdjustedAmountOut);
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
1044
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
1045
|
+
* @returns The amount in
|
|
1046
|
+
*/;
|
|
1047
|
+
_proto.maximumAmountIn = function maximumAmountIn(slippageTolerance, amountIn) {
|
|
1048
|
+
if (amountIn === void 0) {
|
|
1049
|
+
amountIn = this.inputAmount;
|
|
1050
|
+
}
|
|
1051
|
+
!!slippageTolerance.lessThan(ZERO) ? invariant(false, 'SLIPPAGE_TOLERANCE') : void 0;
|
|
1052
|
+
return amountIn;
|
|
1053
|
+
/// does not support exactOutput
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Return the execution price after accounting for slippage tolerance
|
|
1057
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
1058
|
+
* @returns The execution price
|
|
1059
|
+
*/;
|
|
1060
|
+
_proto.worstExecutionPrice = function worstExecutionPrice(slippageTolerance) {
|
|
1061
|
+
return new sdkCore.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Given a list of pools, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
1065
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
1066
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
1067
|
+
* the amount in among multiple routes.
|
|
1068
|
+
* @param pools the pools to consider in finding the best trade
|
|
1069
|
+
* @param nextAmountIn exact amount of input currency to spend
|
|
1070
|
+
* @param currencyOut the desired currency out
|
|
1071
|
+
* @param maxNumResults maximum number of results to return
|
|
1072
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
|
|
1073
|
+
* @param currentPools used in recursion; the current list of pools
|
|
1074
|
+
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
1075
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
1076
|
+
* @returns The exact in trade
|
|
1077
|
+
*/;
|
|
1078
|
+
MixedRouteTrade.bestTradeExactIn =
|
|
1079
|
+
/*#__PURE__*/
|
|
1080
|
+
function () {
|
|
1081
|
+
var _bestTradeExactIn = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(pools, currencyAmountIn, currencyOut, _temp,
|
|
1082
|
+
// used in recursion.
|
|
1083
|
+
currentPools, nextAmountIn, bestTrades) {
|
|
1084
|
+
var _ref5, _ref5$maxNumResults, maxNumResults, _ref5$maxHops, maxHops, amountIn, i, pool, amountInAdjusted, amountOut, _yield$pool$getOutput, poolsExcludingThisPool;
|
|
1085
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
1086
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
1087
|
+
case 0:
|
|
1088
|
+
_ref5 = _temp === void 0 ? {} : _temp, _ref5$maxNumResults = _ref5.maxNumResults, maxNumResults = _ref5$maxNumResults === void 0 ? 3 : _ref5$maxNumResults, _ref5$maxHops = _ref5.maxHops, maxHops = _ref5$maxHops === void 0 ? 3 : _ref5$maxHops;
|
|
1089
|
+
if (currentPools === void 0) {
|
|
1090
|
+
currentPools = [];
|
|
1091
|
+
}
|
|
1092
|
+
if (nextAmountIn === void 0) {
|
|
1093
|
+
nextAmountIn = currencyAmountIn;
|
|
1094
|
+
}
|
|
1095
|
+
if (bestTrades === void 0) {
|
|
1096
|
+
bestTrades = [];
|
|
1097
|
+
}
|
|
1098
|
+
!(pools.length > 0) ? invariant(false, 'POOLS') : void 0;
|
|
1099
|
+
!(maxHops > 0) ? invariant(false, 'MAX_HOPS') : void 0;
|
|
1100
|
+
!(currencyAmountIn === nextAmountIn || currentPools.length > 0) ? invariant(false, 'INVALID_RECURSION') : void 0;
|
|
1101
|
+
amountIn = nextAmountIn;
|
|
1102
|
+
i = 0;
|
|
1103
|
+
case 9:
|
|
1104
|
+
if (!(i < pools.length)) {
|
|
1105
|
+
_context3.next = 49;
|
|
1106
|
+
break;
|
|
1107
|
+
}
|
|
1108
|
+
pool = pools[i];
|
|
1109
|
+
amountInAdjusted = /* pool instanceof V4Pool ? amountIn : */amountIn.wrapped; // pool irrelevant
|
|
1110
|
+
if (!(!pool.token0.equals(amountInAdjusted.currency) && !pool.token1.equals(amountInAdjusted.currency))) {
|
|
1111
|
+
_context3.next = 14;
|
|
1112
|
+
break;
|
|
1113
|
+
}
|
|
1114
|
+
return _context3.abrupt("continue", 46);
|
|
1115
|
+
case 14:
|
|
1116
|
+
if (!(pool instanceof v2Sdk.Pair)) {
|
|
1117
|
+
_context3.next = 17;
|
|
1118
|
+
break;
|
|
1119
|
+
}
|
|
1120
|
+
if (!(pool.reserve0.equalTo(ZERO) || pool.reserve1.equalTo(ZERO))) {
|
|
1121
|
+
_context3.next = 17;
|
|
1122
|
+
break;
|
|
1123
|
+
}
|
|
1124
|
+
return _context3.abrupt("continue", 46);
|
|
1125
|
+
case 17:
|
|
1126
|
+
amountOut = void 0;
|
|
1127
|
+
_context3.prev = 18;
|
|
1128
|
+
_context3.next = 22;
|
|
1129
|
+
return pool.getOutputAmount(amountInAdjusted.wrapped);
|
|
1130
|
+
case 22:
|
|
1131
|
+
_yield$pool$getOutput = _context3.sent;
|
|
1132
|
+
amountOut = _yield$pool$getOutput[0];
|
|
1133
|
+
_context3.next = 31;
|
|
1134
|
+
break;
|
|
1135
|
+
case 26:
|
|
1136
|
+
_context3.prev = 26;
|
|
1137
|
+
_context3.t0 = _context3["catch"](18);
|
|
1138
|
+
if (!_context3.t0.isInsufficientInputAmountError) {
|
|
1139
|
+
_context3.next = 30;
|
|
1140
|
+
break;
|
|
1141
|
+
}
|
|
1142
|
+
return _context3.abrupt("continue", 46);
|
|
1143
|
+
case 30:
|
|
1144
|
+
throw _context3.t0;
|
|
1145
|
+
case 31:
|
|
1146
|
+
if (!amountOut.currency.wrapped.equals(currencyOut.wrapped)) {
|
|
1147
|
+
_context3.next = 42;
|
|
1148
|
+
break;
|
|
1149
|
+
}
|
|
1150
|
+
_context3.t1 = sdkCore.sortedInsert;
|
|
1151
|
+
_context3.t2 = bestTrades;
|
|
1152
|
+
_context3.next = 36;
|
|
1153
|
+
return MixedRouteTrade.fromRoute(new MixedRouteSDK([].concat(currentPools, [pool]), currencyAmountIn.currency, currencyOut), currencyAmountIn, sdkCore.TradeType.EXACT_INPUT);
|
|
1154
|
+
case 36:
|
|
1155
|
+
_context3.t3 = _context3.sent;
|
|
1156
|
+
_context3.t4 = maxNumResults;
|
|
1157
|
+
_context3.t5 = tradeComparator;
|
|
1158
|
+
(0, _context3.t1)(_context3.t2, _context3.t3, _context3.t4, _context3.t5);
|
|
1159
|
+
_context3.next = 46;
|
|
1160
|
+
break;
|
|
1161
|
+
case 42:
|
|
1162
|
+
if (!(maxHops > 1 && pools.length > 1)) {
|
|
1163
|
+
_context3.next = 46;
|
|
1164
|
+
break;
|
|
1165
|
+
}
|
|
1166
|
+
poolsExcludingThisPool = pools.slice(0, i).concat(pools.slice(i + 1, pools.length)); // otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
|
|
1167
|
+
_context3.next = 46;
|
|
1168
|
+
return MixedRouteTrade.bestTradeExactIn(poolsExcludingThisPool, currencyAmountIn, currencyOut, {
|
|
1169
|
+
maxNumResults: maxNumResults,
|
|
1170
|
+
maxHops: maxHops - 1
|
|
1171
|
+
}, [].concat(currentPools, [pool]), amountOut, bestTrades);
|
|
1172
|
+
case 46:
|
|
1173
|
+
i++;
|
|
1174
|
+
_context3.next = 9;
|
|
1175
|
+
break;
|
|
1176
|
+
case 49:
|
|
1177
|
+
return _context3.abrupt("return", bestTrades);
|
|
1178
|
+
case 50:
|
|
1179
|
+
case "end":
|
|
1180
|
+
return _context3.stop();
|
|
1181
|
+
}
|
|
1182
|
+
}, _callee3, null, [[18, 26]]);
|
|
1183
|
+
}));
|
|
1184
|
+
function bestTradeExactIn(_x6, _x7, _x8, _x9, _x10, _x11, _x12) {
|
|
1185
|
+
return _bestTradeExactIn.apply(this, arguments);
|
|
1186
|
+
}
|
|
1187
|
+
return bestTradeExactIn;
|
|
1188
|
+
}();
|
|
1189
|
+
_createClass(MixedRouteTrade, [{
|
|
1190
|
+
key: "route",
|
|
1191
|
+
get: function get() {
|
|
1192
|
+
!(this.swaps.length === 1) ? invariant(false, 'MULTIPLE_ROUTES') : void 0;
|
|
1193
|
+
return this.swaps[0].route;
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* The input amount for the trade assuming no slippage.
|
|
1197
|
+
*/
|
|
1198
|
+
}, {
|
|
1199
|
+
key: "inputAmount",
|
|
1200
|
+
get: function get() {
|
|
1201
|
+
if (this._inputAmount) {
|
|
1202
|
+
return this._inputAmount;
|
|
1203
|
+
}
|
|
1204
|
+
var inputCurrency = this.swaps[0].inputAmount.currency;
|
|
1205
|
+
var totalInputFromRoutes = this.swaps.map(function (_ref6) {
|
|
1206
|
+
var inputAmount = _ref6.inputAmount;
|
|
1207
|
+
return inputAmount;
|
|
1208
|
+
}).reduce(function (total, cur) {
|
|
1209
|
+
return total.add(cur);
|
|
1210
|
+
}, sdkCore.CurrencyAmount.fromRawAmount(inputCurrency, 0));
|
|
1211
|
+
this._inputAmount = totalInputFromRoutes;
|
|
1212
|
+
return this._inputAmount;
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* The output amount for the trade assuming no slippage.
|
|
1216
|
+
*/
|
|
1217
|
+
}, {
|
|
1218
|
+
key: "outputAmount",
|
|
1219
|
+
get: function get() {
|
|
1220
|
+
if (this._outputAmount) {
|
|
1221
|
+
return this._outputAmount;
|
|
1222
|
+
}
|
|
1223
|
+
var outputCurrency = this.swaps[0].outputAmount.currency;
|
|
1224
|
+
var totalOutputFromRoutes = this.swaps.map(function (_ref7) {
|
|
1225
|
+
var outputAmount = _ref7.outputAmount;
|
|
1226
|
+
return outputAmount;
|
|
1227
|
+
}).reduce(function (total, cur) {
|
|
1228
|
+
return total.add(cur);
|
|
1229
|
+
}, sdkCore.CurrencyAmount.fromRawAmount(outputCurrency, 0));
|
|
1230
|
+
this._outputAmount = totalOutputFromRoutes;
|
|
1231
|
+
return this._outputAmount;
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* The price expressed in terms of output amount/input amount.
|
|
1235
|
+
*/
|
|
1236
|
+
}, {
|
|
1237
|
+
key: "executionPrice",
|
|
1238
|
+
get: function get() {
|
|
1239
|
+
var _this$_executionPrice;
|
|
1240
|
+
return (_this$_executionPrice = this._executionPrice) != null ? _this$_executionPrice : this._executionPrice = new sdkCore.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* Returns the percent difference between the route's mid price and the price impact
|
|
1244
|
+
*/
|
|
1245
|
+
}, {
|
|
1246
|
+
key: "priceImpact",
|
|
1247
|
+
get: function get() {
|
|
1248
|
+
if (this._priceImpact) {
|
|
1249
|
+
return this._priceImpact;
|
|
1250
|
+
}
|
|
1251
|
+
var spotOutputAmount = sdkCore.CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
|
|
1252
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(this.swaps), _step4; !(_step4 = _iterator4()).done;) {
|
|
1253
|
+
var _step4$value = _step4.value,
|
|
1254
|
+
route = _step4$value.route,
|
|
1255
|
+
inputAmount = _step4$value.inputAmount;
|
|
1256
|
+
var midPrice = route.midPrice;
|
|
1257
|
+
spotOutputAmount = spotOutputAmount.add(midPrice.quote(inputAmount));
|
|
1258
|
+
}
|
|
1259
|
+
var priceImpact = spotOutputAmount.subtract(this.outputAmount).divide(spotOutputAmount);
|
|
1260
|
+
this._priceImpact = new sdkCore.Percent(priceImpact.numerator, priceImpact.denominator);
|
|
1261
|
+
return this._priceImpact;
|
|
1262
|
+
}
|
|
1263
|
+
}]);
|
|
1264
|
+
return MixedRouteTrade;
|
|
1265
|
+
}();
|
|
1266
|
+
|
|
1267
|
+
(function (Protocol) {
|
|
1268
|
+
Protocol["V2"] = "V2";
|
|
1269
|
+
Protocol["V3"] = "V3";
|
|
1270
|
+
// V4 = 'V4',
|
|
1271
|
+
Protocol["MIXED"] = "MIXED";
|
|
1272
|
+
})(exports.Protocol || (exports.Protocol = {}));
|
|
1273
|
+
|
|
1274
|
+
// V2 route wrapper
|
|
1275
|
+
var RouteV2 = /*#__PURE__*/function (_V2RouteSDK) {
|
|
1276
|
+
_inheritsLoose(RouteV2, _V2RouteSDK);
|
|
1277
|
+
function RouteV2(v2Route) {
|
|
1278
|
+
var _this;
|
|
1279
|
+
_this = _V2RouteSDK.call(this, v2Route.pairs, v2Route.input, v2Route.output) || this;
|
|
1280
|
+
_this.protocol = exports.Protocol.V2;
|
|
1281
|
+
_this.pools = _this.pairs;
|
|
1282
|
+
return _this;
|
|
1283
|
+
}
|
|
1284
|
+
return RouteV2;
|
|
1285
|
+
}(v2Sdk.Route);
|
|
1286
|
+
// V3 route wrapper
|
|
1287
|
+
var RouteV3 = /*#__PURE__*/function (_V3RouteSDK) {
|
|
1288
|
+
_inheritsLoose(RouteV3, _V3RouteSDK);
|
|
1289
|
+
function RouteV3(v3Route) {
|
|
1290
|
+
var _this2;
|
|
1291
|
+
_this2 = _V3RouteSDK.call(this, v3Route.pools, v3Route.input, v3Route.output) || this;
|
|
1292
|
+
_this2.protocol = exports.Protocol.V3;
|
|
1293
|
+
_this2.path = v3Route.tokenPath;
|
|
1294
|
+
return _this2;
|
|
1295
|
+
}
|
|
1296
|
+
return RouteV3;
|
|
1297
|
+
}(v3Sdk.Route);
|
|
1298
|
+
// V4 route wrapper
|
|
1299
|
+
// export class RouteV4<TInput extends Currency, TOutput extends Currency>
|
|
1300
|
+
// extends V4RouteSDK<TInput, TOutput>
|
|
1301
|
+
// implements IRoute<TInput, TOutput, V4Pool>
|
|
1302
|
+
// {
|
|
1303
|
+
// public readonly protocol: Protocol = Protocol.V4
|
|
1304
|
+
// public readonly path: Currency[]
|
|
1305
|
+
// constructor(v4Route: V4RouteSDK<TInput, TOutput>) {
|
|
1306
|
+
// super(v4Route.pools, v4Route.input, v4Route.output)
|
|
1307
|
+
// this.path = v4Route.currencyPath
|
|
1308
|
+
// }
|
|
1309
|
+
// }
|
|
1310
|
+
// Mixed route wrapper
|
|
1311
|
+
var MixedRoute = /*#__PURE__*/function (_MixedRouteSDK) {
|
|
1312
|
+
_inheritsLoose(MixedRoute, _MixedRouteSDK);
|
|
1313
|
+
function MixedRoute(mixedRoute) {
|
|
1314
|
+
var _this3;
|
|
1315
|
+
_this3 = _MixedRouteSDK.call(this, mixedRoute.pools, mixedRoute.input, mixedRoute.output) || this;
|
|
1316
|
+
_this3.protocol = exports.Protocol.MIXED;
|
|
1317
|
+
return _this3;
|
|
1318
|
+
}
|
|
1319
|
+
return MixedRoute;
|
|
1320
|
+
}(MixedRouteSDK);
|
|
1321
|
+
|
|
1322
|
+
var Trade = /*#__PURE__*/function () {
|
|
1323
|
+
// construct a trade across v2 and v3 routes from pre-computed amounts
|
|
1324
|
+
function Trade(_ref) {
|
|
1325
|
+
var _ref$v2Routes = _ref.v2Routes,
|
|
1326
|
+
v2Routes = _ref$v2Routes === void 0 ? [] : _ref$v2Routes,
|
|
1327
|
+
_ref$v3Routes = _ref.v3Routes,
|
|
1328
|
+
v3Routes = _ref$v3Routes === void 0 ? [] : _ref$v3Routes,
|
|
1329
|
+
_ref$mixedRoutes = _ref.mixedRoutes,
|
|
1330
|
+
mixedRoutes = _ref$mixedRoutes === void 0 ? [] : _ref$mixedRoutes,
|
|
1331
|
+
tradeType = _ref.tradeType;
|
|
1332
|
+
this.swaps = [];
|
|
1333
|
+
this.routes = [];
|
|
1334
|
+
// wrap v2 routes
|
|
1335
|
+
for (var _iterator = _createForOfIteratorHelperLoose(v2Routes), _step; !(_step = _iterator()).done;) {
|
|
1336
|
+
var _step$value = _step.value,
|
|
1337
|
+
routev2 = _step$value.routev2,
|
|
1338
|
+
inputAmount = _step$value.inputAmount,
|
|
1339
|
+
outputAmount = _step$value.outputAmount;
|
|
1340
|
+
var route = new RouteV2(routev2);
|
|
1341
|
+
this.routes.push(route);
|
|
1342
|
+
this.swaps.push({
|
|
1343
|
+
route: route,
|
|
1344
|
+
inputAmount: inputAmount,
|
|
1345
|
+
outputAmount: outputAmount
|
|
1346
|
+
});
|
|
1347
|
+
}
|
|
1348
|
+
// wrap v3 routes
|
|
1349
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(v3Routes), _step2; !(_step2 = _iterator2()).done;) {
|
|
1350
|
+
var _step2$value = _step2.value,
|
|
1351
|
+
routev3 = _step2$value.routev3,
|
|
1352
|
+
_inputAmount = _step2$value.inputAmount,
|
|
1353
|
+
_outputAmount = _step2$value.outputAmount;
|
|
1354
|
+
var _route = new RouteV3(routev3);
|
|
1355
|
+
this.routes.push(_route);
|
|
1356
|
+
this.swaps.push({
|
|
1357
|
+
route: _route,
|
|
1358
|
+
inputAmount: _inputAmount,
|
|
1359
|
+
outputAmount: _outputAmount
|
|
1360
|
+
});
|
|
1361
|
+
}
|
|
1362
|
+
// wrap v4 routes
|
|
1363
|
+
// for (const { routev4, inputAmount, outputAmount } of v4Routes) {
|
|
1364
|
+
// const route = new RouteV4(routev4)
|
|
1365
|
+
// this.routes.push(route)
|
|
1366
|
+
// this.swaps.push({
|
|
1367
|
+
// route,
|
|
1368
|
+
// inputAmount,
|
|
1369
|
+
// outputAmount,
|
|
1370
|
+
// })
|
|
1371
|
+
// }
|
|
1372
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(mixedRoutes), _step3; !(_step3 = _iterator3()).done;) {
|
|
1373
|
+
var _step3$value = _step3.value,
|
|
1374
|
+
mixedRoute = _step3$value.mixedRoute,
|
|
1375
|
+
_inputAmount2 = _step3$value.inputAmount,
|
|
1376
|
+
_outputAmount2 = _step3$value.outputAmount;
|
|
1377
|
+
var _route2 = new MixedRoute(mixedRoute);
|
|
1378
|
+
this.routes.push(_route2);
|
|
1379
|
+
this.swaps.push({
|
|
1380
|
+
route: _route2,
|
|
1381
|
+
inputAmount: _inputAmount2,
|
|
1382
|
+
outputAmount: _outputAmount2
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
if (this.swaps.length === 0) {
|
|
1386
|
+
throw new Error('No routes provided when calling Trade constructor');
|
|
1387
|
+
}
|
|
1388
|
+
this.tradeType = tradeType;
|
|
1389
|
+
// each route must have the same input and output currency
|
|
1390
|
+
var inputCurrency = this.swaps[0].inputAmount.currency;
|
|
1391
|
+
var outputCurrency = this.swaps[0].outputAmount.currency;
|
|
1392
|
+
!this.swaps.every(function (_ref2) {
|
|
1393
|
+
var route = _ref2.route;
|
|
1394
|
+
return inputCurrency.wrapped.equals(route.input.wrapped);
|
|
1395
|
+
}) ? invariant(false, 'INPUT_CURRENCY_MATCH') : void 0;
|
|
1396
|
+
!this.swaps.every(function (_ref3) {
|
|
1397
|
+
var route = _ref3.route;
|
|
1398
|
+
return outputCurrency.wrapped.equals(route.output.wrapped);
|
|
1399
|
+
}) ? invariant(false, 'OUTPUT_CURRENCY_MATCH') : void 0;
|
|
1400
|
+
// pools must be unique inter protocols
|
|
1401
|
+
var numPools = this.swaps.map(function (_ref4) {
|
|
1402
|
+
var route = _ref4.route;
|
|
1403
|
+
return route.pools.length;
|
|
1404
|
+
}).reduce(function (total, cur) {
|
|
1405
|
+
return total + cur;
|
|
1406
|
+
}, 0);
|
|
1407
|
+
var poolIdentifierSet = new Set();
|
|
1408
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(this.swaps), _step4; !(_step4 = _iterator4()).done;) {
|
|
1409
|
+
var _route3 = _step4.value.route;
|
|
1410
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(_route3.pools), _step5; !(_step5 = _iterator5()).done;) {
|
|
1411
|
+
var pool = _step5.value;
|
|
1412
|
+
// if (pool instanceof V4Pool) {
|
|
1413
|
+
// poolIdentifierSet.add(pool.poolId)
|
|
1414
|
+
// } else
|
|
1415
|
+
if (pool instanceof v3Sdk.Pool) {
|
|
1416
|
+
poolIdentifierSet.add(v3Sdk.Pool.getAddress(pool.token0, pool.token1, pool.fee));
|
|
1417
|
+
} else if (pool instanceof v2Sdk.Pair) {
|
|
1418
|
+
var pair = pool;
|
|
1419
|
+
poolIdentifierSet.add(v2Sdk.Pair.getAddress(pair.token0, pair.token1));
|
|
1420
|
+
} else {
|
|
1421
|
+
throw new Error('Unexpected pool type in route when constructing trade object');
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
!(numPools === poolIdentifierSet.size) ? invariant(false, 'POOLS_DUPLICATED') : void 0;
|
|
1426
|
+
}
|
|
1427
|
+
var _proto = Trade.prototype;
|
|
1428
|
+
/**
|
|
1429
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
1430
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
1431
|
+
* @returns The amount out
|
|
1432
|
+
*/
|
|
1433
|
+
_proto.minimumAmountOut = function minimumAmountOut(slippageTolerance, amountOut) {
|
|
1434
|
+
if (amountOut === void 0) {
|
|
1435
|
+
amountOut = this.outputAmount;
|
|
1436
|
+
}
|
|
1437
|
+
!!slippageTolerance.lessThan(ZERO) ? invariant(false, 'SLIPPAGE_TOLERANCE') : void 0;
|
|
1438
|
+
if (this.tradeType === sdkCore.TradeType.EXACT_OUTPUT) {
|
|
1439
|
+
return amountOut;
|
|
1440
|
+
} else {
|
|
1441
|
+
var slippageAdjustedAmountOut = new sdkCore.Fraction(ONE).add(slippageTolerance).invert().multiply(amountOut.quotient).quotient;
|
|
1442
|
+
return sdkCore.CurrencyAmount.fromRawAmount(amountOut.currency, slippageAdjustedAmountOut);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
1447
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
1448
|
+
* @returns The amount in
|
|
1449
|
+
*/;
|
|
1450
|
+
_proto.maximumAmountIn = function maximumAmountIn(slippageTolerance, amountIn) {
|
|
1451
|
+
if (amountIn === void 0) {
|
|
1452
|
+
amountIn = this.inputAmount;
|
|
1453
|
+
}
|
|
1454
|
+
!!slippageTolerance.lessThan(ZERO) ? invariant(false, 'SLIPPAGE_TOLERANCE') : void 0;
|
|
1455
|
+
if (this.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1456
|
+
return amountIn;
|
|
1457
|
+
} else {
|
|
1458
|
+
var slippageAdjustedAmountIn = new sdkCore.Fraction(ONE).add(slippageTolerance).multiply(amountIn.quotient).quotient;
|
|
1459
|
+
return sdkCore.CurrencyAmount.fromRawAmount(amountIn.currency, slippageAdjustedAmountIn);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
/**
|
|
1463
|
+
* Return the execution price after accounting for slippage tolerance
|
|
1464
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
1465
|
+
* @returns The execution price
|
|
1466
|
+
*/;
|
|
1467
|
+
_proto.worstExecutionPrice = function worstExecutionPrice(slippageTolerance) {
|
|
1468
|
+
return new sdkCore.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
1469
|
+
};
|
|
1470
|
+
Trade.fromRoutes = /*#__PURE__*/function () {
|
|
1471
|
+
var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(v2Routes, v3Routes, tradeType, mixedRoutes
|
|
1472
|
+
// v4Routes?: {
|
|
1473
|
+
// routev4: V4RouteSDK<TInput, TOutput>
|
|
1474
|
+
// amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>
|
|
1475
|
+
// }[]
|
|
1476
|
+
) {
|
|
1477
|
+
var populatedV2Routes, populatedV3Routes, populatedMixedRoutes, _iterator6, _step6, _step6$value, routev2, _amount, v2Trade, _inputAmount3, _outputAmount3, _iterator7, _step7, _step7$value, routev3, _amount2, v3Trade, _inputAmount4, _outputAmount4, _iterator8, _step8, _step8$value, mixedRoute, amount, mixedRouteTrade, inputAmount, outputAmount;
|
|
1478
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1479
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1480
|
+
case 0:
|
|
1481
|
+
populatedV2Routes = [];
|
|
1482
|
+
populatedV3Routes = []; // const populatedV4Routes: {
|
|
1483
|
+
// routev4: V4RouteSDK<TInput, TOutput>
|
|
1484
|
+
// inputAmount: CurrencyAmount<TInput>
|
|
1485
|
+
// outputAmount: CurrencyAmount<TOutput>
|
|
1486
|
+
// }[] = []
|
|
1487
|
+
populatedMixedRoutes = [];
|
|
1488
|
+
for (_iterator6 = _createForOfIteratorHelperLoose(v2Routes); !(_step6 = _iterator6()).done;) {
|
|
1489
|
+
_step6$value = _step6.value, routev2 = _step6$value.routev2, _amount = _step6$value.amount;
|
|
1490
|
+
v2Trade = new v2Sdk.Trade(routev2, _amount, tradeType);
|
|
1491
|
+
_inputAmount3 = v2Trade.inputAmount, _outputAmount3 = v2Trade.outputAmount;
|
|
1492
|
+
populatedV2Routes.push({
|
|
1493
|
+
routev2: routev2,
|
|
1494
|
+
inputAmount: _inputAmount3,
|
|
1495
|
+
outputAmount: _outputAmount3
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
_iterator7 = _createForOfIteratorHelperLoose(v3Routes);
|
|
1499
|
+
case 5:
|
|
1500
|
+
if ((_step7 = _iterator7()).done) {
|
|
1501
|
+
_context.next = 14;
|
|
1502
|
+
break;
|
|
1503
|
+
}
|
|
1504
|
+
_step7$value = _step7.value, routev3 = _step7$value.routev3, _amount2 = _step7$value.amount;
|
|
1505
|
+
_context.next = 9;
|
|
1506
|
+
return v3Sdk.Trade.fromRoute(routev3, _amount2, tradeType);
|
|
1507
|
+
case 9:
|
|
1508
|
+
v3Trade = _context.sent;
|
|
1509
|
+
_inputAmount4 = v3Trade.inputAmount, _outputAmount4 = v3Trade.outputAmount;
|
|
1510
|
+
populatedV3Routes.push({
|
|
1511
|
+
routev3: routev3,
|
|
1512
|
+
inputAmount: _inputAmount4,
|
|
1513
|
+
outputAmount: _outputAmount4
|
|
1514
|
+
});
|
|
1515
|
+
case 12:
|
|
1516
|
+
_context.next = 5;
|
|
1517
|
+
break;
|
|
1518
|
+
case 14:
|
|
1519
|
+
if (!mixedRoutes) {
|
|
1520
|
+
_context.next = 25;
|
|
1521
|
+
break;
|
|
1522
|
+
}
|
|
1523
|
+
_iterator8 = _createForOfIteratorHelperLoose(mixedRoutes);
|
|
1524
|
+
case 16:
|
|
1525
|
+
if ((_step8 = _iterator8()).done) {
|
|
1526
|
+
_context.next = 25;
|
|
1527
|
+
break;
|
|
1528
|
+
}
|
|
1529
|
+
_step8$value = _step8.value, mixedRoute = _step8$value.mixedRoute, amount = _step8$value.amount;
|
|
1530
|
+
_context.next = 20;
|
|
1531
|
+
return MixedRouteTrade.fromRoute(mixedRoute, amount, tradeType);
|
|
1532
|
+
case 20:
|
|
1533
|
+
mixedRouteTrade = _context.sent;
|
|
1534
|
+
inputAmount = mixedRouteTrade.inputAmount, outputAmount = mixedRouteTrade.outputAmount;
|
|
1535
|
+
populatedMixedRoutes.push({
|
|
1536
|
+
mixedRoute: mixedRoute,
|
|
1537
|
+
inputAmount: inputAmount,
|
|
1538
|
+
outputAmount: outputAmount
|
|
1539
|
+
});
|
|
1540
|
+
case 23:
|
|
1541
|
+
_context.next = 16;
|
|
1542
|
+
break;
|
|
1543
|
+
case 25:
|
|
1544
|
+
return _context.abrupt("return", new Trade({
|
|
1545
|
+
v2Routes: populatedV2Routes,
|
|
1546
|
+
v3Routes: populatedV3Routes,
|
|
1547
|
+
// v4Routes: populatedV4Routes,
|
|
1548
|
+
mixedRoutes: populatedMixedRoutes,
|
|
1549
|
+
tradeType: tradeType
|
|
1550
|
+
}));
|
|
1551
|
+
case 26:
|
|
1552
|
+
case "end":
|
|
1553
|
+
return _context.stop();
|
|
1554
|
+
}
|
|
1555
|
+
}, _callee);
|
|
1556
|
+
}));
|
|
1557
|
+
function fromRoutes(_x, _x2, _x3, _x4) {
|
|
1558
|
+
return _fromRoutes.apply(this, arguments);
|
|
1559
|
+
}
|
|
1560
|
+
return fromRoutes;
|
|
1561
|
+
}();
|
|
1562
|
+
Trade.fromRoute = /*#__PURE__*/function () {
|
|
1563
|
+
var _fromRoute = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(route, amount, tradeType) {
|
|
1564
|
+
var v2Routes, v3Routes, mixedRoutes, v2Trade, inputAmount, outputAmount, v3Trade, _inputAmount5, _outputAmount5, mixedRouteTrade, _inputAmount6, _outputAmount6;
|
|
1565
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
1566
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
1567
|
+
case 0:
|
|
1568
|
+
v2Routes = [];
|
|
1569
|
+
v3Routes = []; // let v4Routes: {
|
|
1570
|
+
// routev4: V4RouteSDK<TInput, TOutput>
|
|
1571
|
+
// inputAmount: CurrencyAmount<TInput>
|
|
1572
|
+
// outputAmount: CurrencyAmount<TOutput>
|
|
1573
|
+
// }[] = []
|
|
1574
|
+
mixedRoutes = [];
|
|
1575
|
+
if (!(route instanceof v2Sdk.Route)) {
|
|
1576
|
+
_context2.next = 9;
|
|
1577
|
+
break;
|
|
1578
|
+
}
|
|
1579
|
+
v2Trade = new v2Sdk.Trade(route, amount, tradeType);
|
|
1580
|
+
inputAmount = v2Trade.inputAmount, outputAmount = v2Trade.outputAmount;
|
|
1581
|
+
v2Routes = [{
|
|
1582
|
+
routev2: route,
|
|
1583
|
+
inputAmount: inputAmount,
|
|
1584
|
+
outputAmount: outputAmount
|
|
1585
|
+
}];
|
|
1586
|
+
_context2.next = 26;
|
|
1587
|
+
break;
|
|
1588
|
+
case 9:
|
|
1589
|
+
if (!(route instanceof v3Sdk.Route)) {
|
|
1590
|
+
_context2.next = 17;
|
|
1591
|
+
break;
|
|
1592
|
+
}
|
|
1593
|
+
_context2.next = 12;
|
|
1594
|
+
return v3Sdk.Trade.fromRoute(route, amount, tradeType);
|
|
1595
|
+
case 12:
|
|
1596
|
+
v3Trade = _context2.sent;
|
|
1597
|
+
_inputAmount5 = v3Trade.inputAmount, _outputAmount5 = v3Trade.outputAmount;
|
|
1598
|
+
v3Routes = [{
|
|
1599
|
+
routev3: route,
|
|
1600
|
+
inputAmount: _inputAmount5,
|
|
1601
|
+
outputAmount: _outputAmount5
|
|
1602
|
+
}];
|
|
1603
|
+
// } else if (route instanceof V4RouteSDK) {
|
|
1604
|
+
// const v4Trade = await V4TradeSDK.fromRoute(route, amount, tradeType)
|
|
1605
|
+
// const { inputAmount, outputAmount } = v4Trade
|
|
1606
|
+
// v4Routes = [{ routev4: route, inputAmount, outputAmount }]
|
|
1607
|
+
_context2.next = 26;
|
|
1608
|
+
break;
|
|
1609
|
+
case 17:
|
|
1610
|
+
if (!(route instanceof MixedRouteSDK)) {
|
|
1611
|
+
_context2.next = 25;
|
|
1612
|
+
break;
|
|
1613
|
+
}
|
|
1614
|
+
_context2.next = 20;
|
|
1615
|
+
return MixedRouteTrade.fromRoute(route, amount, tradeType);
|
|
1616
|
+
case 20:
|
|
1617
|
+
mixedRouteTrade = _context2.sent;
|
|
1618
|
+
_inputAmount6 = mixedRouteTrade.inputAmount, _outputAmount6 = mixedRouteTrade.outputAmount;
|
|
1619
|
+
mixedRoutes = [{
|
|
1620
|
+
mixedRoute: route,
|
|
1621
|
+
inputAmount: _inputAmount6,
|
|
1622
|
+
outputAmount: _outputAmount6
|
|
1623
|
+
}];
|
|
1624
|
+
_context2.next = 26;
|
|
1625
|
+
break;
|
|
1626
|
+
case 25:
|
|
1627
|
+
throw new Error('Invalid route type');
|
|
1628
|
+
case 26:
|
|
1629
|
+
return _context2.abrupt("return", new Trade({
|
|
1630
|
+
v2Routes: v2Routes,
|
|
1631
|
+
v3Routes: v3Routes,
|
|
1632
|
+
// v4Routes,
|
|
1633
|
+
mixedRoutes: mixedRoutes,
|
|
1634
|
+
tradeType: tradeType
|
|
1635
|
+
}));
|
|
1636
|
+
case 27:
|
|
1637
|
+
case "end":
|
|
1638
|
+
return _context2.stop();
|
|
1639
|
+
}
|
|
1640
|
+
}, _callee2);
|
|
1641
|
+
}));
|
|
1642
|
+
function fromRoute(_x5, _x6, _x7) {
|
|
1643
|
+
return _fromRoute.apply(this, arguments);
|
|
1644
|
+
}
|
|
1645
|
+
return fromRoute;
|
|
1646
|
+
}();
|
|
1647
|
+
_createClass(Trade, [{
|
|
1648
|
+
key: "inputAmount",
|
|
1649
|
+
get: function get() {
|
|
1650
|
+
if (this._inputAmount) {
|
|
1651
|
+
return this._inputAmount;
|
|
1652
|
+
}
|
|
1653
|
+
var inputCurrency = this.swaps[0].inputAmount.currency;
|
|
1654
|
+
var totalInputFromRoutes = this.swaps.map(function (_ref5) {
|
|
1655
|
+
var inputAmount = _ref5.inputAmount;
|
|
1656
|
+
return inputAmount;
|
|
1657
|
+
}).reduce(function (total, cur) {
|
|
1658
|
+
return total.add(cur);
|
|
1659
|
+
}, sdkCore.CurrencyAmount.fromRawAmount(inputCurrency, 0));
|
|
1660
|
+
this._inputAmount = totalInputFromRoutes;
|
|
1661
|
+
return this._inputAmount;
|
|
1662
|
+
}
|
|
1663
|
+
}, {
|
|
1664
|
+
key: "outputAmount",
|
|
1665
|
+
get: function get() {
|
|
1666
|
+
if (this._outputAmount) {
|
|
1667
|
+
return this._outputAmount;
|
|
1668
|
+
}
|
|
1669
|
+
var outputCurrency = this.swaps[0].outputAmount.currency;
|
|
1670
|
+
var totalOutputFromRoutes = this.swaps.map(function (_ref6) {
|
|
1671
|
+
var outputAmount = _ref6.outputAmount;
|
|
1672
|
+
return outputAmount;
|
|
1673
|
+
}).reduce(function (total, cur) {
|
|
1674
|
+
return total.add(cur);
|
|
1675
|
+
}, sdkCore.CurrencyAmount.fromRawAmount(outputCurrency, 0));
|
|
1676
|
+
this._outputAmount = totalOutputFromRoutes;
|
|
1677
|
+
return this._outputAmount;
|
|
1678
|
+
}
|
|
1679
|
+
/**
|
|
1680
|
+
* The price expressed in terms of output amount/input amount.
|
|
1681
|
+
*/
|
|
1682
|
+
}, {
|
|
1683
|
+
key: "executionPrice",
|
|
1684
|
+
get: function get() {
|
|
1685
|
+
var _this$_executionPrice;
|
|
1686
|
+
return (_this$_executionPrice = this._executionPrice) != null ? _this$_executionPrice : this._executionPrice = new sdkCore.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
1687
|
+
}
|
|
1688
|
+
/**
|
|
1689
|
+
* Returns the sell tax of the input token
|
|
1690
|
+
*/
|
|
1691
|
+
}, {
|
|
1692
|
+
key: "inputTax",
|
|
1693
|
+
get: function get() {
|
|
1694
|
+
var inputCurrency = this.inputAmount.currency;
|
|
1695
|
+
if (inputCurrency.isNative || !inputCurrency.wrapped.sellFeeBps) return ZERO_PERCENT;
|
|
1696
|
+
return new sdkCore.Percent(inputCurrency.wrapped.sellFeeBps.toNumber(), 10000);
|
|
1697
|
+
}
|
|
1698
|
+
/**
|
|
1699
|
+
* Returns the buy tax of the output token
|
|
1700
|
+
*/
|
|
1701
|
+
}, {
|
|
1702
|
+
key: "outputTax",
|
|
1703
|
+
get: function get() {
|
|
1704
|
+
var outputCurrency = this.outputAmount.currency;
|
|
1705
|
+
if (outputCurrency.isNative || !outputCurrency.wrapped.buyFeeBps) return ZERO_PERCENT;
|
|
1706
|
+
return new sdkCore.Percent(outputCurrency.wrapped.buyFeeBps.toNumber(), 10000);
|
|
1707
|
+
}
|
|
1708
|
+
/**
|
|
1709
|
+
* Returns the percent difference between the route's mid price and the expected execution price
|
|
1710
|
+
* In order to exclude token taxes from the price impact calculation, the spot price is calculated
|
|
1711
|
+
* using a ratio of values that go into the pools, which are the post-tax input amount and pre-tax output amount.
|
|
1712
|
+
*/
|
|
1713
|
+
}, {
|
|
1714
|
+
key: "priceImpact",
|
|
1715
|
+
get: function get() {
|
|
1716
|
+
if (this._priceImpact) {
|
|
1717
|
+
return this._priceImpact;
|
|
1718
|
+
}
|
|
1719
|
+
// returns 0% price impact even though this may be inaccurate as a swap may have occured.
|
|
1720
|
+
// because we're unable to derive the pre-buy-tax amount, use 0% as a placeholder.
|
|
1721
|
+
if (this.outputTax.equalTo(ONE_HUNDRED_PERCENT)) return ZERO_PERCENT;
|
|
1722
|
+
var spotOutputAmount = sdkCore.CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
|
|
1723
|
+
for (var _iterator9 = _createForOfIteratorHelperLoose(this.swaps), _step9; !(_step9 = _iterator9()).done;) {
|
|
1724
|
+
var _step9$value = _step9.value,
|
|
1725
|
+
route = _step9$value.route,
|
|
1726
|
+
inputAmount = _step9$value.inputAmount;
|
|
1727
|
+
var midPrice = route.midPrice;
|
|
1728
|
+
var postTaxInputAmount = inputAmount.multiply(new sdkCore.Fraction(ONE).subtract(this.inputTax));
|
|
1729
|
+
spotOutputAmount = spotOutputAmount.add(midPrice.quote(postTaxInputAmount));
|
|
1730
|
+
}
|
|
1731
|
+
// if the total output of this trade is 0, then most likely the post-tax input was also 0, and therefore this swap
|
|
1732
|
+
// does not move the pools' market price
|
|
1733
|
+
if (spotOutputAmount.equalTo(ZERO)) return ZERO_PERCENT;
|
|
1734
|
+
var preTaxOutputAmount = this.outputAmount.divide(new sdkCore.Fraction(ONE).subtract(this.outputTax));
|
|
1735
|
+
var priceImpact = spotOutputAmount.subtract(preTaxOutputAmount).divide(spotOutputAmount);
|
|
1736
|
+
this._priceImpact = new sdkCore.Percent(priceImpact.numerator, priceImpact.denominator);
|
|
1737
|
+
return this._priceImpact;
|
|
1738
|
+
}
|
|
1739
|
+
}]);
|
|
1740
|
+
return Trade;
|
|
1741
|
+
}();
|
|
1742
|
+
|
|
1743
|
+
/**
|
|
1744
|
+
* Converts a route to a hex encoded path
|
|
1745
|
+
* @notice only supports exactIn route encodings
|
|
1746
|
+
* @param route the mixed path to convert to an encoded path
|
|
1747
|
+
* @returns the exactIn encoded path
|
|
1748
|
+
*/
|
|
1749
|
+
function encodeMixedRouteToPath(route) {
|
|
1750
|
+
// const containsV4Pool = route.pools.some((pool) => pool instanceof V4Pool)
|
|
1751
|
+
var path;
|
|
1752
|
+
var types;
|
|
1753
|
+
// if (containsV4Pool) {
|
|
1754
|
+
// path = [route.adjustedInput.isNative ? ADDRESS_ZERO : route.adjustedInput.address]
|
|
1755
|
+
// types = ['address']
|
|
1756
|
+
// let currencyIn = route.adjustedInput
|
|
1757
|
+
// for (const pool of route.pools) {
|
|
1758
|
+
// const currencyOut = currencyIn.equals(pool.token0) ? pool.token1 : pool.token0
|
|
1759
|
+
// if (pool instanceof V4Pool) {
|
|
1760
|
+
// const v4Fee = pool.fee + MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER
|
|
1761
|
+
// path.push(
|
|
1762
|
+
// v4Fee,
|
|
1763
|
+
// pool.tickSpacing,
|
|
1764
|
+
// pool.hooks,
|
|
1765
|
+
// currencyOut.isNative ? ADDRESS_ZERO : currencyOut.wrapped.address
|
|
1766
|
+
// )
|
|
1767
|
+
// types.push('uint24', 'uint24', 'address', 'address')
|
|
1768
|
+
// } else if (pool instanceof V3Pool) {
|
|
1769
|
+
// const v3Fee = pool.fee + MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER
|
|
1770
|
+
// path.push(v3Fee, currencyOut.wrapped.address)
|
|
1771
|
+
// types.push('uint24', 'address')
|
|
1772
|
+
// } else if (pool instanceof Pair) {
|
|
1773
|
+
// const v2Fee = MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER
|
|
1774
|
+
// path.push(v2Fee, currencyOut.wrapped.address)
|
|
1775
|
+
// types.push('uint8', 'address')
|
|
1776
|
+
// } else {
|
|
1777
|
+
// throw new Error(`Unsupported pool type ${JSON.stringify(pool)}`)
|
|
1778
|
+
// }
|
|
1779
|
+
// currencyIn = currencyOut
|
|
1780
|
+
// }
|
|
1781
|
+
// } else {
|
|
1782
|
+
// TODO: ROUTE-276 - delete this else block
|
|
1783
|
+
// We introduced this else block as a safety measure to prevent non-v4 mixed routes from potentially regressing
|
|
1784
|
+
// We'd like to gain more confidence in the new implementation before removing this block
|
|
1785
|
+
var result = route.pools.reduce(function (_ref, pool, index) {
|
|
1786
|
+
var inputToken = _ref.inputToken,
|
|
1787
|
+
path = _ref.path,
|
|
1788
|
+
types = _ref.types;
|
|
1789
|
+
var outputToken = pool.token0.equals(inputToken) ? pool.token1 : pool.token0;
|
|
1790
|
+
if (index === 0) {
|
|
1791
|
+
return {
|
|
1792
|
+
inputToken: outputToken,
|
|
1793
|
+
types: ['address', 'uint24', 'address'],
|
|
1794
|
+
path: [inputToken.wrapped.address, pool instanceof v3Sdk.Pool ? pool.fee : MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER, outputToken.wrapped.address]
|
|
1795
|
+
};
|
|
1796
|
+
} else {
|
|
1797
|
+
return {
|
|
1798
|
+
inputToken: outputToken,
|
|
1799
|
+
types: [].concat(types, ['uint24', 'address']),
|
|
1800
|
+
path: [].concat(path, [pool instanceof v3Sdk.Pool ? pool.fee : MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER, outputToken.wrapped.address])
|
|
1801
|
+
};
|
|
1802
|
+
}
|
|
1803
|
+
}, {
|
|
1804
|
+
inputToken: route.input,
|
|
1805
|
+
path: [],
|
|
1806
|
+
types: []
|
|
1807
|
+
});
|
|
1808
|
+
path = result.path;
|
|
1809
|
+
types = result.types;
|
|
1810
|
+
// }
|
|
1811
|
+
return solidity.pack(types, path);
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
/**
|
|
1815
|
+
* Utility function to return each consecutive section of Pools or Pairs in a MixedRoute
|
|
1816
|
+
* @param route
|
|
1817
|
+
* @returns a nested array of Pools or Pairs in the order of the route
|
|
1818
|
+
*/
|
|
1819
|
+
var partitionMixedRouteByProtocol = function partitionMixedRouteByProtocol(route) {
|
|
1820
|
+
var acc = [];
|
|
1821
|
+
var left = 0;
|
|
1822
|
+
var right = 0;
|
|
1823
|
+
while (right < route.pools.length) {
|
|
1824
|
+
if (route.pools[left] instanceof v3Sdk.Pool && route.pools[right] instanceof v2Sdk.Pair || route.pools[left] instanceof v2Sdk.Pair && route.pools[right] instanceof v3Sdk.Pool) {
|
|
1825
|
+
acc.push(route.pools.slice(left, right));
|
|
1826
|
+
left = right;
|
|
1827
|
+
}
|
|
1828
|
+
// seek forward with right pointer
|
|
1829
|
+
right++;
|
|
1830
|
+
if (right === route.pools.length) {
|
|
1831
|
+
/// we reached the end, take the rest
|
|
1832
|
+
acc.push(route.pools.slice(left, right));
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
return acc;
|
|
1836
|
+
};
|
|
1837
|
+
/**
|
|
1838
|
+
* Simple utility function to get the output of an array of Pools or Pairs
|
|
1839
|
+
* @param pools
|
|
1840
|
+
* @param firstInputToken
|
|
1841
|
+
* @returns the output token of the last pool in the array
|
|
1842
|
+
*/
|
|
1843
|
+
var getOutputOfPools = function getOutputOfPools(pools, firstInputToken) {
|
|
1844
|
+
var _pools$reduce = pools.reduce(function (_ref, pool) {
|
|
1845
|
+
var inputToken = _ref.inputToken;
|
|
1846
|
+
if (!pool.involvesToken(inputToken)) throw new Error('PATH');
|
|
1847
|
+
var outputToken = pool.token0.equals(inputToken) ? pool.token1 : pool.token0;
|
|
1848
|
+
return {
|
|
1849
|
+
inputToken: outputToken
|
|
1850
|
+
};
|
|
1851
|
+
}, {
|
|
1852
|
+
inputToken: firstInputToken
|
|
1853
|
+
}),
|
|
1854
|
+
outputToken = _pools$reduce.inputToken;
|
|
1855
|
+
return outputToken;
|
|
1856
|
+
};
|
|
1857
|
+
|
|
1858
|
+
var ZERO$1 = /*#__PURE__*/JSBI.BigInt(0);
|
|
1859
|
+
var REFUND_ETH_PRICE_IMPACT_THRESHOLD = /*#__PURE__*/new sdkCore.Percent( /*#__PURE__*/JSBI.BigInt(50), /*#__PURE__*/JSBI.BigInt(100));
|
|
1860
|
+
/**
|
|
1861
|
+
* Represents the Uniswap V2 + V3 SwapRouter02, and has static methods for helping execute trades.
|
|
1862
|
+
*/
|
|
1863
|
+
var SwapRouter = /*#__PURE__*/function () {
|
|
1864
|
+
/**
|
|
1865
|
+
* Cannot be constructed.
|
|
1866
|
+
*/
|
|
1867
|
+
function SwapRouter() {}
|
|
1868
|
+
/**
|
|
1869
|
+
* @notice Generates the calldata for a Swap with a V2 Route.
|
|
1870
|
+
* @param trade The V2Trade to encode.
|
|
1871
|
+
* @param options SwapOptions to use for the trade.
|
|
1872
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
1873
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
1874
|
+
* @returns A string array of calldatas for the trade.
|
|
1875
|
+
*/
|
|
1876
|
+
SwapRouter.encodeV2Swap = function encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
1877
|
+
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance).quotient);
|
|
1878
|
+
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance).quotient);
|
|
1879
|
+
var path = trade.route.path.map(function (token) {
|
|
1880
|
+
return token.address;
|
|
1881
|
+
});
|
|
1882
|
+
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : sdkCore.validateAndParseAddress(options.recipient);
|
|
1883
|
+
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1884
|
+
var exactInputParams = [amountIn, performAggregatedSlippageCheck ? 0 : amountOut, path, recipient];
|
|
1885
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams);
|
|
1886
|
+
} else {
|
|
1887
|
+
var exactOutputParams = [amountOut, amountIn, path, recipient];
|
|
1888
|
+
return SwapRouter.INTERFACE.encodeFunctionData('swapTokensForExactTokens', exactOutputParams);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
/**
|
|
1892
|
+
* @notice Generates the calldata for a Swap with a V3 Route.
|
|
1893
|
+
* @param trade The V3Trade to encode.
|
|
1894
|
+
* @param options SwapOptions to use for the trade.
|
|
1895
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
1896
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
1897
|
+
* @returns A string array of calldatas for the trade.
|
|
1898
|
+
*/;
|
|
1899
|
+
SwapRouter.encodeV3Swap = function encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
1900
|
+
var calldatas = [];
|
|
1901
|
+
for (var _iterator = _createForOfIteratorHelperLoose(trade.swaps), _step; !(_step = _iterator()).done;) {
|
|
1902
|
+
var _step$value = _step.value,
|
|
1903
|
+
route = _step$value.route,
|
|
1904
|
+
inputAmount = _step$value.inputAmount,
|
|
1905
|
+
outputAmount = _step$value.outputAmount;
|
|
1906
|
+
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
1907
|
+
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient);
|
|
1908
|
+
// flag for whether the trade is single hop or not
|
|
1909
|
+
var singleHop = route.pools.length === 1;
|
|
1910
|
+
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : sdkCore.validateAndParseAddress(options.recipient);
|
|
1911
|
+
if (singleHop) {
|
|
1912
|
+
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1913
|
+
var exactInputSingleParams = {
|
|
1914
|
+
tokenIn: route.tokenPath[0].address,
|
|
1915
|
+
tokenOut: route.tokenPath[1].address,
|
|
1916
|
+
fee: route.pools[0].fee,
|
|
1917
|
+
recipient: recipient,
|
|
1918
|
+
amountIn: amountIn,
|
|
1919
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut,
|
|
1920
|
+
sqrtPriceLimitX96: 0
|
|
1921
|
+
};
|
|
1922
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
1923
|
+
} else {
|
|
1924
|
+
var exactOutputSingleParams = {
|
|
1925
|
+
tokenIn: route.tokenPath[0].address,
|
|
1926
|
+
tokenOut: route.tokenPath[1].address,
|
|
1927
|
+
fee: route.pools[0].fee,
|
|
1928
|
+
recipient: recipient,
|
|
1929
|
+
amountOut: amountOut,
|
|
1930
|
+
amountInMaximum: amountIn,
|
|
1931
|
+
sqrtPriceLimitX96: 0
|
|
1932
|
+
};
|
|
1933
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutputSingle', [exactOutputSingleParams]));
|
|
1934
|
+
}
|
|
1935
|
+
} else {
|
|
1936
|
+
var path = v3Sdk.encodeRouteToPath(route, trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT);
|
|
1937
|
+
if (trade.tradeType === sdkCore.TradeType.EXACT_INPUT) {
|
|
1938
|
+
var exactInputParams = {
|
|
1939
|
+
path: path,
|
|
1940
|
+
recipient: recipient,
|
|
1941
|
+
amountIn: amountIn,
|
|
1942
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut
|
|
1943
|
+
};
|
|
1944
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]));
|
|
1945
|
+
} else {
|
|
1946
|
+
var exactOutputParams = {
|
|
1947
|
+
path: path,
|
|
1948
|
+
recipient: recipient,
|
|
1949
|
+
amountOut: amountOut,
|
|
1950
|
+
amountInMaximum: amountIn
|
|
1951
|
+
};
|
|
1952
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutput', [exactOutputParams]));
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
return calldatas;
|
|
1957
|
+
}
|
|
1958
|
+
/**
|
|
1959
|
+
* @notice Generates the calldata for a MixedRouteSwap. Since single hop routes are not MixedRoutes, we will instead generate
|
|
1960
|
+
* them via the existing encodeV3Swap and encodeV2Swap methods.
|
|
1961
|
+
* @param trade The MixedRouteTrade to encode.
|
|
1962
|
+
* @param options SwapOptions to use for the trade.
|
|
1963
|
+
* @param routerMustCustody Flag for whether funds should be sent to the router
|
|
1964
|
+
* @param performAggregatedSlippageCheck Flag for whether we want to perform an aggregated slippage check
|
|
1965
|
+
* @returns A string array of calldatas for the trade.
|
|
1966
|
+
*/;
|
|
1967
|
+
SwapRouter.encodeMixedRouteSwap = function encodeMixedRouteSwap(trade, options, routerMustCustody, performAggregatedSlippageCheck) {
|
|
1968
|
+
var calldatas = [];
|
|
1969
|
+
!(trade.tradeType === sdkCore.TradeType.EXACT_INPUT) ? invariant(false, 'TRADE_TYPE') : void 0;
|
|
1970
|
+
var _loop = function _loop() {
|
|
1971
|
+
var _step2$value = _step2.value,
|
|
1972
|
+
route = _step2$value.route,
|
|
1973
|
+
inputAmount = _step2$value.inputAmount,
|
|
1974
|
+
outputAmount = _step2$value.outputAmount;
|
|
1975
|
+
// if (route.pools.some((pool) => pool instanceof V4Pool)) throw 'Encoding mixed routes with V4 not supported'
|
|
1976
|
+
var amountIn = v3Sdk.toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient);
|
|
1977
|
+
var amountOut = v3Sdk.toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient);
|
|
1978
|
+
// flag for whether the trade is single hop or not
|
|
1979
|
+
var singleHop = route.pools.length === 1;
|
|
1980
|
+
var recipient = routerMustCustody ? ADDRESS_THIS : typeof options.recipient === 'undefined' ? MSG_SENDER : sdkCore.validateAndParseAddress(options.recipient);
|
|
1981
|
+
var mixedRouteIsAllV3 = function mixedRouteIsAllV3(route) {
|
|
1982
|
+
return route.pools.every(function (pool) {
|
|
1983
|
+
return pool instanceof v3Sdk.Pool;
|
|
1984
|
+
});
|
|
1985
|
+
};
|
|
1986
|
+
if (singleHop) {
|
|
1987
|
+
/// For single hop, since it isn't really a mixedRoute, we'll just mimic behavior of V3 or V2
|
|
1988
|
+
/// We don't use encodeV3Swap() or encodeV2Swap() because casting the trade to a V3Trade or V2Trade is overcomplex
|
|
1989
|
+
if (mixedRouteIsAllV3(route)) {
|
|
1990
|
+
var exactInputSingleParams = {
|
|
1991
|
+
tokenIn: route.path[0].wrapped.address,
|
|
1992
|
+
tokenOut: route.path[1].wrapped.address,
|
|
1993
|
+
fee: route.pools[0].fee,
|
|
1994
|
+
recipient: recipient,
|
|
1995
|
+
amountIn: amountIn,
|
|
1996
|
+
amountOutMinimum: performAggregatedSlippageCheck ? 0 : amountOut,
|
|
1997
|
+
sqrtPriceLimitX96: 0
|
|
1998
|
+
};
|
|
1999
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]));
|
|
2000
|
+
} else {
|
|
2001
|
+
var path = route.path.map(function (token) {
|
|
2002
|
+
return token.wrapped.address;
|
|
2003
|
+
});
|
|
2004
|
+
var exactInputParams = [amountIn, performAggregatedSlippageCheck ? 0 : amountOut, path, recipient];
|
|
2005
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', exactInputParams));
|
|
2006
|
+
}
|
|
2007
|
+
} else {
|
|
2008
|
+
var sections = partitionMixedRouteByProtocol(route);
|
|
2009
|
+
var isLastSectionInRoute = function isLastSectionInRoute(i) {
|
|
2010
|
+
return i === sections.length - 1;
|
|
2011
|
+
};
|
|
2012
|
+
var outputToken;
|
|
2013
|
+
var inputToken = route.input.wrapped;
|
|
2014
|
+
for (var i = 0; i < sections.length; i++) {
|
|
2015
|
+
var section = sections[i];
|
|
2016
|
+
/// Now, we get output of this section
|
|
2017
|
+
outputToken = getOutputOfPools(section, inputToken);
|
|
2018
|
+
var newRouteOriginal = new MixedRouteSDK([].concat(section), section[0].token0.equals(inputToken) ? section[0].token0 : section[0].token1, outputToken);
|
|
2019
|
+
var newRoute = new MixedRoute(newRouteOriginal);
|
|
2020
|
+
/// Previous output is now input
|
|
2021
|
+
inputToken = outputToken.wrapped;
|
|
2022
|
+
if (mixedRouteIsAllV3(newRoute)) {
|
|
2023
|
+
var _path = encodeMixedRouteToPath(newRoute);
|
|
2024
|
+
var _exactInputParams = {
|
|
2025
|
+
path: _path,
|
|
2026
|
+
// By default router holds funds until the last swap, then it is sent to the recipient
|
|
2027
|
+
// special case exists where we are unwrapping WETH output, in which case `routerMustCustody` is set to true
|
|
2028
|
+
// and router still holds the funds. That logic bundled into how the value of `recipient` is calculated
|
|
2029
|
+
recipient: isLastSectionInRoute(i) ? recipient : ADDRESS_THIS,
|
|
2030
|
+
amountIn: i === 0 ? amountIn : 0,
|
|
2031
|
+
amountOutMinimum: !isLastSectionInRoute(i) ? 0 : amountOut
|
|
2032
|
+
};
|
|
2033
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [_exactInputParams]));
|
|
2034
|
+
} else {
|
|
2035
|
+
var _exactInputParams2 = [i === 0 ? amountIn : 0, !isLastSectionInRoute(i) ? 0 : amountOut, newRoute.path.map(function (token) {
|
|
2036
|
+
return token.wrapped.address;
|
|
2037
|
+
}), isLastSectionInRoute(i) ? recipient : ADDRESS_THIS];
|
|
2038
|
+
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('swapExactTokensForTokens', _exactInputParams2));
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
};
|
|
2043
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(trade.swaps), _step2; !(_step2 = _iterator2()).done;) {
|
|
2044
|
+
_loop();
|
|
2045
|
+
}
|
|
2046
|
+
return calldatas;
|
|
2047
|
+
};
|
|
2048
|
+
SwapRouter.encodeSwaps = function encodeSwaps(trades, options, isSwapAndAdd) {
|
|
2049
|
+
// If dealing with an instance of the aggregated Trade object, unbundle it to individual trade objects.
|
|
2050
|
+
if (trades instanceof Trade) {
|
|
2051
|
+
!trades.swaps.every(function (swap) {
|
|
2052
|
+
return swap.route.protocol === exports.Protocol.V3 || swap.route.protocol === exports.Protocol.V2 || swap.route.protocol === exports.Protocol.MIXED;
|
|
2053
|
+
}) ? invariant(false, 'UNSUPPORTED_PROTOCOL') : void 0;
|
|
2054
|
+
var individualTrades = [];
|
|
2055
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(trades.swaps), _step3; !(_step3 = _iterator3()).done;) {
|
|
2056
|
+
var _step3$value = _step3.value,
|
|
2057
|
+
route = _step3$value.route,
|
|
2058
|
+
inputAmount = _step3$value.inputAmount,
|
|
2059
|
+
outputAmount = _step3$value.outputAmount;
|
|
2060
|
+
if (route.protocol === exports.Protocol.V2) {
|
|
2061
|
+
individualTrades.push(new v2Sdk.Trade(route, trades.tradeType === sdkCore.TradeType.EXACT_INPUT ? inputAmount : outputAmount, trades.tradeType));
|
|
2062
|
+
} else if (route.protocol === exports.Protocol.V3) {
|
|
2063
|
+
individualTrades.push(v3Sdk.Trade.createUncheckedTrade({
|
|
2064
|
+
route: route,
|
|
2065
|
+
inputAmount: inputAmount,
|
|
2066
|
+
outputAmount: outputAmount,
|
|
2067
|
+
tradeType: trades.tradeType
|
|
2068
|
+
}));
|
|
2069
|
+
} else if (route.protocol === exports.Protocol.MIXED) {
|
|
2070
|
+
individualTrades.push(
|
|
2071
|
+
/// we can change the naming of this function on MixedRouteTrade if needed
|
|
2072
|
+
MixedRouteTrade.createUncheckedTrade({
|
|
2073
|
+
route: route,
|
|
2074
|
+
inputAmount: inputAmount,
|
|
2075
|
+
outputAmount: outputAmount,
|
|
2076
|
+
tradeType: trades.tradeType
|
|
2077
|
+
}));
|
|
2078
|
+
} else {
|
|
2079
|
+
throw new Error('UNSUPPORTED_TRADE_PROTOCOL');
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
trades = individualTrades;
|
|
2083
|
+
}
|
|
2084
|
+
if (!Array.isArray(trades)) {
|
|
2085
|
+
trades = [trades];
|
|
2086
|
+
}
|
|
2087
|
+
var numberOfTrades = trades.reduce(function (numberOfTrades, trade) {
|
|
2088
|
+
return numberOfTrades + (trade instanceof v3Sdk.Trade || trade instanceof MixedRouteTrade ? trade.swaps.length : 1);
|
|
2089
|
+
}, 0);
|
|
2090
|
+
var sampleTrade = trades[0];
|
|
2091
|
+
// All trades should have the same starting/ending currency and trade type
|
|
2092
|
+
!trades.every(function (trade) {
|
|
2093
|
+
return trade.inputAmount.currency.equals(sampleTrade.inputAmount.currency);
|
|
2094
|
+
}) ? invariant(false, 'TOKEN_IN_DIFF') : void 0;
|
|
2095
|
+
!trades.every(function (trade) {
|
|
2096
|
+
return trade.outputAmount.currency.equals(sampleTrade.outputAmount.currency);
|
|
2097
|
+
}) ? invariant(false, 'TOKEN_OUT_DIFF') : void 0;
|
|
2098
|
+
!trades.every(function (trade) {
|
|
2099
|
+
return trade.tradeType === sampleTrade.tradeType;
|
|
2100
|
+
}) ? invariant(false, 'TRADE_TYPE_DIFF') : void 0;
|
|
2101
|
+
var calldatas = [];
|
|
2102
|
+
var inputIsNative = sampleTrade.inputAmount.currency.isNative;
|
|
2103
|
+
var outputIsNative = sampleTrade.outputAmount.currency.isNative;
|
|
2104
|
+
// flag for whether we want to perform an aggregated slippage check
|
|
2105
|
+
// 1. when there are >2 exact input trades. this is only a heuristic,
|
|
2106
|
+
// as it's still more gas-expensive even in this case, but has benefits
|
|
2107
|
+
// in that the reversion probability is lower
|
|
2108
|
+
var performAggregatedSlippageCheck = sampleTrade.tradeType === sdkCore.TradeType.EXACT_INPUT && numberOfTrades > 2;
|
|
2109
|
+
// flag for whether funds should be send first to the router
|
|
2110
|
+
// 1. when receiving ETH (which much be unwrapped from WETH)
|
|
2111
|
+
// 2. when a fee on the output is being taken
|
|
2112
|
+
// 3. when performing swap and add
|
|
2113
|
+
// 4. when performing an aggregated slippage check
|
|
2114
|
+
var routerMustCustody = outputIsNative || !!options.fee || !!isSwapAndAdd || performAggregatedSlippageCheck;
|
|
2115
|
+
// encode permit if necessary
|
|
2116
|
+
if (options.inputTokenPermit) {
|
|
2117
|
+
!sampleTrade.inputAmount.currency.isToken ? invariant(false, 'NON_TOKEN_PERMIT') : void 0;
|
|
2118
|
+
calldatas.push(v3Sdk.SelfPermit.encodePermit(sampleTrade.inputAmount.currency, options.inputTokenPermit));
|
|
2119
|
+
}
|
|
2120
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(trades), _step4; !(_step4 = _iterator4()).done;) {
|
|
2121
|
+
var trade = _step4.value;
|
|
2122
|
+
if (trade instanceof v2Sdk.Trade) {
|
|
2123
|
+
calldatas.push(SwapRouter.encodeV2Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck));
|
|
2124
|
+
} else if (trade instanceof v3Sdk.Trade) {
|
|
2125
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(SwapRouter.encodeV3Swap(trade, options, routerMustCustody, performAggregatedSlippageCheck)), _step5; !(_step5 = _iterator5()).done;) {
|
|
2126
|
+
var calldata = _step5.value;
|
|
2127
|
+
calldatas.push(calldata);
|
|
2128
|
+
}
|
|
2129
|
+
} else if (trade instanceof MixedRouteTrade) {
|
|
2130
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(SwapRouter.encodeMixedRouteSwap(trade, options, routerMustCustody, performAggregatedSlippageCheck)), _step6; !(_step6 = _iterator6()).done;) {
|
|
2131
|
+
var _calldata = _step6.value;
|
|
2132
|
+
calldatas.push(_calldata);
|
|
2133
|
+
}
|
|
2134
|
+
} else {
|
|
2135
|
+
throw new Error('Unsupported trade object');
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
var ZERO_IN = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.inputAmount.currency, 0);
|
|
2139
|
+
var ZERO_OUT = sdkCore.CurrencyAmount.fromRawAmount(sampleTrade.outputAmount.currency, 0);
|
|
2140
|
+
var minimumAmountOut = trades.reduce(function (sum, trade) {
|
|
2141
|
+
return sum.add(trade.minimumAmountOut(options.slippageTolerance));
|
|
2142
|
+
}, ZERO_OUT);
|
|
2143
|
+
var quoteAmountOut = trades.reduce(function (sum, trade) {
|
|
2144
|
+
return sum.add(trade.outputAmount);
|
|
2145
|
+
}, ZERO_OUT);
|
|
2146
|
+
var totalAmountIn = trades.reduce(function (sum, trade) {
|
|
2147
|
+
return sum.add(trade.maximumAmountIn(options.slippageTolerance));
|
|
2148
|
+
}, ZERO_IN);
|
|
2149
|
+
return {
|
|
2150
|
+
calldatas: calldatas,
|
|
2151
|
+
sampleTrade: sampleTrade,
|
|
2152
|
+
routerMustCustody: routerMustCustody,
|
|
2153
|
+
inputIsNative: inputIsNative,
|
|
2154
|
+
outputIsNative: outputIsNative,
|
|
2155
|
+
totalAmountIn: totalAmountIn,
|
|
2156
|
+
minimumAmountOut: minimumAmountOut,
|
|
2157
|
+
quoteAmountOut: quoteAmountOut
|
|
2158
|
+
};
|
|
2159
|
+
}
|
|
2160
|
+
/**
|
|
2161
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
2162
|
+
* @param trades to produce call parameters for
|
|
2163
|
+
* @param options options for the call parameters
|
|
2164
|
+
*/;
|
|
2165
|
+
SwapRouter.swapCallParameters = function swapCallParameters(trades, options) {
|
|
2166
|
+
var _SwapRouter$encodeSwa = SwapRouter.encodeSwaps(trades, options),
|
|
2167
|
+
calldatas = _SwapRouter$encodeSwa.calldatas,
|
|
2168
|
+
sampleTrade = _SwapRouter$encodeSwa.sampleTrade,
|
|
2169
|
+
routerMustCustody = _SwapRouter$encodeSwa.routerMustCustody,
|
|
2170
|
+
inputIsNative = _SwapRouter$encodeSwa.inputIsNative,
|
|
2171
|
+
outputIsNative = _SwapRouter$encodeSwa.outputIsNative,
|
|
2172
|
+
totalAmountIn = _SwapRouter$encodeSwa.totalAmountIn,
|
|
2173
|
+
minimumAmountOut = _SwapRouter$encodeSwa.minimumAmountOut;
|
|
2174
|
+
// unwrap or sweep
|
|
2175
|
+
if (routerMustCustody) {
|
|
2176
|
+
if (outputIsNative) {
|
|
2177
|
+
calldatas.push(PaymentsExtended.encodeUnwrapWETH9(minimumAmountOut.quotient, options.recipient, options.fee));
|
|
2178
|
+
} else {
|
|
2179
|
+
calldatas.push(PaymentsExtended.encodeSweepToken(sampleTrade.outputAmount.currency.wrapped, minimumAmountOut.quotient, options.recipient, options.fee));
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
// must refund when paying in ETH: either with an uncertain input amount OR if there's a chance of a partial fill.
|
|
2183
|
+
// unlike ERC20's, the full ETH value must be sent in the transaction, so the rest must be refunded.
|
|
2184
|
+
if (inputIsNative && (sampleTrade.tradeType === sdkCore.TradeType.EXACT_OUTPUT || SwapRouter.riskOfPartialFill(trades))) {
|
|
2185
|
+
calldatas.push(v3Sdk.Payments.encodeRefundETH());
|
|
2186
|
+
}
|
|
2187
|
+
return {
|
|
2188
|
+
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
2189
|
+
value: v3Sdk.toHex(inputIsNative ? totalAmountIn.quotient : ZERO$1)
|
|
2190
|
+
};
|
|
2191
|
+
}
|
|
2192
|
+
/**
|
|
2193
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
2194
|
+
* @param trades to produce call parameters for
|
|
2195
|
+
* @param options options for the call parameters
|
|
2196
|
+
*/;
|
|
2197
|
+
SwapRouter.swapAndAddCallParameters = function swapAndAddCallParameters(trades, options, position, addLiquidityOptions, tokenInApprovalType, tokenOutApprovalType) {
|
|
2198
|
+
var _SwapRouter$encodeSwa2 = SwapRouter.encodeSwaps(trades, options, true),
|
|
2199
|
+
calldatas = _SwapRouter$encodeSwa2.calldatas,
|
|
2200
|
+
inputIsNative = _SwapRouter$encodeSwa2.inputIsNative,
|
|
2201
|
+
outputIsNative = _SwapRouter$encodeSwa2.outputIsNative,
|
|
2202
|
+
sampleTrade = _SwapRouter$encodeSwa2.sampleTrade,
|
|
2203
|
+
totalAmountSwapped = _SwapRouter$encodeSwa2.totalAmountIn,
|
|
2204
|
+
quoteAmountOut = _SwapRouter$encodeSwa2.quoteAmountOut,
|
|
2205
|
+
minimumAmountOut = _SwapRouter$encodeSwa2.minimumAmountOut;
|
|
2206
|
+
// encode output token permit if necessary
|
|
2207
|
+
if (options.outputTokenPermit) {
|
|
2208
|
+
!quoteAmountOut.currency.isToken ? invariant(false, 'NON_TOKEN_PERMIT_OUTPUT') : void 0;
|
|
2209
|
+
calldatas.push(v3Sdk.SelfPermit.encodePermit(quoteAmountOut.currency, options.outputTokenPermit));
|
|
2210
|
+
}
|
|
2211
|
+
var chainId = sampleTrade.route.chainId;
|
|
2212
|
+
var zeroForOne = position.pool.token0.wrapped.address === totalAmountSwapped.currency.wrapped.address;
|
|
2213
|
+
var _SwapRouter$getPositi = SwapRouter.getPositionAmounts(position, zeroForOne),
|
|
2214
|
+
positionAmountIn = _SwapRouter$getPositi.positionAmountIn,
|
|
2215
|
+
positionAmountOut = _SwapRouter$getPositi.positionAmountOut;
|
|
2216
|
+
// if tokens are native they will be converted to WETH9
|
|
2217
|
+
var tokenIn = inputIsNative ? sdkCore.WETH9[chainId] : positionAmountIn.currency.wrapped;
|
|
2218
|
+
var tokenOut = outputIsNative ? sdkCore.WETH9[chainId] : positionAmountOut.currency.wrapped;
|
|
2219
|
+
// if swap output does not make up whole outputTokenBalanceDesired, pull in remaining tokens for adding liquidity
|
|
2220
|
+
var amountOutRemaining = positionAmountOut.subtract(quoteAmountOut.wrapped);
|
|
2221
|
+
if (amountOutRemaining.greaterThan(sdkCore.CurrencyAmount.fromRawAmount(positionAmountOut.currency, 0))) {
|
|
2222
|
+
// if output is native, this means the remaining portion is included as native value in the transaction
|
|
2223
|
+
// and must be wrapped. Otherwise, pull in remaining ERC20 token.
|
|
2224
|
+
outputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(amountOutRemaining.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenOut, amountOutRemaining.quotient));
|
|
2225
|
+
}
|
|
2226
|
+
// if input is native, convert to WETH9, else pull ERC20 token
|
|
2227
|
+
inputIsNative ? calldatas.push(PaymentsExtended.encodeWrapETH(positionAmountIn.quotient)) : calldatas.push(PaymentsExtended.encodePull(tokenIn, positionAmountIn.quotient));
|
|
2228
|
+
// approve token balances to NFTManager
|
|
2229
|
+
if (tokenInApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenIn, tokenInApprovalType));
|
|
2230
|
+
if (tokenOutApprovalType !== exports.ApprovalTypes.NOT_REQUIRED) calldatas.push(ApproveAndCall.encodeApprove(tokenOut, tokenOutApprovalType));
|
|
2231
|
+
// represents a position with token amounts resulting from a swap with maximum slippage
|
|
2232
|
+
// hence the minimal amount out possible.
|
|
2233
|
+
var minimalPosition = v3Sdk.Position.fromAmounts({
|
|
2234
|
+
pool: position.pool,
|
|
2235
|
+
tickLower: position.tickLower,
|
|
2236
|
+
tickUpper: position.tickUpper,
|
|
2237
|
+
amount0: zeroForOne ? position.amount0.quotient.toString() : minimumAmountOut.quotient.toString(),
|
|
2238
|
+
amount1: zeroForOne ? minimumAmountOut.quotient.toString() : position.amount1.quotient.toString(),
|
|
2239
|
+
useFullPrecision: false
|
|
2240
|
+
});
|
|
2241
|
+
// encode NFTManager add liquidity
|
|
2242
|
+
calldatas.push(ApproveAndCall.encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, options.slippageTolerance));
|
|
2243
|
+
// sweep remaining tokens
|
|
2244
|
+
inputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenIn, ZERO$1));
|
|
2245
|
+
outputIsNative ? calldatas.push(PaymentsExtended.encodeUnwrapWETH9(ZERO$1)) : calldatas.push(PaymentsExtended.encodeSweepToken(tokenOut, ZERO$1));
|
|
2246
|
+
var value;
|
|
2247
|
+
if (inputIsNative) {
|
|
2248
|
+
value = totalAmountSwapped.wrapped.add(positionAmountIn.wrapped).quotient;
|
|
2249
|
+
} else if (outputIsNative) {
|
|
2250
|
+
value = amountOutRemaining.quotient;
|
|
2251
|
+
} else {
|
|
2252
|
+
value = ZERO$1;
|
|
2253
|
+
}
|
|
2254
|
+
return {
|
|
2255
|
+
calldata: MulticallExtended.encodeMulticall(calldatas, options.deadlineOrPreviousBlockhash),
|
|
2256
|
+
value: value.toString()
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
// if price impact is very high, there's a chance of hitting max/min prices resulting in a partial fill of the swap
|
|
2260
|
+
;
|
|
2261
|
+
SwapRouter.riskOfPartialFill = function riskOfPartialFill(trades) {
|
|
2262
|
+
if (Array.isArray(trades)) {
|
|
2263
|
+
return trades.some(function (trade) {
|
|
2264
|
+
return SwapRouter.v3TradeWithHighPriceImpact(trade);
|
|
2265
|
+
});
|
|
2266
|
+
} else {
|
|
2267
|
+
return SwapRouter.v3TradeWithHighPriceImpact(trades);
|
|
2268
|
+
}
|
|
2269
|
+
};
|
|
2270
|
+
SwapRouter.v3TradeWithHighPriceImpact = function v3TradeWithHighPriceImpact(trade) {
|
|
2271
|
+
return !(trade instanceof v2Sdk.Trade) && trade.priceImpact.greaterThan(REFUND_ETH_PRICE_IMPACT_THRESHOLD);
|
|
2272
|
+
};
|
|
2273
|
+
SwapRouter.getPositionAmounts = function getPositionAmounts(position, zeroForOne) {
|
|
2274
|
+
var _position$mintAmounts = position.mintAmounts,
|
|
2275
|
+
amount0 = _position$mintAmounts.amount0,
|
|
2276
|
+
amount1 = _position$mintAmounts.amount1;
|
|
2277
|
+
var currencyAmount0 = sdkCore.CurrencyAmount.fromRawAmount(position.pool.token0, amount0);
|
|
2278
|
+
var currencyAmount1 = sdkCore.CurrencyAmount.fromRawAmount(position.pool.token1, amount1);
|
|
2279
|
+
var _ref = zeroForOne ? [currencyAmount0, currencyAmount1] : [currencyAmount1, currencyAmount0],
|
|
2280
|
+
positionAmountIn = _ref[0],
|
|
2281
|
+
positionAmountOut = _ref[1];
|
|
2282
|
+
return {
|
|
2283
|
+
positionAmountIn: positionAmountIn,
|
|
2284
|
+
positionAmountOut: positionAmountOut
|
|
2285
|
+
};
|
|
2286
|
+
};
|
|
2287
|
+
return SwapRouter;
|
|
2288
|
+
}();
|
|
2289
|
+
SwapRouter.INTERFACE = /*#__PURE__*/new abi.Interface(ISwapRouter02_json.abi);
|
|
2290
|
+
|
|
2291
|
+
exports.ADDRESS_THIS = ADDRESS_THIS;
|
|
2292
|
+
exports.ADDRESS_ZERO = ADDRESS_ZERO;
|
|
2293
|
+
exports.ApproveAndCall = ApproveAndCall;
|
|
2294
|
+
exports.MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER = MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER;
|
|
2295
|
+
exports.MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER = MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER;
|
|
2296
|
+
exports.MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER = MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER;
|
|
2297
|
+
exports.MSG_SENDER = MSG_SENDER;
|
|
2298
|
+
exports.MixedRoute = MixedRoute;
|
|
2299
|
+
exports.MixedRouteSDK = MixedRouteSDK;
|
|
2300
|
+
exports.MixedRouteTrade = MixedRouteTrade;
|
|
2301
|
+
exports.MulticallExtended = MulticallExtended;
|
|
2302
|
+
exports.ONE = ONE;
|
|
2303
|
+
exports.ONE_HUNDRED_PERCENT = ONE_HUNDRED_PERCENT;
|
|
2304
|
+
exports.PaymentsExtended = PaymentsExtended;
|
|
2305
|
+
exports.RouteV2 = RouteV2;
|
|
2306
|
+
exports.RouteV3 = RouteV3;
|
|
2307
|
+
exports.SwapRouter = SwapRouter;
|
|
2308
|
+
exports.Trade = Trade;
|
|
2309
|
+
exports.ZERO = ZERO;
|
|
2310
|
+
exports.ZERO_PERCENT = ZERO_PERCENT;
|
|
2311
|
+
exports.encodeMixedRouteToPath = encodeMixedRouteToPath;
|
|
2312
|
+
exports.getOutputOfPools = getOutputOfPools;
|
|
2313
|
+
exports.isMint = isMint;
|
|
2314
|
+
exports.partitionMixedRouteByProtocol = partitionMixedRouteByProtocol;
|
|
2315
|
+
exports.tradeComparator = tradeComparator;
|
|
2316
|
+
//# sourceMappingURL=router-sdk.cjs.development.js.map
|