@uniswap/token-list-bridge-utils 2.0.0 → 2.2.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.
@@ -872,6 +872,8 @@ var ChainId;
872
872
  ChainId[ChainId["CELO"] = 42220] = "CELO";
873
873
  ChainId[ChainId["BNB"] = 56] = "BNB";
874
874
  ChainId[ChainId["AVALANCHE"] = 43114] = "AVALANCHE";
875
+ ChainId[ChainId["BASE"] = 8453] = "BASE";
876
+ ChainId[ChainId["BASE_GOERLI"] = 84531] = "BASE_GOERLI";
875
877
  })(ChainId || (ChainId = {}));
876
878
 
877
879
  function compareTokenInfos(t1, t2) {
@@ -1097,6 +1099,12 @@ function getRpcUrl(chainId) {
1097
1099
 
1098
1100
  case ChainId.AVALANCHE:
1099
1101
  return 'https://api.avax.network/ext/bc/C/rpc';
1102
+
1103
+ case ChainId.BASE:
1104
+ return 'https://mainnet.base.org';
1105
+
1106
+ case ChainId.BASE_GOERLI:
1107
+ return 'https://goerli.base.org';
1100
1108
  }
1101
1109
 
1102
1110
  throw new Error('Unsupported ChainId');
@@ -1134,7 +1142,13 @@ var _process$env, _process$env$MAINNET_;
1134
1142
  config();
1135
1143
  (_process$env$MAINNET_ = (_process$env = process.env).MAINNET_RPC) != null ? _process$env$MAINNET_ : _process$env.MAINNET_RPC = /*#__PURE__*/getRpcUrl(ChainId.MAINNET);
1136
1144
 
1137
- var networkID = 42161; // ref: https://github.com/OffchainLabs/arb-token-lists/blob/master/src/lib/instantiate_bridge.ts
1145
+ var networkID = 42161;
1146
+
1147
+ var l2Rpc = function l2Rpc(chainId) {
1148
+ if (chainId === 42161) return 'https://arb1.arbitrum.io/rpc';else if (chainId === 421611) return 'https://rinkeby.arbitrum.io/rpc';else if (chainId === 42170) return 'https://nova.arbitrum.io/rpc';else if (chainId === 421613) return 'https://goerli-rollup.arbitrum.io/rpc';
1149
+ throw new Error('No L2 RPC detected');
1150
+ }; // ref: https://github.com/OffchainLabs/arb-token-lists/blob/master/src/lib/instantiate_bridge.ts
1151
+
1138
1152
 
1139
1153
  var getNetworkConfig = /*#__PURE__*/function () {
1140
1154
  var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
@@ -1162,8 +1176,8 @@ var getNetworkConfig = /*#__PURE__*/function () {
1162
1176
 
1163
1177
  case 11:
1164
1178
  l1Network = _context.sent;
1165
- arbProvider = new providers.JsonRpcProvider(l2Network.rpcURL);
1166
- ethProvider = new providers.JsonRpcProvider(l1Network.rpcURL);
1179
+ arbProvider = new providers.JsonRpcProvider(l2Rpc(l2Network.chainID));
1180
+ ethProvider = new providers.JsonRpcProvider(process.env.MAINNET_RPC);
1167
1181
  _context.next = 16;
1168
1182
  return MultiCaller.fromProvider(ethProvider);
1169
1183
 
@@ -1298,7 +1312,7 @@ var ArbitrumMappingProvider = /*#__PURE__*/function () {
1298
1312
  return ArbitrumMappingProvider;
1299
1313
  }();
1300
1314
 
1301
- var optimismTokenListURL = 'https://raw.githubusercontent.com/' + 'ethereum-optimism/ethereum-optimism.github.io/2138386277e4156d159615d1840882cecc398437/optimism.tokenlist.json';
1315
+ var optimismTokenListURL = 'https://raw.githubusercontent.com/' + 'ethereum-optimism/ethereum-optimism.github.io/master/optimism.tokenlist.json';
1302
1316
  /**
1303
1317
  * The Optimism L2 mapping (linked above) is manually maintained by the Optimism team.
1304
1318
  *
@@ -1312,8 +1326,7 @@ var OptimismMappingProvider = /*#__PURE__*/function () {
1312
1326
 
1313
1327
  _proto.provide = /*#__PURE__*/function () {
1314
1328
  var _provide = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
1315
- var tokens, optimismTokens, _iterator, _step, token, _token$extensions;
1316
-
1329
+ var tokens, allTokens, opTokenId_baseAddressMap;
1317
1330
  return _regeneratorRuntime.wrap(function _callee$(_context) {
1318
1331
  while (1) {
1319
1332
  switch (_context.prev = _context.next) {
@@ -1323,19 +1336,27 @@ var OptimismMappingProvider = /*#__PURE__*/function () {
1323
1336
  return getTokenList(optimismTokenListURL);
1324
1337
 
1325
1338
  case 3:
1326
- optimismTokens = _context.sent;
1327
-
1328
- for (_iterator = _createForOfIteratorHelperLoose(optimismTokens.tokens); !(_step = _iterator()).done;) {
1329
- token = _step.value;
1330
-
1331
- if (token.chainId === ChainId.MAINNET) {
1332
- tokens[token.address.toLowerCase()] = token == null ? void 0 : (_token$extensions = token.extensions) == null ? void 0 : _token$extensions.bridgeInfo[ChainId.OPTIMISM].tokenAddress;
1339
+ allTokens = _context.sent;
1340
+ opTokenId_baseAddressMap = {};
1341
+ allTokens.tokens.forEach(function (token) {
1342
+ if (token.chainId === ChainId.OPTIMISM) {
1343
+ var _token$extensions;
1344
+
1345
+ if (typeof ((_token$extensions = token.extensions) == null ? void 0 : _token$extensions.opTokenId) === 'string') {
1346
+ opTokenId_baseAddressMap[token.extensions.opTokenId] = token.address;
1347
+ }
1333
1348
  }
1334
- }
1349
+ });
1350
+ allTokens.tokens.forEach(function (token) {
1351
+ var _token$extensions2;
1335
1352
 
1353
+ if (token.chainId === ChainId.MAINNET && typeof ((_token$extensions2 = token.extensions) == null ? void 0 : _token$extensions2.opTokenId) === 'string' && token.extensions.opTokenId in opTokenId_baseAddressMap) {
1354
+ tokens[token.address.toLowerCase()] = opTokenId_baseAddressMap[token.extensions.opTokenId];
1355
+ }
1356
+ });
1336
1357
  return _context.abrupt("return", tokens);
1337
1358
 
1338
- case 6:
1359
+ case 8:
1339
1360
  case "end":
1340
1361
  return _context.stop();
1341
1362
  }
@@ -5663,9 +5684,131 @@ var AvalancheMappingProvider = /*#__PURE__*/function () {
5663
5684
  return AvalancheMappingProvider;
5664
5685
  }();
5665
5686
 
5687
+ var baseGoerliTokenListURL$1 = 'https://raw.githubusercontent.com/' + 'ethereum-optimism/ethereum-optimism.github.io/master/optimism.tokenlist.json';
5688
+ /**
5689
+ * The Base Goerli mapping (linked above) is manually maintained by the Coinbase team
5690
+ * in this repository: https://github.com/ethereum-optimism/ethereum-optimism.github.io.
5691
+ */
5692
+
5693
+ var BaseGoerliMappingProvider = /*#__PURE__*/function () {
5694
+ function BaseGoerliMappingProvider() {}
5695
+
5696
+ var _proto = BaseGoerliMappingProvider.prototype;
5697
+
5698
+ _proto.provide = /*#__PURE__*/function () {
5699
+ var _provide = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
5700
+ var tokens, allTokens, opTokenId_baseGoerliAddressMap;
5701
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
5702
+ while (1) {
5703
+ switch (_context.prev = _context.next) {
5704
+ case 0:
5705
+ tokens = {};
5706
+ _context.next = 3;
5707
+ return getTokenList(baseGoerliTokenListURL$1);
5708
+
5709
+ case 3:
5710
+ allTokens = _context.sent;
5711
+ opTokenId_baseGoerliAddressMap = {};
5712
+ allTokens.tokens.forEach(function (token) {
5713
+ if (token.chainId === ChainId.BASE_GOERLI) {
5714
+ var _token$extensions;
5715
+
5716
+ if (typeof ((_token$extensions = token.extensions) == null ? void 0 : _token$extensions.opTokenId) === 'string') {
5717
+ opTokenId_baseGoerliAddressMap[token.extensions.opTokenId] = token.address;
5718
+ }
5719
+ }
5720
+ });
5721
+ allTokens.tokens.forEach(function (token) {
5722
+ var _token$extensions2;
5723
+
5724
+ if (token.chainId === ChainId.MAINNET && typeof ((_token$extensions2 = token.extensions) == null ? void 0 : _token$extensions2.opTokenId) === 'string' && token.extensions.opTokenId in opTokenId_baseGoerliAddressMap) {
5725
+ tokens[token.address.toLowerCase()] = opTokenId_baseGoerliAddressMap[token.extensions.opTokenId];
5726
+ }
5727
+ });
5728
+ return _context.abrupt("return", tokens);
5729
+
5730
+ case 8:
5731
+ case "end":
5732
+ return _context.stop();
5733
+ }
5734
+ }
5735
+ }, _callee);
5736
+ }));
5737
+
5738
+ function provide() {
5739
+ return _provide.apply(this, arguments);
5740
+ }
5741
+
5742
+ return provide;
5743
+ }();
5744
+
5745
+ return BaseGoerliMappingProvider;
5746
+ }();
5747
+
5748
+ var baseGoerliTokenListURL = 'https://raw.githubusercontent.com/' + 'ethereum-optimism/ethereum-optimism.github.io/master/optimism.tokenlist.json';
5749
+ /**
5750
+ * The Base mapping (linked above) is manually maintained by the Coinbase team
5751
+ * in this repository: https://github.com/ethereum-optimism/ethereum-optimism.github.io.
5752
+ */
5753
+
5754
+ var BaseMappingProvider = /*#__PURE__*/function () {
5755
+ function BaseMappingProvider() {}
5756
+
5757
+ var _proto = BaseMappingProvider.prototype;
5758
+
5759
+ _proto.provide = /*#__PURE__*/function () {
5760
+ var _provide = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
5761
+ var tokens, allTokens, opTokenId_baseAddressMap;
5762
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
5763
+ while (1) {
5764
+ switch (_context.prev = _context.next) {
5765
+ case 0:
5766
+ tokens = {};
5767
+ _context.next = 3;
5768
+ return getTokenList(baseGoerliTokenListURL);
5769
+
5770
+ case 3:
5771
+ allTokens = _context.sent;
5772
+ opTokenId_baseAddressMap = {};
5773
+ allTokens.tokens.forEach(function (token) {
5774
+ if (token.chainId === ChainId.BASE) {
5775
+ var _token$extensions;
5776
+
5777
+ if (typeof ((_token$extensions = token.extensions) == null ? void 0 : _token$extensions.opTokenId) === 'string') {
5778
+ opTokenId_baseAddressMap[token.extensions.opTokenId] = token.address;
5779
+ }
5780
+ }
5781
+ });
5782
+ allTokens.tokens.forEach(function (token) {
5783
+ var _token$extensions2;
5784
+
5785
+ if (token.chainId === ChainId.MAINNET && typeof ((_token$extensions2 = token.extensions) == null ? void 0 : _token$extensions2.opTokenId) === 'string' && token.extensions.opTokenId in opTokenId_baseAddressMap) {
5786
+ tokens[token.address.toLowerCase()] = opTokenId_baseAddressMap[token.extensions.opTokenId];
5787
+ }
5788
+ });
5789
+ return _context.abrupt("return", tokens);
5790
+
5791
+ case 8:
5792
+ case "end":
5793
+ return _context.stop();
5794
+ }
5795
+ }
5796
+ }, _callee);
5797
+ }));
5798
+
5799
+ function provide() {
5800
+ return _provide.apply(this, arguments);
5801
+ }
5802
+
5803
+ return provide;
5804
+ }();
5805
+
5806
+ return BaseMappingProvider;
5807
+ }();
5808
+
5666
5809
  var web3 = /*#__PURE__*/new Web3(); // chains we support fetching mappings for (can be different than the l2ChainIds arg for buildList)
