@uniswap/universal-router-sdk 2.0.2 → 2.0.4-beta.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.
@@ -3,13 +3,44 @@ import { abi as abi$7 } from '@uniswap/universal-router/artifacts/contracts/Univ
3
3
  import { Interface } from '@ethersproject/abi';
4
4
  import { BigNumber, ethers } from 'ethers';
5
5
  import { defaultAbiCoder } from 'ethers/lib/utils';
6
- import { Trade, Pair } from '@uniswap/v2-sdk';
7
- import { toHex, Trade as Trade$1, encodeRouteToPath, Pool } from '@uniswap/v3-sdk';
8
- import { Protocol, MixedRouteTrade, partitionMixedRouteByProtocol, getOutputOfPools, MixedRouteSDK, MixedRoute, encodeMixedRouteToPath } from '@uniswap/router-sdk';
9
- import { TradeType, Percent } from '@uniswap/sdk-core';
6
+ import { Trade, Pair, Route as Route$1 } from '@uniswap/v2-sdk';
7
+ import { toHex, Trade as Trade$1, encodeRouteToPath, Pool, Route } from '@uniswap/v3-sdk';
8
+ import { Protocol, MixedRouteTrade, partitionMixedRouteByProtocol, getOutputOfPools, MixedRouteSDK, MixedRoute, encodeMixedRouteToPath, Trade as Trade$2 } from '@uniswap/router-sdk';
9
+ import { TradeType, Percent, CurrencyAmount, Ether, Token } from '@uniswap/sdk-core';
10
10
  import 'jsbi';
11
11
  import 'bignumber.js';
12
12
 
13
+ function _toPrimitive(t, r) {
14
+ if ("object" != typeof t || !t) return t;
15
+ var e = t[Symbol.toPrimitive];
16
+ if (void 0 !== e) {
17
+ var i = e.call(t, r || "default");
18
+ if ("object" != typeof i) return i;
19
+ throw new TypeError("@@toPrimitive must return a primitive value.");
20
+ }
21
+ return ("string" === r ? String : Number)(t);
22
+ }
23
+ function _toPropertyKey(t) {
24
+ var i = _toPrimitive(t, "string");
25
+ return "symbol" == typeof i ? i : String(i);
26
+ }
27
+ function _defineProperties(target, props) {
28
+ for (var i = 0; i < props.length; i++) {
29
+ var descriptor = props[i];
30
+ descriptor.enumerable = descriptor.enumerable || false;
31
+ descriptor.configurable = true;
32
+ if ("value" in descriptor) descriptor.writable = true;
33
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
34
+ }
35
+ }
36
+ function _createClass(Constructor, protoProps, staticProps) {
37
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
38
+ if (staticProps) _defineProperties(Constructor, staticProps);
39
+ Object.defineProperty(Constructor, "prototype", {
40
+ writable: false
41
+ });
42
+ return Constructor;
43
+ }
13
44
  function _extends() {
14
45
  _extends = Object.assign ? Object.assign.bind() : function (target) {
15
46
  for (var i = 1; i < arguments.length; i++) {
@@ -73,8 +104,6 @@ var RouterTradeType;
73
104
  RouterTradeType["UniswapTrade"] = "UniswapTrade";
74
105
  RouterTradeType["NFTTrade"] = "NFTTrade";
75
106
  RouterTradeType["UnwrapWETH"] = "UnwrapWETH";
76
- RouterTradeType["WrapSTETH"] = "WrapSTETH";
77
- RouterTradeType["UnwrapSTETH"] = "UnwrapSTETH";
78
107
  })(RouterTradeType || (RouterTradeType = {}));
79
108
 
80
109
  var NFTTrade = function NFTTrade(market, orders) {
@@ -143,8 +172,6 @@ var CommandType;
143
172
  CommandType[CommandType["SEAPORT_V1_4"] = 32] = "SEAPORT_V1_4";
144
173
  CommandType[CommandType["EXECUTE_SUB_PLAN"] = 33] = "EXECUTE_SUB_PLAN";
145
174
  CommandType[CommandType["APPROVE_ERC20"] = 34] = "APPROVE_ERC20";
146
- CommandType[CommandType["WRAP_STETH"] = 35] = "WRAP_STETH";
147
- CommandType[CommandType["UNWRAP_STETH"] = 36] = "UNWRAP_STETH";
148
175
  })(CommandType || (CommandType = {}));
149
176
  var ALLOW_REVERT_FLAG = 0x80;
150
177
  var REVERTIBLE_COMMANDS = /*#__PURE__*/new Set([CommandType.SEAPORT_V1_5, CommandType.SEAPORT_V1_4, CommandType.NFTX, CommandType.LOOKS_RARE_V2, CommandType.X2Y2_721, CommandType.X2Y2_1155, CommandType.FOUNDATION, CommandType.SUDOSWAP, CommandType.NFT20, CommandType.EXECUTE_SUB_PLAN, CommandType.CRYPTOPUNKS, CommandType.ELEMENT_MARKET]);
