@uniswap/router-sdk 1.10.0 → 1.11.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.
@@ -1,4 +1,4 @@
1
- import { Percent, validateAndParseAddress, Price, Fraction, CurrencyAmount, TradeType, sortedInsert, WETH9 } from '@uniswap/sdk-core';
1
+ import { Percent, validateAndParseAddress, Price, CurrencyAmount, Fraction, TradeType, sortedInsert, WETH9 } from '@uniswap/sdk-core';
2
2
  import JSBI from 'jsbi';
3
3
  import { Interface } from '@ethersproject/abi';
4
4
  import invariant from 'tiny-invariant';
@@ -617,6 +617,21 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
617
617
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
618
618
  }
619
619
 
620
+ function isValidTokenPath(prevPool, currentPool, inputToken) {
621
+ if (currentPool.involvesToken(inputToken)) return true;
622
+ // throw if both v4 pools, native/wrapped tokens not interchangeable in v4
623
+ if (prevPool instanceof Pool && currentPool instanceof Pool) return false;
624
+ // v2/v3 --> v4 valid if v2/v3 output is the wrapped version of the v4 pool native currency
625
+ if (currentPool instanceof Pool) {
626
+ if (currentPool.token0.wrapped.equals(inputToken) || currentPool.token1.wrapped.equals(inputToken)) return true;
627
+ }
628
+ // v4 --> v2/v3 valid if v4 output is the native version of the v2/v3 wrapped token
629
+ if (prevPool instanceof Pool) {
630
+ if (currentPool.involvesToken(inputToken.wrapped)) return true;
631
+ }
632
+ return false;
633
+ }
634
+
620
635
  /**
621
636
  * Represents a list of pools or pairs through which a swap can occur
622
637
  * @template TInput The input token
@@ -653,14 +668,14 @@ var MixedRouteSDK = /*#__PURE__*/function () {
653
668
  * Normalizes token0-token1 order and selects the next token/fee step to add to the path
654
669
  * */
655
670
  var tokenPath = [this.adjustedInput];
656
- for (var _iterator = _createForOfIteratorHelperLoose(pools.entries()), _step; !(_step = _iterator()).done;) {
657
- var _step$value = _step.value,
658
- i = _step$value[0],
659
- pool = _step$value[1];
660
- var currentInputToken = tokenPath[i];
661
- !(currentInputToken.equals(pool.token0) || currentInputToken.equals(pool.token1)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'PATH') : invariant(false) : void 0;
662
- var nextToken = currentInputToken.equals(pool.token0) ? pool.token1 : pool.token0;
663
- tokenPath.push(nextToken);
671
+ pools[0].token0.equals(this.adjustedInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
672
+ for (var i = 1; i < pools.length; i++) {
673
+ var prevPool = pools[i - 1];
674
+ var pool = pools[i];
675
+ var inputToken = tokenPath[i];
676
+ var outputToken = pool.token0.wrapped.equals(inputToken.wrapped) ? pool.token1 : pool.token0;
677
+ !isValidTokenPath(prevPool, pool, inputToken) ? process.env.NODE_ENV !== "production" ? invariant(false, 'PATH') : invariant(false) : void 0;
678
+ tokenPath.push(outputToken);
664
679
  }
665
680
  this.pools = pools;
666
681
  this.path = tokenPath;
@@ -711,32 +726,41 @@ function _getOutputAmount() {
711
726
  while (1) switch (_context.prev = _context.next) {
712
727
  case 0:
713
728
  if (!(pool instanceof Pool)) {
714
- _context.next = 11;
729
+ _context.next = 13;
715
730
  break;
716
731
  }
717
732
  if (!pool.involvesCurrency(amountIn.currency)) {
718
- _context.next = 7;
733
+ _context.next = 5;
719
734
  break;
720
735
  }
721
736
  _context.next = 4;
722
737
  return pool.getOutputAmount(amountIn);
723
738
  case 4:
724
739
  return _context.abrupt("return", _context.sent);
725
- case 7:
726
- if (!pool.involvesCurrency(amountIn.currency.wrapped)) {
727
- _context.next = 11;
740
+ case 5:
741
+ if (!pool.token0.wrapped.equals(amountIn.currency)) {
742
+ _context.next = 9;
728
743
  break;
729
744
  }
730
- _context.next = 10;
731
- return pool.getOutputAmount(amountIn.wrapped);
732
- case 10:
745
+ _context.next = 8;
746
+ return pool.getOutputAmount(CurrencyAmount.fromRawAmount(pool.token0, amountIn.quotient));
747
+ case 8:
748
+ return _context.abrupt("return", _context.sent);
749
+ case 9:
750
+ if (!pool.token1.wrapped.equals(amountIn.currency)) {
751
+ _context.next = 13;
752
+ break;
753
+ }
754
+ _context.next = 12;
755
+ return pool.getOutputAmount(CurrencyAmount.fromRawAmount(pool.token1, amountIn.quotient));
756
+ case 12:
733
757
  return _context.abrupt("return", _context.sent);
734
- case 11:
735
- _context.next = 13;
736
- return pool.getOutputAmount(amountIn.wrapped);
737
758
  case 13:
759
+ _context.next = 15;
760
+ return pool.getOutputAmount(amountIn.wrapped);
761
+ case 15:
738
762
  return _context.abrupt("return", _context.sent);
739
- case 14:
763
+ case 16:
740
764
  case "end":
741
765
  return _context.stop();
742
766
  }
@@ -1332,48 +1356,65 @@ var MixedRoute = /*#__PURE__*/function (_MixedRouteSDK) {
1332
1356
  var Trade = /*#__PURE__*/function () {
1333
1357
  // construct a trade across v2 and v3 routes from pre-computed amounts
1334
1358
  function Trade(_ref) {
1335
- var v2Routes = _ref.v2Routes,
1336
- v3Routes = _ref.v3Routes,
1337
- v4Routes = _ref.v4Routes,
1338
- tradeType = _ref.tradeType,
1339
- mixedRoutes = _ref.mixedRoutes;
1359
+ var _ref$v2Routes = _ref.v2Routes,
1360
+ v2Routes = _ref$v2Routes === void 0 ? [] : _ref$v2Routes,
1361
+ _ref$v3Routes = _ref.v3Routes,
1362
+ v3Routes = _ref$v3Routes === void 0 ? [] : _ref$v3Routes,
1363
+ _ref$v4Routes = _ref.v4Routes,
1364
+ v4Routes = _ref$v4Routes === void 0 ? [] : _ref$v4Routes,
1365
+ _ref$mixedRoutes = _ref.mixedRoutes,
1366
+ mixedRoutes = _ref$mixedRoutes === void 0 ? [] : _ref$mixedRoutes,
1367
+ tradeType = _ref.tradeType;
1340
1368
  this.swaps = [];
1341
1369
  this.routes = [];
1342
1370
  // wrap v2 routes
1343
1371
  for (var _iterator = _createForOfIteratorHelperLoose(v2Routes), _step; !(_step = _iterator()).done;) {
1344
1372
  var _step$value = _step.value,
1345
1373
  routev2 = _step$value.routev2,
1346
- _inputAmount = _step$value.inputAmount,
1347
- _outputAmount = _step$value.outputAmount;
1348
- var _route = new RouteV2(routev2);
1349
- this.routes.push(_route);
1374
+ inputAmount = _step$value.inputAmount,
1375
+ outputAmount = _step$value.outputAmount;
1376
+ var route = new RouteV2(routev2);
1377
+ this.routes.push(route);
1350
1378
  this.swaps.push({
1351
- route: _route,
1352
- inputAmount: _inputAmount,
1353
- outputAmount: _outputAmount
1379
+ route: route,
1380
+ inputAmount: inputAmount,
1381
+ outputAmount: outputAmount
1354
1382
  });
1355
1383
  }
1356
1384
  // wrap v3 routes
1357
1385
  for (var _iterator2 = _createForOfIteratorHelperLoose(v3Routes), _step2; !(_step2 = _iterator2()).done;) {
1358
1386
  var _step2$value = _step2.value,
1359
1387
  routev3 = _step2$value.routev3,
1360
- _inputAmount2 = _step2$value.inputAmount,
1361
- _outputAmount2 = _step2$value.outputAmount;
1362
- var _route2 = new RouteV3(routev3);
1363
- this.routes.push(_route2);
1388
+ _inputAmount = _step2$value.inputAmount,
1389
+ _outputAmount = _step2$value.outputAmount;
1390
+ var _route = new RouteV3(routev3);
1391
+ this.routes.push(_route);
1364
1392
  this.swaps.push({
1365
- route: _route2,
1366
- inputAmount: _inputAmount2,
1367
- outputAmount: _outputAmount2
1393
+ route: _route,
1394
+ inputAmount: _inputAmount,
1395
+ outputAmount: _outputAmount
1368
1396
  });
1369
1397
  }
1370
1398
  // wrap v4 routes
1371
1399
  for (var _iterator3 = _createForOfIteratorHelperLoose(v4Routes), _step3; !(_step3 = _iterator3()).done;) {
1372
1400
  var _step3$value = _step3.value,
1373
1401
  routev4 = _step3$value.routev4,
1374
- _inputAmount3 = _step3$value.inputAmount,
1375
- _outputAmount3 = _step3$value.outputAmount;
1376
- var _route3 = new RouteV4(routev4);
1402
+ _inputAmount2 = _step3$value.inputAmount,
1403
+ _outputAmount2 = _step3$value.outputAmount;
1404
+ var _route2 = new RouteV4(routev4);
1405
+ this.routes.push(_route2);
1406
+ this.swaps.push({
1407
+ route: _route2,
1408
+ inputAmount: _inputAmount2,
1409
+ outputAmount: _outputAmount2
1410
+ });
1411
+ }
1412
+ for (var _iterator4 = _createForOfIteratorHelperLoose(mixedRoutes), _step4; !(_step4 = _iterator4()).done;) {
1413
+ var _step4$value = _step4.value,
1414
+ mixedRoute = _step4$value.mixedRoute,
1415
+ _inputAmount3 = _step4$value.inputAmount,
1416
+ _outputAmount3 = _step4$value.outputAmount;
1417
+ var _route3 = new MixedRoute(mixedRoute);
1377
1418
  this.routes.push(_route3);
1378
1419
  this.swaps.push({
1379
1420
  route: _route3,
@@ -1381,22 +1422,6 @@ var Trade = /*#__PURE__*/function () {
1381
1422
  outputAmount: _outputAmount3
1382
1423
  });
1383
1424
  }
1384
- // wrap mixedRoutes
1385
- if (mixedRoutes) {
1386
- for (var _iterator4 = _createForOfIteratorHelperLoose(mixedRoutes), _step4; !(_step4 = _iterator4()).done;) {
1387
- var _step4$value = _step4.value,
1388
- mixedRoute = _step4$value.mixedRoute,
1389
- inputAmount = _step4$value.inputAmount,
1390
- outputAmount = _step4$value.outputAmount;
1391
- var route = new MixedRoute(mixedRoute);
1392
- this.routes.push(route);
1393
- this.swaps.push({
1394
- route: route,
1395
- inputAmount: inputAmount,
1396
- outputAmount: outputAmount
1397
- });
1398
- }
1399
- }
1400
1425
  if (this.swaps.length === 0) {
1401
1426
  throw new Error('No routes provided when calling Trade constructor');
1402
1427
  }
@@ -1482,8 +1507,8 @@ var Trade = /*#__PURE__*/function () {
1482
1507
  return new Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
1483
1508
  };
1484
1509
  Trade.fromRoutes = /*#__PURE__*/function () {
1485
- var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(v2Routes, v3Routes, v4Routes, tradeType, mixedRoutes) {
1486
- var populatedV2Routes, populatedV3Routes, populatedV4Routes, populatedMixedRoutes, _iterator7, _step7, _step7$value, routev2, _amount, v2Trade, _inputAmount4, _outputAmount4, _iterator8, _step8, _step8$value, routev3, _amount2, v3Trade, _inputAmount5, _outputAmount5, _iterator9, _step9, _step9$value, routev4, _amount3, v4Trade, _inputAmount6, _outputAmount6, _iterator10, _step10, _step10$value, mixedRoute, amount, mixedRouteTrade, inputAmount, outputAmount;
1510
+ var _fromRoutes = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(v2Routes, v3Routes, tradeType, mixedRoutes, v4Routes) {
1511
+ var populatedV2Routes, populatedV3Routes, populatedV4Routes, populatedMixedRoutes, _iterator7, _step7, _step7$value, routev2, _amount2, v2Trade, _inputAmount5, _outputAmount5, _iterator8, _step8, _step8$value, routev3, _amount3, v3Trade, _inputAmount6, _outputAmount6, _iterator9, _step9, _step9$value, routev4, amount, v4Trade, inputAmount, outputAmount, _iterator10, _step10, _step10$value, mixedRoute, _amount, mixedRouteTrade, _inputAmount4, _outputAmount4;
1487
1512
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1488
1513
  while (1) switch (_context.prev = _context.next) {
1489
1514
  case 0:
@@ -1492,13 +1517,13 @@ var Trade = /*#__PURE__*/function () {
1492
1517
  populatedV4Routes = [];
1493
1518
  populatedMixedRoutes = [];
1494
1519
  for (_iterator7 = _createForOfIteratorHelperLoose(v2Routes); !(_step7 = _iterator7()).done;) {
1495
- _step7$value = _step7.value, routev2 = _step7$value.routev2, _amount = _step7$value.amount;
1496
- v2Trade = new Trade$3(routev2, _amount, tradeType);
1497
- _inputAmount4 = v2Trade.inputAmount, _outputAmount4 = v2Trade.outputAmount;
1520
+ _step7$value = _step7.value, routev2 = _step7$value.routev2, _amount2 = _step7$value.amount;
1521
+ v2Trade = new Trade$3(routev2, _amount2, tradeType);
1522
+ _inputAmount5 = v2Trade.inputAmount, _outputAmount5 = v2Trade.outputAmount;
1498
1523
  populatedV2Routes.push({
1499
1524
  routev2: routev2,
1500
- inputAmount: _inputAmount4,
1501
- outputAmount: _outputAmount4
1525
+ inputAmount: _inputAmount5,
1526
+ outputAmount: _outputAmount5
1502
1527
  });
1503
1528
  }
1504
1529
  _iterator8 = _createForOfIteratorHelperLoose(v3Routes);
@@ -1507,67 +1532,71 @@ var Trade = /*#__PURE__*/function () {
1507
1532
  _context.next = 15;
1508
1533
  break;
1509
1534
  }
1510
- _step8$value = _step8.value, routev3 = _step8$value.routev3, _amount2 = _step8$value.amount;
1535
+ _step8$value = _step8.value, routev3 = _step8$value.routev3, _amount3 = _step8$value.amount;
1511
1536
  _context.next = 10;
1512
- return Trade$2.fromRoute(routev3, _amount2, tradeType);
1537
+ return Trade$2.fromRoute(routev3, _amount3, tradeType);
1513
1538
  case 10:
1514
1539
  v3Trade = _context.sent;
1515
- _inputAmount5 = v3Trade.inputAmount, _outputAmount5 = v3Trade.outputAmount;
1540
+ _inputAmount6 = v3Trade.inputAmount, _outputAmount6 = v3Trade.outputAmount;
1516
1541
  populatedV3Routes.push({
1517
1542
  routev3: routev3,
1518
- inputAmount: _inputAmount5,
1519
- outputAmount: _outputAmount5
1543
+ inputAmount: _inputAmount6,
1544
+ outputAmount: _outputAmount6
1520
1545
  });
1521
1546
  case 13:
1522
1547
  _context.next = 6;
1523
1548
  break;
1524
1549
  case 15:
1550
+ if (!v4Routes) {
1551
+ _context.next = 26;
1552
+ break;
1553
+ }
1525
1554
  _iterator9 = _createForOfIteratorHelperLoose(v4Routes);
1526
- case 16:
1555
+ case 17:
1527
1556
  if ((_step9 = _iterator9()).done) {
1528
- _context.next = 25;
1557
+ _context.next = 26;
1529
1558
  break;
1530
1559
  }
1531
- _step9$value = _step9.value, routev4 = _step9$value.routev4, _amount3 = _step9$value.amount;
1532
- _context.next = 20;
1533
- return Trade$1.fromRoute(routev4, _amount3, tradeType);
1534
- case 20:
1560
+ _step9$value = _step9.value, routev4 = _step9$value.routev4, amount = _step9$value.amount;
1561
+ _context.next = 21;
1562
+ return Trade$1.fromRoute(routev4, amount, tradeType);
1563
+ case 21:
1535
1564
  v4Trade = _context.sent;
1536
- _inputAmount6 = v4Trade.inputAmount, _outputAmount6 = v4Trade.outputAmount;
1565
+ inputAmount = v4Trade.inputAmount, outputAmount = v4Trade.outputAmount;
1537
1566
  populatedV4Routes.push({
1538
1567
  routev4: routev4,
1539
- inputAmount: _inputAmount6,
1540
- outputAmount: _outputAmount6
1568
+ inputAmount: inputAmount,
1569
+ outputAmount: outputAmount
1541
1570
  });
1542
- case 23:
1543
- _context.next = 16;
1571
+ case 24:
1572
+ _context.next = 17;
1544
1573
  break;
1545
- case 25:
1574
+ case 26:
1546
1575
  if (!mixedRoutes) {
1547
- _context.next = 36;
1576
+ _context.next = 37;
1548
1577
  break;
1549
1578
  }
1550
1579
  _iterator10 = _createForOfIteratorHelperLoose(mixedRoutes);
1551
- case 27:
1580
+ case 28:
1552
1581
  if ((_step10 = _iterator10()).done) {
1553
- _context.next = 36;
1582
+ _context.next = 37;
1554
1583
  break;
1555
1584
  }
1556
- _step10$value = _step10.value, mixedRoute = _step10$value.mixedRoute, amount = _step10$value.amount;
1557
- _context.next = 31;
1558
- return MixedRouteTrade.fromRoute(mixedRoute, amount, tradeType);
1559
- case 31:
1585
+ _step10$value = _step10.value, mixedRoute = _step10$value.mixedRoute, _amount = _step10$value.amount;
1586
+ _context.next = 32;
1587
+ return MixedRouteTrade.fromRoute(mixedRoute, _amount, tradeType);
1588
+ case 32:
1560
1589
  mixedRouteTrade = _context.sent;
1561
- inputAmount = mixedRouteTrade.inputAmount, outputAmount = mixedRouteTrade.outputAmount;
1590
+ _inputAmount4 = mixedRouteTrade.inputAmount, _outputAmount4 = mixedRouteTrade.outputAmount;
1562
1591
  populatedMixedRoutes.push({
1563
1592
  mixedRoute: mixedRoute,
1564
- inputAmount: inputAmount,
1565
- outputAmount: outputAmount
1593
+ inputAmount: _inputAmount4,
1594
+ outputAmount: _outputAmount4
1566
1595
  });
1567
- case 34:
1568
- _context.next = 27;
1596
+ case 35:
1597
+ _context.next = 28;
1569
1598
  break;
1570
- case 36:
1599
+ case 37:
1571
1600
  return _context.abrupt("return", new Trade({
1572
1601
  v2Routes: populatedV2Routes,
1573
1602
  v3Routes: populatedV3Routes,
@@ -1575,7 +1604,7 @@ var Trade = /*#__PURE__*/function () {
1575
1604
  mixedRoutes: populatedMixedRoutes,
1576
1605
  tradeType: tradeType
1577
1606
  }));
1578
- case 37:
1607
+ case 38:
1579
1608
  case "end":
1580
1609
  return _context.stop();
1581
1610
  }