5667
5810
 
5668
- var CHAINS_WITH_MAPPING_PROVIDERS = [ChainId.ARBITRUM_ONE, ChainId.POLYGON, ChainId.OPTIMISM, ChainId.BNB, ChainId.AVALANCHE];
5811
+ var CHAINS_WITH_MAPPING_PROVIDERS = [ChainId.ARBITRUM_ONE, ChainId.POLYGON, ChainId.OPTIMISM, ChainId.BNB, ChainId.AVALANCHE, ChainId.BASE, ChainId.BASE_GOERLI];
5669
5812
  function buildList(_x, _x2) {
5670
5813
  return _buildList.apply(this, arguments);
5671
5814
  } // using a symbol lookup contract call to check whether the token exists on the L2
@@ -5857,6 +6000,12 @@ function getMappingProvider(chainId, l1TokenList) {
5857
6000
  case ChainId.AVALANCHE:
5858
6001
  return new AvalancheMappingProvider();
5859
6002
 
6003
+ case ChainId.BASE:
6004
+ return new BaseMappingProvider();
6005
+
6006
+ case ChainId.BASE_GOERLI:
6007
+ return new BaseGoerliMappingProvider();
6008
+
5860
6009
  default:
5861
6010
  throw new Error("Chain " + chainId + " not supported for fetching mappings.");
5862
6011
  }