@@ -152,7 +179,7 @@ var PERMIT_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 non
152
179
  var PERMIT_BATCH_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 nonce)[] details,address spender,uint256 sigDeadline)';
153
180
  var PERMIT2_TRANSFER_FROM_STRUCT = '(address from,address to,uint160 amount,address token)';
154
181
  var PERMIT2_TRANSFER_FROM_BATCH_STRUCT = PERMIT2_TRANSFER_FROM_STRUCT + '[]';
155
- var ABI_DEFINITION = (_ABI_DEFINITION = {}, _ABI_DEFINITION[CommandType.EXECUTE_SUB_PLAN] = ['bytes', 'bytes[]'], _ABI_DEFINITION[CommandType.PERMIT2_PERMIT] = [PERMIT_STRUCT, 'bytes'], _ABI_DEFINITION[CommandType.PERMIT2_PERMIT_BATCH] = [PERMIT_BATCH_STRUCT, 'bytes'], _ABI_DEFINITION[CommandType.PERMIT2_TRANSFER_FROM] = ['address', 'address', 'uint160'], _ABI_DEFINITION[CommandType.PERMIT2_TRANSFER_FROM_BATCH] = [PERMIT2_TRANSFER_FROM_BATCH_STRUCT], _ABI_DEFINITION[CommandType.V3_SWAP_EXACT_IN] = ['address', 'uint256', 'uint256', 'bytes', 'bool'], _ABI_DEFINITION[CommandType.V3_SWAP_EXACT_OUT] = ['address', 'uint256', 'uint256', 'bytes', 'bool'], _ABI_DEFINITION[CommandType.V2_SWAP_EXACT_IN] = ['address', 'uint256', 'uint256', 'address[]', 'bool'], _ABI_DEFINITION[CommandType.V2_SWAP_EXACT_OUT] = ['address', 'uint256', 'uint256', 'address[]', 'bool'], _ABI_DEFINITION[CommandType.WRAP_ETH] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.UNWRAP_WETH] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.SWEEP] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.SWEEP_ERC721] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.SWEEP_ERC1155] = ['address', 'address', 'uint256', 'uint256'], _ABI_DEFINITION[CommandType.TRANSFER] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.PAY_PORTION] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.BALANCE_CHECK_ERC20] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.OWNER_CHECK_721] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.OWNER_CHECK_1155] = ['address', 'address', 'uint256', 'uint256'], _ABI_DEFINITION[CommandType.APPROVE_ERC20] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.WRAP_STETH] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.UNWRAP_STETH] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.SEAPORT_V1_5] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.SEAPORT_V1_4] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.NFTX] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.LOOKS_RARE_V2] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.X2Y2_721] = ['uint256', 'bytes', 'address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.X2Y2_1155] = ['uint256', 'bytes', 'address', 'address', 'uint256', 'uint256'], _ABI_DEFINITION[CommandType.FOUNDATION] = ['uint256', 'bytes', 'address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.SUDOSWAP] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.NFT20] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.CRYPTOPUNKS] = ['uint256', 'address', 'uint256'], _ABI_DEFINITION[CommandType.ELEMENT_MARKET] = ['uint256', 'bytes'], _ABI_DEFINITION);
182
+ var ABI_DEFINITION = (_ABI_DEFINITION = {}, _ABI_DEFINITION[CommandType.EXECUTE_SUB_PLAN] = ['bytes', 'bytes[]'], _ABI_DEFINITION[CommandType.PERMIT2_PERMIT] = [PERMIT_STRUCT, 'bytes'], _ABI_DEFINITION[CommandType.PERMIT2_PERMIT_BATCH] = [PERMIT_BATCH_STRUCT, 'bytes'], _ABI_DEFINITION[CommandType.PERMIT2_TRANSFER_FROM] = ['address', 'address', 'uint160'], _ABI_DEFINITION[CommandType.PERMIT2_TRANSFER_FROM_BATCH] = [PERMIT2_TRANSFER_FROM_BATCH_STRUCT], _ABI_DEFINITION[CommandType.V3_SWAP_EXACT_IN] = ['address', 'uint256', 'uint256', 'bytes', 'bool'], _ABI_DEFINITION[CommandType.V3_SWAP_EXACT_OUT] = ['address', 'uint256', 'uint256', 'bytes', 'bool'], _ABI_DEFINITION[CommandType.V2_SWAP_EXACT_IN] = ['address', 'uint256', 'uint256', 'address[]', 'bool'], _ABI_DEFINITION[CommandType.V2_SWAP_EXACT_OUT] = ['address', 'uint256', 'uint256', 'address[]', 'bool'], _ABI_DEFINITION[CommandType.WRAP_ETH] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.UNWRAP_WETH] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.SWEEP] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.SWEEP_ERC721] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.SWEEP_ERC1155] = ['address', 'address', 'uint256', 'uint256'], _ABI_DEFINITION[CommandType.TRANSFER] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.PAY_PORTION] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.BALANCE_CHECK_ERC20] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.OWNER_CHECK_721] = ['address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.OWNER_CHECK_1155] = ['address', 'address', 'uint256', 'uint256'], _ABI_DEFINITION[CommandType.APPROVE_ERC20] = ['address', 'uint256'], _ABI_DEFINITION[CommandType.SEAPORT_V1_5] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.SEAPORT_V1_4] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.NFTX] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.LOOKS_RARE_V2] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.X2Y2_721] = ['uint256', 'bytes', 'address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.X2Y2_1155] = ['uint256', 'bytes', 'address', 'address', 'uint256', 'uint256'], _ABI_DEFINITION[CommandType.FOUNDATION] = ['uint256', 'bytes', 'address', 'address', 'uint256'], _ABI_DEFINITION[CommandType.SUDOSWAP] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.NFT20] = ['uint256', 'bytes'], _ABI_DEFINITION[CommandType.CRYPTOPUNKS] = ['uint256', 'address', 'uint256'], _ABI_DEFINITION[CommandType.ELEMENT_MARKET] = ['uint256', 'bytes'], _ABI_DEFINITION);
156
183
  var RoutePlanner = /*#__PURE__*/function () {
157
184
  function RoutePlanner() {
158
185
  this.commands = '0x';
@@ -187,97 +214,71 @@ function createCommand(type, parameters) {
187
214
  }
188
215
 
189
216
  var _CHAIN_CONFIGS;
190
- var NOT_SUPPORTED_ON_CHAIN = '0x0000000000000000000000000000000000000000';
217
+ var WETH_NOT_SUPPORTED_ON_CHAIN = '0x0000000000000000000000000000000000000000';
191
218
  var CHAIN_CONFIGS = (_CHAIN_CONFIGS = {}, _CHAIN_CONFIGS[1] = {
192
219
  router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
193
220
  weth: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
194
- steth: '0xae7ab96520de3a18e5e111b5eaab095312d7fe84',
195
- wsteth: '0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
196
221
  creationBlock: 17143817
197
222
  }, _CHAIN_CONFIGS[5] = {
198
223
  router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
199
224
  weth: '0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6',
200
- steth: '0x1643E812aE58766192Cf7D2Cf9567dF2C37e9B7F',
201
- wsteth: '0x6320cD32aA674d2898A68ec82e869385Fc5f7E2f',
202
225
  creationBlock: 8940568
203
226
  }, _CHAIN_CONFIGS[11155111] = {
204
227
  router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
205
228
  weth: '0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14',
206
- steth: NOT_SUPPORTED_ON_CHAIN,
207
- wsteth: NOT_SUPPORTED_ON_CHAIN,
208
229
  creationBlock: 3543575
209
230
  }, _CHAIN_CONFIGS[137] = {
210
- router: '0x643770E279d5D0733F21d6DC03A8efbABf3255B4',
231
+ router: '0xec7BE89e9d109e7e3Fec59c222CF297125FEFda2',
211
232
  weth: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
212
- steth: NOT_SUPPORTED_ON_CHAIN,
213
- wsteth: NOT_SUPPORTED_ON_CHAIN,
214
- creationBlock: 46866777
233
+ creationBlock: 52210153
215
234
  }, _CHAIN_CONFIGS[80001] = {
216
235
  router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
217
236
  weth: '0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889',
218
- steth: NOT_SUPPORTED_ON_CHAIN,
219
- wsteth: NOT_SUPPORTED_ON_CHAIN,
220
237
  creationBlock: 35176052
221
238
  }, _CHAIN_CONFIGS[10] = {
222
- router: '0xeC8B0F7Ffe3ae75d7FfAb09429e3675bb63503e4',
239
+ router: '0xCb1355ff08Ab38bBCE60111F1bb2B784bE25D7e8',
223
240
  weth: '0x4200000000000000000000000000000000000006',
224
- steth: NOT_SUPPORTED_ON_CHAIN,
225
- wsteth: NOT_SUPPORTED_ON_CHAIN,
226
- creationBlock: 108825869
241
+ creationBlock: 114702266
227
242
  }, _CHAIN_CONFIGS[420] = {
228
243
  router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
229
244
  weth: '0x4200000000000000000000000000000000000006',
230
- steth: NOT_SUPPORTED_ON_CHAIN,
231
- wsteth: NOT_SUPPORTED_ON_CHAIN,
232
245
  creationBlock: 8887728
233
246
  }, _CHAIN_CONFIGS[42161] = {
234
- router: '0xeC8B0F7Ffe3ae75d7FfAb09429e3675bb63503e4',
247
+ router: '0x5E325eDA8064b456f4781070C0738d849c824258',
235
248
  weth: '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1',
236
- steth: NOT_SUPPORTED_ON_CHAIN,
237
- wsteth: NOT_SUPPORTED_ON_CHAIN,
238
- creationBlock: 125861718
249
+ creationBlock: 169472836
239
250
  }, _CHAIN_CONFIGS[421613] = {
240
251
  router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
241
252
  weth: '0xe39Ab88f8A4777030A534146A9Ca3B52bd5D43A3',
242
- steth: NOT_SUPPORTED_ON_CHAIN,
243
- wsteth: NOT_SUPPORTED_ON_CHAIN,
244
253
  creationBlock: 18815277
245
254
  }, _CHAIN_CONFIGS[42220] = {
246
- router: '0x88a3ED7F21A3fCF6adb86b6F878C5B7a02D20e9b',
247
- weth: NOT_SUPPORTED_ON_CHAIN,
248
- steth: NOT_SUPPORTED_ON_CHAIN,
249
- wsteth: NOT_SUPPORTED_ON_CHAIN,
250
- creationBlock: 21116361
255
+ router: '0x643770e279d5d0733f21d6dc03a8efbabf3255b4',
256
+ weth: WETH_NOT_SUPPORTED_ON_CHAIN,
257
+ creationBlock: 21407637
251
258
  }, _CHAIN_CONFIGS[44787] = {
252
259
  router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
253
- weth: NOT_SUPPORTED_ON_CHAIN,
254
- steth: NOT_SUPPORTED_ON_CHAIN,
255
- wsteth: NOT_SUPPORTED_ON_CHAIN,
260
+ weth: WETH_NOT_SUPPORTED_ON_CHAIN,
256
261
  creationBlock: 17566658
257
262
  }, _CHAIN_CONFIGS[56] = {
258
- router: '0xeC8B0F7Ffe3ae75d7FfAb09429e3675bb63503e4',
263
+ router: '0x4Dae2f939ACf50408e13d58534Ff8c2776d45265',
259
264
  weth: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
260
- steth: NOT_SUPPORTED_ON_CHAIN,
261
- wsteth: NOT_SUPPORTED_ON_CHAIN,
262
- creationBlock: 31254967
265
+ creationBlock: 35160263
263
266
  }, _CHAIN_CONFIGS[43114] = {
264
- router: '0x82635AF6146972cD6601161c4472ffe97237D292',
267
+ router: '0x4Dae2f939ACf50408e13d58534Ff8c2776d45265',
265
268
  weth: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7',
266
- steth: NOT_SUPPORTED_ON_CHAIN,
267
- wsteth: NOT_SUPPORTED_ON_CHAIN,
268
- creationBlock: 34491144
269
+ creationBlock: 40237257
269
270
  }, _CHAIN_CONFIGS[84531] = {
270
271
  router: '0xd0872d928672ae2ff74bdb2f5130ac12229cafaf',
271
272
  weth: '0x4200000000000000000000000000000000000006',
272
- steth: NOT_SUPPORTED_ON_CHAIN,
273
- wsteth: NOT_SUPPORTED_ON_CHAIN,
274
273
  creationBlock: 6915289
275
274
  }, _CHAIN_CONFIGS[8453] = {
276
- router: '0xeC8B0F7Ffe3ae75d7FfAb09429e3675bb63503e4',
275
+ router: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
277
276
  weth: '0x4200000000000000000000000000000000000006',
278
- steth: NOT_SUPPORTED_ON_CHAIN,
279
- wsteth: NOT_SUPPORTED_ON_CHAIN,
280
- creationBlock: 3229053
277
+ creationBlock: 9107268
278
+ }, _CHAIN_CONFIGS[81457] = {
279
+ router: '0x643770E279d5D0733F21d6DC03A8efbABf3255B4',
280
+ weth: '0x4300000000000000000000000000000000000004',
281
+ creationBlock: 1116444
281
282
  }, _CHAIN_CONFIGS);
282
283
  var UNIVERSAL_ROUTER_ADDRESS = function UNIVERSAL_ROUTER_ADDRESS(chainId) {
283
284
  if (!(chainId in CHAIN_CONFIGS)) throw new Error("Universal Router not deployed on chain " + chainId);
@@ -289,17 +290,13 @@ var UNIVERSAL_ROUTER_CREATION_BLOCK = function UNIVERSAL_ROUTER_CREATION_BLOCK(c
289
290
  };
290
291
  var WETH_ADDRESS = function WETH_ADDRESS(chainId) {
291
292
  if (!(chainId in CHAIN_CONFIGS)) throw new Error("Universal Router not deployed on chain " + chainId);
292
- if (CHAIN_CONFIGS[chainId].weth == NOT_SUPPORTED_ON_CHAIN) throw new Error("Chain " + chainId + " does not have WETH");
293
+ if (CHAIN_CONFIGS[chainId].weth == WETH_NOT_SUPPORTED_ON_CHAIN) throw new Error("Chain " + chainId + " does not have WETH");
293
294
  return CHAIN_CONFIGS[chainId].weth;
294
295
  };
295
- var STETH_ADDRESS = function STETH_ADDRESS(chainId) {
296
- if (!(chainId in CHAIN_CONFIGS)) throw new Error("Universal Router not deployed on chain " + chainId);
297
- if (CHAIN_CONFIGS[chainId].steth == NOT_SUPPORTED_ON_CHAIN) throw new Error("Chain " + chainId + " does not have STETH support");
298
- return CHAIN_CONFIGS[chainId].steth;
299
- };
300
296
  var PERMIT2_ADDRESS = '0x000000000022D473030F116dDEE9F6B43aC78BA3';
301
297
  var CONTRACT_BALANCE = /*#__PURE__*/BigNumber.from(2).pow(255);
302
298
  var ETH_ADDRESS = '0x0000000000000000000000000000000000000000';
299
+ var E_ETH_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
303
300
  var ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
304
301
  var SENDER_AS_RECIPIENT = '0x0000000000000000000000000000000000000001';
305
302
  var ROUTER_AS_RECIPIENT = '0x0000000000000000000000000000000000000002';
@@ -319,17 +316,15 @@ var UniswapTrade = /*#__PURE__*/function () {
319
316
  this.options = options;
320
317
  this.tradeType = RouterTradeType.UniswapTrade;
321
318
  if (!!options.fee && !!options.flatFee) throw new Error('Only one fee option permitted');
319
+ if (this.inputRequiresWrap) this.payerIsUser = false;else if (this.options.useRouterBalance) this.payerIsUser = false;else this.payerIsUser = true;
322
320
  }
323
321
  var _proto = UniswapTrade.prototype;
324
322
  _proto.encode = function encode(planner, _config) {
325
323
  var _this$options$recipie;
326
- var payerIsUser = !this.options.payerIsRouter;
327
324
  // If the input currency is the native currency, we need to wrap it with the router as the recipient
328
- if (this.trade.inputAmount.currency.isNative) {
325
+ if (this.inputRequiresWrap) {
329
326
  // TODO: optimize if only one v2 pool we can directly send this to the pool
330
327
  planner.addCommand(CommandType.WRAP_ETH, [ROUTER_AS_RECIPIENT, this.trade.maximumAmountIn(this.options.slippageTolerance).quotient.toString()]);
331
- // since WETH is now owned by the router, the router pays for inputs
332
- payerIsUser = false;
333
328
  }
334
329
  // The overall recipient at the end of the trade, SENDER_AS_RECIPIENT uses the msg.sender
335
330
  this.options.recipient = (_this$options$recipie = this.options.recipient) != null ? _this$options$recipie : SENDER_AS_RECIPIENT;
@@ -339,19 +334,18 @@ var UniswapTrade = /*#__PURE__*/function () {
339
334
  // in that the reversion probability is lower
340
335
  var performAggregatedSlippageCheck = this.trade.tradeType === TradeType.EXACT_INPUT && this.trade.routes.length > 2;
341
336
  var outputIsNative = this.trade.outputAmount.currency.isNative;
342
- var inputIsNative = this.trade.inputAmount.currency.isNative;
343
337
  var routerMustCustody = performAggregatedSlippageCheck || outputIsNative || hasFeeOption(this.options);
344
338
  for (var _iterator = _createForOfIteratorHelperLoose(this.trade.swaps), _step; !(_step = _iterator()).done;) {
345
339
  var swap = _step.value;
346
340
  switch (swap.route.protocol) {
347
341
  case Protocol.V2:
348
- addV2Swap(planner, swap, this.trade.tradeType, this.options, payerIsUser, routerMustCustody);
342
+ addV2Swap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody);
349
343
  break;
350
344
  case Protocol.V3:
351
- addV3Swap(planner, swap, this.trade.tradeType, this.options, payerIsUser, routerMustCustody);
345
+ addV3Swap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody);
352
346
  break;
353
347
  case Protocol.MIXED:
354
- addMixedSwap(planner, swap, this.trade.tradeType, this.options, payerIsUser, routerMustCustody);
348
+ addMixedSwap(planner, swap, this.trade.tradeType, this.options, this.payerIsUser, routerMustCustody);
355
349
  break;
356
350
  default:
357
351
  throw new Error('UNSUPPORTED_TRADE_PROTOCOL');
@@ -391,12 +385,19 @@ var UniswapTrade = /*#__PURE__*/function () {
391
385
  planner.addCommand(CommandType.SWEEP, [this.trade.outputAmount.currency.wrapped.address, this.options.recipient, minimumAmountOut]);
392
386
  }
393
387
  }
394
- if (inputIsNative && (this.trade.tradeType === TradeType.EXACT_OUTPUT || riskOfPartialFill(this.trade))) {
388
+ if (this.inputRequiresWrap && (this.trade.tradeType === TradeType.EXACT_OUTPUT || riskOfPartialFill(this.trade))) {
395
389
  // for exactOutput swaps that take native currency as input
396
390
  // we need to send back the change to the user
397
391
  planner.addCommand(CommandType.UNWRAP_WETH, [this.options.recipient, 0]);
398
392
  }
393
+ if (this.options.safeMode) planner.addCommand(CommandType.SWEEP, [ETH_ADDRESS, this.options.recipient, 0]);
399
394
  };
395
+ _createClass(UniswapTrade, [{
396
+ key: "inputRequiresWrap",
397
+ get: function get() {
398
+ return this.trade.inputAmount.currency.isNative;
399
+ }
400
+ }]);
400
401
  return UniswapTrade;
401
402
  }();
402
403
  // encode a uniswap v2 swap
@@ -626,18 +627,11 @@ var SwapRouter = /*#__PURE__*/function () {
626
627
  allowRevert: false
627
628
  });
628
629
  currentNativeValueInRouter = currentNativeValueInRouter.add(UnwrapWETH.amount);
629
- /**
630
- * is (Un)WrapSTETH
631
- */
632
- } else if (trade.tradeType == RouterTradeType.WrapSTETH || trade.tradeType == RouterTradeType.UnwrapSTETH) {
633
- trade.encode(planner, {
634
- allowRevert: false
635
- });
636
630
  /**
637
631
  * else
638
632
  */
639
633
  } else {
640
- throw 'trade must be of instance: UniswapTrade, NFTTrade, UnwrapWETH, WrapSTETH';
634
+ throw 'trade must be of instance: UniswapTrade or NFTTrade';
641
635
  }
642
636
  }
643
637
  // TODO: matches current logic for now, but should eventually only sweep for multiple NFT trades
@@ -8201,7 +8195,7 @@ var SeaportTrade = /*#__PURE__*/function (_NFTTrade) {
8201
8195
  var considerationRecipients = [];
8202
8196
  for (var i in protocolDatas) {
8203
8197
  var protocolData = protocolDatas[i];
8204
- var _loop = function _loop(j) {
8198
+ var _loop = function _loop() {
8205
8199
  var item = protocolData.parameters.consideration[j];
8206
8200
  if (considerationRecipients.findIndex(function (x) {
8207
8201
  return x === item.recipient;
@@ -8224,7 +8218,7 @@ var SeaportTrade = /*#__PURE__*/function (_NFTTrade) {
8224
8218
  }
8225
8219
  };
8226
8220
  for (var j in protocolData.parameters.consideration) {
8227
- _loop(j);
8221
+ _loop();
8228
8222
  }
8229
8223
  }
8230
8224
  return considerationFulfillments;
@@ -10590,45 +10584,123 @@ var UnwrapWETH = /*#__PURE__*/function () {
10590
10584
  return UnwrapWETH;
10591
10585
  }();
10592
10586
 
10593
- var WrapSTETH = /*#__PURE__*/function () {
10594
- function WrapSTETH(amount, chainId, permit2, wrapAmount) {
10595
- this.tradeType = RouterTradeType.WrapSTETH;
10596
- this.stethAddress = STETH_ADDRESS(chainId);
10597
- this.amount = amount;
10598
- this.wrapAmount = wrapAmount != null ? wrapAmount : CONTRACT_BALANCE;
10599
- if (!!permit2) {
10600
- !(permit2.details.token.toLowerCase() === this.stethAddress.toLowerCase()) ? process.env.NODE_ENV !== "production" ? invariant(false, "must be permitting STETH address: " + this.stethAddress) : invariant(false) : void 0;
10601
- !(permit2.details.amount >= amount) ? process.env.NODE_ENV !== "production" ? invariant(false, "Did not permit enough STETH for unwrapSTETH transaction") : invariant(false) : void 0;
10602
- this.permit2Data = permit2;
10603
- }
10604
- }
10605
- var _proto = WrapSTETH.prototype;
10606
- _proto.encode = function encode(planner, _) {
10607
- encodeInputTokenOptions(planner, {
10608
- permit2Permit: this.permit2Data,
10609
- permit2TransferFrom: {
10610
- token: this.stethAddress,
10611
- amount: this.amount.toString()
10587
+ var PoolType;
10588
+ (function (PoolType) {
10589
+ PoolType["V2Pool"] = "v2-pool";
10590
+ PoolType["V3Pool"] = "v3-pool";
10591
+ })(PoolType || (PoolType = {}));
10592
+ var isNativeCurrency = function isNativeCurrency(address) {
10593
+ return address.toLowerCase() === ETH_ADDRESS.toLowerCase() || address.toLowerCase() === E_ETH_ADDRESS.toLowerCase();
10594
+ };
10595
+ // Helper class to convert routing-specific quote entities to RouterTrade entities
10596
+ // the returned RouterTrade can then be used to build the UniswapTrade entity in this package
10597
+ var RouterTradeAdapter = /*#__PURE__*/function () {
10598
+ function RouterTradeAdapter() {}
10599
+ // Generate a RouterTrade using fields from a classic quote response
10600
+ RouterTradeAdapter.fromClassicQuote = function fromClassicQuote(quote) {
10601
+ var route = quote.route,
10602
+ tokenIn = quote.tokenIn,
10603
+ tokenOut = quote.tokenOut;
10604
+ if (!route) throw new Error('Expected route to be present');
10605
+ if (!route.length) throw new Error('Expected there to be at least one route');
10606
+ if (route.some(function (r) {
10607
+ return !r.length;
10608
+ })) throw new Error('Expected all routes to have at least one pool');
10609
+ var firstRoute = route[0];
10610
+ var tokenInData = firstRoute[0].tokenIn;
10611
+ var tokenOutData = firstRoute[firstRoute.length - 1].tokenOut;
10612
+ if (!tokenInData || !tokenOutData) throw new Error('Expected both tokenIn and tokenOut to be present');
10613
+ if (tokenInData.chainId !== tokenOutData.chainId) throw new Error('Expected tokenIn and tokenOut to be have same chainId');
10614
+ var parsedCurrencyIn = RouterTradeAdapter.toCurrency(isNativeCurrency(tokenIn), tokenInData);
10615
+ var parsedCurrencyOut = RouterTradeAdapter.toCurrency(isNativeCurrency(tokenOut), tokenOutData);
10616
+ var typedRoutes = route.map(function (subRoute) {
10617
+ var rawAmountIn = subRoute[0].amountIn;
10618
+ var rawAmountOut = subRoute[subRoute.length - 1].amountOut;
10619
+ if (!rawAmountIn || !rawAmountOut) {
10620
+ throw new Error('Expected both raw amountIn and raw amountOut to be present');
10612
10621
  }
10622
+ var inputAmount = CurrencyAmount.fromRawAmount(parsedCurrencyIn, rawAmountIn);
10623
+ var outputAmount = CurrencyAmount.fromRawAmount(parsedCurrencyOut, rawAmountOut);
10624
+ var isOnlyV2 = RouterTradeAdapter.isVersionedRoute(PoolType.V2Pool, subRoute);
10625
+ var isOnlyV3 = RouterTradeAdapter.isVersionedRoute(PoolType.V3Pool, subRoute);
10626
+ return {
10627
+ routev3: isOnlyV3 ? new Route(subRoute.map(RouterTradeAdapter.toPool), parsedCurrencyIn, parsedCurrencyOut) : null,
10628
+ routev2: isOnlyV2 ? new Route$1(subRoute.map(RouterTradeAdapter.toPair), parsedCurrencyIn, parsedCurrencyOut) : null,
10629
+ mixedRoute: !isOnlyV3 && !isOnlyV2 ? new MixedRouteSDK(subRoute.map(RouterTradeAdapter.toPoolOrPair), parsedCurrencyIn, parsedCurrencyOut) : null,
10630
+ inputAmount: inputAmount,
10631
+ outputAmount: outputAmount
10632
+ };
10633
+ });
10634
+ return new Trade$2({
10635
+ v2Routes: typedRoutes.filter(function (route) {
10636
+ return route.routev2;
10637
+ }).map(function (route) {
10638
+ return {
10639
+ routev2: route.routev2,
10640
+ inputAmount: route.inputAmount,
10641
+ outputAmount: route.outputAmount
10642
+ };
10643
+ }),
10644
+ v3Routes: typedRoutes.filter(function (route) {
10645
+ return route.routev3;
10646
+ }).map(function (route) {
10647
+ return {
10648
+ routev3: route.routev3,
10649
+ inputAmount: route.inputAmount,
10650
+ outputAmount: route.outputAmount
10651
+ };
10652
+ }),
10653
+ mixedRoutes: typedRoutes.filter(function (route) {
10654
+ return route.mixedRoute;
10655
+ }).map(function (route) {
10656
+ return {
10657
+ mixedRoute: route.mixedRoute,
10658
+ inputAmount: route.inputAmount,
10659
+ outputAmount: route.outputAmount
10660
+ };
10661
+ }),
10662
+ tradeType: quote.tradeType
10613
10663
  });
10614
- planner.addCommand(CommandType.WRAP_STETH, [ROUTER_AS_RECIPIENT, this.wrapAmount]);
10615
10664
  };
10616
- return WrapSTETH;
10617
- }();
10618
-
10619
- var UnwrapSTETH = /*#__PURE__*/function () {
10620
- function UnwrapSTETH(recipient, amountMinimum, chainId) {
10621
- this.tradeType = RouterTradeType.UnwrapSTETH;
10622
- this.recipient = recipient;
10623
- this.amountMinimum = amountMinimum;
10624
- !(STETH_ADDRESS(chainId) != NOT_SUPPORTED_ON_CHAIN) ? process.env.NODE_ENV !== "production" ? invariant(false, "STETH not supported on chain " + chainId) : invariant(false) : void 0;
10625
- }
10626
- var _proto = UnwrapSTETH.prototype;
10627
- _proto.encode = function encode(planner, _) {
10628
- planner.addCommand(CommandType.UNWRAP_STETH, [this.recipient, this.amountMinimum]);
10665
+ RouterTradeAdapter.toCurrency = function toCurrency(isNative, token) {
10666
+ if (isNative) {
10667
+ return Ether.onChain(token.chainId);
10668
+ }
10669
+ return this.toToken(token);
10629
10670
  };
10630
- return UnwrapSTETH;
10671
+ RouterTradeAdapter.toToken = function toToken(token) {
10672
+ var chainId = token.chainId,
10673
+ address = token.address,
10674
+ decimals = token.decimals,
10675
+ symbol = token.symbol,
10676
+ buyFeeBps = token.buyFeeBps,
10677
+ sellFeeBps = token.sellFeeBps;
10678
+ return new Token(chainId, address, parseInt(decimals.toString()), symbol, /* name */undefined, false, buyFeeBps ? BigNumber.from(buyFeeBps) : undefined, sellFeeBps ? BigNumber.from(sellFeeBps) : undefined);
10679
+ };
10680
+ RouterTradeAdapter.toPool = function toPool(_ref) {
10681
+ var fee = _ref.fee,
10682
+ sqrtRatioX96 = _ref.sqrtRatioX96,
10683
+ liquidity = _ref.liquidity,
10684
+ tickCurrent = _ref.tickCurrent,
10685
+ tokenIn = _ref.tokenIn,
10686
+ tokenOut = _ref.tokenOut;
10687
+ return new Pool(RouterTradeAdapter.toToken(tokenIn), RouterTradeAdapter.toToken(tokenOut), parseInt(fee), sqrtRatioX96, liquidity, parseInt(tickCurrent));
10688
+ };
10689
+ RouterTradeAdapter.isVersionedRoute = function isVersionedRoute(type, route) {
10690
+ return route.every(function (pool) {
10691
+ return pool.type === type;
10692
+ });
10693
+ };
10694
+ return RouterTradeAdapter;
10631
10695
  }();
10696
+ RouterTradeAdapter.toPoolOrPair = function (pool) {
10697
+ return pool.type === PoolType.V3Pool ? RouterTradeAdapter.toPool(pool) : RouterTradeAdapter.toPair(pool);
10698
+ };
10699
+ RouterTradeAdapter.toPair = function (_ref2) {
10700
+ var reserve0 = _ref2.reserve0,
10701
+ reserve1 = _ref2.reserve1;
10702
+ return new Pair(CurrencyAmount.fromRawAmount(RouterTradeAdapter.toToken(reserve0.token), reserve0.quotient), CurrencyAmount.fromRawAmount(RouterTradeAdapter.toToken(reserve1.token), reserve1.quotient));
10703
+ };
10632
10704
 
10633
- export { CommandType, CryptopunkTrade, FoundationTrade, LooksRareV2Trade, Market, NFT20Trade, NFTTrade, NFTXTrade, PERMIT2_ADDRESS, ROUTER_AS_RECIPIENT, RoutePlanner, RouterTradeType, SeaportTrade, SudoswapTrade, SwapRouter, TokenType, UNIVERSAL_ROUTER_ADDRESS, UNIVERSAL_ROUTER_CREATION_BLOCK, UniswapTrade, UnwrapSTETH, UnwrapWETH, WETH_ADDRESS, WrapSTETH, X2Y2Trade };
10705
+ export { CommandType, CryptopunkTrade, FoundationTrade, LooksRareV2Trade, Market, NFT20Trade, NFTTrade, NFTXTrade, PERMIT2_ADDRESS, PoolType, ROUTER_AS_RECIPIENT, RoutePlanner, RouterTradeAdapter, RouterTradeType, SeaportTrade, SudoswapTrade, SwapRouter, TokenType, UNIVERSAL_ROUTER_ADDRESS, UNIVERSAL_ROUTER_CREATION_BLOCK, UniswapTrade, UnwrapWETH, WETH_ADDRESS, X2Y2Trade, isNativeCurrency };
10634
10706
  //# sourceMappingURL=universal-router-sdk.esm.js.map