@uniswap/token-list-bridge-utils 1.0.8 → 1.1.0

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.
@@ -869,6 +869,7 @@ var ChainId;
869
869
  ChainId[ChainId["OPTIMISTIC_KOVAN"] = 69] = "OPTIMISTIC_KOVAN";
870
870
  ChainId[ChainId["POLYGON"] = 137] = "POLYGON";
871
871
  ChainId[ChainId["POLYGON_MUMBAI"] = 80001] = "POLYGON_MUMBAI";
872
+ ChainId[ChainId["CELO"] = 42220] = "CELO";
872
873
  })(ChainId || (ChainId = {}));
873
874
 
874
875
  function compareTokenInfos(t1, t2) {
@@ -1071,7 +1072,8 @@ function getRpcUrl(chainId) {
1071
1072
  return 'https://cloudflare-eth.com/';
1072
1073
 
1073
1074
  case ChainId.OPTIMISM:
1074
- return 'https://mainnet.optimism.io';
1075
+ return 'https://rpc.ankr.com/optimism';
1076
+ // seems to have higher rate limit than https://mainnet.optimism.io/
1075
1077
 
1076
1078
  case ChainId.OPTIMISTIC_KOVAN:
1077
1079
  return 'https://kovan.optimism.io';
@@ -1587,103 +1589,139 @@ var abi = [{
1587
1589
  type: 'event'
1588
1590
  }];
1589
1591
 
1590
- var web3 = /*#__PURE__*/new Web3();
1592
+ var web3 = /*#__PURE__*/new Web3(); // chains we support fetching mappings for (can be different than the l2ChainIds arg for buildList)
1593
+
1594
+ var SUPPORTED_L2_CHAINS = [ChainId.ARBITRUM_ONE, ChainId.POLYGON, ChainId.OPTIMISM];
1591
1595
  function buildList(_x, _x2) {
1592
1596
  return _buildList.apply(this, arguments);
1593
1597
  } // using a symbol lookup contract call to check whether the token exists on the L2
1594
1598
 
1595
1599
  function _buildList() {
1596
- _buildList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(chainId, l1TokenList) {
1597
- var tokenAddressMap, mappedTokens, _iterator, _step, rootToken, childToken, childTokenAddress, childTokenValid, _bridgeInfo, _bridgeInfo2, toRootExtensions, toChildExtensions, rootTokenInfo, childTokenInfo, tokenList;
1600
+ _buildList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(l2ChainIds, l1TokenList) {
1601
+ var multiChainedTokens, chainIdToMappingsMap, _loop, _iterator, _step, tokenList;
1598
1602
 
1599
- return _regeneratorRuntime.wrap(function _callee$(_context) {
1603
+ return _regeneratorRuntime.wrap(function _callee2$(_context3) {
1600
1604
  while (1) {
1601
- switch (_context.prev = _context.next) {
1605
+ switch (_context3.prev = _context3.next) {
1602
1606
  case 0:
1603
- web3.setProvider(getRpcUrl(chainId));
1604
- _context.next = 3;
1605
- return getMappingProvider(chainId, l1TokenList).provide();
1607
+ multiChainedTokens = [];
1608
+ _context3.next = 3;
1609
+ return generateTokenMappings(l2ChainIds, l1TokenList);
1606
1610
 
1607
1611
  case 3:
1608
- tokenAddressMap = _context.sent;
1609
- mappedTokens = [];
1612
+ chainIdToMappingsMap = _context3.sent;
1613
+ _loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop() {
1614
+ var l1Token, chainIdToChildTokenDetailsMap, l2MappingExtension;
1615
+ return _regeneratorRuntime.wrap(function _loop$(_context2) {
1616
+ while (1) {
1617
+ switch (_context2.prev = _context2.next) {
1618
+ case 0:
1619
+ l1Token = _step.value;
1620
+
1621
+ if (!(l1Token.chainId === ChainId.MAINNET)) {
1622
+ _context2.next = 7;
1623
+ break;
1624
+ }
1625
+
1626
+ chainIdToChildTokenDetailsMap = {};
1627
+ l2MappingExtension = {
1628
+ extensions: {
1629
+ bridgeInfo: {}
1630
+ }
1631
+ }; // build out the extensions.bridgeInfo data containing mappings for each L2 chain
1632
+
1633
+ _context2.next = 6;
1634
+ return Promise.all(l2ChainIds.map( /*#__PURE__*/function () {
1635
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(chainId) {
1636
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
1637
+ while (1) {
1638
+ switch (_context.prev = _context.next) {
1639
+ case 0:
1640
+ _context.next = 2;
1641
+ return getChildTokenDetails(l1Token, chainId, chainIdToMappingsMap);
1642
+
1643
+ case 2:
1644
+ chainIdToChildTokenDetailsMap[chainId] = _context.sent;
1645
+
1646
+ if (chainIdToChildTokenDetailsMap[chainId].childTokenValid) {
1647
+ l2MappingExtension.extensions.bridgeInfo[chainId] = {
1648
+ tokenAddress: chainIdToChildTokenDetailsMap[chainId].childTokenAddress
1649
+ };
1650
+ }
1651
+
1652
+ case 4:
1653
+ case "end":
1654
+ return _context.stop();
1655
+ }
1656
+ }
1657
+ }, _callee);
1658
+ }));
1659
+
1660
+ return function (_x10) {
1661
+ return _ref.apply(this, arguments);
1662
+ };
1663
+ }()));
1664
+
1665
+ case 6:
1666
+ // build the TokenInfo objects with bridgeInfo extension
1667
+ l2ChainIds.concat([ChainId.MAINNET]).forEach(function (chainId) {
1668
+ if (chainId === ChainId.MAINNET || chainIdToChildTokenDetailsMap[chainId].childTokenValid) {
1669
+ var _bridgeInfo;
1670
+
1671
+ var tokenInfo = chainId === ChainId.MAINNET ? _extends({}, l1Token, {
1672
+ extensions: Object.keys(l2MappingExtension.extensions.bridgeInfo).length > 0 ? {
1673
+ bridgeInfo: l2MappingExtension.extensions.bridgeInfo
1674
+ } : undefined
1675
+ }) : _extends({}, l1Token, {
1676
+ chainId: chainId,
1677
+ address: chainIdToChildTokenDetailsMap[chainId].childTokenAddress,
1678
+ extensions: {
1679
+ bridgeInfo: (_bridgeInfo = {}, _bridgeInfo[ChainId.MAINNET] = {
1680
+ tokenAddress: ethers.utils.getAddress(l1Token.address)
1681
+ }, _bridgeInfo)
1682
+ }
1683
+ });
1684
+ multiChainedTokens.push(tokenInfo);
1685
+ }
1686
+ });
1687
+
1688
+ case 7:
1689
+ case "end":
1690
+ return _context2.stop();
1691
+ }
1692
+ }
1693
+ }, _loop);
1694
+ });
1610
1695
  _iterator = _createForOfIteratorHelperLoose(l1TokenList.tokens);
1611
1696
 
1612
1697
  case 6:
1613
1698
  if ((_step = _iterator()).done) {
1614
- _context.next = 21;
1615
- break;
1616
- }
1617
-
1618
- rootToken = _step.value;
1619
- childToken = tokenAddressMap[rootToken.address.toLowerCase()];
1620
- childTokenAddress = childToken ? ethers.utils.getAddress(typeof childToken === 'object' ? childToken.child_token : childToken) : undefined;
1621
- _context.t0 = Boolean;
1622
- _context.t1 = childTokenAddress && (typeof childToken === 'object' ? !childToken.deleted : true);
1623
-
1624
- if (!_context.t1) {
1625
- _context.next = 16;
1699
+ _context3.next = 10;
1626
1700
  break;
1627
1701
  }
1628
1702
 
1629
- _context.next = 15;
1630
- return hasExistingTokenContract(childTokenAddress, chainId);
1631
-
1632
- case 15:
1633
- _context.t1 = _context.sent;
1634
-
1635
- case 16:
1636
- _context.t2 = _context.t1;
1637
- childTokenValid = (0, _context.t0)(_context.t2);
1638
-
1639
- if (rootToken.chainId === ChainId.MAINNET) {
1640
- // build extension info if available
1641
- toRootExtensions = childTokenValid ? {
1642
- extensions: {
1643
- bridgeInfo: (_bridgeInfo = {}, _bridgeInfo[rootToken.chainId] = {
1644
- tokenAddress: ethers.utils.getAddress(rootToken.address)
1645
- }, _bridgeInfo)
1646
- }
1647
- } : {};
1648
- toChildExtensions = childTokenValid ? {
1649
- extensions: {
1650
- bridgeInfo: (_bridgeInfo2 = {}, _bridgeInfo2[chainId] = {
1651
- tokenAddress: childTokenAddress
1652
- }, _bridgeInfo2)
1653
- }
1654
- } : {};
1655
- rootTokenInfo = _extends({}, rootToken, toChildExtensions);
1656
-
1657
- if (childTokenValid) {
1658
- childTokenInfo = _extends({}, rootToken, {
1659
- chainId: chainId,
1660
- address: childTokenAddress
1661
- }, toRootExtensions);
1662
- mappedTokens.push(childTokenInfo);
1663
- }
1664
-
1665
- mappedTokens.push(rootTokenInfo);
1666
- }
1703
+ return _context3.delegateYield(_loop(), "t0", 8);
1667
1704
 
1668
- case 19:
1669
- _context.next = 6;
1705
+ case 8:
1706
+ _context3.next = 6;
1670
1707
  break;
1671
1708
 
1672
- case 21:
1709
+ case 10:
1710
+ // build and return final chainified token list
1673
1711
  tokenList = {
1674
- name: "(ChainId: " + chainId + ") " + l1TokenList.name,
1712
+ name: "(ChainIds: " + l2ChainIds + ") " + l1TokenList.name,
1675
1713
  timestamp: new Date().toISOString(),
1676
1714
  version: l1TokenList.version,
1677
- tokens: mappedTokens.sort(compareTokenInfos)
1715
+ tokens: multiChainedTokens.sort(compareTokenInfos)
1678
1716
  };
1679
- return _context.abrupt("return", tokenList);
1717
+ return _context3.abrupt("return", tokenList);
1680
1718
 
1681
- case 23:
1719
+ case 12:
1682
1720
  case "end":
1683
- return _context.stop();
1721
+ return _context3.stop();
1684
1722
  }
1685
1723
  }
1686
- }, _callee);
1724
+ }, _callee2);
1687
1725
  }));
