provider-infura.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.InfuraProvider = exports.InfuraWebSocketProvider = void 0;
  4. /**
  5. * [[link-infura]] provides a third-party service for connecting to
  6. * various blockchains over JSON-RPC.
  7. *
  8. * **Supported Networks**
  9. *
  10. * - Ethereum Mainnet (``mainnet``)
  11. * - Goerli Testnet (``goerli``)
  12. * - Sepolia Testnet (``sepolia``)
  13. * - Arbitrum (``arbitrum``)
  14. * - Arbitrum Goerli Testnet (``arbitrum-goerli``)
  15. * - Arbitrum Sepolia Testnet (``arbitrum-sepolia``)
  16. * - Base (``base``)
  17. * - Base Goerlia Testnet (``base-goerli``)
  18. * - Base Sepolia Testnet (``base-sepolia``)
  19. * - BNB Smart Chain Mainnet (``bnb``)
  20. * - BNB Smart Chain Testnet (``bnbt``)
  21. * - Linea (``linea``)
  22. * - Linea Goerli Testnet (``linea-goerli``)
  23. * - Linea Sepolia Testnet (``linea-sepolia``)
  24. * - Optimism (``optimism``)
  25. * - Optimism Goerli Testnet (``optimism-goerli``)
  26. * - Optimism Sepolia Testnet (``optimism-sepolia``)
  27. * - Polygon (``matic``)
  28. * - Polygon Amoy Testnet (``matic-amoy``)
  29. * - Polygon Mumbai Testnet (``matic-mumbai``)
  30. *
  31. * @_subsection: api/providers/thirdparty:INFURA [providers-infura]
  32. */
  33. const index_js_1 = require("../utils/index.js");
  34. const community_js_1 = require("./community.js");
  35. const network_js_1 = require("./network.js");
  36. const provider_jsonrpc_js_1 = require("./provider-jsonrpc.js");
  37. const provider_websocket_js_1 = require("./provider-websocket.js");
  38. const defaultProjectId = "84842078b09946638c03157f83405213";
  39. function getHost(name) {
  40. switch (name) {
  41. case "mainnet":
  42. return "mainnet.infura.io";
  43. case "goerli":
  44. return "goerli.infura.io";
  45. case "sepolia":
  46. return "sepolia.infura.io";
  47. case "arbitrum":
  48. return "arbitrum-mainnet.infura.io";
  49. case "arbitrum-goerli":
  50. return "arbitrum-goerli.infura.io";
  51. case "arbitrum-sepolia":
  52. return "arbitrum-sepolia.infura.io";
  53. case "base":
  54. return "base-mainnet.infura.io";
  55. case "base-goerlia":
  56. return "base-goerli.infura.io";
  57. case "base-sepolia":
  58. return "base-sepolia.infura.io";
  59. case "bnb":
  60. return "bnbsmartchain-mainnet.infura.io";
  61. case "bnbt":
  62. return "bnbsmartchain-testnet.infura.io";
  63. case "linea":
  64. return "linea-mainnet.infura.io";
  65. case "linea-goerli":
  66. return "linea-goerli.infura.io";
  67. case "linea-sepolia":
  68. return "linea-sepolia.infura.io";
  69. case "matic":
  70. return "polygon-mainnet.infura.io";
  71. case "matic-amoy":
  72. return "polygon-amoy.infura.io";
  73. case "matic-mumbai":
  74. return "polygon-mumbai.infura.io";
  75. case "optimism":
  76. return "optimism-mainnet.infura.io";
  77. case "optimism-goerli":
  78. return "optimism-goerli.infura.io";
  79. case "optimism-sepolia":
  80. return "optimism-sepolia.infura.io";
  81. }
  82. (0, index_js_1.assertArgument)(false, "unsupported network", "network", name);
  83. }
  84. /**
  85. * The **InfuraWebSocketProvider** connects to the [[link-infura]]
  86. * WebSocket end-points.
  87. *
  88. * By default, a highly-throttled API key is used, which is
  89. * appropriate for quick prototypes and simple scripts. To
  90. * gain access to an increased rate-limit, it is highly
  91. * recommended to [sign up here](link-infura-signup).
  92. */
  93. class InfuraWebSocketProvider extends provider_websocket_js_1.WebSocketProvider {
  94. /**
  95. * The Project ID for the INFURA connection.
  96. */
  97. projectId;
  98. /**
  99. * The Project Secret.
  100. *
  101. * If null, no authenticated requests are made. This should not
  102. * be used outside of private contexts.
  103. */
  104. projectSecret;
  105. /**
  106. * Creates a new **InfuraWebSocketProvider**.
  107. */
  108. constructor(network, projectId) {
  109. const provider = new InfuraProvider(network, projectId);
  110. const req = provider._getConnection();
  111. (0, index_js_1.assert)(!req.credentials, "INFURA WebSocket project secrets unsupported", "UNSUPPORTED_OPERATION", { operation: "InfuraProvider.getWebSocketProvider()" });
  112. const url = req.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
  113. super(url, provider._network);
  114. (0, index_js_1.defineProperties)(this, {
  115. projectId: provider.projectId,
  116. projectSecret: provider.projectSecret
  117. });
  118. }
  119. isCommunityResource() {
  120. return (this.projectId === defaultProjectId);
  121. }
  122. }
  123. exports.InfuraWebSocketProvider = InfuraWebSocketProvider;
  124. /**
  125. * The **InfuraProvider** connects to the [[link-infura]]
  126. * JSON-RPC end-points.
  127. *
  128. * By default, a highly-throttled API key is used, which is
  129. * appropriate for quick prototypes and simple scripts. To
  130. * gain access to an increased rate-limit, it is highly
  131. * recommended to [sign up here](link-infura-signup).
  132. */
  133. class InfuraProvider extends provider_jsonrpc_js_1.JsonRpcProvider {
  134. /**
  135. * The Project ID for the INFURA connection.
  136. */
  137. projectId;
  138. /**
  139. * The Project Secret.
  140. *
  141. * If null, no authenticated requests are made. This should not
  142. * be used outside of private contexts.
  143. */
  144. projectSecret;
  145. /**
  146. * Creates a new **InfuraProvider**.
  147. */
  148. constructor(_network, projectId, projectSecret) {
  149. if (_network == null) {
  150. _network = "mainnet";
  151. }
  152. const network = network_js_1.Network.from(_network);
  153. if (projectId == null) {
  154. projectId = defaultProjectId;
  155. }
  156. if (projectSecret == null) {
  157. projectSecret = null;
  158. }
  159. const request = InfuraProvider.getRequest(network, projectId, projectSecret);
  160. super(request, network, { staticNetwork: network });
  161. (0, index_js_1.defineProperties)(this, { projectId, projectSecret });
  162. }
  163. _getProvider(chainId) {
  164. try {
  165. return new InfuraProvider(chainId, this.projectId, this.projectSecret);
  166. }
  167. catch (error) { }
  168. return super._getProvider(chainId);
  169. }
  170. isCommunityResource() {
  171. return (this.projectId === defaultProjectId);
  172. }
  173. /**
  174. * Creates a new **InfuraWebSocketProvider**.
  175. */
  176. static getWebSocketProvider(network, projectId) {
  177. return new InfuraWebSocketProvider(network, projectId);
  178. }
  179. /**
  180. * Returns a prepared request for connecting to %%network%%
  181. * with %%projectId%% and %%projectSecret%%.
  182. */
  183. static getRequest(network, projectId, projectSecret) {
  184. if (projectId == null) {
  185. projectId = defaultProjectId;
  186. }
  187. if (projectSecret == null) {
  188. projectSecret = null;
  189. }
  190. const request = new index_js_1.FetchRequest(`https:/\/${getHost(network.name)}/v3/${projectId}`);
  191. request.allowGzip = true;
  192. if (projectSecret) {
  193. request.setCredentials("", projectSecret);
  194. }
  195. if (projectId === defaultProjectId) {
  196. request.retryFunc = async (request, response, attempt) => {
  197. (0, community_js_1.showThrottleMessage)("InfuraProvider");
  198. return true;
  199. };
  200. }
  201. return request;
  202. }
  203. }
  204. exports.InfuraProvider = InfuraProvider;
  205. //# sourceMappingURL=provider-infura.js.map