provider-infura.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 {
  31. defineProperties, FetchRequest, assert, assertArgument
  32. } from "../utils/index.js";
  33. import { showThrottleMessage } from "./community.js";
  34. import { Network } from "./network.js";
  35. import { JsonRpcProvider } from "./provider-jsonrpc.js";
  36. import { WebSocketProvider } from "./provider-websocket.js";
  37. import type { AbstractProvider } from "./abstract-provider.js";
  38. import type { CommunityResourcable } from "./community.js";
  39. import type { Networkish } from "./network.js";
  40. const defaultProjectId = "84842078b09946638c03157f83405213";
  41. function getHost(name: string): string {
  42. switch(name) {
  43. case "mainnet":
  44. return "mainnet.infura.io";
  45. case "goerli":
  46. return "goerli.infura.io";
  47. case "sepolia":
  48. return "sepolia.infura.io";
  49. case "arbitrum":
  50. return "arbitrum-mainnet.infura.io";
  51. case "arbitrum-goerli":
  52. return "arbitrum-goerli.infura.io";
  53. case "arbitrum-sepolia":
  54. return "arbitrum-sepolia.infura.io";
  55. case "base":
  56. return "base-mainnet.infura.io";
  57. case "base-goerlia":
  58. return "base-goerli.infura.io";
  59. case "base-sepolia":
  60. return "base-sepolia.infura.io";
  61. case "bnb":
  62. return "bnbsmartchain-mainnet.infura.io";
  63. case "bnbt":
  64. return "bnbsmartchain-testnet.infura.io";
  65. case "linea":
  66. return "linea-mainnet.infura.io";
  67. case "linea-goerli":
  68. return "linea-goerli.infura.io";
  69. case "linea-sepolia":
  70. return "linea-sepolia.infura.io";
  71. case "matic":
  72. return "polygon-mainnet.infura.io";
  73. case "matic-amoy":
  74. return "polygon-amoy.infura.io";
  75. case "matic-mumbai":
  76. return "polygon-mumbai.infura.io";
  77. case "optimism":
  78. return "optimism-mainnet.infura.io";
  79. case "optimism-goerli":
  80. return "optimism-goerli.infura.io";
  81. case "optimism-sepolia":
  82. return "optimism-sepolia.infura.io";
  83. }
  84. assertArgument(false, "unsupported network", "network", name);
  85. }
  86. /**
  87. * The **InfuraWebSocketProvider** connects to the [[link-infura]]
  88. * WebSocket end-points.
  89. *
  90. * By default, a highly-throttled API key is used, which is
  91. * appropriate for quick prototypes and simple scripts. To
  92. * gain access to an increased rate-limit, it is highly
  93. * recommended to [sign up here](link-infura-signup).
  94. */
  95. export class InfuraWebSocketProvider extends WebSocketProvider implements CommunityResourcable {
  96. /**
  97. * The Project ID for the INFURA connection.
  98. */
  99. readonly projectId!: string;
  100. /**
  101. * The Project Secret.
  102. *
  103. * If null, no authenticated requests are made. This should not
  104. * be used outside of private contexts.
  105. */
  106. readonly projectSecret!: null | string;
  107. /**
  108. * Creates a new **InfuraWebSocketProvider**.
  109. */
  110. constructor(network?: Networkish, projectId?: string) {
  111. const provider = new InfuraProvider(network, projectId);
  112. const req = provider._getConnection();
  113. assert(!req.credentials, "INFURA WebSocket project secrets unsupported",
  114. "UNSUPPORTED_OPERATION", { operation: "InfuraProvider.getWebSocketProvider()" });
  115. const url = req.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
  116. super(url, provider._network);
  117. defineProperties<InfuraWebSocketProvider>(this, {
  118. projectId: provider.projectId,
  119. projectSecret: provider.projectSecret
  120. });
  121. }
  122. isCommunityResource(): boolean {
  123. return (this.projectId === defaultProjectId);
  124. }
  125. }
  126. /**
  127. * The **InfuraProvider** connects to the [[link-infura]]
  128. * JSON-RPC end-points.
  129. *
  130. * By default, a highly-throttled API key is used, which is
  131. * appropriate for quick prototypes and simple scripts. To
  132. * gain access to an increased rate-limit, it is highly
  133. * recommended to [sign up here](link-infura-signup).
  134. */
  135. export class InfuraProvider extends JsonRpcProvider implements CommunityResourcable {
  136. /**
  137. * The Project ID for the INFURA connection.
  138. */
  139. readonly projectId!: string;
  140. /**
  141. * The Project Secret.
  142. *
  143. * If null, no authenticated requests are made. This should not
  144. * be used outside of private contexts.
  145. */
  146. readonly projectSecret!: null | string;
  147. /**
  148. * Creates a new **InfuraProvider**.
  149. */
  150. constructor(_network?: Networkish, projectId?: null | string, projectSecret?: null | string) {
  151. if (_network == null) { _network = "mainnet"; }
  152. const network = Network.from(_network);
  153. if (projectId == null) { projectId = defaultProjectId; }
  154. if (projectSecret == null) { projectSecret = null; }
  155. const request = InfuraProvider.getRequest(network, projectId, projectSecret);
  156. super(request, network, { staticNetwork: network });
  157. defineProperties<InfuraProvider>(this, { projectId, projectSecret });
  158. }
  159. _getProvider(chainId: number): AbstractProvider {
  160. try {
  161. return new InfuraProvider(chainId, this.projectId, this.projectSecret);
  162. } catch (error) { }
  163. return super._getProvider(chainId);
  164. }
  165. isCommunityResource(): boolean {
  166. return (this.projectId === defaultProjectId);
  167. }
  168. /**
  169. * Creates a new **InfuraWebSocketProvider**.
  170. */
  171. static getWebSocketProvider(network?: Networkish, projectId?: string): InfuraWebSocketProvider {
  172. return new InfuraWebSocketProvider(network, projectId);
  173. }
  174. /**
  175. * Returns a prepared request for connecting to %%network%%
  176. * with %%projectId%% and %%projectSecret%%.
  177. */
  178. static getRequest(network: Network, projectId?: null | string, projectSecret?: null | string): FetchRequest {
  179. if (projectId == null) { projectId = defaultProjectId; }
  180. if (projectSecret == null) { projectSecret = null; }
  181. const request = new FetchRequest(`https:/\/${ getHost(network.name) }/v3/${ projectId }`);
  182. request.allowGzip = true;
  183. if (projectSecret) { request.setCredentials("", projectSecret); }
  184. if (projectId === defaultProjectId) {
  185. request.retryFunc = async (request, response, attempt) => {
  186. showThrottleMessage("InfuraProvider");
  187. return true;
  188. };
  189. }
  190. return request;
  191. }
  192. }