1688
1726
  return _buildList.apply(this, arguments);
1689
1727
  }
@@ -1693,35 +1731,36 @@ function hasExistingTokenContract(_x3, _x4) {
1693
1731
  }
1694
1732
 
1695
1733
  function _hasExistingTokenContract() {
1696
- _hasExistingTokenContract = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(address, chainId) {
1734
+ _hasExistingTokenContract = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(address, chainId) {
1697
1735
  var contract;
1698
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
1736
+ return _regeneratorRuntime.wrap(function _callee3$(_context4) {
1699
1737
  while (1) {
1700
- switch (_context2.prev = _context2.next) {
1738
+ switch (_context4.prev = _context4.next) {
1701
1739
  case 0:
1702
- _context2.prev = 0;
1740
+ web3.setProvider(getRpcUrl(chainId));
1741
+ _context4.prev = 1;
1703
1742
  contract = new web3.eth.Contract(abi, address);
1704
- _context2.next = 4;
1743
+ _context4.next = 5;
1705
1744
  return getTokenSymbolFromContract(contract);
1706
1745
 
1707
- case 4:
1708
- _context2.next = 9;
1746
+ case 5:
1747
+ _context4.next = 10;
1709
1748
  break;
1710
1749
 
1711
- case 6:
1712
- _context2.prev = 6;
1713
- _context2.t0 = _context2["catch"](0);
1714
- return _context2.abrupt("return", false);
1715
-
1716
- case 9:
1717
- return _context2.abrupt("return", true);
1750
+ case 7:
1751
+ _context4.prev = 7;
1752
+ _context4.t0 = _context4["catch"](1);
1753
+ return _context4.abrupt("return", false);
1718
1754
 
1719
1755
  case 10:
1756
+ return _context4.abrupt("return", true);
1757
+
1758
+ case 11:
1720
1759
  case "end":
1721
- return _context2.stop();
1760
+ return _context4.stop();
1722
1761
  }
1723
1762
  }
1724
- }, _callee2, null, [[0, 6]]);
1763
+ }, _callee3, null, [[1, 7]]);
1725
1764
  }));
