default-provider.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { assert } from "../utils/index.js";
  2. import { AnkrProvider } from "./provider-ankr.js";
  3. import { AlchemyProvider } from "./provider-alchemy.js";
  4. import { ChainstackProvider } from "./provider-chainstack.js";
  5. import { CloudflareProvider } from "./provider-cloudflare.js";
  6. import { EtherscanProvider } from "./provider-etherscan.js";
  7. import { InfuraProvider } from "./provider-infura.js";
  8. //import { PocketProvider } from "./provider-pocket.js";
  9. import { QuickNodeProvider } from "./provider-quicknode.js";
  10. import { FallbackProvider } from "./provider-fallback.js";
  11. import { JsonRpcProvider } from "./provider-jsonrpc.js";
  12. import { Network } from "./network.js";
  13. import { WebSocketProvider } from "./provider-websocket.js";
  14. import type { AbstractProvider } from "./abstract-provider.js";
  15. import type { Networkish } from "./network.js";
  16. import { WebSocketLike } from "./provider-websocket.js";
  17. function isWebSocketLike(value: any): value is WebSocketLike {
  18. return (value && typeof(value.send) === "function" &&
  19. typeof(value.close) === "function");
  20. }
  21. const Testnets = "goerli kovan sepolia classicKotti optimism-goerli arbitrum-goerli matic-mumbai bnbt".split(" ");
  22. /**
  23. * Returns a default provider for %%network%%.
  24. *
  25. * If %%network%% is a [[WebSocketLike]] or string that begins with
  26. * ``"ws:"`` or ``"wss:"``, a [[WebSocketProvider]] is returned backed
  27. * by that WebSocket or URL.
  28. *
  29. * If %%network%% is a string that begins with ``"HTTP:"`` or ``"HTTPS:"``,
  30. * a [[JsonRpcProvider]] is returned connected to that URL.
  31. *
  32. * Otherwise, a default provider is created backed by well-known public
  33. * Web3 backends (such as [[link-infura]]) using community-provided API
  34. * keys.
  35. *
  36. * The %%options%% allows specifying custom API keys per backend (setting
  37. * an API key to ``"-"`` will omit that provider) and ``options.exclusive``
  38. * can be set to either a backend name or and array of backend names, which
  39. * will whitelist **only** those backends.
  40. *
  41. * Current backend strings supported are:
  42. * - ``"alchemy"``
  43. * - ``"ankr"``
  44. * - ``"cloudflare"``
  45. * - ``"chainstack"``
  46. * - ``"etherscan"``
  47. * - ``"infura"``
  48. * - ``"publicPolygon"``
  49. * - ``"quicknode"``
  50. *
  51. * @example:
  52. * // Connect to a local Geth node
  53. * provider = getDefaultProvider("http://localhost:8545/");
  54. *
  55. * // Connect to Ethereum mainnet with any current and future
  56. * // third-party services available
  57. * provider = getDefaultProvider("mainnet");
  58. *
  59. * // Connect to Polygon, but only allow Etherscan and
  60. * // INFURA and use "MY_API_KEY" in calls to Etherscan.
  61. * provider = getDefaultProvider("matic", {
  62. * etherscan: "MY_API_KEY",
  63. * exclusive: [ "etherscan", "infura" ]
  64. * });
  65. */
  66. export function getDefaultProvider(network?: string | Networkish | WebSocketLike, options?: any): AbstractProvider {
  67. if (options == null) { options = { }; }
  68. const allowService = (name: string) => {
  69. if (options[name] === "-") { return false; }
  70. if (typeof(options.exclusive) === "string") {
  71. return (name === options.exclusive);
  72. }
  73. if (Array.isArray(options.exclusive)) {
  74. return (options.exclusive.indexOf(name) !== -1);
  75. }
  76. return true;
  77. };
  78. if (typeof(network) === "string" && network.match(/^https?:/)) {
  79. return new JsonRpcProvider(network);
  80. }
  81. if (typeof(network) === "string" && network.match(/^wss?:/) || isWebSocketLike(network)) {
  82. return new WebSocketProvider(network);
  83. }
  84. // Get the network and name, if possible
  85. let staticNetwork: null | Network = null;
  86. try {
  87. staticNetwork = Network.from(network);
  88. } catch (error) { }
  89. const providers: Array<AbstractProvider> = [ ];
  90. if (allowService("publicPolygon") && staticNetwork) {
  91. if (staticNetwork.name === "matic") {
  92. providers.push(new JsonRpcProvider("https:/\/polygon-rpc.com/", staticNetwork, { staticNetwork }));
  93. } else if (staticNetwork.name === "matic-amoy") {
  94. providers.push(new JsonRpcProvider("https:/\/rpc-amoy.polygon.technology/", staticNetwork, { staticNetwork }));
  95. }
  96. }
  97. if (allowService("alchemy")) {
  98. try {
  99. providers.push(new AlchemyProvider(network, options.alchemy));
  100. } catch (error) { }
  101. }
  102. if (allowService("ankr") && options.ankr != null) {
  103. try {
  104. providers.push(new AnkrProvider(network, options.ankr));
  105. } catch (error) { }
  106. }
  107. if (allowService("chainstack")) {
  108. try {
  109. providers.push(new ChainstackProvider(network, options.chainstack));
  110. } catch (error) { }
  111. }
  112. if (allowService("cloudflare")) {
  113. try {
  114. providers.push(new CloudflareProvider(network));
  115. } catch (error) { }
  116. }
  117. if (allowService("etherscan")) {
  118. try {
  119. providers.push(new EtherscanProvider(network, options.etherscan));
  120. } catch (error) { }
  121. }
  122. if (allowService("infura")) {
  123. try {
  124. let projectId = options.infura;
  125. let projectSecret: undefined | string = undefined;
  126. if (typeof(projectId) === "object") {
  127. projectSecret = projectId.projectSecret;
  128. projectId = projectId.projectId;
  129. }
  130. providers.push(new InfuraProvider(network, projectId, projectSecret));
  131. } catch (error) { }
  132. }
  133. /*
  134. if (options.pocket !== "-") {
  135. try {
  136. let appId = options.pocket;
  137. let secretKey: undefined | string = undefined;
  138. let loadBalancer: undefined | boolean = undefined;
  139. if (typeof(appId) === "object") {
  140. loadBalancer = !!appId.loadBalancer;
  141. secretKey = appId.secretKey;
  142. appId = appId.appId;
  143. }
  144. providers.push(new PocketProvider(network, appId, secretKey, loadBalancer));
  145. } catch (error) { console.log(error); }
  146. }
  147. */
  148. if (allowService("quicknode")) {
  149. try {
  150. let token = options.quicknode;
  151. providers.push(new QuickNodeProvider(network, token));
  152. } catch (error) { }
  153. }
  154. assert(providers.length, "unsupported default network", "UNSUPPORTED_OPERATION", {
  155. operation: "getDefaultProvider"
  156. });
  157. // No need for a FallbackProvider
  158. if (providers.length === 1) { return providers[0]; }
  159. // We use the floor because public third-party providers can be unreliable,
  160. // so a low number of providers with a large quorum will fail too often
  161. let quorum = Math.floor(providers.length / 2);
  162. if (quorum > 2) { quorum = 2; }
  163. // Testnets don't need as strong a security gaurantee and speed is
  164. // more useful during testing
  165. if (staticNetwork && Testnets.indexOf(staticNetwork.name) !== -1) { quorum = 1; }
  166. // Provided override qorum takes priority
  167. if (options && options.quorum) { quorum = options.quorum; }
  168. return new FallbackProvider(providers, undefined, { quorum });
  169. }