@@ -5920,18 +6069,27 @@ function getChildTokenDetails(_x7, _x8, _x9) {
5920
6069
 
5921
6070
  function _getChildTokenDetails() {
5922
6071
  _getChildTokenDetails = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(l1Token, chainId, chainIdToMappingsMap) {
5923
- var _l1Token$extensions, _l1Token$extensions$b, _l1Token$extensions$b2;
6072
+ var _l1Token$extensions;
5924
6073
 
5925
- var existingMapping, childToken, childTokenAddress, childTokenValid, decimals;
6074
+ var bridgeInfo, existingMapping, bridgeInfoForChain, childToken, childTokenAddress, childTokenValid, decimals;
5926
6075
  return _regeneratorRuntime.wrap(function _callee5$(_context6) {
5927
6076
  while (1) {
5928
6077
  switch (_context6.prev = _context6.next) {
5929
6078
  case 0:
5930
- 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
6079
+ bridgeInfo = l1Token == null ? void 0 : (_l1Token$extensions = l1Token.extensions) == null ? void 0 : _l1Token$extensions.bridgeInfo;
6080
+
6081
+ if (bridgeInfo && typeof bridgeInfo === 'object') {
6082
+ bridgeInfoForChain = bridgeInfo[chainId];
6083
+
6084
+ if (bridgeInfoForChain && typeof bridgeInfoForChain === 'object') {
6085
+ existingMapping = bridgeInfoForChain.tokenAddress;
6086
+ }
6087
+ } // use the externally fetched mappings if manual entry doesn't exist for the token/chain mapping
5931
6088
  // and the given chain is supported for fetching mappings
5932
6089
 
6090
+
5933
6091
  if (!(CHAINS_WITH_MAPPING_PROVIDERS.includes(chainId) && existingMapping === undefined)) {
5934
- _context6.next = 14;
6092
+ _context6.next = 15;
5935
6093
  break;
5936
6094
  }
5937
6095
 
@@ -5941,17 +6099,17 @@ function _getChildTokenDetails() {
5941
6099
  _context6.t1 = childTokenAddress;
5942
6100
 
5943
6101
  if (!_context6.t1) {
5944
- _context6.next = 10;
6102
+ _context6.next = 11;
5945
6103
  break;
5946
6104
  }
5947
6105
 
5948
- _context6.next = 9;
6106
+ _context6.next = 10;
5949
6107
  return hasExistingTokenContract(childTokenAddress, chainId);
5950
6108
 
5951
- case 9:
6109
+ case 10:
5952
6110
  _context6.t1 = _context6.sent;
5953
6111
 
5954
- case 10:
6112
+ case 11:
5955
6113
  _context6.t2 = _context6.t1;
5956
6114
  childTokenValid = (0, _context6.t0)(_context6.t2);
5957
6115
  decimals = childToken && (chainId === ChainId.BNB || chainId === ChainId.AVALANCHE) ? childToken.decimals : undefined;
@@ -5961,13 +6119,13 @@ function _getChildTokenDetails() {
5961
6119
  decimals: decimals
5962
6120
  });
5963
6121
 
5964
- case 14:
6122
+ case 15:
5965
6123
  return _context6.abrupt("return", {
5966
6124
  childTokenValid: !!existingMapping,
5967
6125
  childTokenAddress: existingMapping
5968
6126
  });
5969
6127
 
5970
- case 15:
6128
+ case 16:
5971
6129
  case "end":
5972
6130
  return _context6.stop();
5973
6131
  }
@@ -6057,7 +6215,7 @@ function chainify(_x) {
6057
6215
 
6058
6216
  function _chainify() {
6059
6217
  _chainify = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(l1TokenListOrPathOrUrl) {
6060
- var l1TokenList, l2Chains, chainified;
6218
+ var l1TokenList, l2Chains, chainified, merged;
6061
6219
  return _regeneratorRuntime.wrap(function _callee$(_context) {
6062
6220
  while (1) {
6063
6221
  switch (_context.prev = _context.next) {
@@ -6067,16 +6225,17 @@ function _chainify() {
6067
6225
 
6068
6226
  case 2:
6069
6227
  l1TokenList = _context.sent;
6070
- l2Chains = [ChainId.POLYGON, ChainId.ARBITRUM_ONE, ChainId.OPTIMISM, ChainId.CELO, ChainId.BNB, ChainId.AVALANCHE];
6228
+ l2Chains = [ChainId.POLYGON, ChainId.ARBITRUM_ONE, ChainId.OPTIMISM, ChainId.CELO, ChainId.BNB, ChainId.AVALANCHE, ChainId.BASE, ChainId.BASE_GOERLI];
6071
6229
  _context.next = 6;
6072
6230
  return chainifyTokenList(l2Chains, l1TokenListOrPathOrUrl);
6073
6231
 
6074
6232
  case 6:
6075
6233
  chainified = _context.sent;
6076
- return _context.abrupt("return", mergeTokenLists(l1TokenList, // providing l1 first to make sure duplicated tokens resolve to this list
6077
- chainified));
6234
+ merged = mergeTokenLists(l1TokenList, // providing l1 first to make sure duplicated tokens resolve to this list
6235
+ chainified);
6236
+ return _context.abrupt("return", merged);
6078
6237
 
6079
- case 8:
6238
+ case 9:
6080
6239
  case "end":
6081
6240
  return _context.stop();
6082
6241
  }
@@ -6141,7 +6300,7 @@ function mergeTokenLists(primary, secondary) {
6141
6300
 
6142
6301
  var merged = merge(group[0], group[1]);
6143
6302
 
6144
- if ((_merged$extensions = merged.extensions) != null && _merged$extensions.bridgeInfo) {
6303
+ if ((_merged$extensions = merged.extensions) != null && _merged$extensions.bridgeInfo && typeof merged.extensions.bridgeInfo === 'object') {
6145
6304
  // remove reference to self-chain from merge
6146
6305
  delete merged.extensions.bridgeInfo[merged.chainId];
6147
6306
  }