1726
1765
  return _hasExistingTokenContract.apply(this, arguments);
1727
1766
  }
@@ -1738,10 +1777,123 @@ function getMappingProvider(chainId, l1TokenList) {
1738
1777
  return new PolygonMappingProvider();
1739
1778
 
1740
1779
  default:
1741
- throw new Error("Chain " + chainId + " not supported.");
1780
+ throw new Error("Chain " + chainId + " not supported for fetching mappings.");
1742
1781
  }
1743
1782
  }
1744
1783
 
1784
+ function generateTokenMappings(_x5, _x6) {
1785
+ return _generateTokenMappings.apply(this, arguments);
1786
+ } // handles both string and object cases for childToken (Polygon mappings return object)
1787
+
1788
+
1789
+ function _generateTokenMappings() {
1790
+ _generateTokenMappings = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(chainIds, l1TokenList) {
1791
+ var chainIdToMappingsMap, _iterator2, _step2, chainId;
1792
+
1793
+ return _regeneratorRuntime.wrap(function _callee4$(_context5) {
1794
+ while (1) {
1795
+ switch (_context5.prev = _context5.next) {
1796
+ case 0:
1797
+ chainIdToMappingsMap = {};
1798
+ _iterator2 = _createForOfIteratorHelperLoose(chainIds);
1799
+
1800
+ case 2:
1801
+ if ((_step2 = _iterator2()).done) {
1802
+ _context5.next = 10;
1803
+ break;
1804
+ }
1805
+
1806
+ chainId = _step2.value;
1807
+
1808
+ if (!SUPPORTED_L2_CHAINS.includes(chainId)) {
1809
+ _context5.next = 8;
1810
+ break;
1811
+ }
1812
+
1813
+ _context5.next = 7;
1814
+ return getMappingProvider(chainId, l1TokenList).provide();
1815
+
1816
+ case 7:
1817
+ chainIdToMappingsMap[chainId] = _context5.sent;
1818
+
1819
+ case 8:
1820
+ _context5.next = 2;
1821
+ break;
1822
+
1823
+ case 10:
1824
+ return _context5.abrupt("return", chainIdToMappingsMap);
1825
+
1826
+ case 11:
1827
+ case "end":
1828
+ return _context5.stop();
1829
+ }
1830
+ }
1831
+ }, _callee4);
1832
+ }));
1833
+ return _generateTokenMappings.apply(this, arguments);
1834
+ }
1835
+
1836
+ function getChildTokenDetails(_x7, _x8, _x9) {
1837
+ return _getChildTokenDetails.apply(this, arguments);
1838
+ }
1839
+
1840
+ function _getChildTokenDetails() {
1841
+ _getChildTokenDetails = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(l1Token, chainId, chainIdToMappingsMap) {
1842
+ var _l1Token$extensions, _l1Token$extensions$b, _l1Token$extensions$b2;
1843
+
1844
+ var existingMapping, childToken, childTokenAddress, childTokenValid;
1845
+ return _regeneratorRuntime.wrap(function _callee5$(_context6) {
1846
+ while (1) {
1847
+ switch (_context6.prev = _context6.next) {
1848
+ case 0:
1849
+ existingMapping = l1Token == null ? void 0 : (_l1Token$extensions = l1Token.extensions) == null ? void 0 : (_l1Token$extensions$b = _l1Token$extensions.bridgeInfo) == null ? void 0 : (_l1Token$extensions$b2 = _l1Token$extensions$b[chainId]) == null ? void 0 : _l1Token$extensions$b2.tokenAddress; // use the externally fetched mappings if manual entry doesn't exist for the token/chain mapping
1850
+ // and the given L2 chain is supported for fetching mappings
1851
+
1852
+ if (!(SUPPORTED_L2_CHAINS.includes(chainId) && existingMapping === undefined)) {
1853
+ _context6.next = 13;
1854
+ break;
1855
+ }
1856
+
1857
+ childToken = chainIdToMappingsMap[chainId][l1Token.address.toLowerCase()];
1858
+ childTokenAddress = childToken ? ethers.utils.getAddress(typeof childToken === 'object' ? childToken.child_token : childToken) : undefined;
1859
+ _context6.t0 = Boolean;
1860
+ _context6.t1 = childTokenAddress && (typeof childToken === 'object' ? !childToken.deleted : true);
1861
+
1862
+ if (!_context6.t1) {
1863
+ _context6.next = 10;
1864
+ break;
1865
+ }
1866
+
1867
+ _context6.next = 9;
1868
+ return hasExistingTokenContract(childTokenAddress, chainId);
1869
+
1870
+ case 9:
1871
+ _context6.t1 = _context6.sent;
1872
+
1873
+ case 10:
1874
+ _context6.t2 = _context6.t1;
1875
+ childTokenValid = (0, _context6.t0)(_context6.t2);
1876
+ return _context6.abrupt("return", {
1877
+ childTokenValid: childTokenValid,
1878
+ childTokenAddress: childTokenAddress
1879
+ });
1880
+
1881
+ case 13:
1882
+ return _context6.abrupt("return", {
1883
+ childTokenValid: !!existingMapping,
1884
+ childTokenAddress: existingMapping
1885
+ });
1886
+
1887
+ case 14:
1888
+ case "end":
1889
+ return _context6.stop();
1890
+ }
1891
+ }
1892
+ }, _callee5);
1893
+ }));
1894
+ return _getChildTokenDetails.apply(this, arguments);
1895
+ }
1896
+
1745
1897
  /**
1746
1898
  * Verifies that for each token with extensions.bridgeInfo defined, for every
1747
1899
  * chainId there exists a token with that chainId at the root-level of the
@@ -1815,14 +1967,14 @@ function chainify(_x) {
1815
1967
  }
1816
1968
  /**
1817
1969
  * Given a network and a TokenList, returns the TokenList with `extensions` filled.
1818
- * @param chainId chainId to operate on
1970
+ * @param l2ChainIds layer 2 chainIds to operate on
1819
1971
  * @param l1TokenListOrPathOrUrl either an L1 TokenList object or a path/url to a TokenList
1820
1972
  * @returns L1 TokenList with `extensions` filled for the given network
1821
1973
  */
