provider-infura.js 6.8 KB

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