1822
1974
 
1823
1975
  function _chainify() {
1824
1976
  _chainify = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(l1TokenListOrPathOrUrl) {
1825
- var l1TokenList, optimisimed, polygoned, arbified;
1977
+ var l1TokenList, l2Chains, chainified;
1826
1978
  return _regeneratorRuntime.wrap(function _callee$(_context) {
1827
1979
  while (1) {
1828
1980
  switch (_context.prev = _context.next) {
@@ -1832,25 +1984,16 @@ function _chainify() {
1832
1984
 
1833
1985
  case 2:
1834
1986
  l1TokenList = _context.sent;
1835
- _context.next = 5;
1836
- return chainifyTokenList(ChainId.OPTIMISM, l1TokenListOrPathOrUrl);
1837
-
1838
- case 5:
1839
- optimisimed = _context.sent;
1840
- _context.next = 8;
1841
- return chainifyTokenList(ChainId.POLYGON, l1TokenListOrPathOrUrl);
1842
-
1843
- case 8:
1844
- polygoned = _context.sent;
1845
- _context.next = 11;
1846
- return chainifyTokenList(ChainId.ARBITRUM_ONE, l1TokenListOrPathOrUrl);
1987
+ l2Chains = [ChainId.POLYGON, ChainId.ARBITRUM_ONE, ChainId.OPTIMISM, ChainId.CELO];
1988
+ _context.next = 6;
1989
+ return chainifyTokenList(l2Chains, l1TokenListOrPathOrUrl);
1847
1990
 
1848
- case 11:
1849
- arbified = _context.sent;
1991
+ case 6:
1992
+ chainified = _context.sent;
1850
1993
  return _context.abrupt("return", mergeTokenLists(l1TokenList, // providing l1 first to make sure duplicated tokens resolve to this list
1851
- mergeTokenLists(mergeTokenLists(arbified, optimisimed), polygoned)));
1994
+ chainified));
1852
1995
 
1853
- case 13:
1996
+ case 8:
1854
1997
  case "end":
1855
1998
  return _context.stop();
1856
1999
  }
@@ -1866,7 +2009,7 @@ function chainifyTokenList(_x2, _x3) {
1866
2009
  /** Merges two token lists, resolving conflicts to primary list. */
1867
2010
 
1868
2011
  function _chainifyTokenList() {
1869
- _chainifyTokenList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(chainId, l1TokenListOrPathOrUrl) {
2012
+ _chainifyTokenList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(l2ChainIds, l1TokenListOrPathOrUrl) {
1870
2013
  var l1TokenList, tokenList;
1871
2014
  return _regeneratorRuntime.wrap(function _callee2$(_context2) {
1872
2015
  while (1) {
@@ -1879,7 +2022,7 @@ function _chainifyTokenList() {
1879
2022
  case 3:
1880
2023
  l1TokenList = _context2.sent;
1881
2024
  _context2.next = 6;
1882
- return buildList(chainId, l1TokenList);
2025
+ return buildList(l2ChainIds, l1TokenList);
1883
2026
 
1884
2027
  case 6:
1885
2028
  tokenList = _context2.sent;